Download presentation
Presentation is loading. Please wait.
Published byClementine Bell Modified over 8 years ago
1
SQLSaturday Paris 2015 Plans d’execution Optimisations
2
SQLSaturday Paris 2015 Speakers David BARBARIN Société : Expérience MSSQL : ~ depuis 2002, SQL Server 7 à 2016 Site / Blog : http://blog.developpez.com/mikedavem Mail : mikedavem1@hotmail.com Sarah BESSARD Société : Expérience MSSQL : ~ depuis 2006, SQL Server 7 à 2014 Site / Blog : http://www.concatskills.com Mail : sarah.bessard.pro@gmail.com
3
SQLSaturday Paris 2015 Merci à nos sponsors…
4
SQLSaturday Paris 2015 Merci à nos volontaires…
5
SQLSaturday Paris 2015 Agenda Optimisateur de requêtes Mécanisme de cache Patterns d’optimisation La face cachée de l’optimiseur
6
SQLSaturday Paris 2015 Optimiseur de requête - architecture Simplification Trivial plan Statistics loading Exploration (phases) Convert to executable plan
7
SQLSaturday Paris 2015 Optimiseur de requête – Sortie Parsing/Binding : Arbre logique SELECT c.StoreID, COUNT(*) AS CountOrders FROM Sales.Customer c INNER JOIN Sales.SalesOrderHeader h ON c.CustomerID = h.CustomerID WHEREh.OrderDate > '20061231' GROUP BY c.StoreID
8
SQLSaturday Paris 2015 Optimiseur de requête – Phase de simplification Simplification Trivial plan Statistics loading Exploration (phases) Convert to executable plan Constant folding Domain simplification Predicate push-down Join simplification Contradictions
9
SQLSaturday Paris 2015 Optimiseur de requête – Plusieurs plans possible ou Trivial ? Simplification Trivial plan Statistics loading Exploration (phases) Convert to executable plan
10
SQLSaturday Paris 2015 Optimiseur de requête – Statistics loading Simplification Trivial plan Statistics loading Exploration (phases) Convert to executable plan
11
SQLSaturday Paris 2015 Optimiseur de requête – Processus de recompilation et statistiques Cache lookup Query optimization Load all of interesting statistics Are stats stale? Refresh all of the stats that need refreshing Generates the query plan Set recompilation thresholds of all the tables referenced in the query Check the query plan for correctness related reasons (schema checks) Schema valid? Do we have newer stats available? Any stats stale? Begin query execution Failure Yes No Yes
12
SQLSaturday Paris 2015 Mécanisme de cache – Introduction Begin query execution Simplification Trivial plan Statistics loading Exploration (phases) Convert to executable plan By pass Next query Reuse execution plan in cache Compilation
13
SQLSaturday Paris 2015 Cache et objets compilés Objtype = ‘Proc’ Procédures stockées, triggers, UDFs et TVFs Requêtes préparées Contrôle total des paramètres d’entrées (domaine et type) Recompilation des plans d’exécutions WITH RECOMPILE vs OPTION (RECOMPILE) “parameter sniffing”
14
SQLSaturday Paris 2015 Patterns d’optimisation Kitchen Sink Search ARGumentable #Temp VS @Temp Filtre résiduel
15
SQLSaturday Paris 2015 Patterns d’optimisation : Kitchen Sink DECLARE @CustomerId INT = NULL DECLARE @StartOrderDate DATETIME = '2006-04-01' DECLARE @EndOrderDate DATETIME = NULL DECLARE @MinTotalDue MONEY = 1000 SELECT SalesOrderID FROMSales.SalesOrderHeader WHERE (@CustomerId IS NULL OR CustomerId = @CustomerId) AND (@StartOrderDate IS NULL OR OrderDate >= @StartOrderDate) AND (@EndOrderDate IS NULL OR OrderDate <= @EndOrderDate) AND (@MinTotalDue IS NULL OR TotalDue <= @MinTotalDue)
16
SQLSaturday Paris 2015 Patterns d’optimisation : Kitchen Sink (variante) DECLARE @CustomerId INT = NULL DECLARE @StartOrderDate DATETIME = '2006-04-01' DECLARE @EndOrderDate DATETIME = NULL DECLARE @MinTotalDue MONEY = 1000 SELECT SalesOrderID FROMSales.SalesOrderHeader WHERE CustomerId = ISNULL(@CustomerId, CustomerId) AND OrderDate >= ISNULL(@StartOrderDate, OrderDate) AND OrderDate <= ISNULL(@EndOrderDate, OrderDate) AND TotalDue >= ISNULL(@MinTotalDue, TotalDue )
17
SQLSaturday Paris 2015 Patterns d’optimisation : Kitchen Sink VS sp_executesql DECLARE @CustomerId INT = NULL DECLARE @StartOrderDate DATETIME = '2006-04-01' DECLARE @EndOrderDate DATETIME = NULL DECLARE @MinTotalDue MONEY = 1000 DECLARE @SQLQuery AS NVARCHAR(1500) DECLARE @ParameterDefinition AS NVARCHAR(100) = N'@CustomerId INT, @StartOrderDate DATETIME, @EndOrderDate DATETIME, @MinTotalDue MONEY' SET @SQLQuery = ' SELECT SaleOrderId ' SET @SQLQuery += ' FROMSales.SalesOrderHeader ' SET @SQLQuery += ' WHERE 1 = 1 ' IF @CustomerId IS NOT NULL SET @SQLQuery += ' AND CustomerId = @CustomerId' IF @StartOrderDate IS NOT NULL AND @EndOrderDate IS NOT NULL SET @SQLQuery += ' AND OrderDate BETWEEN @StartOrderDate AND @EndOrderDate' IF @MinTotalDue IS NOT NULL SET @SQLQuery += ' AND TotalDue >= @MinTotalDue' EXECUTE sp_executesql @SQLQuery, @ParameterDefinition, @CustomerId, @StartOrderDate, @EndOrderDate, @MinTotalDue
18
SQLSaturday Paris 2015 Patterns d’optimisation : Condition sargable (Search ARGumentable) Condition non sargableCondition sargable SELECT * FROM Person.Person WHERE LastName LIKE '%Ledyard' SELECT * FROM Person.Person WHERE LastName LIKE 'Ledyard%' SELECT * FROM Sales.SalesOrderHeader WHERE CONVERT(CHAR(10), OrderDate, 103) = CONVERT(CHAR(10), @Value, 103) SELECT * FROM Sales.SalesOrderHeader WHERE OrderDate >= CAST(@Value AS DATE) AND OrderDate < CAST(DATEADD(DAY, 1, @Value) AS DATE) SELECT * FROM Person.Person WHERE UPPER(LastName) = @Value -- Use COLLATION French_CI_AI on column « LastName » SELECT * FROM Person.Person WHERE LastName = @Value
19
SQLSaturday Paris 2015 Patterns d’optimisation : #Temp VS @Temp #Temp@Temp StockageTempdb Nombre de lignes max.Non< 1000 lignes StatistiquesOuiSQL Server 2014 : Oui Traceflag 2453 Index/KeyOui1 PK / 1 UK SQL Server 2014 : Oui TruncateOuiNon RecompilationOuiNon ParallélismeOuiNon TransactionOuiNon ScopeGlobal (##), 1 session (#)1 session
20
SQLSaturday Paris 2015 Patterns d’optimisation : Filtre résiduel SELECT p1.ListPrice FROMProduction.Product p1 INNER JOIN Production.Product p2 ON p1.ProductSubcategoryID = p2.ProductSubcategoryID AND p1.ListPrice - p2.ListPrice = 0
21
SQLSaturday Paris 2015 La face cachée de l’optimiseur
22
SQLSaturday Paris 2015 Questions
23
SQLSaturday Paris 2015 …Et en plus on peut gagner des cadeaux http://GUSS.pro/sqlsat
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.