SlideShare une entreprise Scribd logo
1  sur  23
Execution Plans 
Ram Kedem
Copyright 2014 © Ram Kedem. All rights reserved. Not to be reproduced without written consent 
Display Estimated Execution Plan 
Display EEP 
•First time for query number 1 
•Second time for query number 2 
•Third time for both of them - which cost is cheaper and why ? 
•You can use CTRL-L as a shortcut to Display Estimated Execution Plan 
SELECT ProductID, Name, Color 
FROM Production.Product 
WHERE Color IN ('Blue','Red') 
ORDER BY Name; 
GO 
SELECT ProductID, Name, Color 
FROM Production.Product 
WHERE Color IN ('Blue','Red') 
ORDER BY ProductID; 
GO
Copyright 2014 © Ram Kedem. All rights reserved. Not to be reproduced without written consent 
Display Estimated Execution Plan 
Display EEP 
•First time for CREATE TABLE 
•Second time for INSERT, is it possible ? 
•Third time for both of them, again - is it possible ? 
CREATE TABLE SomeTable 
( SomeTableID INT IDENTITY(1, 1) PRIMARY KEY, 
FullName varchar(35)) 
INSERT INTO SomeTable VALUES('Hello'),('There'); 
SELECT * FROM SomeTable; 
DROP TABLE SomeTable; 
GO
Copyright 2014 © Ram Kedem. All rights reserved. Not to be reproduced without written consent 
Display Estimated Execution Plan 
•Review the following tables : 
SELECT * FROM [Production].[Product] 
SELECT * FROM [Production].[ProductModel] 
•Display Estimated Execution Plan 
•First time for query number 1 
•Second time for query number 2 
•Now use the estimated execution plan to compare two queries, How do you explain that such different queries return the same plan? 
SELECT p.ProductID, p.Name, p.Color, p.Size 
FROM Production.Product AS p 
LEFT OUTER JOIN Production.ProductModel AS pm 
ON p.ProductModelID = pm.ProductModelID 
WHERE pm.ProductModelID IS NOT NULL; 
GO 
SELECT p.ProductID, p.Name, p.Color, p.Size 
FROM Production.Product AS p 
WHERE EXISTS(SELECT 1 FROM Production.ProductModel AS pm 
WHERE p.ProductModelID = pm.ProductModelID); 
GO
Copyright 2014 © Ram Kedem. All rights reserved. Not to be reproduced without written consent 
Include Actual Execution Plan 
•Execute the query and include Actual Execution Plan 
•Shortcut for Include Actual Execution Plan : CTRL + M 
SELECT ProductID, Name, Color 
FROM Production.Product 
WHERE Color IN ('Blue','Red') 
ORDER BY Name; 
GO 
•Save EP & Show EP as XML 
•Right-click on the plan and choose "Save Execution Plan As" 
•Right-click on the plan and choose "Show Execution Plan XML"
Copyright 2014 © Ram Kedem. All rights reserved. Not to be reproduced without written consent 
SET SHOWPLAN_TEXT ON / OFF 
•Causes Microsoft® SQL Server™ not to execute Transact-SQL statements. Instead, SQL Server returns detailed information about how the statements are executed. 
•Can be executed via SSMS or SQLCMD 
•In order to use SQLCMD from command prompt type -SQLCMD -S <server name>/<instance name) for example : sqlcmd -S WIN-48RSPI12UJHINST01 
USE AdventureWorks2012; 
GO 
SET SHOWPLAN_TEXT ON; 
GO 
SELECT * 
FROM Production.Product 
WHERE ProductID = 905; 
GO 
SET SHOWPLAN_TEXT OFF; 
GO
Copyright 2014 © Ram Kedem. All rights reserved. Not to be reproduced without written consent 
SET SHOWPLAN_ALL ON / OFF 
•Causes Microsoft SQL Server not to execute Transact-SQL statements. Instead, SQL Server returns detailed information about how the statements are executed and provides estimates of the resource requirements for the statements 
•Can be executed via SSMS or SQLCMD 
•In order to use SQLCMD from command prompt type - SQLCMD -S <server name>/<instance name) for example : sqlcmd -S WIN-48RSPI12UJHINST01 
USE AdventureWorks2012; 
GO 
SET SHOWPLAN_ALL ON; 
GO 
SELECT * 
FROM Production.Product 
WHERE ProductID = 905; 
GO 
SET SHOWPLAN_ALL OFF; 
GO
Different Access Paths
Copyright 2014 © Ram Kedem. All rights reserved. Not to be reproduced without written consent 
Heap Table 
•Full Table Scan 
•Non Clustered Index Seek 
•Non Clustered Index Scan 
SELECT * FROM sys.indexes WHERE object_id = object_id ('dbo.DatabaseLog') 
-- 1. Full Table Scan 
--------------------- 
SELECT * FROM dbo.DatabaseLog; 
GO 
SELECT * FROM dbo.DatabaseLog WHERE event = 'ALTER_TABLE' 
-- 2. Non Clustered Index Seek 
------------------------------------------ 
-- retrieves selective rows from the table 
SELECT * FROM dbo.DatabaseLog WHERE DatabaseLogID = 1 ; 
GO 
-- 3. Non Clustered Index Scan 
-------------------------------------- - 
-- retrieves all the rows from the index 
SELECT DatabaseLogID From DatabaseLog
Copyright 2014 © Ram Kedem. All rights reserved. Not to be reproduced without written consent 
Clustered Index 
•Clustered Index Scan 
•Clustered Index Seek 
•Non Clustered Index Seek 
•Non Clustered Index Scan 
SELECT * FROM sys.indexes WHERE object_id = object_id ('Person.ContactType') 
-- 1. Clustered Index Scan 
--------------------------------------- 
-- retrieves all the rows from the table 
SELECT * FROM Person.ContactType; 
GO 
-- 2. Clustered Index Seek 
------------------------------------------ 
-- retrieves selective rows from the table 
SELECT * FROM Person.ContactType WHERE [ContactTypeID] = 12; 
GO 
SELECT * FROM Person.ContactType WHERE [ContactTypeID] BETWEEN 10 AND 20; 
GO
Copyright 2014 © Ram Kedem. All rights reserved. Not to be reproduced without written consent 
Clustered Index 
-- 3. Non Clustered Index Seek 
------------------------------------------ 
-- retrieves selective rows from the table 
SELECT * FROM Person.ContactType WHERE [Name] = 'some value'; 
GO 
-- 4. Non Clustered Index Scan 
---------------------------------------- 
-- retrieves all the rows from the index 
SELECT [Name] FROM Person.ContactType ; 
GO
Copyright 2014 © Ram Kedem. All rights reserved. Not to be reproduced without written consent 
Sort Operations 
SELECT * FROM sys.indexes WHERE object_id = object_id ('Production.ProductInventory') 
-- Compare the different queries and note the costs : 
SELECT * 
FROM Production.ProductInventory 
ORDER BY Shelf; 
GO 
SELECT * 
FROM Production.ProductInventory 
ORDER BY [ProductID] , [LocationID]; 
GO 
SELECT * 
FROM Production.ProductInventory 
ORDER BY [LocationID] , [ProductID] ; 
GO
Copyright 2014 © Ram Kedem. All rights reserved. Not to be reproduced without written consent 
Merge Join VS. Hash Join VS. Nested Loop 
-------------------------------------------------------------------------------------------- 
-- Test 1, Big Table 
-------------------------------------------------------------------------------------------- 
DROP TABLE tableA 
GO 
DROP TABLE tableB 
GO 
create table tableA (id int identity ,name varchar(50)) 
declare @i int 
set @i=0 
while (@i<100) 
begin 
insert into tableA (name) 
select name from master.dbo.spt_values 
set @i=@i+1 
end 
go 
select COUNT(*) from dbo.tableA --251500
Copyright 2014 © Ram Kedem. All rights reserved. Not to be reproduced without written consent 
Merge Join VS. Hash Join VS. Nested Loop 
create table tableB (id int identity ,name varchar(50)) 
declare @i int 
set @i=0 
while (@i<100) 
begin 
insert into tableB (name) 
select name from master.dbo.spt_values 
set @i=@i+1 
end 
select COUNT(*) from dbo.tableB --251500 
SELECT TOP(10) * FROM dbo.tableB 
SELECT TOP(10) * FROM dbo.tableA 
select TOP(10) * from dbo.tableA A join tableB B 
on (a.id=b.id) 
select COUNT(*) from dbo.tableA A join tableB B 
on (a.id=b.id)
Copyright 2014 © Ram Kedem. All rights reserved. Not to be reproduced without written consent 
Merge Join VS. Hash Join VS. Nested Loop 
----------------------------------------------------- 
-- Test 1A - Both tables dont have index - Hash Join 
----------------------------------------------------- 
-- Check Excecution Plan 
select * from dbo.tableA A join tableB B 
on (a.id=b.id) 
----------------------------------------------------- 
-- Test 1B - One table have index - Hash Join 
----------------------------------------------------- 
create unique clustered index cx_tableA on tableA (id) 
-- Check Excecution Plan again 
select * from dbo.tableA A join tableB B 
on (a.id=b.id) 
----------------------------------------------------- 
-- Test 1C - Both tables have index - Merge Join 
----------------------------------------------------- 
create unique clustered index cx_tableB on tableB (id) 
-- Check Excecution Plan again 
select * from dbo.tableA A join tableB B 
on (a.id=b.id)
Copyright 2014 © Ram Kedem. All rights reserved. Not to be reproduced without written consent 
Merge Join VS. Hash Join VS. Nested Loop 
-------------------------------------------------------------------------------------------- 
-- Test 2, Medium Table 
-------------------------------------------------------------------------------------------- 
DROP TABLE tableA 
GO 
DROP TABLE tableB 
GO 
create table tableA (id int identity ,name varchar(50)) 
declare @i int 
set @i=0 
while (@i<1) 
begin 
insert into tableA (name) 
select name from master.dbo.spt_values 
set @i=@i+1 
end 
go 
select COUNT(*) from dbo.tableA --2515
Copyright 2014 © Ram Kedem. All rights reserved. Not to be reproduced without written consent 
Merge Join VS. Hash Join VS. Nested Loop 
create table tableB (id int identity ,name varchar(50)) 
declare @i int 
set @i=0 
while (@i<1) 
begin 
insert into tableB (name) 
select name from master.dbo.spt_values 
set @i=@i+1 
end 
select COUNT(*) from dbo.tableB --2515 
SELECT TOP(10) * FROM dbo.tableB 
SELECT TOP(10) * FROM dbo.tableA 
select TOP(10) * from dbo.tableA A join tableB B 
on (a.id=b.id) 
select COUNT(*) from dbo.tableA A join tableB B 
on (a.id=b.id)
Copyright 2014 © Ram Kedem. All rights reserved. Not to be reproduced without written consent 
Merge Join VS. Hash Join VS. Nested Loop 
----------------------------------------------------- 
-- Test 2A - Both tables dont have index - Hash Join 
----------------------------------------------------- 
-- Check Excecution Plan 
select * from dbo.tableA A join tableB B 
on (a.id=b.id) 
----------------------------------------------------- 
-- Test 2B - One table have index - Merge Join 
----------------------------------------------------- 
create unique clustered index cx_tableA on tableA (id) 
-- Check Excecution Plan again 
select * from dbo.tableA A join tableB B 
on (a.id=b.id) 
----------------------------------------------------- 
-- Test 2C - Both tables have index - Merge Join 
----------------------------------------------------- 
create unique clustered index cx_tableB on tableB (id) 
-- Check Excecution Plan again 
select * from dbo.tableA A join tableB B 
on (a.id=b.id)
Copyright 2014 © Ram Kedem. All rights reserved. Not to be reproduced without written consent 
Merge Join VS. Hash Join VS. Nested Loop 
-------------------------------------------------------------------------------------------- 
-- Test 3, Small Table 
-------------------------------------------------------------------------------------------- 
DROP TABLE tableA 
GO 
DROP TABLE tableB 
GO 
create table tableA (id int identity ,name varchar(50)) 
declare @i int 
set @i=0 
while (@i<1) 
begin 
insert into tableA (name) 
select top (10) name from master.dbo.spt_values 
set @i=@i+1 
end 
go 
select COUNT(*) from dbo.tableA --10
Copyright 2014 © Ram Kedem. All rights reserved. Not to be reproduced without written consent 
Merge Join VS. Hash Join VS. Nested Loop 
create table tableB (id int identity ,name varchar(50)) 
declare @i int 
set @i=0 
while (@i<1) 
begin 
insert into tableB (name) 
select top (10) name from master.dbo.spt_values 
set @i=@i+1 
end 
select COUNT(*) from dbo.tableB --10 
SELECT TOP(10) * FROM dbo.tableB 
SELECT TOP(10) * FROM dbo.tableA 
select TOP(10) * from dbo.tableA A join tableB B 
on (a.id=b.id) 
select COUNT(*) from dbo.tableA A join tableB B 
on (a.id=b.id)
Copyright 2014 © Ram Kedem. All rights reserved. Not to be reproduced without written consent 
Merge Join VS. Hash Join VS. Nested Loop 
----------------------------------------------------- 
-- Test 3A - Both tables dont have index - Hash Join 
----------------------------------------------------- 
-- Check Excecution Plan 
select * from dbo.tableA A join tableB B 
on (a.id=b.id) 
----------------------------------------------------- 
-- Test 3B - One table have index - Nested Loop 
----------------------------------------------------- 
create unique clustered index cx_tableA on tableA (id) 
-- Check Excecution Plan again 
select * from dbo.tableA A join tableB B 
on (a.id=b.id) 
----------------------------------------------------- 
-- Test 3C - Both tables have index - Nested Loop 
----------------------------------------------------- 
create unique clustered index cx_tableB on tableB (id) 
-- Check Excecution Plan again 
select * from dbo.tableA A join tableB B 
on (a.id=b.id)
Copyright 2014 © Ram Kedem. All rights reserved. Not to be reproduced without written consent 
Join Methods 
-- 1. Nested Loops 
------------------- 
USE Northwind 
GO 
SELECT COUNT(*) FROM products 
SELECT COUNT(*) FROM suppliers 
SELECT pro.productName , sup.SupplierID 
FROM products pro JOIN suppliers sup 
ON pro.SupplierID = sup.SupplierID 
-- 2. Hash Join 
--------------- 
USE AdventureWorks2012 
GO 
SELECT COUNT(*) FROM [Sales].[Customer] 
SELECT COUNT(*) FROM [Sales].[Store] 
SELECT cust.customerID , st.Name 
FROM [Sales].[Customer] cust JOIN [Sales].[Store] st 
ON cust.[StoreID] = st.[BusinessEntityID] 
SELECT sal.SalesOrderID , sal.OrderQty, prod.Name 
FROM Sales.SalesOrderDetail sal JOIN Production.Product prod 
ON Sal.ProductID = Prod.ProductID
Copyright 2014 © Ram Kedem. All rights reserved. Not to be reproduced without written consent 
Join Methods 
SELECT * FROM Sales.Customer 
SELECT * FROM Sales.SalesOrderHeader 
-- Hash Join 
SELECT c.CustomerID , s.OrderDate 
FROM sales.customer c LEFT OUTER JOIN Sales.SalesOrderHeader s 
ON c.CustomerID = s.CustomerID; 
-- Merge Join 
SELECT c.CustomerID 
FROM sales.customer c LEFT OUTER JOIN Sales.SalesOrderHeader s 
ON c.CustomerID = s.CustomerID;

