SlideShare a Scribd company logo
1 of 36
Advanced SQL Programming and
Optimization
Case studies in
Re-writing SQL Code
About Soaring Eagle
Since 1997, Soaring Eagle Consulting has been helping enterprise clients improve their overall system performance at the database
tier, arguably the most volatile and critical component of distributed application architecture. Our clients range in size from fledgling
startups through Fortune 100 companies and leading financial institutions.
Soaring Eagle has been a leader in software development, architecture, performance and tuning databases, while promoting
mentoring and training all over the world for over a decade. Many of our employees, and partners have written books, speak at
seminars about leading edge technologies. We have expertise in all business tiers, financial; health, manufacturing, government
agencies and many ecommerce businesses.
Consulting
• Performance & Tuning
• Data Performance
Management
• Emergency Triage
• Performance & Security
Audits
• Staff Augmentation
• Project management
• Database architecture
• Scalability assessment
and planning
Training
• Onsite/Web based
• Microsoft
• Sybase
• Oracle
• APM
• Six Sigma
Software
• Application Performance
Management
• Database performance
accelerator
• Database performance
management
• Database Security
Managed
Services
• Remote Database
Management
• Performance
management
• Emergency db Service
• Proactive mitigation
• Problem notification
• Problem resolution
9
Microsoft SQL server, SQL EM,
Query Analyzer are all trademarks of Microsoft Inc.
This presentation is copyrighted.
This presentation is not for re-sale
This presentation shall not be used or modified without
express written consent of Soaring Eagle Consulting,
Inc.
Acknowledgements
Page 6 - 3
Why other people’s code stinks
• Poor performance
• Poor documentation
• Inadherence to standards
• From a real-life problem
• Your task is to migrate from a source
database to a new target, which has
incremental values on the cost sheet detail
information.
• Your task is to make fun of the first two
options, and to understand the third
Case Study #1:
Very Bad, Bad, Not so Bad
CREATE TABLE #WKCostSheet(
WKCostSheetHeaderID int NOT NULL,
Sequence int identity,
ItemDescription varchar(50) NULL,
Value money NULL,
GiftInKind money NULL,
KWNAmount money NULL,
CheckNumber varchar(50) NULL)
Very Bad
CREATE TABLE #WKCostSheet2(
WKCostSheetHeaderID int NOT NULL,
Sequence int,
ItemDescription varchar(50) NULL,
Value money NULL,
GiftInKind money NULL,
KWNAmount money NULL,
CheckNumber varchar(50) NULL
)
Very Bad (continued)
INSERT INTO #WKCostSheet
(WKCostSheetHeaderID
,ItemDescription
,Value
,GiftInKind
,KWNAmount
,CheckNumber
)
Very Bad (cont’d)
SELECT WKCostSheetHeaderID, exp ,val ,gik ,kwn ,chkNum
FROM
KWN_Access_Admin.dbo.CostSheet c join
KWN_dev.dbo.WKCostSheetHeader h
on h.WKID = c.KID and
h.WKCostSheetTypeID= c.WishNum
where
KID in (select WKID from dbo.WKs)
order by
WKCostSheetHeaderID
Very Bad (cont’d)
insert
#WKCostSheet2 select * from #WKCostSheet
create clustered index a on
#WKCostSheet2 (WKCostSheetHeaderID,
Sequence)
Very Bad (cont’d)
while exists (select * from #WKCostSheet2)
begin
INSERT INTO KWN_dev.dbo.WKCostSheet
(WKCostSheetHeaderID
,Sequence
,ItemDescription
,Value
,GiftInKind
,KWNAmount
,CheckNumber
)
Very Bad (cont’d)
select * from #WKCostSheet2
where
WKCostSheetHeaderID =
(select MIN (WKCostSheetHeaderID)
from #WKCostSheet2)
delete from #WKCostSheet2
where WKCostSheetHeaderID =
(select MIN (WKCostSheetHeaderID)
from #WKCostSheet2)
update #WKCostSheet2
set Sequence = Sequence –
(select MIN ( sequence) from #WKCostSheet2 ) + 1
end -- While
Very Bad (cont’d)
• Multiple temp tables
• Enormous amount of IO from second temp
table, heavily due to the large quantity of
deletions & multiple passes through the
temp table
Very Bad – summary
CREATE TABLE #WKCostSheet(
WKCostSheetHeaderID int NOT NULL,
Sequence int identity,
ItemDescription varchar(50) NULL,
Value money NULL,
GiftInKind money NULL,
KWNAmount money NULL,
CheckNumber varchar(50) NULL)
Less bad
INSERT INTO #WKCostSheet
/* (SAME) */
SELECT WKCostSheetHeaderID
/* (SAME) */
create clustered index a on
#WKCostSheet(WKCostSheetHeaderID,
Sequence)
Less bad (cont’d)
INSERT INTO WKCostSheet
(WKCostSheetHeaderID
,Sequence
,ItemDescription
,Value
,GiftInKind
,KWNAmount
,CheckNumber
)
/* Continued */
Less bad (cont’d)
Less bad (cont’d)
select a.WKCostSheetHeaderID
,a.Sequence - b.minseq + 1
,a.ItemDescription ,a.Value ,a.GiftInKind
,a.KWNAmount ,a.CheckNumber
from #WKCostSheet a join
(select c.WKCostSheetHeaderID,
MIN (c.sequence) as minseq
from #WKCostSheet c
group by WKCostSheetHeaderID) as b
on a.WKCostSheetHeaderID = b.WKCostSheetHeaderID
• Still need a temp table
• Join is far less costly than the repeated
deletions
Less bad – summary
SELECT
WKCostSheetHeaderID, row_number() over
(partition by WKCostSheetHeaderID order by val)
,exp ,val ,gik ,kwn ,chkNum
FROM KWN_Access_Admin.dbo.CostSheet c join
KWN_dev.dbo.WKCostSheetHeader h on h.WKID =
c.KID and h.WKCostSheetTypeID= c.WishNum
where KID in (select WKID from dbo.WKs)
order by WKCostSheetHeaderID
Not so bad
• No temp table, no join, no trouble
Not so bad – summary
• We’re fans of data-driven design; anything that keeps us from having to push code
back through QA is a good thing. But, we’re going to make fun of the code that
accesses the hierarchical data (note: It was written in SQL Server 2005, prior to
hierarchy IDs being available), starting with naming conventions. The below character
string is a table name.
• [_SynComs.Orders.OrderItem.product->SynComs.Products.PrinterCartridge]
• Nontrivial to type, contains special characters… not a lot right with this.
• The interesting thing, from their perspective, is that the same query is used for every
single database call. That’s right, one query only for every access. The catch is,
there’s an unlimited number of recursive calls to get the database results, and the
structure was set up to put real (data) information into the physical schema, a nifty
way to create extra contention in the system tables.
• For the record, the CTE changed approach brought query time from 9.5 seconds
down to .23 seconds.
Case Study #2:
From horrid code to CTE
SELECT
0 as generation,
major_id as tableId
into #tblguid
FROM
sys.extended_properties
WHERE (value in
(
'SynComs.Orders.Order, SynComs'
)
and name = 'ParentType')
create clustered index CItablID on #tblguid(tableId)
Original Code
declare @generation int
select @generation=0
while (1=1) begin
select @generation=@generation+1
insert into #tblguid (generation, tableId)
SELECT @generation, parent.major_id
FROM
#tblguid tbl JOIN
sys.extended_properties child
on tbl.tableId = child.major_id and
child.name = 'ChildType' and generation = @generation -1
JOIN sys.extended_properties parent on
child.value = parent.value and parent.name = 'ParentType'
Original Code (cont’d)
where not exists
(select *
from
#tblguid lookitup
where
parent.major_id = lookitup.tableId)
if (@@ROWCOUNT=0) break
end
select
name as tableName
from
sys.tables join
#tblguid
on object_id = tableId
Original Code (cont’d)
/*
Do you like this? We’re about to recursively create
/ execute a large view… good candidate for
rewrite / approach change
*/
Original Code (cont’d)
declare @string varchar(max)
select @string = '
create view my_view as
select * from
[_SynComs.Orders.Order.billingAddress-
>SynComs.Customers.CustomerAddress]
union all /* At least it’s “union all” here */
select * from [_SynComs.Orders.Order.discounts-
>SynComs.Orders.Discounts.Discount]
/* … for brevity, I’ve removed about 12 more of these */
exec (@string)
go
Original Code (cont’d)
select 0 as generation, parentObjectGuid,
childObjectGuid, fieldName, parentType, childType,
_guid_, _pk_
into
#guids
from
my_view
where
parentObjectGuid IN (
'3ee588d1-2096-4ddb-adc6-
d5a140725721',/* about 70 more removed */);
Original Code (cont’d)
update @guids28927 set generation=0
create clustered index CI_GUID on #guids (_guid_)
create nonclustered index NCI_childobject_generation on
#guids (generation,childObjectGuid,parentObjectGuid)
declare @generation int
select @generation=0
Original Code (cont’d)
while (1=1)
begin
select @generation=@generation+1
insert into #guids
(generation, parentObjectGuid, childObjectGuid, fieldName, parentType,
childType, _guid_, _pk_)
select @generation, parentObjectGuid, childObjectGuid, fieldName,
parentType, childType, _guid_, _pk_
from my_view
where
parentObjectGuid in (select childObjectGuid from #guids where
generation=(@generation-1))
and
not exists (select * from #guids where
my_view._guid_ =#guids._guid_ )
Original Code (cont’d)
if (@@ROWCOUNT=0) break
end
Select parentObjectGuid, childObjectGuid, fieldName,
parentType, childType, _guid_, _pk_
from
#guids
Original Code (cont’d)
WITH RecursionRelationship (
generation, parentObjectGuid, childObjectGuid,fieldName, parentType,
childType, [_guid_], [_pk_] )
AS
(
-- Anchor Query
select 0 as generation, parentObjectGuid, childObjectGuid,
fieldName,parentType, childType, _guid_, _pk_
from
dbo.ObjectRelationship
where
parentObjectGuid IN (
'3ee588d1-2096-4ddb-adc6-d5a140725721', /* same list as above */)
Revised Code
UNION ALL
-- Recursion Query
Select r_r.generation +1, o_r.parentObjectGuid, o_r.childObjectGuid,
o_r.fieldName, o_r.parentType, o_r.childType, o_r._guid_, o_r._pk_
from
dbo.ObjectRelationship o_r JOIN RecursionRelationship r_r on
o_r.parentObjectGuid = r_r.childObjectGuid)
select parentObjectGuid, childObjectGuid, fieldName, parentType,
childType, _guid_, _pk_
from
RecursionRelationship option (maxrecursion 32767)
Revised Code (cont’d)
Jeff Garbus – Email me for a copy!
jeff@soaringeagle.guru
813.641.3434
mssqlperformance.blogspot.com
http://www.youtube.com/user/soaringeagledba/
Microsoft Transact - SQL, The Definitive Guide
– 35% off from jblearning.com. Use code "GARBUS"
Upcoming Webinars
Check our web site: www.soaringeagle.guru
Find us on Social Media @SoaringEagleDBA
Like us on Facebook: Facebook.com/SoaringEagleDBA
33 - 60 © Soaring Eagle Consulting 8/19/2013
Soaring Eagle Flight Center FREE! For 3 months
• Why Try Soaring Eagle Flight Center?
• See your environment health in seconds
• The hundreds or thousand email alerts are corralled
into alerts that you control
• Flight provides Predictive Analysis Tools
Email sales@soaringeagle.guru to take
advantage
Purchase SQL
Diagnostic
Manager and
get SQL Doctor
FREE!
Limited Time Offer!
Try Any IDERA Products
Free for 14-Days!

More Related Content

What's hot (11)

Oraclesql
OraclesqlOraclesql
Oraclesql
 
Nested Types in Impala
Nested Types in ImpalaNested Types in Impala
Nested Types in Impala
 
Microsoft MCSA 70-457 it exams dumps
Microsoft MCSA 70-457 it exams dumpsMicrosoft MCSA 70-457 it exams dumps
Microsoft MCSA 70-457 it exams dumps
 
Access 04
Access 04Access 04
Access 04
 
Tale of the Gilded Rose
Tale of the Gilded RoseTale of the Gilded Rose
Tale of the Gilded Rose
 
Databases and SQL - Lecture C
Databases and SQL - Lecture CDatabases and SQL - Lecture C
Databases and SQL - Lecture C
 
70433 Dumps DB
70433 Dumps DB70433 Dumps DB
70433 Dumps DB
 
PHP webboard
PHP webboardPHP webboard
PHP webboard
 
Database development coding standards
Database development coding standardsDatabase development coding standards
Database development coding standards
 
TSQL Coding Guidelines
TSQL Coding GuidelinesTSQL Coding Guidelines
TSQL Coding Guidelines
 
Mpg Dec07 Gian Lorenzetto
Mpg Dec07 Gian Lorenzetto Mpg Dec07 Gian Lorenzetto
Mpg Dec07 Gian Lorenzetto
 

Viewers also liked

Sql queries interview questions
Sql queries interview questionsSql queries interview questions
Sql queries interview questions
Pyadav010186
 

Viewers also liked (9)

SQL 101 for business experts and stakeholders
SQL 101 for business experts and stakeholdersSQL 101 for business experts and stakeholders
SQL 101 for business experts and stakeholders
 
Complex queries in sql
Complex queries in sqlComplex queries in sql
Complex queries in sql
 
SQL querys in detail || Sql query slides
SQL querys in detail || Sql query slidesSQL querys in detail || Sql query slides
SQL querys in detail || Sql query slides
 
SQL Join Basic
SQL Join BasicSQL Join Basic
SQL Join Basic
 
SQL | Computer Science
SQL | Computer ScienceSQL | Computer Science
SQL | Computer Science
 
Sql subquery
Sql  subquerySql  subquery
Sql subquery
 
100 sql queries
100 sql queries100 sql queries
100 sql queries
 
SQL Server Learning Drive
SQL Server Learning Drive SQL Server Learning Drive
SQL Server Learning Drive
 
Sql queries interview questions
Sql queries interview questionsSql queries interview questions
Sql queries interview questions
 

Similar to Geek Sync | Rewriting Bad SQL Code 101

Top 5 Magento Secure Coding Best Practices
Top 5 Magento Secure Coding Best PracticesTop 5 Magento Secure Coding Best Practices
Top 5 Magento Secure Coding Best Practices
Oleksandr Zarichnyi
 
Building Better Applications with Data::Manager
Building Better Applications with Data::ManagerBuilding Better Applications with Data::Manager
Building Better Applications with Data::Manager
Jay Shirley
 
Business Intelligence Portfolio
Business Intelligence PortfolioBusiness Intelligence Portfolio
Business Intelligence Portfolio
Chris Seebacher
 
Evolving a Clean, Pragmatic Architecture at JBCNConf 2019
Evolving a Clean, Pragmatic Architecture at JBCNConf 2019Evolving a Clean, Pragmatic Architecture at JBCNConf 2019
Evolving a Clean, Pragmatic Architecture at JBCNConf 2019
Victor Rentea
 
Кирилл Безпалый, .NET Developer, Ciklum
Кирилл Безпалый, .NET Developer, CiklumКирилл Безпалый, .NET Developer, Ciklum
Кирилл Безпалый, .NET Developer, Ciklum
Alina Vilk
 

Similar to Geek Sync | Rewriting Bad SQL Code 101 (20)

3 indexes
3 indexes3 indexes
3 indexes
 
Beyond PHP - It's not (just) about the code
Beyond PHP - It's not (just) about the codeBeyond PHP - It's not (just) about the code
Beyond PHP - It's not (just) about the code
 
Advanced integration services on microsoft ssis 1
Advanced integration services on microsoft ssis 1Advanced integration services on microsoft ssis 1
Advanced integration services on microsoft ssis 1
 
Super spike
Super spikeSuper spike
Super spike
 
Agile Database Development with JSON
Agile Database Development with JSONAgile Database Development with JSON
Agile Database Development with JSON
 
SQL Server 2008 Portfolio
SQL Server 2008 PortfolioSQL Server 2008 Portfolio
SQL Server 2008 Portfolio
 
Linq
LinqLinq
Linq
 
Top 5 Magento Secure Coding Best Practices
Top 5 Magento Secure Coding Best PracticesTop 5 Magento Secure Coding Best Practices
Top 5 Magento Secure Coding Best Practices
 
Introduction to SQL Antipatterns
Introduction to SQL AntipatternsIntroduction to SQL Antipatterns
Introduction to SQL Antipatterns
 
[MongoDB.local Bengaluru 2018] Just in Time Validation with JSON Schema
[MongoDB.local Bengaluru 2018] Just in Time Validation with JSON Schema[MongoDB.local Bengaluru 2018] Just in Time Validation with JSON Schema
[MongoDB.local Bengaluru 2018] Just in Time Validation with JSON Schema
 
テスト用のプレゼンテーション
テスト用のプレゼンテーションテスト用のプレゼンテーション
テスト用のプレゼンテーション
 
Boost delivery stream with code discipline engineering
Boost delivery stream with code discipline engineeringBoost delivery stream with code discipline engineering
Boost delivery stream with code discipline engineering
 
How to work with Subquery in Data Mining?
How to work with Subquery in Data Mining?How to work with Subquery in Data Mining?
How to work with Subquery in Data Mining?
 
Building Better Applications with Data::Manager
Building Better Applications with Data::ManagerBuilding Better Applications with Data::Manager
Building Better Applications with Data::Manager
 
ADBMS ASSIGNMENT
ADBMS ASSIGNMENTADBMS ASSIGNMENT
ADBMS ASSIGNMENT
 
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
 
Business Intelligence Portfolio
Business Intelligence PortfolioBusiness Intelligence Portfolio
Business Intelligence Portfolio
 
Evolving a Clean, Pragmatic Architecture at JBCNConf 2019
Evolving a Clean, Pragmatic Architecture at JBCNConf 2019Evolving a Clean, Pragmatic Architecture at JBCNConf 2019
Evolving a Clean, Pragmatic Architecture at JBCNConf 2019
 
Кирилл Безпалый, .NET Developer, Ciklum
Кирилл Безпалый, .NET Developer, CiklumКирилл Безпалый, .NET Developer, Ciklum
Кирилл Безпалый, .NET Developer, Ciklum
 
Dublin Ireland Spark Meetup October 15, 2015
Dublin Ireland Spark Meetup October 15, 2015Dublin Ireland Spark Meetup October 15, 2015
Dublin Ireland Spark Meetup October 15, 2015
 

More from IDERA Software

Idera live 2021: The Power of Abstraction by Steve Hoberman
Idera live 2021:  The Power of Abstraction by Steve HobermanIdera live 2021:  The Power of Abstraction by Steve Hoberman
Idera live 2021: The Power of Abstraction by Steve Hoberman
IDERA Software
 
Idera live 2021: Keynote Presentation The Future of Data is The Data Cloud b...
Idera live 2021:  Keynote Presentation The Future of Data is The Data Cloud b...Idera live 2021:  Keynote Presentation The Future of Data is The Data Cloud b...
Idera live 2021: Keynote Presentation The Future of Data is The Data Cloud b...
IDERA Software
 
Idera live 2021: Database Auditing - on-Premises and in the Cloud by Craig M...
Idera live 2021:  Database Auditing - on-Premises and in the Cloud by Craig M...Idera live 2021:  Database Auditing - on-Premises and in the Cloud by Craig M...
Idera live 2021: Database Auditing - on-Premises and in the Cloud by Craig M...
IDERA Software
 

More from IDERA Software (20)

The role of the database administrator (DBA) in 2020: Changes, challenges, an...
The role of the database administrator (DBA) in 2020: Changes, challenges, an...The role of the database administrator (DBA) in 2020: Changes, challenges, an...
The role of the database administrator (DBA) in 2020: Changes, challenges, an...
 
Problems and solutions for migrating databases to the cloud
Problems and solutions for migrating databases to the cloudProblems and solutions for migrating databases to the cloud
Problems and solutions for migrating databases to the cloud
 
Public cloud uses and limitations
Public cloud uses and limitationsPublic cloud uses and limitations
Public cloud uses and limitations
 
Optimize the performance, cost, and value of databases.pptx
Optimize the performance, cost, and value of databases.pptxOptimize the performance, cost, and value of databases.pptx
Optimize the performance, cost, and value of databases.pptx
 
Monitor cloud database with SQL Diagnostic Manager for SQL Server
Monitor cloud database with SQL Diagnostic Manager for SQL ServerMonitor cloud database with SQL Diagnostic Manager for SQL Server
Monitor cloud database with SQL Diagnostic Manager for SQL Server
 
Database administrators (dbas) face increasing pressure to monitor databases
Database administrators (dbas) face increasing pressure to monitor databasesDatabase administrators (dbas) face increasing pressure to monitor databases
Database administrators (dbas) face increasing pressure to monitor databases
 
Six tips for cutting sql server licensing costs
Six tips for cutting sql server licensing costsSix tips for cutting sql server licensing costs
Six tips for cutting sql server licensing costs
 
Idera live 2021: The Power of Abstraction by Steve Hoberman
Idera live 2021:  The Power of Abstraction by Steve HobermanIdera live 2021:  The Power of Abstraction by Steve Hoberman
Idera live 2021: The Power of Abstraction by Steve Hoberman
 
Idera live 2021: Why Data Lakes are Critical for AI, ML, and IoT By Brian Flug
Idera live 2021:  Why Data Lakes are Critical for AI, ML, and IoT  By Brian FlugIdera live 2021:  Why Data Lakes are Critical for AI, ML, and IoT  By Brian Flug
Idera live 2021: Why Data Lakes are Critical for AI, ML, and IoT By Brian Flug
 
Idera live 2021: Will Data Vault add Value to Your Data Warehouse? 3 Signs th...
Idera live 2021: Will Data Vault add Value to Your Data Warehouse? 3 Signs th...Idera live 2021: Will Data Vault add Value to Your Data Warehouse? 3 Signs th...
Idera live 2021: Will Data Vault add Value to Your Data Warehouse? 3 Signs th...
 
Idera live 2021: Managing Digital Transformation on a Budget by Bert Scalzo
Idera live 2021:  Managing Digital Transformation on a Budget by Bert ScalzoIdera live 2021:  Managing Digital Transformation on a Budget by Bert Scalzo
Idera live 2021: Managing Digital Transformation on a Budget by Bert Scalzo
 
Idera live 2021: Keynote Presentation The Future of Data is The Data Cloud b...
Idera live 2021:  Keynote Presentation The Future of Data is The Data Cloud b...Idera live 2021:  Keynote Presentation The Future of Data is The Data Cloud b...
Idera live 2021: Keynote Presentation The Future of Data is The Data Cloud b...
 
Idera live 2021: Managing Databases in the Cloud - the First Step, a Succes...
Idera live 2021:   Managing Databases in the Cloud - the First Step, a Succes...Idera live 2021:   Managing Databases in the Cloud - the First Step, a Succes...
Idera live 2021: Managing Databases in the Cloud - the First Step, a Succes...
 
Idera live 2021: Database Auditing - on-Premises and in the Cloud by Craig M...
Idera live 2021:  Database Auditing - on-Premises and in the Cloud by Craig M...Idera live 2021:  Database Auditing - on-Premises and in the Cloud by Craig M...
Idera live 2021: Database Auditing - on-Premises and in the Cloud by Craig M...
 
Idera live 2021: Performance Tuning Azure SQL Database by Monica Rathbun
Idera live 2021:  Performance Tuning Azure SQL Database by Monica RathbunIdera live 2021:  Performance Tuning Azure SQL Database by Monica Rathbun
Idera live 2021: Performance Tuning Azure SQL Database by Monica Rathbun
 
Geek Sync | How to Be the DBA When You Don't Have a DBA - Eric Cobb | IDERA
Geek Sync | How to Be the DBA When You Don't Have a DBA - Eric Cobb | IDERAGeek Sync | How to Be the DBA When You Don't Have a DBA - Eric Cobb | IDERA
Geek Sync | How to Be the DBA When You Don't Have a DBA - Eric Cobb | IDERA
 
How Users of a Performance Monitoring Tool Can Benefit from an Inventory Mana...
How Users of a Performance Monitoring Tool Can Benefit from an Inventory Mana...How Users of a Performance Monitoring Tool Can Benefit from an Inventory Mana...
How Users of a Performance Monitoring Tool Can Benefit from an Inventory Mana...
 
Benefits of Third Party Tools for MySQL | IDERA
Benefits of Third Party Tools for MySQL | IDERABenefits of Third Party Tools for MySQL | IDERA
Benefits of Third Party Tools for MySQL | IDERA
 
Achieve More with Less Resources | IDERA
Achieve More with Less Resources | IDERAAchieve More with Less Resources | IDERA
Achieve More with Less Resources | IDERA
 
Benefits of SQL Server 2017 and 2019 | IDERA
Benefits of SQL Server 2017 and 2019 | IDERABenefits of SQL Server 2017 and 2019 | IDERA
Benefits of SQL Server 2017 and 2019 | IDERA
 

Recently uploaded

+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
?#DUbAI#??##{{(☎️+971_581248768%)**%*]'#abortion pills for sale in dubai@
 

Recently uploaded (20)

AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
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
 
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...
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...
Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...
Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...
 
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live StreamsTop 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 

Geek Sync | Rewriting Bad SQL Code 101

  • 1. Advanced SQL Programming and Optimization Case studies in Re-writing SQL Code
  • 2. About Soaring Eagle Since 1997, Soaring Eagle Consulting has been helping enterprise clients improve their overall system performance at the database tier, arguably the most volatile and critical component of distributed application architecture. Our clients range in size from fledgling startups through Fortune 100 companies and leading financial institutions. Soaring Eagle has been a leader in software development, architecture, performance and tuning databases, while promoting mentoring and training all over the world for over a decade. Many of our employees, and partners have written books, speak at seminars about leading edge technologies. We have expertise in all business tiers, financial; health, manufacturing, government agencies and many ecommerce businesses. Consulting • Performance & Tuning • Data Performance Management • Emergency Triage • Performance & Security Audits • Staff Augmentation • Project management • Database architecture • Scalability assessment and planning Training • Onsite/Web based • Microsoft • Sybase • Oracle • APM • Six Sigma Software • Application Performance Management • Database performance accelerator • Database performance management • Database Security Managed Services • Remote Database Management • Performance management • Emergency db Service • Proactive mitigation • Problem notification • Problem resolution 9
  • 3. Microsoft SQL server, SQL EM, Query Analyzer are all trademarks of Microsoft Inc. This presentation is copyrighted. This presentation is not for re-sale This presentation shall not be used or modified without express written consent of Soaring Eagle Consulting, Inc. Acknowledgements Page 6 - 3
  • 4. Why other people’s code stinks • Poor performance • Poor documentation • Inadherence to standards
  • 5. • From a real-life problem • Your task is to migrate from a source database to a new target, which has incremental values on the cost sheet detail information. • Your task is to make fun of the first two options, and to understand the third Case Study #1: Very Bad, Bad, Not so Bad
  • 6. CREATE TABLE #WKCostSheet( WKCostSheetHeaderID int NOT NULL, Sequence int identity, ItemDescription varchar(50) NULL, Value money NULL, GiftInKind money NULL, KWNAmount money NULL, CheckNumber varchar(50) NULL) Very Bad
  • 7. CREATE TABLE #WKCostSheet2( WKCostSheetHeaderID int NOT NULL, Sequence int, ItemDescription varchar(50) NULL, Value money NULL, GiftInKind money NULL, KWNAmount money NULL, CheckNumber varchar(50) NULL ) Very Bad (continued)
  • 9. SELECT WKCostSheetHeaderID, exp ,val ,gik ,kwn ,chkNum FROM KWN_Access_Admin.dbo.CostSheet c join KWN_dev.dbo.WKCostSheetHeader h on h.WKID = c.KID and h.WKCostSheetTypeID= c.WishNum where KID in (select WKID from dbo.WKs) order by WKCostSheetHeaderID Very Bad (cont’d)
  • 10. insert #WKCostSheet2 select * from #WKCostSheet create clustered index a on #WKCostSheet2 (WKCostSheetHeaderID, Sequence) Very Bad (cont’d)
  • 11. while exists (select * from #WKCostSheet2) begin INSERT INTO KWN_dev.dbo.WKCostSheet (WKCostSheetHeaderID ,Sequence ,ItemDescription ,Value ,GiftInKind ,KWNAmount ,CheckNumber ) Very Bad (cont’d)
  • 12. select * from #WKCostSheet2 where WKCostSheetHeaderID = (select MIN (WKCostSheetHeaderID) from #WKCostSheet2) delete from #WKCostSheet2 where WKCostSheetHeaderID = (select MIN (WKCostSheetHeaderID) from #WKCostSheet2) update #WKCostSheet2 set Sequence = Sequence – (select MIN ( sequence) from #WKCostSheet2 ) + 1 end -- While Very Bad (cont’d)
  • 13. • Multiple temp tables • Enormous amount of IO from second temp table, heavily due to the large quantity of deletions & multiple passes through the temp table Very Bad – summary
  • 14. CREATE TABLE #WKCostSheet( WKCostSheetHeaderID int NOT NULL, Sequence int identity, ItemDescription varchar(50) NULL, Value money NULL, GiftInKind money NULL, KWNAmount money NULL, CheckNumber varchar(50) NULL) Less bad
  • 15. INSERT INTO #WKCostSheet /* (SAME) */ SELECT WKCostSheetHeaderID /* (SAME) */ create clustered index a on #WKCostSheet(WKCostSheetHeaderID, Sequence) Less bad (cont’d)
  • 17. Less bad (cont’d) select a.WKCostSheetHeaderID ,a.Sequence - b.minseq + 1 ,a.ItemDescription ,a.Value ,a.GiftInKind ,a.KWNAmount ,a.CheckNumber from #WKCostSheet a join (select c.WKCostSheetHeaderID, MIN (c.sequence) as minseq from #WKCostSheet c group by WKCostSheetHeaderID) as b on a.WKCostSheetHeaderID = b.WKCostSheetHeaderID
  • 18. • Still need a temp table • Join is far less costly than the repeated deletions Less bad – summary
  • 19. SELECT WKCostSheetHeaderID, row_number() over (partition by WKCostSheetHeaderID order by val) ,exp ,val ,gik ,kwn ,chkNum FROM KWN_Access_Admin.dbo.CostSheet c join KWN_dev.dbo.WKCostSheetHeader h on h.WKID = c.KID and h.WKCostSheetTypeID= c.WishNum where KID in (select WKID from dbo.WKs) order by WKCostSheetHeaderID Not so bad
  • 20. • No temp table, no join, no trouble Not so bad – summary
  • 21. • We’re fans of data-driven design; anything that keeps us from having to push code back through QA is a good thing. But, we’re going to make fun of the code that accesses the hierarchical data (note: It was written in SQL Server 2005, prior to hierarchy IDs being available), starting with naming conventions. The below character string is a table name. • [_SynComs.Orders.OrderItem.product->SynComs.Products.PrinterCartridge] • Nontrivial to type, contains special characters… not a lot right with this. • The interesting thing, from their perspective, is that the same query is used for every single database call. That’s right, one query only for every access. The catch is, there’s an unlimited number of recursive calls to get the database results, and the structure was set up to put real (data) information into the physical schema, a nifty way to create extra contention in the system tables. • For the record, the CTE changed approach brought query time from 9.5 seconds down to .23 seconds. Case Study #2: From horrid code to CTE
  • 22. SELECT 0 as generation, major_id as tableId into #tblguid FROM sys.extended_properties WHERE (value in ( 'SynComs.Orders.Order, SynComs' ) and name = 'ParentType') create clustered index CItablID on #tblguid(tableId) Original Code
  • 23. declare @generation int select @generation=0 while (1=1) begin select @generation=@generation+1 insert into #tblguid (generation, tableId) SELECT @generation, parent.major_id FROM #tblguid tbl JOIN sys.extended_properties child on tbl.tableId = child.major_id and child.name = 'ChildType' and generation = @generation -1 JOIN sys.extended_properties parent on child.value = parent.value and parent.name = 'ParentType' Original Code (cont’d)
  • 24. where not exists (select * from #tblguid lookitup where parent.major_id = lookitup.tableId) if (@@ROWCOUNT=0) break end select name as tableName from sys.tables join #tblguid on object_id = tableId Original Code (cont’d)
  • 25. /* Do you like this? We’re about to recursively create / execute a large view… good candidate for rewrite / approach change */ Original Code (cont’d)
  • 26. declare @string varchar(max) select @string = ' create view my_view as select * from [_SynComs.Orders.Order.billingAddress- >SynComs.Customers.CustomerAddress] union all /* At least it’s “union all” here */ select * from [_SynComs.Orders.Order.discounts- >SynComs.Orders.Discounts.Discount] /* … for brevity, I’ve removed about 12 more of these */ exec (@string) go Original Code (cont’d)
  • 27. select 0 as generation, parentObjectGuid, childObjectGuid, fieldName, parentType, childType, _guid_, _pk_ into #guids from my_view where parentObjectGuid IN ( '3ee588d1-2096-4ddb-adc6- d5a140725721',/* about 70 more removed */); Original Code (cont’d)
  • 28. update @guids28927 set generation=0 create clustered index CI_GUID on #guids (_guid_) create nonclustered index NCI_childobject_generation on #guids (generation,childObjectGuid,parentObjectGuid) declare @generation int select @generation=0 Original Code (cont’d)
  • 29. while (1=1) begin select @generation=@generation+1 insert into #guids (generation, parentObjectGuid, childObjectGuid, fieldName, parentType, childType, _guid_, _pk_) select @generation, parentObjectGuid, childObjectGuid, fieldName, parentType, childType, _guid_, _pk_ from my_view where parentObjectGuid in (select childObjectGuid from #guids where generation=(@generation-1)) and not exists (select * from #guids where my_view._guid_ =#guids._guid_ ) Original Code (cont’d)
  • 30. if (@@ROWCOUNT=0) break end Select parentObjectGuid, childObjectGuid, fieldName, parentType, childType, _guid_, _pk_ from #guids Original Code (cont’d)
  • 31. WITH RecursionRelationship ( generation, parentObjectGuid, childObjectGuid,fieldName, parentType, childType, [_guid_], [_pk_] ) AS ( -- Anchor Query select 0 as generation, parentObjectGuid, childObjectGuid, fieldName,parentType, childType, _guid_, _pk_ from dbo.ObjectRelationship where parentObjectGuid IN ( '3ee588d1-2096-4ddb-adc6-d5a140725721', /* same list as above */) Revised Code
  • 32. UNION ALL -- Recursion Query Select r_r.generation +1, o_r.parentObjectGuid, o_r.childObjectGuid, o_r.fieldName, o_r.parentType, o_r.childType, o_r._guid_, o_r._pk_ from dbo.ObjectRelationship o_r JOIN RecursionRelationship r_r on o_r.parentObjectGuid = r_r.childObjectGuid) select parentObjectGuid, childObjectGuid, fieldName, parentType, childType, _guid_, _pk_ from RecursionRelationship option (maxrecursion 32767) Revised Code (cont’d)
  • 33. Jeff Garbus – Email me for a copy! jeff@soaringeagle.guru 813.641.3434 mssqlperformance.blogspot.com http://www.youtube.com/user/soaringeagledba/ Microsoft Transact - SQL, The Definitive Guide – 35% off from jblearning.com. Use code "GARBUS" Upcoming Webinars Check our web site: www.soaringeagle.guru Find us on Social Media @SoaringEagleDBA Like us on Facebook: Facebook.com/SoaringEagleDBA 33 - 60 © Soaring Eagle Consulting 8/19/2013
  • 34. Soaring Eagle Flight Center FREE! For 3 months • Why Try Soaring Eagle Flight Center? • See your environment health in seconds • The hundreds or thousand email alerts are corralled into alerts that you control • Flight provides Predictive Analysis Tools Email sales@soaringeagle.guru to take advantage
  • 35. Purchase SQL Diagnostic Manager and get SQL Doctor FREE! Limited Time Offer!
  • 36. Try Any IDERA Products Free for 14-Days!