I have question about reducing database connection in for every time retrieve data from DB. I assume I have 5 tables (Notifications, Orders, Users, Products, Transactions)
In dashboard, I have to show:
For the easiest ways (pseudo-code):
$notis = Notification::where('unread')->get();
$orderStat = Order::join('user')->where('count(order.id)', 'status')->groupby('status');
$product = Product::count();
$transactions = Transaction::join('user')->where('count(trans.id)', 'status')->groupby('status');
So I have to run 4 separate queries, as my mentor said that this solution would reduce the speech of server in case there are many many records in each tables or the dashboard needs more tables (not for joining) to query.
I already did:
I want to ask: Are there any else method to reduce processing time for the above case?.
And other question about connection pool
, he said use it to increase speed. After researching a found Laravel already did connection pool, isn't it?
Edit:
User have many notification, orders, transaction.
I just want to ask which method to improve the performance that I not mention above.
Firstly correct your pseudo code which should be like:
$notis = Notification::where('unread')->get();
$orderStat = Order::join('user')->select('count(order.id)', 'status')->groupBy('status');
$product = Product::count();
$transactions = Transaction::join('user')->select('count(trans.id)', 'status')->groupBy('status');
I have a few additional tips which you can take into consideration:
1) Do not use a query cache (The query cache is deprecated as of MySQL 5.7.20, and is removed in MySQL 8.0.)
2) use InnoDB as Storage Engine for your tables and SQL Server buffer pool to improve a capacity - https://dev.mysql.com/doc/refman/5.5/en/innodb-buffer-pool.html
3) If it is possible do not get all records from query use LIMIT and OFFSET to narrow data (limit and skip method in Laravel or only paginate)
4) If you do not need user data do not use INNER JOIN with users table (if each transaction and order always has user)
5) test requests using ab http://httpd.apache.org/docs/2.2/programs/ab.html to set your server correctly
6) use php >= 7.0 with fpm and opcache it will increase the limit of requests
7) store results of your queries with Cache API https://laravel.com/docs/5.5/cache (update cache if data is changed)
8) Profile Your Queries using https://github.com/barryvdh/laravel-debugbar
At this situation, you better use Cache and doesn't need to hit the database for every time.
It can reduce the processing time and have better performance.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With