简体   繁体   English

SQL Server中的子查询v / s内部联接

[英]Subquery v/s inner join in sql server

I have following queries 我有以下查询

First one using inner join 第一个使用内部联接

SELECT item_ID,item_Code,item_Name 
FROM [Pharmacy].[tblitemHdr] I 
    INNER JOIN  EMR.tblFavourites F ON I.item_ID=F.itemID
WHERE F.doctorID = @doctorId AND F.favType = 'I'

second one using sub query like 第二个使用子查询

SELECT item_ID,item_Code,item_Name from [Pharmacy].[tblitemHdr]
WHERE item_ID IN
(SELECT itemID FROM EMR.tblFavourites
WHERE doctorID = @doctorId AND favType = 'I'
)

In this item table [Pharmacy].[tblitemHdr] Contains 15 columns and 2000 records. 在此表中, [Pharmacy].[tblitemHdr]包含15列和2000条记录。 And [Pharmacy].[tblitemHdr] contains 5 columns and around 100 records. [Pharmacy].[tblitemHdr]包含5栏和大约100条记录。 in this scenario which query gives me better performance? 在这种情况下which query gives me better performance?

Usually joins will work faster than inner queries, but in reality it will depend on the execution plan generated by SQL Server. 通常,联接将比内部查询更快地运行,但实际上,它将取决于SQL Server生成的执行计划。 No matter how you write your query, SQL Server will always transform it on an execution plan. 无论您如何编写查询,SQL Server都将始终根据执行计划对其进行转换。 If it is "smart" enough to generate the same plan from both queries, you will get the same result. 如果它足够“聪明”,可以从两个查询中生成相同的计划,那么您将获得相同的结果。

Here and here some links to help. 这里这里一些帮助的链接。

In Sql Server Management Studio you can enable " Client Statistics " and also Include Actual Execution Plan . 在Sql Server Management Studio中,您可以启用“ 客户端统计信息 ”,还可以包括实际执行计划 This will give you the ability to know precisely the execution time and load of each request. 这使您能够准确知道每个请求的执行时间和负载。

Also between each request clean the cache to avoid cache side effect on performance 还在每个请求之间清理缓存,以避免缓存对性能的副作用

USE <YOURDATABASENAME>;
GO
CHECKPOINT;
GO
DBCC DROPCLEANBUFFERS;
GO

I think it's always best to see with our own eyes than relying on theory ! 我认为用肉眼看总是比依靠理论更好!

join is faster than subquery. 连接比子查询快。

subquery makes for busy disk access, think of hard disk's read-write needle(head?) that goes back and forth when it access: User, SearchExpression, PageSize, DrilldownPageSize, User, SearchExpression, PageSize, DrilldownPageSize, User... and so on. 子查询导致磁盘访问繁忙,请考虑访问硬盘时来回读写的针(头?):User,SearchExpression,PageSize,DrilldownPageSize,User,SearchExpression,PageSize,DrilldownPageSize,User ...等上。

join works by concentrating the operation on the result of the first two tables, any subsequent joins would concentrate joining on the in-memory(or cached to disk) result of the first joined tables, and so on. 联接通过将操作集中在前两个表的结果上而起作用,任何后续联接将把联接集中在第一个联接表的内存中(或缓存到磁盘)结果上,依此类推。 less read-write needle movement, thus faster 更少的读写针移动,从而更快

Source: Here 资料来源: 这里

Sub-query Vs Join 子查询与连接

Table one 20 rows,2 cols 表一20行,2列

Table two 20 rows,2 cols 表两20行,2列

sub-query 20*20 子查询20 * 20

join 20*2 参加20 * 2

logical, rectify 合理地纠正

Detailed 详细

在此处输入图片说明

在此处输入图片说明

The scan count indicates multiplication effect as the system will have to go through again and again to fetch data, for your performance measure, just look at the time 扫描计数表示倍增效果,因为系统必须一次又一次地读取数据,对于性能指标而言,只需查看时间即可

First query is better than second query.. because first query we are joining both table. 第一个查询比第二个查询要好..因为第一个查询我们将两个表都加入了。 and also check the explain plan for both queries... 并检查两个查询的解释计划...

It all depends on data and relational mapping between tables. 这完全取决于表之间的数据和关系映射。 If RDBMS rules are not followed then even the first query will be slow on execution and data fetching. 如果不遵循RDBMS规则,那么即使第一个查询在执行和数据获取上也将很慢。

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

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