Answer the question
In order to leave comments, you need to log in
Which way to count rows is faster and consumes less SQL or PHP resources?
There are records with two field values stat = 1 and 2.
DB::table('table')->select(
DB::raw('`stat`, count(`stat`) as `count`')
)->where('tid','=',$tid)->groupBy('stat')->get();
Answer the question
In order to leave comments, you need to log in
In such cases, such calculations are always shifted to the database, for 1 request you get an answer and return it as a result.
If you try to get records for counting in PHP, then on large selections there is a high probability of getting too large an array of data that will not fit in the memory allocated for PHP, not to mention the data iteration time.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question