简体   繁体   English

子查询选择语句vs内部联接

[英]Sub query select statment vs inner join

I'm confused about these two statements who's faster and more common to use and best for memory 我对这两个语句感到困惑,它们是更快,更常见且最适合存储的语句

select p.id, p.name, w.id, w.name 
from person p 
inner join work w on w.id = p.wid 
where p.id in (somenumbers)
vs

select p.id, p.name, (select id from work where id=p.wid) , (select name from work where id=p.wid)
from person p 
where p.id in (somenumbers)

The whole idea of this is that if I have I huge database and I want to make inner join it will take memory and less performance to johin work table and person table but the sub query select statments it will only select one statment at the time so which is the best here 这样做的整个想法是,如果我有一个庞大的数据库,并且想进行内部联接,它将占用内存,并且对johin工作表和人员表的性能降低,但是子查询选择语句,则此时只会选择一个语句。这是最好的

First, the two queries are not the same. 首先,两个查询相同。 The first filters out any rows that have no matching rows in work . 第一个过滤掉work没有匹配行的所有行。

The equivalent first query uses a left join : 等效的第一个查询使用left join

select p.id, p.name, w.id, w.name 
from person p left join
     work w
     on w.id = p.wid 
where p.id in (somenumbers);

Then, the second query can be simplified to: 然后,第二个查询可以简化为:

select p.id, p.name, p.wid,
       (select name from work where w.id = p.wid)
from person p 
where p.id in (somenumbers);

There is no reason to look up the id in work when it is already present in person . 没有任何理由来查找ID在work时,它已经存在于person

If you want optimized queries, then you want indexes on person(id, wid, name) and work(id, name) . 如果要优化查询,则需要对person(id, wid, name)work(id, name)索引。

With these indexes, the two queries should have basically the same performance. 使用这些索引,两个查询应该具有基本相同的性能。 The subquery will use the index on work for fetching the rows from work and the where clause will use the index on person . 子查询将使用索引的work取出来自行workwhere子句将使用索引上person Either query should be fast and scalable. 两种查询都应快速且可扩展。

The subqueries in your second example will execute once for every row , which will perform badly. 第二个示例中的子查询将对每行执行一次 ,这将导致性能下降。 That said, some optimizers may be able to convert it to a join for you - YMMV. 也就是说,某些优化程序可能会为您将其转换为联接-YMMV。

A good rule to follow in general is: much prefer joins to subqueries. 通常要遵循的一个好规则是:非常喜欢联接子查询。

joins give better performance as comparison with sub-query .if there is join on Int column or have index on join column gives best performance . 与子查询相比,joins可以提供更好的性能。如果Int列上有join或join列上有索引可以提供最佳性能。

select p.id, p.name, w.id, w.name 
from person p 
inner join work w on w.id = p.wid 
where p.id in (somenumbers)

It really depends on how you want to optimaze the query (includie but not limited to add/removing/reordering the index), 这实际上取决于您要如何优化查询(包括但不限于添加/删除/重新排序索引),

I found the setup which makes join soars might let subquery suffer, the opposite may also be true. 我发现使连接飙升的设置可能会使子查询遭受损失,相反的情况也可能成立。 Thus there is not that much point to compare them with the same setup. 因此,将它们与相同的设置进行比较没有太多意义。

I choose to use and optimize with join. 我选择使用并优化join。 In my experince join at its best condition setup, rarely loses to subquery, but a lot eaiser to read. 以我的经验加入其最佳状态设置后,很少会丢失子查询,但非常易于阅读。

When the vendor stuff an extreme load of queries with subqueries to the system. 当供应商给系统带来大量查询和子查询时。 Unless the performance start to crawl, due to my other work's query optimization, it simply doesn't worth the effort to change them. 除非性能开始下降,否则由于我其他工作的查询优化问题,根本就不值得去改变它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM