SlideShare a Scribd company logo
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;

More Related Content

What's hot

Sql
SqlSql
SQL Tutorial - Basic Commands
SQL Tutorial - Basic CommandsSQL Tutorial - Basic Commands
SQL Tutorial - Basic Commands
1keydata
 
A Tour to MySQL Commands
A Tour to MySQL CommandsA Tour to MySQL Commands
A Tour to MySQL Commands
Hikmat Dhamee
 
Database security
Database securityDatabase security
Database security
Rambabu Duddukuri
 
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#
Vagif Abilov
 
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)
Jennifer Berk
 
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
 
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
DBrow Adm
 
Chetan postgresql partitioning
Chetan postgresql partitioningChetan postgresql partitioning
Chetan postgresql partitioning
OpenSourceIndia
 
Sql queries
Sql queriesSql queries
Sql queries
narendrababuc
 
View & index in SQL
View & index in SQLView & index in SQL
View & index in SQL
Swapnali Pawar
 
PyCon 2010 SQLAlchemy tutorial
PyCon 2010 SQLAlchemy tutorialPyCon 2010 SQLAlchemy tutorial
PyCon 2010 SQLAlchemy tutorial
jbellis
 
Les02
Les02Les02
Les02
Akmal Rony
 
Sql
SqlSql
Css3 and gwt in perfect harmony
Css3 and gwt in perfect harmonyCss3 and gwt in perfect harmony
Css3 and gwt in perfect harmony
jdramaix
 
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
tomi vanek
 
Les08
Les08Les08
Inside Gorm
Inside GormInside Gorm
Inside Gorm
krimple
 
