Laravel 5.4中get()与select()->get()的性能差异及适用场景
$model->get() vs $model->select(...)->get() Performance Differences Great question—this is a common point of confusion when optimizing Laravel queries, so let’s break this down clearly.
Core Performance Difference: Database & Memory Overhead
First, let’s dispel your misconception: select() isn’t an "extra query operation." It’s just part of building your SQL statement. Both $model->get() and $model->select(...)->get() execute one single query—the only difference is what’s in the SELECT clause of that SQL.
$model->get()generates a query likeSELECT * FROM your_table, which fetches every column in the table (includingcreated_at,updated_at, and any large columns likeTEXTorBLOB).$model->select('column1', 'column2')->get()generatesSELECT column1, column2 FROM your_table, which only pulls the fields you explicitly need.
The performance win with select() comes from two key areas:
- Reduced data transfer: Less data travels from your database server to your app server—critical if your table has many columns, large fields, or you’re fetching a large result set.
- Lower memory usage: Your Laravel models will only hold the data you need, which can make a big difference when working with hundreds or thousands of records.
When is select()->get() Better?
In most practical scenarios where you don’t need every column, select() is the better choice:
- You’re building an API endpoint that only returns specific fields (e.g., a user list showing just name and email, not their entire profile).
- Your table has large columns (like a
descriptionTEXT field) that you don’t need for the current operation. - You’re processing a large dataset (e.g., exporting a subset of data)—reducing memory usage prevents your app from hitting PHP memory limits.
Example:
// Better if you only need these fields $users = User::select('id', 'name', 'email')->get(); // Less efficient if you don't need all 20+ columns in the users table $users = User::get();
When Might get() Be Preferable?
There are cases where get() is either just as performant or more practical:
- You actually need all columns: If you’re loading a record for an edit form where every field is displayed/modified,
get()is cleaner than listing every column inselect(). - Small tables with few columns: If your table only has 3-4 small columns (e.g., a
settingstable withid,key,value), the difference in data transfer and memory is negligible. WritingSettings::get()is simpler than specifying all fields. - Uncertain future field usage: If your code later might access a column you didn’t include in
select(), you’ll either get an error (since the model doesn’t have that data) or trigger an extra query to fetch it (leading to N+1 problems). In these cases, pulling all fields avoids headaches.
Bonus: Combine select() with Other Optimizations
To maximize performance, pair select() with other Laravel best practices:
- Use
with()to preload relationships (avoids N+1 queries). - Use
chunk()for very large datasets to process records in batches instead of loading everything into memory at once. - Add indexes to the columns you’re filtering/sorting on—this speeds up the database query itself, regardless of whether you use
select()or not.
内容的提问来源于stack exchange,提问作者Valdis Aglonietis

