0
0
Ruby on Railsframework~20 mins

Index creation in Ruby on Rails - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Index Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
Effect of adding an index on query speed

Consider a Rails application with a users table having 1 million rows. You add an index on the email column. What is the expected effect on the following query?

SELECT * FROM users WHERE email = 'example@example.com';
AThe query result will change and return fewer rows.
BThe query will run slower because indexes add overhead to SELECT queries.
CThe query will fail because indexes require a special syntax in SELECT statements.
DThe query will run faster because the database can quickly locate the email using the index.
Attempts:
2 left
💡 Hint

Think about how indexes help databases find data.

📝 Syntax
intermediate
2:00remaining
Correct syntax to add an index in Rails migration

Which of the following is the correct way to add an index on the username column of the users table in a Rails migration?

Aadd_index users, username
Badd_index :users, :username
Cadd_index(:users, username)
Dadd_index(:users, :username, unique: true)
Attempts:
2 left
💡 Hint

Remember to use symbols for table and column names.

optimization
advanced
2:00remaining
Choosing the best index type for a search query

You have a products table with columns name (string) and description (text). You want to optimize search queries that look for keywords in the description. Which index type is best to improve full-text search performance?

AA full-text index on the <code>description</code> column.
BA unique index on the <code>description</code> column.
CNo index is needed for text search queries.
DA regular B-tree index on the <code>description</code> column.
Attempts:
2 left
💡 Hint

Think about which index type supports searching words inside large text fields.

🔧 Debug
advanced
2:00remaining
Why does adding an index fail in Rails migration?

You run this migration code:

add_index :orders, :customer_id, unique: true

But it fails with an error saying the index already exists. What is the most likely cause?

AAn index on <code>customer_id</code> already exists, so adding another unique index causes a conflict.
BThe <code>customer_id</code> column does not exist in the <code>orders</code> table.
CThe syntax is incorrect; <code>unique: true</code> should be inside curly braces.
DRails migrations do not support adding unique indexes.
Attempts:
2 left
💡 Hint

Check if the index already exists before adding it.

🧠 Conceptual
expert
3:00remaining
Impact of composite indexes on query performance

You create a composite index on (category_id, price) columns in the products table. Which of the following queries will benefit from this index?

1. SELECT * FROM products WHERE category_id = 5;
2. SELECT * FROM products WHERE price = 100;
3. SELECT * FROM products WHERE category_id = 5 AND price = 100;
4. SELECT * FROM products WHERE price = 100 AND category_id = 5;
AAll queries 1, 2, 3, and 4 will benefit equally from the composite index.
BOnly queries 2 and 4 will benefit from the composite index.
COnly queries 1, 3, and 4 will benefit from the composite index.
DOnly query 3 will benefit; queries 1, 2, and 4 will not.
Attempts:
2 left
💡 Hint

Think about how composite indexes work and the order of columns.