Download presentation
Presentation is loading. Please wait.
Published bySaara Tamminen Modified over 6 years ago
1
“Magic numbers”, local variable and performance
Як стиль написання запитів впливає на швидкодію системи Андрій Зробок
2
Need Java / NET programmer
Java/NET programming – 80% SQL programming – 20% Framework Query builder Simple queries etc. 2 | 4/7/2019 | Magic numbers, local variables and performance
3
Java “Magic numbers” anti-pattern
3 | 4/7/2019 | Magic numbers, local variables and performance
4
Test data database: AdventureWorks2012 table: Person.Address 4 |
4 | 4/7/2019 | Magic numbers, local variables and performance
5
“Magic numbers” in SQL –code
/*original code*/ select addressid, AddressLine1, addressline2, city from [Person].[Address] where [StateProvinceID] = 9 go from [Person].[Address] where [StateProvinceID] = 119 /*refactored code*/ int = 9 select addressid, AddressLine1, addressline2, city from [Person].[Address] where [StateProvinceID] go int = 119 from [Person].[Address] where [StateProvinceID] 5 | 4/7/2019 | Magic numbers, local variables and performance
6
Original queries (IO stat, execution plans)
(4564 row(s) affected) Table 'Address'. Scan count 1, logical reads 216, … (1 row(s) affected) logical reads 4, … 6 | 4/7/2019 | Magic numbers, local variables and performance
7
Original queries (Rows Estimated vs Actual)
7 | 4/7/2019 | Magic numbers, local variables and performance
8
“New” queries (IO stat, execution plans)
(4564 row(s) affected) Table 'Address'. Scan count 1, logical reads 216, … (1 row(s) affected) 8 | 4/7/2019 | Magic numbers, local variables and performance
9
“New” queries (Rows Estimated vs Actual)
9 | 4/7/2019 | Magic numbers, local variables and performance
10
Statistics. DBCC SHOW_STATISTICS ('[Person].[Address]', IX_Address_StateProvinceID) 10 | 4/7/2019 | Magic numbers, local variables and performance
11
Statistics: Estimated Row Count
11 | 4/7/2019 | Magic numbers, local variables and performance
12
Statistics: 30% declare @i int = 179
select addressid, AddressLine1, addressline2, city from [Person].[Address] where [StateProvinceID] go (62 row(s) affected) Table 'Address'. Scan count 1, logical reads 216, … 12 | 4/7/2019 | Magic numbers, local variables and performance
13
Statistics: 30% 13 | 4/7/2019 | Magic numbers, local variables and performance
14
Statistics: 30% declare @i int = 0
select addressid, AddressLine1, addressline2, city from [Person].[Address] where [StateProvinceID] go 14 | 4/7/2019 | Magic numbers, local variables and performance
15
Execution plan – re-usage
Parser – makes sure that the T-SQL query has a valid syntax, Output – Parse Tree Algebrizer – is responsible for objects and columns names verification. identifies all data types which are being processed for a given query. verify that GROUP BY and aggregate columns are placed in right place or not Output – Algebrized Tree Optimizer – execution plan creation Output – Execution Plan Execution – query execution 15 | 4/7/2019 | Magic numbers, local variables and performance
16
Execution plan – re-usage
SELECT A.col5, SUM(C.col6) AS col6sum FROM TableA AS A INNER JOIN TableB AS B ON A.col1 = B.col1 INNER JOIN TableC AS C ON B.col2 = c.col2 WHERE A.col3 = constant1 AND B.col4 = constant2 GROUP BY A.col5; FROM (3! = 6 OR (4! / 2!) = 12 variants) : left-deep tree: JOIN( JOIN( JOIN(A, B), C), D). n! (6 tables – 720) bushy tree: JOIN(JOIN(A, B), JOIN(C, D)). (2n-2)!/(n-1)! (6 tables – 30,240) Realization (connection type): (Hash, merge, nested loop, star): 4 Access (index seek, index scan, table scan): 3 WHERE: AND : 2 GROUP BY: 2 ways (Stream (ordered) / Hash Match aggregate ) 6*4*2*2*3 = 288 OR 12 *4*2*2*3 = 576 16 | 4/7/2019 | Magic numbers, local variables and performance
17
Parameterization auto forced manual DBCC FREEPROCCACHE GO
select addressid, AddressLine1, addressline2, city from [Person].[Address] where [StateProvinceID] = 9 int = 119 from [Person].[Address] where [StateProvinceID] from [Person].[Address] where [StateProvinceID] = 119 int = 9 where [StateProvinceID] auto forced manual 17 | 4/7/2019 | Magic numbers, local variables and performance
18
Parameterization: Adhoc queries
SELECT usecounts, cacheobjtype, objtype, text FROM sys.dm_exec_cached_plans CROSS APPLY sys.dm_exec_sql_text(plan_handle) WHERE text like '%Address%' ORDER BY usecounts DESC; GO 18 | 4/7/2019 | Magic numbers, local variables and performance
19
Auto Parameterization
select addressid, addressline1, addressline2, city from [Person].[Address] where [AddressID] = 9; go select addressid, addressline1, addressline2, city from [Person].[Address] where [AddressID] = 119; 19 | 4/7/2019 | Magic numbers, local variables and performance
20
Forced Parameterization
alter database AdventureWorks2012 set parameterization forced; go select addressid, addressline1, addressline2, city from [Person].[Address] where [StateProvinceID] = 9; select addressid, addressline1, addressline2, city from [Person].[Address] where [StateProvinceID] = 119; alter database AdventureWorks2012 set parameterization simple; 20 | 4/7/2019 | Magic numbers, local variables and performance
21
Manual Parameterization (sp_executeSQL)
nvarchar(max) nvarchar(max) int = N'select addressid, addressline1, addressline2, city from [Person].[Address] where [StateProvinceID] = int' = 9 execute = 119 go 21 | 4/7/2019 | Magic numbers, local variables and performance
22
Manual Parameterization (procedures)
create procedure dbo.get_address int ) as begin select addressid, addressline1, addressline2, city from [Person].[Address] where [StateProvinceID] end exec dbo.get_address 9 go exec dbo.get_address 119 22 | 4/7/2019 | Magic numbers, local variables and performance
23
SP side effect: non-effective plan
23 | 4/7/2019 | Magic numbers, local variables and performance
24
SP side effect: non-effective plan
24 | 4/7/2019 | Magic numbers, local variables and performance
25
Parameterization side effect: data conversion
25 | 4/7/2019 | Magic numbers, local variables and performance
26
Non-effective plan: how to avoid
exec dbo.get_address 9 go exec sp_recompile 'dbo.get_address' exec dbo.get_address 119 exec dbo.get_address 9 go exec dbo.get_address 119 WITH RECOMPILE create procedure dbo.get_address int) WITH RECOMPILE as begin select addressid, addressline1, addressline2, city from [Person].[Address] where [StateProvinceID] end go Query hint: OPTION (RECOMPILE) 26 | 4/7/2019 | Magic numbers, local variables and performance
27
local variable: how to generate optimal plan
int = 119 select addressid, AddressLine1, addressline2, city from [Person].[Address] where [StateProvinceID] OPTION (recompile) go 27 | 4/7/2019 | Magic numbers, local variables and performance
28
No caching Option (Recompile) declare @state_gironde_FR int = 119
select addressid, AddressLine1, addressline2, city from [Person].[Address] where [StateProvinceID] OPTION (recompile) go select addressid, AddressLine1, addressline2, city from [Person].[Address] where [StateProvinceID] = 119 go No caching 28 | 4/7/2019 | Magic numbers, local variables and performance
29
Circle closed Option (recompile) Ad hoc queries Stored procedures
Parameterization Stored procedures Option (recompile) 29 | 4/7/2019 | Magic numbers, local variables and performance
30
Side effect of the parameter sniffing
create procedure dbo.get_address_sniffing int ) as begin + 1 select addressid, addressline1, addressline2, city from [Person].[Address] where [StateProvinceID] end Go DBCC FREEPROCCACHE exec dbo.get_address_sniffing 9 go 30 | 4/7/2019 | Magic numbers, local variables and performance
31
Side effect (parameter sniffing): how to avoid
create procedure dbo.get_address_sniffing int ) as begin + 1 select addressid, addressline1, addressline2, city from [Person].[Address] where [StateProvinceID] OPTION (recompile) end Go DBCC FREEPROCCACHE exec dbo.get_address_sniffing 9 go 31 | 4/7/2019 | Magic numbers, local variables and performance
32
Option (Recompile): data filtering
if object_id('dbo.get_address_by_filter','P') is not null drop procedure dbo.get_address_by_filter go create procedure dbo.get_address_by_filter ( @city nvarchar(30) = null nvarchar(15) = null int = null ) as begin SELECT addressid, AddressLine1, addressline2, city FROM [AdventureWorks2012].[Person].[Address] where (city is null) and (postalcode is null) and (stateprovinceid is null) end 32 | 4/7/2019 | Magic numbers, local variables and performance
33
Option (Recompile): data filtering
execute dbo.get_address_by_filter @stateprovinceid = 48 = N'Las Cruces' 33 | 4/7/2019 | Magic numbers, local variables and performance
34
Option (Recompile): data filtering
if object_id('dbo.get_address_by_filter','P') is not null drop procedure dbo.get_address_by_filter go create procedure dbo.get_address_by_filter ( @city nvarchar(30) = null nvarchar(15) = null int = null ) as begin SELECT addressid, AddressLine1, addressline2, city FROM [AdventureWorks2012].[Person].[Address] where (city is null) and (postalcode is null) and (stateprovinceid is null) option (recompile) end 34 | 4/7/2019 | Magic numbers, local variables and performance
35
Option (Recompile): data filtering
execute dbo.get_address_by_filter @stateprovinceid = 48 = N'Las Cruces' 35 | 4/7/2019 | Magic numbers, local variables and performance
36
Option (Recompile): data ordering
if object_id('dbo.get_address_by_order','P') is not null drop procedure dbo.get_address_by_order go create procedure dbo.get_address_by_order ( @orderby nvarchar(254) ) as begin SELECT top 100 addressid, AddressLine1, addressline2, city FROM [AdventureWorks2012].[Person].[Address] order by when N'city' then city when N'postalcode' then postalcode when N'stateprovinceid' then stateprovinceid end 36 | 4/7/2019 | Magic numbers, local variables and performance
37
Option (Recompile): data ordering
execute dbo.get_address_by_order N'stateprovinceid' 37 | 4/7/2019 | Magic numbers, local variables and performance
38
Option (Recompile): data ordering
if object_id('dbo.get_address_by_order','P') is not null drop procedure dbo.get_address_by_order go create procedure dbo.get_address_by_order ( @orderby nvarchar(254) ) as begin SELECT top 100 addressid, AddressLine1, addressline2, city FROM [AdventureWorks2012].[Person].[Address] order by when N'city' then city when N'postalcode' then postalcode when N'stateprovinceid' then stateprovinceid End Option (recompile) end 38 | 4/7/2019 | Magic numbers, local variables and performance
39
Option (Recompile): data ordering
execute dbo.get_address_by_order N'stateprovinceid' SELECT top 100 addressid, AddressLine1, addressline2, city FROM [Person].[Address] order by stateprovinceid 39 | 4/7/2019 | Magic numbers, local variables and performance
40
Option (Recompile): data ordering
execute dbo.get_address_by_order N‘city' order by when N'city' then city when N'postalcode' then postalcode when N'stateprovinceid' then stateprovinceid End 40 | 4/7/2019 | Magic numbers, local variables and performance
41
Option (Recompile): data ordering
if object_id('dbo.get_address_by_order','P') is not null drop procedure dbo.get_address_by_order go create procedure dbo.get_address_by_order ( @orderby nvarchar(254) ) as begin SELECT top 100 addressid, AddressLine1, addressline2, city FROM [AdventureWorks2012].[Person].[Address] order by when N'city' then city when N'postalcode' then postalcode when N'stateprovinceid' then stateprovinceid else cast('' as sql_variant) end option (recompile) 41 | 4/7/2019 | Magic numbers, local variables and performance
42
“Magic numbers”, local variable and performance
Q & A The end 42 | 4/7/2019 | Magic numbers, local variables and performance
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.