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

Oraclesql
OraclesqlOraclesql
Oraclesql
Priya Goyal
 
Nested Types in Impala
Nested Types in ImpalaNested Types in Impala
Nested Types in Impala
Myung Ho Yun
 
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 dumpslilylucy
 
Access 04
Access 04Access 04
Access 04
Alexander Babich
 
Tale of the Gilded Rose
Tale of the Gilded RoseTale of the Gilded Rose
Tale of the Gilded Rose
Lee-Jon Ball
 
Databases and SQL - Lecture C
Databases and SQL - Lecture CDatabases and SQL - Lecture C
Databases and SQL - Lecture C
CMDLearning
 
PHP webboard
PHP webboardPHP webboard
PHP webboard
tumetr1
 
Database development coding standards
Database development coding standardsDatabase development coding standards
Database development coding standardsAlessandro Baratella
 
TSQL Coding Guidelines
TSQL Coding GuidelinesTSQL Coding Guidelines
TSQL Coding Guidelines
Chris Adkin
 

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 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
Iván Stepaniuk
 
Complex queries in sql
Complex queries in sqlComplex queries in sql
Complex queries in sql
Charan Reddy
 
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
gourav kottawar
 
SQL Join Basic
SQL Join BasicSQL Join Basic
SQL Join Basic
Naimul Arif
 
SQL | Computer Science
SQL | Computer ScienceSQL | Computer Science
SQL | Computer Science
Transweb Global Inc
 
Sql subquery
Sql  subquerySql  subquery
Sql subquery
Raveena Thakur
 
100 sql queries
100 sql queries100 sql queries
100 sql queries
Srinimf-Slides
 
SQL Server Learning Drive
SQL Server Learning Drive SQL Server Learning Drive
SQL Server Learning Drive
TechandMate
 
Sql queries interview questions
Sql queries interview questionsSql queries interview questions
Sql queries interview questionsPyadav010186
 

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

3 indexes
3 indexes3 indexes
3 indexes
Ram Kedem
 
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
Wim Godden
 
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
Skillwise Group
 
Super spike
Super spikeSuper spike
Super spike
Michael Falanga
 
Agile Database Development with JSON
Agile Database Development with JSONAgile Database Development with JSON
Agile Database Development with JSON
Chris Saxon
 
SQL Server 2008 Portfolio
SQL Server 2008 PortfolioSQL Server 2008 Portfolio
SQL Server 2008 Portfolio
lilredlokita
 
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 PracticesOleksandr Zarichnyi
 
Introduction to SQL Antipatterns
Introduction to SQL AntipatternsIntroduction to SQL Antipatterns
Introduction to SQL Antipatterns
Krishnakumar S
 
