What is multi_query doing under the hood?
- Just sending all the queries to the server at once instead of one at a time, and retrieving all the results in one go. Nothing more complicated than that.
Does multi_query simply hit the server x number of times and aggregates the results?
- It "hits" the server twice - once to send the queries and once to retrieve the results.
Is there a case where single queries may be more efficient than multiple queries?
- depends how you define "efficient". multi_query()
is light on the network but memory heavy, running query()
in a loop vice versa.
For many SELECT
statements that return large result sets the loss in memory consumption is likely to vastly outweigh the gain in terms of the network and most of the time you'd be better to issue the queries and process the result sets one at a time - although this depends on exactly what you are doing with the data. But if you needed to run many UPDATE
statements, it is likely that multi_query()
would be better since the return values are just success/fail and the memory consumption will be light.
You would have to weigh up all the factors like what you are doing, how long you expect it to take, the network latency between the (database) server and client, the available resources (mostly memory) on the server and the client, etc, etc... and take it on a case by case basis.
I found this record of some performance testing done a while ago, where the conclusion is that there is an overall efficiency gain to be found from using multi_query()
. However the test case was simply running 4 queries, each one SELECT
ing a single result, and the definition of "more efficient" is simply "faster". There is no testing for larger numbers of queries or larger result sets and, while speed is important, it is not the be-all and end-all - I can make anything run incredibly fast if I give it an unlimited amount of memory, but an attempt to do anything concurrently will fail miserably. It is also not a real world test, since the end result could be achieved with a single JOIN
ed query. It does make for some interesting read though.
Personally I feel this is somewhat academic because if you are running a large set of statements at once, 90% of the time they will vary only in the data that is being passed and the query structure will remain the same - which is an obvious candidate for prepared statements.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…