Should Laravel database connections use cached prepared statements? #46808
Replies: 5 comments 6 replies
-
Hi, @tlaverdure.
I think, well prepared and tested implementation could be part of the framework, but nobody just wants to deal with caching. |
Beta Was this translation helpful? Give feedback.
-
I looked into Wordpress caching a few years ago and I can say that getting it right is not easy.
|
Beta Was this translation helpful? Give feedback.
-
Please note, it seems that the database could be already caching prepared statements, as per MySQL 8.x documentation - https://dev.mysql.com/doc/refman/8.0/en/statement-caching.html So maybe the benchmark is misleading here. |
Beta Was this translation helpful? Give feedback.
-
I think this is a very useful change. Many loop commands can benefit from this. Preparestatement cause 3 times database command (prepare, execute, deallocate)
May be |
Beta Was this translation helpful? Give feedback.
-
Whether or not caching prepared statements is advantageous depends on the specific query and its usage. Decent database software will handle statement caching internally and it will be done more efficiently. |
Beta Was this translation helpful? Give feedback.
-
I was just looking at Illuminate\Database\Connection and noticed that prepared statements are not being reused. It seems like good practice to use an existing prepared statement to avoid recompiling a query and improve performance.
This is also stated in the PHP PDO docs:
https://www.php.net/manual/en/pdo.prepare.php
I've tested this with Laravel Vapor (Octane) and have seen 50-70% improvement on query latency with recurring statements.
Benefits
Implementation:
Are there any known side effects that would discourage this from being part of Laravel?
Beta Was this translation helpful? Give feedback.
All reactions