Pages

01 May, 2023

Why does caching feels slower?

My app runs a lot of identical queries on small tables with less than 10 items. The tables are like statuses for orders. ​ ID value 1 pending 2 taken for example When I run the following code, I get a weird output. $timer = new Timer(); echo 'normal '; $timer->start(); for ($i = 0; $i < 1; $i++) { Status::all(); } echo $timer->stop()->asString() . " "; $timer->start(); for ($i = 0; $i < 10; $i++) { Status::all(); } echo $timer->stop()->asString() . " "; $timer->start(); for ($i = 0; $i < 100; $i++) { Status::all(); } echo $timer->stop()->asString() . " "; echo 'cache '; $timer->start(); for ($i = 0; $i < 1; $i++) { Cache::remember('Status', now()->addMinutes(10), function () { return Status::all(); }); } echo $timer->stop()->asString() . " "; $timer->start(); for ($i = 0; $i < 10; $i++) { Cache::remember('Status', now()->addMinutes(10), function () { return Status::all(); }); } echo $timer->stop()->asString() . " "; $timer->start(); for ($i = 0; $i < 100; $i++) { Cache::remember('Status', now()->addMinutes(10), function () { return Status::all(); }); } echo $timer->stop()->asString() . " "; echo 'normal '; $timer->start(); for ($i = 0; $i < 1; $i++) { Status::whereValue('pending')->get(); } echo $timer->stop()->asString() . " "; $timer->start(); for ($i = 0; $i < 10; $i++) { Status::whereValue('pending')->get(); } echo $timer->stop()->asString() . " "; $timer->start(); for ($i = 0; $i < 100; $i++) { Status::whereValue('pending')->get(); } echo $timer->stop()->asString() . " "; echo 'cache '; $timer->start(); for ($i = 0; $i < 1; $i++) { Cache::remember('Status', now()->addMinutes(10), function () { Status::whereValue('pending')->get(); }); } echo $timer->stop()->asString() . " "; $timer->start(); for ($i = 0; $i < 10; $i++) { Cache::remember('Status', now()->addMinutes(10), function () { Status::whereValue('pending')->get(); }); } echo $timer->stop()->asString() . " "; $timer->start(); for ($i = 0; $i < 100; $i++) { Cache::remember('Status', now()->addMinutes(10), function () { Status::whereValue('pending')->get(); }); } echo $timer->stop()->asString() . " "; With this output normal 00:00.002 00:00.023 00:00.237 cache 00:00.015 00:00.150 00:01.516 normal 00:00.001 00:00.015 00:00.154 cache 00:00.014 00:00.153 00:01.518 Why does caching make the process 10 times slower? If caching isn't usable for small and simple queries, what should I do to improve my performance? submitted by /u/bububeti [link] [comments]

No comments:

Post a Comment

Thanks