php


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');

Related Links

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

Categories

HOME
openshift
focus
seedstack
qc
operating-system
loopbackjs
celery
apache-kafka-connect
ontology
onvif
migrate
fasm
alljoyn
parsley.js
watch-os-3
pm2
jqxgrid
pickle
infrared
conda
mime-types
sybase-ase
managed-c++
basex
javax.persistence
splunk-query
copy-protection
autodesk-model-derivative
nsmutablearray
gsp
db2-luw
sspi
selenide
get-event-store
dagger
latexml
intellij-lombok-plugin
swift2.3
watir-webdriver
exc-bad-access
popen
emacs25
predicates
dynamics-crm-4
mpmovieplayercontroller
openerp-6
google-shopping-api
nrf51
streamwriter
tsqlt
declarative
yii2-model
django-redis
okta-api
teensy
chicagoboss
featuretoggle
supercomputers
domino-designer-eclipse
componentart
symja
dnsjava
maven-central
network-printers
fileshare
tabletools
jms-serializer
lsa
eclim
acitree
delphi-xe4
background-thread
jquery-dialog
wndproc
live-wallpaper
image-formats
uitextfielddelegate
zotonic
hardcode
mmc3
glassfish-esb
startupscript
tablet-pc
suphp
android-2.1-eclair
chunking
jdownloader
iphone-sdk-4.3
rawcontacts
ppc
main-method
fuzzy-comparison
isapi-redirect
remote-working
bindable-linq

Resources

Encrypt Message



code
soft
python
ios
c
html
jquery
cloud
mobile