Performance: Select queries
Select queries affect how fast data is fetched from the database and how quickly the page can show content.
$users = DB::table('users')->select('id', 'name', 'email')->get();
$users = DB::table('users')->get();| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Select all columns without filter | N/A | N/A | High data transfer delays paint | [X] Bad |
| Select specific columns | N/A | N/A | Less data speeds paint | [OK] Good |
| N queries in loop | N/A | N/A | Multiple DB hits delay response | [X] Bad |
| Eager loading related data | N/A | N/A | Fewer queries speed paint | [OK] Good |
| Raw queries without caching | N/A | N/A | Repeated slow queries delay paint | [X] Bad |
| Cached query results | N/A | N/A | Faster repeated loads | [OK] Good |
select() method do in a Laravel query builder?select()select() method tells Laravel which columns you want to get from the database table.insert(), delete(), and update() perform different actions, not selecting columns.select() chooses columns [OK]select() accepts an array of column names to select multiple columns.select('name, email'), which selects a single column named 'name, email'. Options B and D use invalid operators & and +. DB::table('users')->select(['name', 'email'])->get(); correctly uses an array ['name', 'email'].DB::table('products')->select('name')->where('price', '>', 100)->get();DB::table('orders')->select('id', 'total')->where('status' = 'pending')->get();where('status', 'pending'), not where('status' = 'pending').where(['active' => 1, 'email_verified' => 1]). DB::table('users')->select('id', 'email')->where('active' == 1 && 'email_verified' == 1)->get(); uses invalid syntax with == and && inside where(). DB::table('users')->select(['id', 'email'])->where('active', '=', 1, 'email_verified', '=', 1)->get(); passes too many arguments to a single where(). DB::table('users')->select(['id', 'email'])->where('active' = 1)->where('email_verified' = 1)->get(); uses = instead of , to separate where() arguments, causing syntax error.Pass the test and this pattern moves unknown