Contenu connexe

Tendances

New Features of SQL Server 2016
New Features of SQL Server 2016New Features of SQL Server 2016
New Features of SQL Server 2016
Mir Mahmood
 
Chetan postgresql partitioning
Chetan postgresql partitioningChetan postgresql partitioning
Chetan postgresql partitioning
OpenSourceIndia
 

Tendances (20)

Sql
SqlSql
Sql
 
SQL Tutorial - Basic Commands
SQL Tutorial - Basic CommandsSQL Tutorial - Basic Commands
SQL Tutorial - Basic Commands
 
A Tour to MySQL Commands
A Tour to MySQL CommandsA Tour to MySQL Commands
A Tour to MySQL Commands
 
Database security
Database securityDatabase security
Database security
 
Typed? Dynamic? Both! Cross-platform DSLs in C#
Typed? Dynamic? Both! Cross-platform DSLs in C#Typed? Dynamic? Both! Cross-platform DSLs in C#
Typed? Dynamic? Both! Cross-platform DSLs in C#
 
Introduction to SQL (for Chicago Booth MBA technology club)
Introduction to SQL (for Chicago Booth MBA technology club)Introduction to SQL (for Chicago Booth MBA technology club)
Introduction to SQL (for Chicago Booth MBA technology club)
 
New Features of SQL Server 2016
New Features of SQL Server 2016New Features of SQL Server 2016
New Features of SQL Server 2016
 
