-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSQLServer2012_OptimizingQueriesUsingTesting.sql
More file actions
149 lines (129 loc) · 7.78 KB
/
SQLServer2012_OptimizingQueriesUsingTesting.sql
File metadata and controls
149 lines (129 loc) · 7.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
----Note: the following code clears the previous execution plan from the system and should be used for testing
CHECKPOINT;
GO
DBCC DROPCLEANBUFFERS; ---Clears query cache
GO
DBCC FREEPROCCACHE; ---Clears execution plan cache
GO
-----if the tables tblProducts and tblProductSales exist then delete them
if(Exists(select * from INFORMATION_SCHEMA.tables
where table_name = 'tblProductSales'))
begin
drop table tblProductSales
end;
if(Exists(select * from INFORMATION_SCHEMA.tables
where table_name = 'tblProducts'))
begin
drop table tblProducts
end;
------ create tables tblProducts and tblPRoductSales
create table tblProducts
(
Id int identity primary key,
Name varchar(50),
[Description] varchar(100)
)
create table tblProductSales
(
id int primary key identity,
ProductId int foreign key references tblProducts(Id),
UnitPrice money,
QuantitySold int
)
-----------------------------------------------------------
-------- Inserting five hundred thousand rows for testing
Declare @Id int
Set @Id = 1
While(@Id <= 500000)
Begin
Insert into tblProducts values('ProductName-' + CAST(@Id as varchar(20)),
'ProductDesc-' + CAST(@Id as varchar(20)))
Print @Id
Set @Id = @Id + 1
End
select * from tblProducts
---------------------------------------------------------------------
---declare variables to hold a random productId, unit price qtysold
declare @RandomProductId int
declare @RandomUnitPrice money
declare @RandomQuantitySold int
----declare and set variables to generate a random prodid between 1 and 300000 leaving some items unsold in tblProducts
declare @UpperLimitForProdId int
declare @LowerLimitForProdId int
set @LowerLimitForProdId = 1
set @UpperLimitForProdId = 300000
--------
declare @UpperLimitForQtySold int
declare @LowerLimitForQtySold int
set @LowerLimitForQtySold = 1
set @UpperLimitForQtySold = 10
-------- set upper unit price as $100.00
declare @UpperLimitUnitPrice int
declare @LowerLimitUnitPrice int
set @LowerLimitUnitPrice = 1
set @UpperLimitUnitPrice = 100
---- insert one million sales into tblProductSales
declare @Counter int
set @Counter = 1
While(@Counter <= 500000)
Begin
select @RandomProductId = Round(((@UpperLimitForProdId - @LowerLimitForProdId) * RAND() + @LowerLimitForProdId),0)
select @RandomQuantitySold = Round(((@UpperLimitForQtySold - @LowerLimitForQtySold) * RAND() + @LowerLimitForQtySold),0)
select @RandomUnitPrice = Round(((@UpperLimitUnitPrice - @LowerLimitUnitPrice) * RAND() + @LowerLimitUnitPrice),0)
insert into tblProductSales values (@RandomProductId,@RandomUnitPrice,@RandomQuantitySold)
print @Counter
set @Counter = @Counter + 1
End
select * from tblProductSales
------ Which is better a subquery or a join?
----Note: the following code clears the previous execution plan from the system and should be used for testing
CHECKPOINT;
GO
DBCC DROPCLEANBUFFERS; ---Clears query cache
GO
DBCC FREEPROCCACHE; ---Clears execution plan cache
GO
--- USE 'CLIENT STATISTICS' AND 'ACTUAL EXECUTION PLAN' TO COMPARE METHODS
---- subquery
---3 seconds returning 243276 records for test 1
select
Id,
Name
from
tblProducts
where Id in (select productId from tblProductSales);
---- join
select
distinct
tblProducts.Id,
Name
from
tblProducts
inner join tblProductSales
on tblProducts.id = tblProductSales.ProductId;
CHECKPOINT;
GO
DBCC DROPCLEANBUFFERS; ---Clears query cache
GO
DBCC FREEPROCCACHE; ---Clears execution plan cache
GO
----- subquery vs. join testing
---subquery
select
Id,
Name,
[Description]
from
tblProducts
where Not Exists(select * from tblProductSales where ProductId = tblProducts.Id);
---Join
Select
tblProducts.Id,
Name,
[Description]
from
tblProducts
left join tblProductSales
on tblProducts.Id = tblProductSales.ProductId
where tblProductSales.ProductId IS NULL;