Establish a test environment:
Create a table department and employee, insert a 50W record into the department, insert a 200W record into the employee,
Let's take the question of "How many employee DepartmentID from 150000 to 380000 per department?"
To compare performance, and how they are used more effectively.
Use Test
Go
If object_id (' Employee ') is not Null
Drop Table Employee
If object_id (' Department ') is not Null
Drop Table Department
Go
Create Table Department
(
ID int Identity (a) is not Null,
Name nvarchar () not Null,
Constraint pk_department Primary Key (ID Asc)
)
Go
Create Table Employee
(
ID int Identity (a) is not Null,
DepartmentID int not Null,
Name nvarchar () not Null,
Constraint pk_employee Primary Key (ID Asc),
Constraint fk_employee_departmentid Foreign Key (departmentid) References Department (ID)
)
Create nonclustered Index Ix_employee_departmentid on Employee (DepartmentID ASC)
GO
; With
T0 as (select Id=1 Union all select id=1),
T1 as (Select a.id from T0 as a,t0 as B),
T2 as (Select a.id from T1 as a,t1 as B),
T3 as (Select a.id from T2 as a,t2 as B),
T4 as (Select a.id from T3 as a,t3 as B),
T5 as (Select a.id from T4 as a,t4 as B),
T6 as (Select Id=row_number () over (Order by a.ID) from T5 as a,t5 as B)
Insert into Department (Name)
Select N ' Dept ' +rtrim (ID)
From T6
Where id<=500000
; With
T0 as (select Id=1 Union all select id=1),
T1 as (Select a.id from T0 as a,t0 as B),
T2 as (Select a.id from T1 as a,t1 as B),
T3 as (Select a.id from T2 as a,t2 as B),
T4 as (Select a.id from T3 as a,t3 as B),
T5 as (Select a.id from T4 as a,t4 as B),
T6 as (Select Id=row_number () over (Order by a.ID) from T5 as a,t5 as B)
Insert into Employee (departmentid,name)
Select id%500000+1,n ' Emp ' +rtrim (ID)
From T6
Where id<=2000000
Go
To start the test:
Use Test
Go
DBCC Freeproccache with No_infomsgs
DBCC Dropcleanbuffers with No_infomsgs
Go
Set Statistics Io on
Set Statistics time on
Go
Select A.id,a.name,b.qty
From Department as a
Outer Apply (Select qty=count (*)
From Employee
Where departmentid=a.id
) as B
Where a.id between 150000 and 380000
Select A.id,a.name,b.qty
From Department as a
Left Outer Join (Select qty=count (*), DepartmentID
From Employee
Group by DepartmentID
) as B on b.departmentid=a.id
Where a.id between 150000 and 380000
Go
Set Statistics Time Off
Set Statistics Io Off
/*
SQL Server parse and compile time:
CPU time = 31 milliseconds, elapsed time = 65 milliseconds.
(230001 rows affected)
Table ' Employee '. Scan Count 230001, logical read 735,944 times, physical read 159 times, read 16 times, LOB logic read 0 times, lob physical read 0 times, lob read 0 times.
Table ' Department '. Scan count 1, logical read 1063 times, physical read 3 times, read 1060 times, LOB logic read 0 times, lob physical read 0 times, lob read 0 times.
SQL Server Execution Time:
CPU time = 3094 milliseconds, elapsed time = 6440 milliseconds.
(230001 rows affected)
Table ' Department '. Scan count 1, logical read 1063 times, physical read 22 times, read 0 times, LOB logic read 0 times, lob physical read 0 times, lob read 0 times.
Table ' Employee '. Scan count 1, logical read 1258 times, physical read 6 times, read 8 times, LOB logic read 0 times, lob physical read 0 times, lob read 0 times.
SQL Server Execution Time:
CPU time = 750 milliseconds, elapsed time = 7149 milliseconds.
SQL Server parse and compile time:
CPU time = 0 milliseconds, elapsed time = 1 milliseconds.
*/
As can be seen from the above example, when using outer Apply, the table employee is scanned 230,001 times, from the buffer to fetch the page several times
735944, far more than the number of times the left Outer Join was used.
Again to test a situation where the Outer apply () is not in the where condition, while the left Outer Join is on 1 = 1:
Use Test
Go
DBCC Freeproccache with No_infomsgs
DBCC Dropcleanbuffers with No_infomsgs
Go
Set Statistics IO on
Set Statistics time on
Go
Select A.id,a.name,b.qty
From Department as a
Outer Apply (Select qty=count (*)
From Employee
) as B
Select A.id,a.name,b.qty
From Department as a
Left Outer Join (Select qty=count (*)
From Employee
) as B on 1=1
Go
Set Statistics Time Off
Set Statistics Io Off
/*
SQL Server parse and compile time:
CPU time = 16 milliseconds, elapsed time = 28 milliseconds.
(500000 rows affected)
Table ' worktable '. Scan count 1, logical read 0 times, physical read 0 times, read 0 times, LOB logic read 0 times, lob physical read 0 times, lob read 0 times.
Table ' Employee '. Scan count 1, logical read 2,731 times, physical read 3 times, read 2,716 times, LOB logic read 0 times, lob physical read 0 times, lob read 0 times.
Table ' Department '. Scan count 1, logical read 2,276 times, physical read 3 times, read 2,253 times, LOB logic read 0 times, lob physical read 0 times, lob read 0 times.
SQL Server Execution Time:
CPU time = 812 milliseconds, elapsed time = 13823 milliseconds.
(500000 rows affected)
Table ' worktable '. Scan count 1, logical read 0 times, physical read 0 times, read 0 times, LOB logic read 0 times, lob physical read 0 times, lob read 0 times.
Table ' Employee '. Scan count 1, logical read 2,731 times, physical read 0 times, read 0 times, LOB logic read 0 times, lob physical read 0 times, lob read 0 times.
Table ' Department '. Scan count 1, logical read 2,276 times, physical read 0 times, read 0 times, LOB logic read 0 times, lob physical read 0 times, lob read 0 times.
SQL Server Execution Time:
CPU time = 969 milliseconds, elapsed time = 12327 milliseconds.
SQL Server parse and compile time:
CPU time = 0 milliseconds, elapsed time = 1 milliseconds.
*/
As can be seen here, the two methods, the I/O display information is consistent, and the execution time is relatively close. Performance is comparable.
This is the case with comparable performance, which is also happening in this environment:
Use Test
Go
DBCC Freeproccache with No_infomsgs
DBCC Dropcleanbuffers with No_infomsgs
Go
Set Statistics IO on
Set Statistics time on
Go
Select A.id,a.name,b.qty
From Department as a
Outer Apply (Select qty=count (*)
From Employee
Where departmentid=a.id
) as B
Where a.id=454514
Select A.id,a.name,b.qty
From Department as a
Left Outer Join (Select qty=count (*), DepartmentID
From Employee
Group by DepartmentID
) as B on b.departmentid=a.id
Where a.id=454514
Go
Set Statistics Time Off
Set Statistics Io Off
/*
SQL Server parse and compile time:
CPU time = 15 milliseconds, elapsed time = 37 milliseconds.
(1 rows affected)
Table ' Employee '. Scan count 1, logical read 3 times, physical read 3 times, read 0 times, LOB logic read 0 times, lob physical read 0 times, lob read 0 times.
Table ' Department '. Scan count 0, logical read 3 times, physical read 3 times, read 0 times, LOB logic read 0 times, lob physical read 0 times, lob read 0 times.
SQL Server Execution Time:
CPU time = 0 milliseconds, elapsed time = 35 milliseconds.
(1 rows affected)
Table ' Employee '. Scan count 1, logical read 3 times, physical read 0 times, read 0 times, LOB logic read 0 times, lob physical read 0 times, lob read 0 times.
Table ' Department '. Scan count 0, logical read 3 times, physical read 0 times, read 0 times, LOB logic read 0 times, lob physical read 0 times, lob read 0 times.
SQL Server Execution Time:
CPU time = 0 milliseconds, elapsed time = 1 milliseconds.
SQL Server parse and compile time:
CPU time = 0 milliseconds, elapsed time = 1 milliseconds.
*/
It can be found from several tests that Outer Apply should not be abused in place of the left Outer Join.
In fact, the apply definition in the online help is this:
Use the Apply operator to invoke a table-valued function for each row returned by an external table expression that implements a query operation. The table-valued function acts as the right input and the outer table expression as the left input. The resulting row is combined as the final output by evaluating the right input for each row in the left input. The list of columns generated by the APPLY operator is the column set in the left input, followed by the list of columns returned by the right input. “
When the table-valued function is entered on the right, we can consider using apply, which can bring us a lot of convenience. Or the condition is similar to the last side test, using Outer Apply and the left Outer jion performance is the same. In a more complex environment, be careful, you need to do more testing, use a variety of methods to debug to know which method is good, better performance.
Knowledge Points:
Seen in the second Test,
Table ' worktable '. Scan count 1, logical read 0 times, physical read 0 times, read 0 times, LOB logic read 0 times, lob physical read 0 times, lob read 0 times.
This explains the meaning of "worktable":
The relational engine may need to build a worktable to perform a logical operation specified in an SQL statement. Worktables is internal tables that is used to the hold intermediate results. Worktables is generated for certain GROUP by, ORDER by, or UNION queries. For example, if a ORDER by clause references columns that is not covered by any indexes, the relational engine may need To generate a worktable to sort the result set into the order requested. Worktables is also sometimes used as spools that temporarily hold the result of executing a part of a query plan. Worktables is built in tempdb and is dropped automatically when they is no longer needed.
The relational engine may need to generate a worksheet to perform the logical operations specified in the SQL statement. A worksheet is an internal table used to hold intermediate results. A worksheet is generated in some GROUP by, ORDER by, or UNION queries. For example, if the ORDER BY clause references a column that is not covered by any index, the relational engine may need to generate a worksheet to sort the result set in the order requested. Worksheets are sometimes also used as a spool to temporarily save results from a partial query plan. The worksheet is generated in tempdb and is automatically deleted when it is no longer needed.
Finish
Leftouterjoin and Outerapply Performance comparison (RPM)