Tutorial - Learn SQL with Live Online Database
Tutorial - Learn SQL with Live Online DatabaseTutorial - Learn SQL with Live Online Database
Tutorial - Learn SQL with Live Online Database
 
Chetan postgresql partitioning
Chetan postgresql partitioningChetan postgresql partitioning
Chetan postgresql partitioning
 
Sql queries
Sql queriesSql queries
Sql queries
 
View & index in SQL
View & index in SQLView & index in SQL
View & index in SQL
 
PyCon 2010 SQLAlchemy tutorial
PyCon 2010 SQLAlchemy tutorialPyCon 2010 SQLAlchemy tutorial
PyCon 2010 SQLAlchemy tutorial
 
Les02
Les02Les02
Les02
 
Sql
SqlSql
Sql
 
Css3 and gwt in perfect harmony
Css3 and gwt in perfect harmonyCss3 and gwt in perfect harmony
Css3 and gwt in perfect harmony
 
Spring Batch in Code - simple DB to DB batch applicaiton
Spring Batch in Code - simple DB to DB batch applicaitonSpring Batch in Code - simple DB to DB batch applicaiton
Spring Batch in Code - simple DB to DB batch applicaiton
 
Les08
Les08Les08
Les08
 
Inside Gorm
Inside GormInside Gorm
Inside Gorm
 
