找到你要的答案

Q:Which one is faster single big query or few small queries?

Q:哪一个是快单大查询还是几个小查询?

So I want to grab data from database, which method is faster, create several queries or one multi-query?

所以我想从数据库中抓取数据,哪个方法更快,创建多个查询或多个查询?

answer1: 回答1:

Each "round trip" to the database will have some overhead. So the fewer round-trips, the less overhead. Consider also that fewer requests means fewer packets from client to server. If the result of the consolidated query gives you just what you want, then single query is the way to go. If your single query is returning extra or redundant data (perhaps because of de-normalization) then the overhead savings of a single round trip may be lost in the extra data transferred.

Another consideration is latency. If the queries have to be completed in sequence because some part of the output of one is needed in the input of the next, consolidating into one query will cut out all the network latencies in between all the individual smaller queries, so a final result can be delivered faster. However, if the smaller queries are independent of each other, launching them in parallel can get all the results delivered faster, albeit less efficiently.

Bottom line: the answer depends on the specifics of your situation. The best way to get an answer will probably be to implement both ways, test, and compare the resource usage of each implementation.

每个“往返”到数据库将有一些开销。所以往返次数越少,开销越少。还考虑较少的请求意味着从客户端到服务器的数据包更少。如果合并查询的结果给了你所需的信息,则单查询是一种方法。如果您的单个查询返回额外或多余的数据(可能是因为取消正常化),那么在一个单一的往返开销节省可能会丢失额外的数据传输。

另一个考虑是延迟。如果查询必须按顺序完成,因为在下一个输入中需要一部分输出的一部分,合并成一个查询会减少所有较小的查询之间的所有网络延迟,因此最终结果可以更快地传递。然而,如果较小的查询是相互独立的,并行推出可以得到所有的结果交付更快,尽管效率较低。

底线:答案取决于你的情况的具体情况。得到答案的最好方法可能是实现两种方法,测试和比较每个实现的资源使用情况。

answer2: 回答2:

In general it would be a multi query. But it depends on a lot of stuff such as the hardware, datastructure etc.

But connections do take a little time each one of them.

一般来说,这将是一个多查询。但这取决于很多东西,如硬件、数据结构等。

但是连接每一个都需要一点时间。

mysql  multi-query