SlideShare ist ein Scribd-Unternehmen logo
1 von 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!

Weitere ähnliche Inhalte

Was ist angesagt?

Nested Types in Impala
Nested Types in ImpalaNested Types in Impala
Nested Types in ImpalaMyung 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
 
Tale of the Gilded Rose
Tale of the Gilded RoseTale of the Gilded Rose
Tale of the Gilded RoseLee-Jon Ball
 
Databases and SQL - Lecture C
Databases and SQL - Lecture CDatabases and SQL - Lecture C
Databases and SQL - Lecture CCMDLearning
 
PHP webboard
PHP webboardPHP webboard
PHP webboardtumetr1
 
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 GuidelinesChris Adkin
 

Was ist angesagt? (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
 

Andere mochten auch

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 stakeholdersIván Stepaniuk
 
Complex queries in sql
Complex queries in sqlComplex queries in sql
Complex queries in sqlCharan 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 slidesgourav kottawar
 
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
 

Andere mochten auch (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
 

Ähnlich wie Geek Sync | Rewriting Bad SQL Code 101

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 codeWim 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 1Skillwise Group
 
Agile Database Development with JSON
Agile Database Development with JSONAgile Database Development with JSON
Agile Database Development with JSONChris Saxon
 
SQL Server 2008 Portfolio
SQL Server 2008 PortfolioSQL Server 2008 Portfolio
SQL Server 2008 Portfoliolilredlokita
 
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 AntipatternsKrishnakumar 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 SchemaMongoDB
 
テスト用のプレゼンテーション
テスト用のプレゼンテーションテスト用のプレゼンテーション
テスト用のプレゼンテーション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 engineeringMiro 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 ASSIGNMENTLori 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 DatabaseDBrow 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 2019Victor Rentea
 
Кирилл Безпалый, .NET Developer, Ciklum
Кирилл Безпалый, .NET Developer, CiklumКирилл Безпалый, .NET Developer, Ciklum
Кирилл Безпалый, .NET Developer, CiklumAlina 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, 2015eddiebaggott
 

Ähnlich wie 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
 

Mehr von 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 cloudIDERA Software
 
Public cloud uses and limitations
Public cloud uses and limitationsPublic cloud uses and limitations
Public cloud uses and limitationsIDERA 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.pptxIDERA 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 ServerIDERA 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 databasesIDERA 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 costsIDERA 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 HobermanIDERA 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 FlugIDERA 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 ScalzoIDERA 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 RathbunIDERA 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 | IDERAIDERA 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 | IDERAIDERA Software
 
Achieve More with Less Resources | IDERA
Achieve More with Less Resources | IDERAAchieve More with Less Resources | IDERA
Achieve More with Less Resources | IDERAIDERA 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 | IDERAIDERA Software
 

Mehr von 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
 

Kürzlich hochgeladen

EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWERMadyBayot
 
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot ModelMcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot ModelDeepika Singh
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Victor Rentea
 
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...apidays
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDropbox
 
[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdfSandro Moreira
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Jeffrey Haguewood
 
CNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In PakistanCNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In Pakistandanishmna97
 
Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​Bhuvaneswari Subramani
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingEdi Saputra
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native ApplicationsWSO2
 
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...DianaGray10
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfsudhanshuwaghmare1
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...apidays
 
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 Takeoffsammart93
 
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, Adobeapidays
 
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...apidays
 
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 educationjfdjdjcjdnsjd
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsNanddeep Nachan
 

Kürzlich hochgeladen (20)

EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
 
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot ModelMcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
 
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 
[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
 
CNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In PakistanCNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In Pakistan
 
Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​
 
Understanding the FAA Part 107 License ..
Understanding the FAA Part 107 License ..Understanding the FAA Part 107 License ..
Understanding the FAA Part 107 License ..
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
 
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...
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 
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
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
 
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
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectors
 

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!