What is multi_query doing under the hood? - simply sending all requests to the server at the same time, and not one at a time, and receiving all the results at a time. Nothing more complicated.
Does multi_query simply hit the server x number of times and aggregates the results? - He "hits" the server twice - once for sending requests and once for receiving results.
Is there a case where single queries may be more efficient than multiple queries? - Depends on how you define "effective." multi_query() glows on the network, but the memory is heavy, query() works in a loop the other way around.
For many SELECT that return large result sets, memory loss is likely to significantly outweigh the gain in network terms, and most of the time you will be better off issuing queries and processing the result sets one at a time, although it depends on what you do with data. But if you need to run many UPDATE , it is likely that multi_query() will be better, as the return values will simply be successful / unsuccessful and the memory consumption will be bright.
You will have to weigh all factors, such as what you do, how much time you expect from it, latency of the network between the server (client) and the client, available resources (mainly memory) on the server and client, etc. etc .... and take it on a case-by-case basis.
I found this report on some performance tests , made some time ago, when the conclusion is that the overall performance gain can be found from using multi_query() . However, the test script just ran 4 queries, each of which is SELECT in one result, and the definition of “more efficient” is simply “faster”. There is no testing for more queries or large result sets, and although speed is important, this is not all and all - I can do something incredibly fast if I give it an unlimited amount of memory, but trying to do something at the same time will fail. It is also not a real world test, as the end result can be achieved with a single JOIN ed query. It really makes some interesting reads, though.
Personally, I think this is quite academic, because if you run a large set of operators at the same time, 90% of the time they will change only in the transmitted data, and the structure of the requests will remain the same - an obvious candidate for prepared statements.
Daverandom
source share