Download presentation
Presentation is loading. Please wait.
Published byJohanna Ingeborg Svensson Modified over 6 years ago
1
In the name of the resources - aggregating data
Dejan Dular In the name of the resources - aggregating data
2
Sponsors
3
About me Dejan Dular .NET Developer SQL Developer
Dynamics CRM specialist IoT fan DIY & eco maniac Twitter: @dejandular Mail: LinkedIn: dejandular
4
Execution plan is your friend
Include actual execution plan (seek for seek) Review suggested missing index – CHANGE THE NAME! Compare client statistics Discard results after query execution [Tools-Options-Query results-Results to grid] Use external tools SSMS Tools Pack SQL Sentry Plan Explorer Test hypothetical indexes WITH (STATISTICS_OLNY = 1) and AUTOPILOT
5
CTE vs @temp vs #temp SPEED CTE "Sub-query" No index or statistics
In memory Re-run possible SPEED * most of the time Table variable In memory* No index or statistics Treated as one row Reusable Temporary table has statistics can be indexed
6
Getting OVER() window functions
SELECT s.TerritoryID, p.FirstName, p.LastName, s.SalesYTD ROW_NUMBER() OVER(PARTITION BY s.TerritoryId ORDER BY s.SalesYTD) FROM Sales.SalesPerson s JOIN Person.Person p ON s.BusinessEntityID = p.BusinessEntityID ORDER TerritoryID FirstName LastName SalesYTD Row Number 1 Pamela Ansman-Wolfe ,13 David Campbell ,94 2 Tete Mensa-Annan ,20 3 Michael Blythe ,18 Jillian Carson ,37 4 Shu Ito ,62 Linda Mitchell ,55 PARTITION
7
CROSS APPLY Get these rows, use (apply) them to this set
and display the data Like a JOIN. Same same, but different. In most cases the same execution plan when only joining the tables Possible to use the rowset on a table valued UDF SELECT P.Id, P.FirstName, P.LastName, O.OrderDate, O.OrderStatus FROM Person P CROSS APPLY ( SELECT TOP 1 OrderDate, OrderStatus FROM SalesOrder SO WHERE SO.CustomerId = P.ID ORDER BY OrderDate DESC ) O WHERE P.zip = '94604'
8
Aggregate on the fly How many products do we have with Tax 0%, 9% and 22% in three price ranges <100€, €, >500€? Tax <100€ 100 – 500€ >=500€ 0% 2263 9201 11726 9% 7081 28700 36114 22% 10347 42001 52567
9
Aggregate, Then Join JOIN is slow
Aggregate if possible to get less rows, then join
10
Intervals ... WHERE Price BETWEEN 100 AND 500 What does BETWEEN x AND y mean? [x,y] | (x,y) | (x,y] | [x,y) Sum of sales over the years in Q1 Year Sales in Q1 2012 64422,11 2013 708132,64 2014 154322,22 2015 201678,12 2016 235213,17
11
Anti-patterns OR between columns in different tables T1 JOIN T2… WHERE T1.col1 = x OR T2.col2 = y WHERE col1 = WHEN 1 THEN 'val1' WHEN 2 THEN 'val2' ELSE col2
12
Simulating production environment
DBCC OPTIMIZER WHAT_IF (undocumented) DBCC Optimizer_WhatIf (1,32) --set 32 CPUs GO DBCC Optimizer_WhatIf (2,131072) --set 128 GB of RAM DBCC Optimizer_WhatIf (3,64) -- 64 bit system -- Execute query DBCC Optimizer_WhatIf (1, 0) --clear CPU DBCC Optimizer_WhatIf (2, 0) --clear RAM DBCC Optimizer_WhatIf (3, 0) --clear 64 bits SET OPTIONS Statistics Numbers of rows and pages in the table Available physical memory Number of CPUs 32bit / 64 bit Export metadata from production database
13
Very important slide Use correct data types
14
Very important slide Use correct data types
Avoid correlated sub-queries SELECT T1.Column1, Column2 = (SELECT T2.Column2 FROM T2 WHERE T2.Col2 = T1.Column2 ) FROM T1
15
Very important slide Use correct data types
Avoid correlated sub-queries Avoid using functions within ON, WHERE or GROUP BY Remove unnecessary tables Foreign keys are not indexed by default NOT IN vs NOT EXISTS Query plan numbers are only estimations – NOT! Use query hints with caution If you are using cursors, you have to buy me a beer! WHERE Column1 = dbo.SomeFunction(Column2) SELECT Car.Id FROM Car JOIN Service ON Car.Id = Service.CarId * ISNULL() is also a function!
16
Another important slide
Do not use query builders Investigate execution plans, check indexes, include selected columns Make as little scans as possible, seek is what you are looking for Limit the size of the working data set Only select fields you really need WITH(NOLOCK), WITH(READPAST) or ISOLATION LEVEL READ UNCOMMITED is OK only when you know what you are doing Configure Index and Table file partitions Upgrade SQL server to newest version
17
while (tooSlow) { Change(); Test(); Evaluate(); }
18
Que?
19
Resources Almost every page has at least one link in the notes section. Use it to learn more about that topic.
20
Session Evaluations: Event Evaluations:
Event Evaluations:
21
Window functions are your friends
This room at 4 PM Unicorns and rainbows included!
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.