单个大查询或几个小查询哪个更快? [英] Which one is faster single big query or few small queries?

查看:110
本文介绍了单个大查询或几个小查询哪个更快?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

推荐答案

每次到数据库的往返"都会有一些开销.因此,往返次数越少,开销也越少.还请考虑更少的请求意味着更少的从客户端到服务器的数据包.如果合并查询的结果满足您的需求,那么单查询就是您的理想之选.如果您的单个查询返回的是多余或冗余的数据(可能是由于归一化),则单次往返的开销节省可能会在传输的额外数据中丢失.

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.

这篇关于单个大查询或几个小查询哪个更快?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