What is the effect when the sum does not use get() ? Laravel 5.3
I have 1 million data When I do sum use get() like this : $cache = Summary::where('type', $type) ->where('keys->year', '=', (string)$year) ->get(); $summary = $cache->sum('summary'); It's very slowly But when I do sum without use get() like this : $cache = Summary::where('type', $type) ->where('keys->year', '=', (string)$year); $summary = $cache->sum('summary'); It's very fast Whether it is okay if the sum without using get?
If you use ->get() you are actually retrieving the information from the database. If you have a lot of rows this can slow a query down a lot. The ->sum() just sums the rows, a method that is lightweight in comparison to ->get(). If you don't actually need the information from the rows, but only the sum of all rows you should only use ->sum() and not ->get() as well. You don't need to use ->get() to have the ->sum() working. Laravel Documentation containing the following examples: $users = DB::table('users')->count(); $price = DB::table('orders')->max('price'); Both do not use ->get() in order to ->count() or ->max(). I guess you could even do: $sum = Summary::where('type', $type)->where('keys->year', '=', (string)$year)->sum();
Using get() fetches all the data and then you're manually finding the sum on the collection of retrieved data. But when you don't use get() $cache is still a query and fetching sum on the query would run a aggregate query on the database instead of fetching all the data. $summary = Summary::where('type', $type) ->where('keys->year', '=', (string)$year) ->sum('summary');
Testing models and services with Laravel, PHPSpec and PHPUnit
Generate array specific for each row of a while loop
Basic Authentication works in localhost but fails in Production Server
how to get wordpress post id externally?
Video thumbnail not appearing in wordpress homepage
connect two tables in mysql giving duplicate results
PHP-Related Post with description by category?
Looping array error again :(
php two Multidimensional Array difference
design pattery strategy with access to parent class
PHP MVC folder structure / Model-Folder-Structure
Class not dispatching
Magento Local Block Won't Override
Class 'FacebookSession' not found - Facebook PHP SDK v4
How can I use parameterized PHP PDO to drop a MySQL event?
shell_exec throws: sudo: no tty present and no askpass program specified