Challenges
Slow Queries
Identify columns missing indexes based on controller queries and add them to the migration.
The Problem
Your application is running slowly. pg_stat_statements shows that two queries are taking hundreds of milliseconds to execute because they are performing sequential scans on the posts table.
What You Need to Fix
Load this challenge:
php artisan challenge:start db-slow-queriesYou have three files:
Http/controllers/users/posts.php— gets posts by user IDHttp/controllers/posts/published.php— gets posts by statusdatabase/migrations/004_add_indexes.sql— an empty migration file
What You Must Do
- Read the two controllers and identify which columns they are filtering on in their
WHEREclauses. - Open
database/migrations/004_add_indexes.sql. - Write
CREATE INDEXstatements to add indexes to the missing columns on thepoststable. - You need to create two separate indexes: one for the user ID and one for the status.
Hints
- The syntax is
CREATE INDEX index_name ON table_name (column_name); - You need an index on
user_idand an index onstatus. - Name the indexes something logical, e.g.,
idx_posts_user_id.
Verify
php artisan challenge:verify