以下哪个SQL查询会更快?两个表的联接还是连续的查询? [英] Which of the following SQL queries would be faster? A join on two tables or successive queries?

查看:133
本文介绍了以下哪个SQL查询会更快?两个表的联接还是连续的查询?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里有两个表:

ITEMS
ID| DETAILS| .....| OWNER

USERS:
ID| NAME|....

其中 ITEMS.OWNER = USERS.ID

我正在列出项目以及它们各自的所有者名称。为此,我可以在两个表上使用联接,也可以选择所有的ITEMS并遍历它们,进行SQL查询以检索该ITEM所有者的元组。就像这样:

I'm listing the items out with their respective owners names. For this I could use a join on both tables or I could select all the ITEMS and loop through them making a sql query to retrieve the tuple of that itmes owner. Thats like:

1个具有JOIN
的sql与
1x20单表sql查询

1 sql with a JOIN versus 1x20 single table sql queries

在速度方面,哪种方法更好?
谢谢

Which would be a better apporach to take in terms of speed? Thanks

推荐答案

每个查询都有开销。如果您可以对一个查询执行某项操作,那么(几乎)总是可以对一个查询执行此操作。而且大多数数据库引擎都比您聪明。即使以某种方式拆分查询更好,数据库也会发现自己。

Every query has overhead. If you can do something with one query, it's (almost) always better to do it with one query. And most database engines are smarter than you. Even if it's better to split a query in some way, the database will find out himself.

一个开销示例:如果执行100个查询,将会有很多问题应用程序和Web服务器之间的流量更多。

An example of overhead: if you perform 100 queries, there will be a lot more traffic between your application and your webserver.

通常,如果您真的想了解性能,请对各种方法进行基准测试,并测量您感兴趣的参数并根据becnhmark的结果做出决定。

In general, if you really want to know something about performance, benchmark the various approaches, measure the parameters you're interested in and make a decision based on the results of the becnhmark.

祝你好运!

这篇关于以下哪个SQL查询会更快?两个表的联接还是连续的查询?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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