[1062BPY12001] Data analysis with R / week 2
[1062BPY12001] Data analysis with R / week 2[1062BPY12001] Data analysis with R / week 2
[1062BPY12001] Data analysis with R / week 2
 
Les09
Les09Les09
Les09
 

En vedette (6)

Lesson 1 configuring
Lesson 1   configuringLesson 1   configuring
Lesson 1 configuring
 
Deploy SSIS
Deploy SSISDeploy SSIS
Deploy SSIS
 
SSAS Cubes & Hierarchies
SSAS Cubes & HierarchiesSSAS Cubes & Hierarchies
SSAS Cubes & Hierarchies
 
Data Mining in SSAS
Data Mining in SSASData Mining in SSAS
Data Mining in SSAS
 
Data Warehouse Design Considerations
Data Warehouse Design ConsiderationsData Warehouse Design Considerations
Data Warehouse Design Considerations
 
Power Pivot and Power View
Power Pivot and Power ViewPower Pivot and Power View
Power Pivot and Power View
 

Similaire à 4 execution plans

May Woo Bi Portfolio
May Woo Bi PortfolioMay Woo Bi Portfolio
May Woo Bi Portfolio
maywoo
 
Set Focus SQL Portfolio
Set Focus SQL PortfolioSet Focus SQL Portfolio
Set Focus SQL Portfolio
brentbybee
 
RELATIONAL DATABASES & Database designCIS276EmployeeNumFir.docx
RELATIONAL DATABASES & Database designCIS276EmployeeNumFir.docxRELATIONAL DATABASES & Database designCIS276EmployeeNumFir.docx
RELATIONAL DATABASES & Database designCIS276EmployeeNumFir.docx
sodhi3
 
Short Intro to PHP and MySQL
Short Intro to PHP and MySQLShort Intro to PHP and MySQL
Short Intro to PHP and MySQL
Jussi Pohjolainen
 
Build .NET Applications with Reporting and Dashboard
Build .NET Applications with Reporting and DashboardBuild .NET Applications with Reporting and Dashboard
Build .NET Applications with Reporting and Dashboard
Iron Speed
 
SQL Performance Solutions: Refactor Mercilessly, Index Wisely
SQL Performance Solutions: Refactor Mercilessly, Index WiselySQL Performance Solutions: Refactor Mercilessly, Index Wisely
SQL Performance Solutions: Refactor Mercilessly, Index Wisely
Enkitec
 
Micro-ORM Introduction - Don't overcomplicate
Micro-ORM Introduction - Don't overcomplicateMicro-ORM Introduction - Don't overcomplicate
Micro-ORM Introduction - Don't overcomplicate
Kiev ALT.NET
 
