SQL点滴之with语句和子查询的性能比较

数据库 SQL Server
之前笔者和大家分享了《使用with语句来写一个稍微复杂sql语句》,这一次笔者针对with语句和子查询做了一个性能的比较。

之前笔者和大家分享了《使用with语句来写一个稍微复杂sql语句》,这一次笔者针对with语句和子查询做了一个性能的比较。

在博友SingleCat的提醒下,对with语句做一些性能测试,这里使用的测试工具是SQL Server Profile。我选择了***一个语句,因为这个语句比较复杂一点。开始的时候单独执行一次发现他们的差别不大,就差几个毫秒,后来想让他们多执行几次,连续执行10

次看看执行的结果。下面贴出测试用的语句。

  1. /*with查询*/  
  2. declare @withquery varchar(5000)  
  3. declare @execcount int=0  
  4. set @withquery='with TheseEmployees as(  
  5. select empid from hr.employees where country=N''USA''),  
  6. CharacteristicFunctions as(  
  7. select custid,  
  8.        case when custid in (select custid from sales.orders as o where o.empid=e.empid) then 1 else 0 end as charfun  
  9. from sales.customers as c cross join TheseEmployees as e)  
  10. select custid from CharacteristicFunctions group by custid having min(charfun)=1 order by custid  
  11. '  
  12. while @execcount<10  
  13. begin 
  14. exec (@withquery);  
  15. set @execcount=@execcount+1  
  16. end 
  17.  
  18. /*子查询*/  
  19. declare @subquery varchar(5000)  
  20. declare @execcount int=0  
  21. set @subquery='select custid from Sales.Orders where empid in 
  22. (select empid from HR.Employees where country = N''USA''group by custid  
  23. having count(distinct empid)=(select count(*) from HR.Employees where country = N''USA'');  
  24. '  
  25. while @execcount<10  
  26. begin 
  27. exec (@subquery);  
  28. set @execcount=@execcount+1  
  29. end 

从SQL Server Profile中截图如下

从图中可以看到子查询语句的执行时间要少于with语句,我觉得主要是with查询中有一个cross join做了笛卡尔积的关系,于是又实验了上面的那个简单一点的,下面是测试语句。

  1. /*with语句*/  
  2. declare @withquery varchar(5000)  
  3. declare @execcount int=0  
  4. set @withquery='with c(orderyear,custid) as(  
  5. select YEAR(orderdate),custid from sales.orders)  
  6. select orderyear,COUNT(distinct(custid)) numCusts from c group by c.orderyear'   
  7. while @execcount<100  
  8. begin 
  9. exec (@withquery);  
  10. set @execcount=@execcount+1  
  11. end 
  12.  
  13. /*子查询*/  
  14. declare @subquery varchar(5000)  
  15. declare @execcount int=0  
  16. set @subquery='select orderyear,COUNT(distinct(custid)) numCusts  
  17. from (select YEAR(orderdate),custid from sales.orders) as D(orderyear,custid)  
  18. group by orderyear'  
  19. while @execcount<100  
  20. begin 
  21. exec (@subquery);  
  22. set @execcount=@execcount+1  
  23. end 

这次做10次查询还是没有多大的差距,with语句用10个duration,子查询用了11个,有时候还会翻过来。于是把执行次数改成100,这次还是子查询使用的时间要少,截图如下

最终结论,子查询好比with语句效率高。

原文链接:http://www.cnblogs.com/tylerdonet/archive/2011/04/18/2020225.html

【编辑推荐】

  1. SQL点滴之使用attach功能出现错误及解决方法
  2. SQL点滴之一个简单的字符串分割函数
  3. SQL点滴之重置win7登录密码对SQL登录的影响
  4. SQL点滴之SSIS中的事务处理
  5. SQL点滴之使用with语句来写一个稍微复杂sql语句

 

责任编辑:艾婧 来源: 博客园
相关推荐

2013-12-16 10:20:48

MySQL数据库

2011-08-02 13:04:40

SQL Server

2012-12-03 10:26:51

Scala

2015-02-05 09:25:51

HTTPSSPDYHTTP2

2020-07-27 08:24:42

编程语言C语言Java

2011-09-09 10:10:13

SQL数据库点滴

2014-08-20 09:49:50

虚拟机Linux Conta

2011-04-27 16:34:06

withSQL Server

2011-04-15 10:26:38

JavaMVC

2011-08-03 13:32:00

SQL Server优化

2011-04-20 11:11:33

SQLSET QUOTED_

2009-05-25 08:39:08

iPhone苹果移动OS

2011-05-18 14:52:04

XML

2009-07-01 18:12:18

JSP的优势性能比较

2011-08-09 10:21:55

SQL Server存储过程分页

2009-12-16 14:10:22

路由技术性能比较

2010-03-10 16:35:23

Python编程语言

2011-07-06 14:18:40

Percona SerMySQL

2023-11-20 10:34:09

语言

2009-12-16 14:10:12

路由技术性能比较
点赞
收藏

51CTO技术栈公众号