[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
Kevin Chun-Hsien Hsu
 
Les09
Les09Les09

What's hot (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
 

Viewers also liked

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

Viewers also liked (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
 

Similar to 4 execution plans

DNN Database Tips & Tricks
DNN Database Tips & TricksDNN Database Tips & Tricks
DNN Database Tips & Tricks
Will Strohl
 
Dbms lab Manual
Dbms lab ManualDbms lab Manual
Dbms lab Manual
Vivek Kumar Sinha
 
May Woo Bi Portfolio
May Woo Bi PortfolioMay Woo Bi Portfolio
May Woo Bi Portfolio
maywoo
 
Plsql examples
Plsql examplesPlsql examples
Plsql examples
srinivas sunkara
 
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
 
J. Adcock Bi Portfolio
J. Adcock   Bi PortfolioJ. Adcock   Bi Portfolio
J. Adcock Bi Portfolio
Jerry Adcock
 
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
 
Oracle Database 12c Application Development
Oracle Database 12c Application DevelopmentOracle Database 12c Application Development
Oracle Database 12c Application Development
Saurabh K. Gupta
 
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
gvenzl
 
SQL introduction
SQL introductionSQL introduction
SQL introduction
traningoraclecseit
 
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
nishantdavid9
 
[Pgday.Seoul 2019] Citus를 이용한 분산 데이터베이스
[Pgday.Seoul 2019] Citus를 이용한 분산 데이터베이스[Pgday.Seoul 2019] Citus를 이용한 분산 데이터베이스
[Pgday.Seoul 2019] Citus를 이용한 분산 데이터베이스
PgDay.Seoul
 
Sql Tuning
Sql TuningSql Tuning
Beginers guide for oracle sql
Beginers guide for oracle sqlBeginers guide for oracle sql
Beginers guide for oracle sql
N.Jagadish Kumar
 
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
 
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
Dataconomy Media
 

Similar to 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
 

More from Ram Kedem

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

More from 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
 

Recently uploaded

Climate Impact of Software Testing at Nordic Testing Days
Climate Impact of Software Testing at Nordic Testing DaysClimate Impact of Software Testing at Nordic Testing Days
Climate Impact of Software Testing at Nordic Testing Days
Kari Kakkonen
 
Removing Uninteresting Bytes in Software Fuzzing
Removing Uninteresting Bytes in Software FuzzingRemoving Uninteresting Bytes in Software Fuzzing
Removing Uninteresting Bytes in Software Fuzzing
Aftab Hussain
 
CAKE: Sharing Slices of Confidential Data on Blockchain
CAKE: Sharing Slices of Confidential Data on BlockchainCAKE: Sharing Slices of Confidential Data on Blockchain
CAKE: Sharing Slices of Confidential Data on Blockchain
Claudio Di Ciccio
 
June Patch Tuesday
June Patch TuesdayJune Patch Tuesday
June Patch Tuesday
Ivanti
 
Best 20 SEO Techniques To Improve Website Visibility In SERP
Best 20 SEO Techniques To Improve Website Visibility In SERPBest 20 SEO Techniques To Improve Website Visibility In SERP
Best 20 SEO Techniques To Improve Website Visibility In SERP
Pixlogix Infotech
 
20240609 QFM020 Irresponsible AI Reading List May 2024
20240609 QFM020 Irresponsible AI Reading List May 202420240609 QFM020 Irresponsible AI Reading List May 2024
20240609 QFM020 Irresponsible AI Reading List May 2024
Matthew Sinclair
 
Let's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with Slack
Let's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with SlackLet's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with Slack
Let's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with Slack
shyamraj55
 
Presentation of the OECD Artificial Intelligence Review of Germany
Presentation of the OECD Artificial Intelligence Review of GermanyPresentation of the OECD Artificial Intelligence Review of Germany
Presentation of the OECD Artificial Intelligence Review of Germany
innovationoecd
 
Ocean lotus Threat actors project by John Sitima 2024 (1).pptx
Ocean lotus Threat actors project by John Sitima 2024 (1).pptxOcean lotus Threat actors project by John Sitima 2024 (1).pptx
Ocean lotus Threat actors project by John Sitima 2024 (1).pptx
SitimaJohn
 
“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...
“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...
“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...
Edge AI and Vision Alliance
 
Your One-Stop Shop for Python Success: Top 10 US Python Development Providers
Your One-Stop Shop for Python Success: Top 10 US Python Development ProvidersYour One-Stop Shop for Python Success: Top 10 US Python Development Providers
Your One-Stop Shop for Python Success: Top 10 US Python Development Providers
akankshawande
 
Generating privacy-protected synthetic data using Secludy and Milvus
Generating privacy-protected synthetic data using Secludy and MilvusGenerating privacy-protected synthetic data using Secludy and Milvus
Generating privacy-protected synthetic data using Secludy and Milvus
Zilliz
 
Full-RAG: A modern architecture for hyper-personalization
Full-RAG: A modern architecture for hyper-personalizationFull-RAG: A modern architecture for hyper-personalization
Full-RAG: A modern architecture for hyper-personalization
Zilliz
 
Driving Business Innovation: Latest Generative AI Advancements & Success Story
Driving Business Innovation: Latest Generative AI Advancements & Success StoryDriving Business Innovation: Latest Generative AI Advancements & Success Story
Driving Business Innovation: Latest Generative AI Advancements & Success Story
Safe Software
 
Taking AI to the Next Level in Manufacturing.pdf
Taking AI to the Next Level in Manufacturing.pdfTaking AI to the Next Level in Manufacturing.pdf
Taking AI to the Next Level in Manufacturing.pdf
ssuserfac0301
 
Uni Systems Copilot event_05062024_C.Vlachos.pdf
Uni Systems Copilot event_05062024_C.Vlachos.pdfUni Systems Copilot event_05062024_C.Vlachos.pdf
Uni Systems Copilot event_05062024_C.Vlachos.pdf
Uni Systems S.M.S.A.
 
How to use Firebase Data Connect For Flutter
How to use Firebase Data Connect For FlutterHow to use Firebase Data Connect For Flutter
How to use Firebase Data Connect For Flutter
Daiki Mogmet Ito
 
HCL Notes and Domino License Cost Reduction in the World of DLAU
HCL Notes and Domino License Cost Reduction in the World of DLAUHCL Notes and Domino License Cost Reduction in the World of DLAU
HCL Notes and Domino License Cost Reduction in the World of DLAU
panagenda
 
Video Streaming: Then, Now, and in the Future
Video Streaming: Then, Now, and in the FutureVideo Streaming: Then, Now, and in the Future
Video Streaming: Then, Now, and in the Future
Alpen-Adria-Universität
 
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
名前 です男
 

Recently uploaded (20)

Climate Impact of Software Testing at Nordic Testing Days
Climate Impact of Software Testing at Nordic Testing DaysClimate Impact of Software Testing at Nordic Testing Days
Climate Impact of Software Testing at Nordic Testing Days
 
Removing Uninteresting Bytes in Software Fuzzing
Removing Uninteresting Bytes in Software FuzzingRemoving Uninteresting Bytes in Software Fuzzing
Removing Uninteresting Bytes in Software Fuzzing
 
CAKE: Sharing Slices of Confidential Data on Blockchain
CAKE: Sharing Slices of Confidential Data on BlockchainCAKE: Sharing Slices of Confidential Data on Blockchain
CAKE: Sharing Slices of Confidential Data on Blockchain
 
June Patch Tuesday
June Patch TuesdayJune Patch Tuesday
June Patch Tuesday
 
Best 20 SEO Techniques To Improve Website Visibility In SERP
Best 20 SEO Techniques To Improve Website Visibility In SERPBest 20 SEO Techniques To Improve Website Visibility In SERP
Best 20 SEO Techniques To Improve Website Visibility In SERP
 
20240609 QFM020 Irresponsible AI Reading List May 2024
20240609 QFM020 Irresponsible AI Reading List May 202420240609 QFM020 Irresponsible AI Reading List May 2024
20240609 QFM020 Irresponsible AI Reading List May 2024
 
Let's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with Slack
Let's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with SlackLet's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with Slack
Let's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with Slack
 
Presentation of the OECD Artificial Intelligence Review of Germany
Presentation of the OECD Artificial Intelligence Review of GermanyPresentation of the OECD Artificial Intelligence Review of Germany
Presentation of the OECD Artificial Intelligence Review of Germany
 
Ocean lotus Threat actors project by John Sitima 2024 (1).pptx
Ocean lotus Threat actors project by John Sitima 2024 (1).pptxOcean lotus Threat actors project by John Sitima 2024 (1).pptx
Ocean lotus Threat actors project by John Sitima 2024 (1).pptx
 
“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...
“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...
“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...
 
Your One-Stop Shop for Python Success: Top 10 US Python Development Providers
Your One-Stop Shop for Python Success: Top 10 US Python Development ProvidersYour One-Stop Shop for Python Success: Top 10 US Python Development Providers
Your One-Stop Shop for Python Success: Top 10 US Python Development Providers
 
Generating privacy-protected synthetic data using Secludy and Milvus
Generating privacy-protected synthetic data using Secludy and MilvusGenerating privacy-protected synthetic data using Secludy and Milvus
Generating privacy-protected synthetic data using Secludy and Milvus
 
Full-RAG: A modern architecture for hyper-personalization
Full-RAG: A modern architecture for hyper-personalizationFull-RAG: A modern architecture for hyper-personalization
Full-RAG: A modern architecture for hyper-personalization
 
Driving Business Innovation: Latest Generative AI Advancements & Success Story
Driving Business Innovation: Latest Generative AI Advancements & Success StoryDriving Business Innovation: Latest Generative AI Advancements & Success Story
Driving Business Innovation: Latest Generative AI Advancements & Success Story
 
Taking AI to the Next Level in Manufacturing.pdf
Taking AI to the Next Level in Manufacturing.pdfTaking AI to the Next Level in Manufacturing.pdf
Taking AI to the Next Level in Manufacturing.pdf
 
Uni Systems Copilot event_05062024_C.Vlachos.pdf
Uni Systems Copilot event_05062024_C.Vlachos.pdfUni Systems Copilot event_05062024_C.Vlachos.pdf
Uni Systems Copilot event_05062024_C.Vlachos.pdf
 
How to use Firebase Data Connect For Flutter
How to use Firebase Data Connect For FlutterHow to use Firebase Data Connect For Flutter
How to use Firebase Data Connect For Flutter
 
HCL Notes and Domino License Cost Reduction in the World of DLAU
HCL Notes and Domino License Cost Reduction in the World of DLAUHCL Notes and Domino License Cost Reduction in the World of DLAU
HCL Notes and Domino License Cost Reduction in the World of DLAU
 
Video Streaming: Then, Now, and in the Future
Video Streaming: Then, Now, and in the FutureVideo Streaming: Then, Now, and in the Future
Video Streaming: Then, Now, and in the Future
 
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
 

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;