Performance Comparison Between with statements and subqueries

Source: Internet
Author: User

Previously, I shared with you "using with statements to write a slightly complex SQL statement". This time I made a Performance Comparison Between with statements and subqueries.

As instructed by Boyou SingleCat, perform some performance tests on the with statement. The test tool used here is SQL Server Profile. I chose the last statement because it is more complex. At the beginning, they were executed only once and found that the difference was not big, just a few milliseconds. Later, they wanted to execute several more times and execute 10 consecutive times.

Check the execution result. The following is a test statement.

 
 
  1. /* With query */
  2. Declare @ withquery varchar (5000)
  3. Declare @ execcount int = 0
  4. Set @ withquery = 'with TheseEmployees (
  5. Select empid from hr. employees where country = n''usa ''),
  6. CharacteristicFunctions (
  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. /* Subquery */
  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

The SQL Server Profile is as follows:

We can see that the execution time of the subquery statement is less than that of the with statement. I think there is a cross join in the with query that has a Cartesian Product relationship, so I tried the simple one above. below is the test statement.

 
 
  1. /* With statement */
  2. Declare @ withquery varchar (5000)
  3. Declare @ execcount int = 0
  4. Set @ withquery = 'with c (orderyear, custid) (
  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. /* Subquery */
  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

There is still no big gap in this 10 queries. The with statement uses 10 durations and 11 subqueries, sometimes turning over. Therefore, the number of executions is changed to 100. This time the subquery takes less time, as shown below:

The final conclusion is that subqueries are more efficient than with statements.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.