You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Laravel 5.4中get()与select()->get()的性能差异及适用场景

Laravel 5.4: $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 like SELECT * FROM your_table, which fetches every column in the table (including created_at, updated_at, and any large columns like TEXT or BLOB).
  • $model->select('column1', 'column2')->get() generates SELECT column1, column2 FROM your_table, which only pulls the fields you explicitly need.

The performance win with select() comes from two key areas:

  1. 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.
  2. 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 description TEXT 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:

  1. 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 in select().
  2. Small tables with few columns: If your table only has 3-4 small columns (e.g., a settings table with id, key, value), the difference in data transfer and memory is negligible. Writing Settings::get() is simpler than specifying all fields.
  3. 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 10:34:21