Sydney Oracle Meetup - execution plans
Sydney Oracle Meetup - execution plansSydney Oracle Meetup - execution plans
Sydney Oracle Meetup - execution plans
paulguerin
 

Similaire à 4 execution plans (20)

DNN Database Tips & Tricks
DNN Database Tips & TricksDNN Database Tips & Tricks
DNN Database Tips & Tricks
 
Dbms lab Manual
Dbms lab ManualDbms lab Manual
Dbms lab Manual
 
May Woo Bi Portfolio
May Woo Bi PortfolioMay Woo Bi Portfolio
May Woo Bi Portfolio
 
Plsql examples
Plsql examplesPlsql examples
Plsql examples
 
Set Focus SQL Portfolio
Set Focus SQL PortfolioSet Focus SQL Portfolio
Set Focus SQL Portfolio
 
RELATIONAL DATABASES & Database designCIS276EmployeeNumFir.docx
RELATIONAL DATABASES & Database designCIS276EmployeeNumFir.docxRELATIONAL DATABASES & Database designCIS276EmployeeNumFir.docx
RELATIONAL DATABASES & Database designCIS276EmployeeNumFir.docx
 
J. Adcock Bi Portfolio
J. Adcock   Bi PortfolioJ. Adcock   Bi Portfolio
J. Adcock Bi Portfolio
 
Short Intro to PHP and MySQL
Short Intro to PHP and MySQLShort Intro to PHP and MySQL
Short Intro to PHP and MySQL
 
Build .NET Applications with Reporting and Dashboard
Build .NET Applications with Reporting and DashboardBuild .NET Applications with Reporting and Dashboard
Build .NET Applications with Reporting and Dashboard
 
SQL Performance Solutions: Refactor Mercilessly, Index Wisely
SQL Performance Solutions: Refactor Mercilessly, Index WiselySQL Performance Solutions: Refactor Mercilessly, Index Wisely
SQL Performance Solutions: Refactor Mercilessly, Index Wisely
 
Oracle Database 12c Application Development
Oracle Database 12c Application DevelopmentOracle Database 12c Application Development
Oracle Database 12c Application Development
 
Oracle Database features every developer should know about
Oracle Database features every developer should know aboutOracle Database features every developer should know about
Oracle Database features every developer should know about
 
SQL introduction
SQL introductionSQL introduction
SQL introduction
 
Stored procedure tuning and optimization t sql
Stored procedure tuning and optimization t sqlStored procedure tuning and optimization t sql
Stored procedure tuning and optimization t sql
 
[Pgday.Seoul 2019] Citus를 이용한 분산 데이터베이스
[Pgday.Seoul 2019] Citus를 이용한 분산 데이터베이스[Pgday.Seoul 2019] Citus를 이용한 분산 데이터베이스
[Pgday.Seoul 2019] Citus를 이용한 분산 데이터베이스
 
Sql Tuning
Sql TuningSql Tuning
Sql Tuning
 
Beginers guide for oracle sql
Beginers guide for oracle sqlBeginers guide for oracle sql
Beginers guide for oracle sql
 
Micro-ORM Introduction - Don't overcomplicate
Micro-ORM Introduction - Don't overcomplicateMicro-ORM Introduction - Don't overcomplicate
Micro-ORM Introduction - Don't overcomplicate
 
Sydney Oracle Meetup - execution plans
Sydney Oracle Meetup - execution plansSydney Oracle Meetup - execution plans
Sydney Oracle Meetup - execution plans
 
DN 2017 | Reducing pain in data engineering | Martin Loetzsch | Project A
DN 2017 | Reducing pain in data engineering | Martin Loetzsch | Project ADN 2017 | Reducing pain in data engineering | Martin Loetzsch | Project A
DN 2017 | Reducing pain in data engineering | Martin Loetzsch | Project A
 

Plus de Ram Kedem

Plus de Ram Kedem (20)

Impala use case @ edge
Impala use case @ edgeImpala use case @ edge
Impala use case @ edge
 
Advanced SQL Webinar
Advanced SQL WebinarAdvanced SQL Webinar
Advanced SQL Webinar
 
Managing oracle Database Instance
Managing oracle Database InstanceManaging oracle Database Instance
Managing oracle Database Instance
 
Data mining In SSAS
Data mining In SSASData mining In SSAS
Data mining In SSAS
 
SQL Injections - Oracle
SQL Injections - OracleSQL Injections - Oracle
SQL Injections - Oracle
 
SSAS Attributes
SSAS AttributesSSAS Attributes
SSAS Attributes
 
SSRS Matrix
SSRS MatrixSSRS Matrix
SSRS Matrix
 
DDL Practice (Hebrew)
DDL Practice (Hebrew)DDL Practice (Hebrew)
DDL Practice (Hebrew)
 
DML Practice (Hebrew)
DML Practice (Hebrew)DML Practice (Hebrew)
DML Practice (Hebrew)
 
Exploring Oracle Database Architecture (Hebrew)
Exploring Oracle Database Architecture (Hebrew)Exploring Oracle Database Architecture (Hebrew)
Exploring Oracle Database Architecture (Hebrew)
 
Introduction to SQL
Introduction to SQLIntroduction to SQL
Introduction to SQL
 
