哪一个更快单个大查询或少量小查询?

时间:2022-03-28 01:12:42

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

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

2 个解决方案

#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.

底线:答案取决于您的具体情况。获得答案的最佳方式可能是实现两种方式,测试和比较每种实现的资源使用情况。

#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.

但是连接确实需要花费一点时间。

#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.

底线:答案取决于您的具体情况。获得答案的最佳方式可能是实现两种方式,测试和比较每种实现的资源使用情况。

#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.

但是连接确实需要花费一点时间。