[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
MongoDB
 
テスト用のプレゼンテーション
テスト用のプレゼンテーションテスト用のプレゼンテーション
テスト用のプレゼンテーション
gooseboi
 
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
Miro Wengner
 
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?
loginworks software
 
Building Better Applications with Data::Manager
Building Better Applications with Data::ManagerBuilding Better Applications with Data::Manager
Building Better Applications with Data::ManagerJay Shirley
 
ADBMS ASSIGNMENT
ADBMS ASSIGNMENTADBMS ASSIGNMENT
ADBMS ASSIGNMENT
Lori Moore
 
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
 
Business Intelligence Portfolio
Business Intelligence PortfolioBusiness Intelligence Portfolio
Business Intelligence PortfolioChris 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
 
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
eddiebaggott
 

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

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...
IDERA Software
 
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
IDERA Software
 
Public cloud uses and limitations
Public cloud uses and limitationsPublic cloud uses and limitations
Public cloud uses and limitations
IDERA Software
 
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
IDERA Software
 
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
IDERA Software
 
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
IDERA Software
 
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 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: 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 Software
 
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 Software
 
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 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: 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 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
 
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
IDERA Software
 
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
IDERA Software
 
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...
IDERA Software
 
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
IDERA Software
 
Achieve More with Less Resources | IDERA
Achieve More with Less Resources | IDERAAchieve More with Less Resources | IDERA
Achieve More with Less Resources | IDERA
IDERA Software
 
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
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

The Future of Platform Engineering
The Future of Platform EngineeringThe Future of Platform Engineering
The Future of Platform Engineering
Jemma Hussein Allen
 
By Design, not by Accident - Agile Venture Bolzano 2024
By Design, not by Accident - Agile Venture Bolzano 2024By Design, not by Accident - Agile Venture Bolzano 2024
By Design, not by Accident - Agile Venture Bolzano 2024
Pierluigi Pugliese
 
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 previewState of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
Prayukth K V
 
Epistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI supportEpistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI support
Alan Dix
 
Elizabeth Buie - Older adults: Are we really designing for our future selves?
Elizabeth Buie - Older adults: Are we really designing for our future selves?Elizabeth Buie - Older adults: Are we really designing for our future selves?
Elizabeth Buie - Older adults: Are we really designing for our future selves?
Nexer Digital
 
DevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA ConnectDevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA Connect
Kari Kakkonen
 
Microsoft - Power Platform_G.Aspiotis.pdf
Microsoft - Power Platform_G.Aspiotis.pdfMicrosoft - Power Platform_G.Aspiotis.pdf
Microsoft - Power Platform_G.Aspiotis.pdf
Uni Systems S.M.S.A.
 
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
Neo4j
 
Pushing the limits of ePRTC: 100ns holdover for 100 days
Pushing the limits of ePRTC: 100ns holdover for 100 daysPushing the limits of ePRTC: 100ns holdover for 100 days
Pushing the limits of ePRTC: 100ns holdover for 100 days
Adtran
 
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
 
GridMate - End to end testing is a critical piece to ensure quality and avoid...
GridMate - End to end testing is a critical piece to ensure quality and avoid...GridMate - End to end testing is a critical piece to ensure quality and avoid...
GridMate - End to end testing is a critical piece to ensure quality and avoid...
ThomasParaiso2
 
20240607 QFM018 Elixir Reading List May 2024
20240607 QFM018 Elixir Reading List May 202420240607 QFM018 Elixir Reading List May 2024
20240607 QFM018 Elixir Reading List May 2024
Matthew Sinclair
 
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdfFIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance
 
Introduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - CybersecurityIntroduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - Cybersecurity
mikeeftimakis1
 
Monitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR EventsMonitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR Events
Ana-Maria Mihalceanu
 
SAP Sapphire 2024 - ASUG301 building better apps with SAP Fiori.pdf
SAP Sapphire 2024 - ASUG301 building better apps with SAP Fiori.pdfSAP Sapphire 2024 - ASUG301 building better apps with SAP Fiori.pdf
SAP Sapphire 2024 - ASUG301 building better apps with SAP Fiori.pdf
Peter Spielvogel
 
Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !
KatiaHIMEUR1
 
Communications Mining Series - Zero to Hero - Session 1
Communications Mining Series - Zero to Hero - Session 1Communications Mining Series - Zero to Hero - Session 1
Communications Mining Series - Zero to Hero - Session 1
DianaGray10
 
Free Complete Python - A step towards Data Science
Free Complete Python - A step towards Data ScienceFree Complete Python - A step towards Data Science
Free Complete Python - A step towards Data Science
RinaMondal9
 
FIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdfFIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance
 

Recently uploaded (20)

The Future of Platform Engineering
The Future of Platform EngineeringThe Future of Platform Engineering
The Future of Platform Engineering
 
By Design, not by Accident - Agile Venture Bolzano 2024
By Design, not by Accident - Agile Venture Bolzano 2024By Design, not by Accident - Agile Venture Bolzano 2024
By Design, not by Accident - Agile Venture Bolzano 2024
 
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 previewState of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
 
Epistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI supportEpistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI support
 
Elizabeth Buie - Older adults: Are we really designing for our future selves?
Elizabeth Buie - Older adults: Are we really designing for our future selves?Elizabeth Buie - Older adults: Are we really designing for our future selves?
Elizabeth Buie - Older adults: Are we really designing for our future selves?
 
DevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA ConnectDevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA Connect
 
Microsoft - Power Platform_G.Aspiotis.pdf
Microsoft - Power Platform_G.Aspiotis.pdfMicrosoft - Power Platform_G.Aspiotis.pdf
Microsoft - Power Platform_G.Aspiotis.pdf
 
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
 
Pushing the limits of ePRTC: 100ns holdover for 100 days
Pushing the limits of ePRTC: 100ns holdover for 100 daysPushing the limits of ePRTC: 100ns holdover for 100 days
Pushing the limits of ePRTC: 100ns holdover for 100 days
 
Removing Uninteresting Bytes in Software Fuzzing
Removing Uninteresting Bytes in Software FuzzingRemoving Uninteresting Bytes in Software Fuzzing
Removing Uninteresting Bytes in Software Fuzzing
 
GridMate - End to end testing is a critical piece to ensure quality and avoid...
GridMate - End to end testing is a critical piece to ensure quality and avoid...GridMate - End to end testing is a critical piece to ensure quality and avoid...
GridMate - End to end testing is a critical piece to ensure quality and avoid...
 
20240607 QFM018 Elixir Reading List May 2024
20240607 QFM018 Elixir Reading List May 202420240607 QFM018 Elixir Reading List May 2024
20240607 QFM018 Elixir Reading List May 2024
 
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdfFIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
 
Introduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - CybersecurityIntroduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - Cybersecurity
 
Monitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR EventsMonitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR Events
 
SAP Sapphire 2024 - ASUG301 building better apps with SAP Fiori.pdf
SAP Sapphire 2024 - ASUG301 building better apps with SAP Fiori.pdfSAP Sapphire 2024 - ASUG301 building better apps with SAP Fiori.pdf
SAP Sapphire 2024 - ASUG301 building better apps with SAP Fiori.pdf
 
Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !
 
Communications Mining Series - Zero to Hero - Session 1
Communications Mining Series - Zero to Hero - Session 1Communications Mining Series - Zero to Hero - Session 1
Communications Mining Series - Zero to Hero - Session 1
 
Free Complete Python - A step towards Data Science
Free Complete Python - A step towards Data ScienceFree Complete Python - A step towards Data Science
Free Complete Python - A step towards Data Science
 
FIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdfFIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdf
 

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!