Introduction to Databases
Introduction to DatabasesIntroduction to Databases
Introduction to Databases
 
Deploy SSRS Project - SQL Server 2014
Deploy SSRS Project - SQL Server 2014Deploy SSRS Project - SQL Server 2014
Deploy SSRS Project - SQL Server 2014
 
Pig - Processing XML data
Pig - Processing XML dataPig - Processing XML data
Pig - Processing XML data
 
SSRS Basic Parameters
SSRS Basic ParametersSSRS Basic Parameters
SSRS Basic Parameters
 
SSRS Gauges
SSRS GaugesSSRS Gauges
SSRS Gauges
 
SSRS Conditional Formatting
SSRS Conditional FormattingSSRS Conditional Formatting
SSRS Conditional Formatting
 
SSRS Calculated Fields
SSRS Calculated FieldsSSRS Calculated Fields
SSRS Calculated Fields
 
SSRS Groups
SSRS GroupsSSRS Groups
SSRS Groups
 
SSIS Incremental ETL process
SSIS Incremental ETL processSSIS Incremental ETL process
SSIS Incremental ETL process
 

Dernier

Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Victor Rentea
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
panagenda
 

Dernier (20)

[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
 
WSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering DevelopersWSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering Developers
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
 
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
 
Understanding the FAA Part 107 License ..
Understanding the FAA Part 107 License ..Understanding the FAA Part 107 License ..
Understanding the FAA Part 107 License ..
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectors
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
CNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In PakistanCNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In Pakistan
 

4 execution plans

  • 2. Copyright 2014 © Ram Kedem. All rights reserved. Not to be reproduced without written consent Display Estimated Execution Plan Display EEP •First time for query number 1 •Second time for query number 2 •Third time for both of them - which cost is cheaper and why ? •You can use CTRL-L as a shortcut to Display Estimated Execution Plan SELECT ProductID, Name, Color FROM Production.Product WHERE Color IN ('Blue','Red') ORDER BY Name; GO SELECT ProductID, Name, Color FROM Production.Product WHERE Color IN ('Blue','Red') ORDER BY ProductID; GO
  • 3. Copyright 2014 © Ram Kedem. All rights reserved. Not to be reproduced without written consent Display Estimated Execution Plan Display EEP •First time for CREATE TABLE •Second time for INSERT, is it possible ? •Third time for both of them, again - is it possible ? CREATE TABLE SomeTable ( SomeTableID INT IDENTITY(1, 1) PRIMARY KEY, FullName varchar(35)) INSERT INTO SomeTable VALUES('Hello'),('There'); SELECT * FROM SomeTable; DROP TABLE SomeTable; GO
  • 4. Copyright 2014 © Ram Kedem. All rights reserved. Not to be reproduced without written consent Display Estimated Execution Plan •Review the following tables : SELECT * FROM [Production].[Product] SELECT * FROM [Production].[ProductModel] •Display Estimated Execution Plan •First time for query number 1 •Second time for query number 2 •Now use the estimated execution plan to compare two queries, How do you explain that such different queries return the same plan? SELECT p.ProductID, p.Name, p.Color, p.Size FROM Production.Product AS p LEFT OUTER JOIN Production.ProductModel AS pm ON p.ProductModelID = pm.ProductModelID WHERE pm.ProductModelID IS NOT NULL; GO SELECT p.ProductID, p.Name, p.Color, p.Size FROM Production.Product AS p WHERE EXISTS(SELECT 1 FROM Production.ProductModel AS pm WHERE p.ProductModelID = pm.ProductModelID); GO
  • 5. Copyright 2014 © Ram Kedem. All rights reserved. Not to be reproduced without written consent Include Actual Execution Plan •Execute the query and include Actual Execution Plan •Shortcut for Include Actual Execution Plan : CTRL + M SELECT ProductID, Name, Color FROM Production.Product WHERE Color IN ('Blue','Red') ORDER BY Name; GO •Save EP & Show EP as XML •Right-click on the plan and choose "Save Execution Plan As" •Right-click on the plan and choose "Show Execution Plan XML"
  • 6. Copyright 2014 © Ram Kedem. All rights reserved. Not to be reproduced without written consent SET SHOWPLAN_TEXT ON / OFF •Causes Microsoft® SQL Server™ not to execute Transact-SQL statements. Instead, SQL Server returns detailed information about how the statements are executed. •Can be executed via SSMS or SQLCMD •In order to use SQLCMD from command prompt type -SQLCMD -S <server name>/<instance name) for example : sqlcmd -S WIN-48RSPI12UJHINST01 USE AdventureWorks2012; GO SET SHOWPLAN_TEXT ON; GO SELECT * FROM Production.Product WHERE ProductID = 905; GO SET SHOWPLAN_TEXT OFF; GO
  • 7. Copyright 2014 © Ram Kedem. All rights reserved. Not to be reproduced without written consent SET SHOWPLAN_ALL ON / OFF •Causes Microsoft SQL Server not to execute Transact-SQL statements. Instead, SQL Server returns detailed information about how the statements are executed and provides estimates of the resource requirements for the statements •Can be executed via SSMS or SQLCMD •In order to use SQLCMD from command prompt type - SQLCMD -S <server name>/<instance name) for example : sqlcmd -S WIN-48RSPI12UJHINST01 USE AdventureWorks2012; GO SET SHOWPLAN_ALL ON; GO SELECT * FROM Production.Product WHERE ProductID = 905; GO SET SHOWPLAN_ALL OFF; GO
  • 9. Copyright 2014 © Ram Kedem. All rights reserved. Not to be reproduced without written consent Heap Table •Full Table Scan •Non Clustered Index Seek •Non Clustered Index Scan SELECT * FROM sys.indexes WHERE object_id = object_id ('dbo.DatabaseLog') -- 1. Full Table Scan --------------------- SELECT * FROM dbo.DatabaseLog; GO SELECT * FROM dbo.DatabaseLog WHERE event = 'ALTER_TABLE' -- 2. Non Clustered Index Seek ------------------------------------------ -- retrieves selective rows from the table SELECT * FROM dbo.DatabaseLog WHERE DatabaseLogID = 1 ; GO -- 3. Non Clustered Index Scan -------------------------------------- - -- retrieves all the rows from the index SELECT DatabaseLogID From DatabaseLog
  • 10. Copyright 2014 © Ram Kedem. All rights reserved. Not to be reproduced without written consent Clustered Index •Clustered Index Scan •Clustered Index Seek •Non Clustered Index Seek •Non Clustered Index Scan SELECT * FROM sys.indexes WHERE object_id = object_id ('Person.ContactType') -- 1. Clustered Index Scan --------------------------------------- -- retrieves all the rows from the table SELECT * FROM Person.ContactType; GO -- 2. Clustered Index Seek ------------------------------------------ -- retrieves selective rows from the table SELECT * FROM Person.ContactType WHERE [ContactTypeID] = 12; GO SELECT * FROM Person.ContactType WHERE [ContactTypeID] BETWEEN 10 AND 20; GO
  • 11. Copyright 2014 © Ram Kedem. All rights reserved. Not to be reproduced without written consent Clustered Index -- 3. Non Clustered Index Seek ------------------------------------------ -- retrieves selective rows from the table SELECT * FROM Person.ContactType WHERE [Name] = 'some value'; GO -- 4. Non Clustered Index Scan ---------------------------------------- -- retrieves all the rows from the index SELECT [Name] FROM Person.ContactType ; GO
  • 12. Copyright 2014 © Ram Kedem. All rights reserved. Not to be reproduced without written consent Sort Operations SELECT * FROM sys.indexes WHERE object_id = object_id ('Production.ProductInventory') -- Compare the different queries and note the costs : SELECT * FROM Production.ProductInventory ORDER BY Shelf; GO SELECT * FROM Production.ProductInventory ORDER BY [ProductID] , [LocationID]; GO SELECT * FROM Production.ProductInventory ORDER BY [LocationID] , [ProductID] ; GO
  • 13. Copyright 2014 © Ram Kedem. All rights reserved. Not to be reproduced without written consent Merge Join VS. Hash Join VS. Nested Loop -------------------------------------------------------------------------------------------- -- Test 1, Big Table -------------------------------------------------------------------------------------------- DROP TABLE tableA GO DROP TABLE tableB GO create table tableA (id int identity ,name varchar(50)) declare @i int set @i=0 while (@i<100) begin insert into tableA (name) select name from master.dbo.spt_values set @i=@i+1 end go select COUNT(*) from dbo.tableA --251500
  • 14. Copyright 2014 © Ram Kedem. All rights reserved. Not to be reproduced without written consent Merge Join VS. Hash Join VS. Nested Loop create table tableB (id int identity ,name varchar(50)) declare @i int set @i=0 while (@i<100) begin insert into tableB (name) select name from master.dbo.spt_values set @i=@i+1 end select COUNT(*) from dbo.tableB --251500 SELECT TOP(10) * FROM dbo.tableB SELECT TOP(10) * FROM dbo.tableA select TOP(10) * from dbo.tableA A join tableB B on (a.id=b.id) select COUNT(*) from dbo.tableA A join tableB B on (a.id=b.id)
  • 15. Copyright 2014 © Ram Kedem. All rights reserved. Not to be reproduced without written consent Merge Join VS. Hash Join VS. Nested Loop ----------------------------------------------------- -- Test 1A - Both tables dont have index - Hash Join ----------------------------------------------------- -- Check Excecution Plan select * from dbo.tableA A join tableB B on (a.id=b.id) ----------------------------------------------------- -- Test 1B - One table have index - Hash Join ----------------------------------------------------- create unique clustered index cx_tableA on tableA (id) -- Check Excecution Plan again select * from dbo.tableA A join tableB B on (a.id=b.id) ----------------------------------------------------- -- Test 1C - Both tables have index - Merge Join ----------------------------------------------------- create unique clustered index cx_tableB on tableB (id) -- Check Excecution Plan again select * from dbo.tableA A join tableB B on (a.id=b.id)
  • 16. Copyright 2014 © Ram Kedem. All rights reserved. Not to be reproduced without written consent Merge Join VS. Hash Join VS. Nested Loop -------------------------------------------------------------------------------------------- -- Test 2, Medium Table -------------------------------------------------------------------------------------------- DROP TABLE tableA GO DROP TABLE tableB GO create table tableA (id int identity ,name varchar(50)) declare @i int set @i=0 while (@i<1) begin insert into tableA (name) select name from master.dbo.spt_values set @i=@i+1 end go select COUNT(*) from dbo.tableA --2515
  • 17. Copyright 2014 © Ram Kedem. All rights reserved. Not to be reproduced without written consent Merge Join VS. Hash Join VS. Nested Loop create table tableB (id int identity ,name varchar(50)) declare @i int set @i=0 while (@i<1) begin insert into tableB (name) select name from master.dbo.spt_values set @i=@i+1 end select COUNT(*) from dbo.tableB --2515 SELECT TOP(10) * FROM dbo.tableB SELECT TOP(10) * FROM dbo.tableA select TOP(10) * from dbo.tableA A join tableB B on (a.id=b.id) select COUNT(*) from dbo.tableA A join tableB B on (a.id=b.id)
  • 18. Copyright 2014 © Ram Kedem. All rights reserved. Not to be reproduced without written consent Merge Join VS. Hash Join VS. Nested Loop ----------------------------------------------------- -- Test 2A - Both tables dont have index - Hash Join ----------------------------------------------------- -- Check Excecution Plan select * from dbo.tableA A join tableB B on (a.id=b.id) ----------------------------------------------------- -- Test 2B - One table have index - Merge Join ----------------------------------------------------- create unique clustered index cx_tableA on tableA (id) -- Check Excecution Plan again select * from dbo.tableA A join tableB B on (a.id=b.id) ----------------------------------------------------- -- Test 2C - Both tables have index - Merge Join ----------------------------------------------------- create unique clustered index cx_tableB on tableB (id) -- Check Excecution Plan again select * from dbo.tableA A join tableB B on (a.id=b.id)
  • 19. Copyright 2014 © Ram Kedem. All rights reserved. Not to be reproduced without written consent Merge Join VS. Hash Join VS. Nested Loop -------------------------------------------------------------------------------------------- -- Test 3, Small Table -------------------------------------------------------------------------------------------- DROP TABLE tableA GO DROP TABLE tableB GO create table tableA (id int identity ,name varchar(50)) declare @i int set @i=0 while (@i<1) begin insert into tableA (name) select top (10) name from master.dbo.spt_values set @i=@i+1 end go select COUNT(*) from dbo.tableA --10
  • 20. Copyright 2014 © Ram Kedem. All rights reserved. Not to be reproduced without written consent Merge Join VS. Hash Join VS. Nested Loop create table tableB (id int identity ,name varchar(50)) declare @i int set @i=0 while (@i<1) begin insert into tableB (name) select top (10) name from master.dbo.spt_values set @i=@i+1 end select COUNT(*) from dbo.tableB --10 SELECT TOP(10) * FROM dbo.tableB SELECT TOP(10) * FROM dbo.tableA select TOP(10) * from dbo.tableA A join tableB B on (a.id=b.id) select COUNT(*) from dbo.tableA A join tableB B on (a.id=b.id)
  • 21. Copyright 2014 © Ram Kedem. All rights reserved. Not to be reproduced without written consent Merge Join VS. Hash Join VS. Nested Loop ----------------------------------------------------- -- Test 3A - Both tables dont have index - Hash Join ----------------------------------------------------- -- Check Excecution Plan select * from dbo.tableA A join tableB B on (a.id=b.id) ----------------------------------------------------- -- Test 3B - One table have index - Nested Loop ----------------------------------------------------- create unique clustered index cx_tableA on tableA (id) -- Check Excecution Plan again select * from dbo.tableA A join tableB B on (a.id=b.id) ----------------------------------------------------- -- Test 3C - Both tables have index - Nested Loop ----------------------------------------------------- create unique clustered index cx_tableB on tableB (id) -- Check Excecution Plan again select * from dbo.tableA A join tableB B on (a.id=b.id)
  • 22. Copyright 2014 © Ram Kedem. All rights reserved. Not to be reproduced without written consent Join Methods -- 1. Nested Loops ------------------- USE Northwind GO SELECT COUNT(*) FROM products SELECT COUNT(*) FROM suppliers SELECT pro.productName , sup.SupplierID FROM products pro JOIN suppliers sup ON pro.SupplierID = sup.SupplierID -- 2. Hash Join --------------- USE AdventureWorks2012 GO SELECT COUNT(*) FROM [Sales].[Customer] SELECT COUNT(*) FROM [Sales].[Store] SELECT cust.customerID , st.Name FROM [Sales].[Customer] cust JOIN [Sales].[Store] st ON cust.[StoreID] = st.[BusinessEntityID] SELECT sal.SalesOrderID , sal.OrderQty, prod.Name FROM Sales.SalesOrderDetail sal JOIN Production.Product prod ON Sal.ProductID = Prod.ProductID
  • 23. Copyright 2014 © Ram Kedem. All rights reserved. Not to be reproduced without written consent Join Methods SELECT * FROM Sales.Customer SELECT * FROM Sales.SalesOrderHeader -- Hash Join SELECT c.CustomerID , s.OrderDate FROM sales.customer c LEFT OUTER JOIN Sales.SalesOrderHeader s ON c.CustomerID = s.CustomerID; -- Merge Join SELECT c.CustomerID FROM sales.customer c LEFT OUTER JOIN Sales.SalesOrderHeader s ON c.CustomerID = s.CustomerID;