Is mysqli::multi_query more efficient than several single queries?

前端 未结 2 1675
借酒劲吻你
借酒劲吻你 2020-12-16 04:05

Someone brought up the MySQLi multi_query function in an answer claiming that it would be better than looping through 3 separate queries. I tried to Google some sort of answ

2条回答
  •  时光取名叫无心
    2020-12-16 04:55

    The answer above is wrong.

    What is multi_query doing under the hood?

    It just does send all the queries to the server at once, while retrieving the result for the first one only. So, in effect, multi_query is a sort of wrapper for the asynchronous query execution.

    Does multi_query simply hit the server x number of times and aggregates the results?

    multi_query hits the server only once. All the other hits you have to do manually, by calling next_result, in order to get all the other queries' results.

    Is there a case where single queries may be more efficient than multiple queries?

    this question assumes that there is a case when multi_query is faster. Which is a questionable statement. For the usual query, the network latency is a negligible part of the whole execution time. If you're really concerned in the speed by such margin, take a look a the HandlerSocket - it will be blazing fast. However, for a regular development such a difference between single and multi query would be your least concern. If you care for the real life matters, not imaginary ones.

提交回复
热议问题