1

I have a project(Laravel 5.4) where i need to improve performance as much as i can. So i was wondering what is the performance difference between:

$model->get()

get method takes all variables('created_at','updated_at', etc), so the select should be faster.

$model->select('many variables to select')->get();

select method is an additional query, so it takes more time, so maybe just get is faster?

I wanted to know if select and get is better in all occasions or are there any moments where just get is better?

1 Answers1

1

The differences between Model::get() and Model::select(['f1', 'f2'])->get() is only at the query

// Model::get()
SELECT * FROM table

// Model::select(['f1', 'f2'])->get()
SELECT f1, f2 FROM table

Both runs database query ONCE, and prepare the collection of model instances to you. select simply construct eloquent to only select fields you need. Performance gain is almost negligible or can be worse. Read about it here: Is it bad for performance to select all columns?

Lionel Chan
  • 7,894
  • 5
  • 40
  • 69