SlideShare a Scribd company logo
1 of 51
SQL Server Infernals
A Beginner’s Guide to SQL Server Worst Practices
Gianluca Sartori
@spaghettidba
Gianluca Sartori
Independent SQL Server consultant
SQL Server MVP, MCTS, MCITP, MCT
Works with SQL Server since version 7
DBA @ Scuderia Ferrari
Blog: spaghettidba.com
Twitter: @spaghettidba
Agenda
 Best practices or Worst practices?
 What can go wrong?
 Design
 Development
 Installation
 Administration
Disclaimer:
 Not everything is black or white
 «It depends» is the most likely answer
There are edge cases when some of these worst practices are the
only possible solution, or not such a bad idea…
Best Practices vs. Worst Practices
 Why Best Practices are not enough
 Too many
 No time
 Lack of experience
 Not always clear what happens if we don’t follow them
 Why Worst Practices help
 They show the mistakes to avoid
 We can learn from someone else’s mistakes
Worst Practices Areas
Design Development Installation Administration
Schema design
Naming
Data Types
Environment HW validation
OS configuration
SQL installation
Recovery
Security
Capacity
Performance
Monitoring
Code
Test
SQL Server Infernals
 Worst Practices are sins that will put
you in the SQL Server hell!!
 I will guide you through the circles, as
Virgil did with Dante
CIRCLE 1:
Undernormalizers
Schema Design
 Not normalizing the schema
 1NF:
A primary key, atomic attributes only
 2NF:
Every attribute depends on the whole key
 3NF:
Every attribute depends only on the key
«The key, the whole key, nothing but the key,
so help me Codd»
Clues of denormalization
 Repeating data  redundancies
 Inconsistent data between tables anomalies
 Data separated by «,»
 Ex: john@gmail.com, john@business.com
 Structured data in «notes» columns
 Columns with a numeric suffix
 Ex: Zone1, Zone2, Zone3 …
CIRCLE 2:
Generalizers
Lookup Tables
Orders
PK order_id int
order_date datetime
FK2 customer_id int
FK1 status_id char(2)
FK3 priority_id tinyint
Order_Status
PK status_id char(2)
status_description nvarchar(50)
Customers
PK customer_id int
name varchar(100)
address varchar(50)
ZIP char(5)
city nvarchar(50)
FK2 state_id char(2)
FK1 country_id char(3)
Countries
PK country_id char(3)
description nvarchar(50)
States
PK state_id char(2)
description nvarchar(50)
Order_Priorities
PK priority_id tinyint
priority_description nvarchar(50)
One lookup table for each attribute
OTLT: One True Lookup Table
Orders
PK order_id int
order_date datetime
FK1 customer_id int
status_id char(2)
priority_id tinyint
Customers
PK customer_id int
name nvarchar(100)
address nvarchar(50)
ZIP char(5)
city nvarchar(50)
state_id char(2)
country_id char(3)
LookupTable
PK table_name sysname
PK lookup_code nvarchar(500)
lookup_description nvarchar(4000)
CREATE TABLE LookupTable (
table_name sysname,
lookup_code nvarchar(500),
lookup_description nvarchar(4000)
)
One lookup table for all attributes
OTLT: One True Lookup Table
 No Foreign Keys
 Generic data types  nvarchar(SomeHighNumber)
Implicit Conversions, Incorrect Data, Huge memory grants…
 CHECK constraints may help to a point…
 Locking
CHECK(
CASE
WHEN lookup_code = 'states' AND lookup_code LIKE '[A-Z][A-Z]' THEN 1
WHEN lookup_code = 'priorities' AND lookup_code LIKE '[0-9]' THEN 1
WHEN lookup_code = 'countries' AND lookup_code LIKE '[0-9][0-9][0-9]' THEN 1
WHEN lookup_code = 'status' AND lookup_code LIKE '[A-Z][A-Z]' THEN 1
ELSE 0
END = 1
)
EAV: Entity, Attribute, Value
Customers
PK customer_id int
name nvarchar(100)
address nvarchar(50)
ZIP char(5)
city nvarchar(50)
state_id char(2)
country_id char(3)
AttributeNames
PK attribute_id int
PK,FK1 entity_id int
attribute_name nvarchar(128)
AttributeValues
PK,FK1 attribute_id int
PK,FK1 entity_id int
PK,FK2,FK3 id int
value nvarchar(4000)
Entities
PK entity_id int
entity_name nvarchar(128)
Orders
PK order_id int
order_date datetime
customer_id int
status_id char(2)
priority_id tinyint
EAV: Entity, Attribute, Value
Disadvantages:
 Generic data types  Ex: varchar(4000)
 No Foreign Keys
 No CHECK constraints
 Multiple accesses to the same table
 One access per attribute
Advantages
 Dynamic schema: no need to alter the database
 Replication, distributed environments
EAV: Entity, Attribute, Value
 Reporting is insanely hard.
 Writing to the EAV schema is a mess
 Workaround:
 Reads: PIVOT / Crosstab
 Writes: View + INSTEAD OF triggers
 Alternatives:
 SPARSE columns
 XML/JSON
 Key-value store databases
 Document-oriented databases
DEMO:
EAV Design
CIRCLE 3:
Shaky Typers
Data type Worst Practices
 Numeric data types for non-numeric data
 Storing data as their human-readable representation
 Using deprecated data types
 Using larger data types “just in case”
 Using variable length data types for fixed size data
 Storing durations in date/datetime columns
 Getting Unicode wrong
 Using different data types for the same data in different tables
CIRCLE 4:
Anarchic Designers
Chaos Belongs to Hell
 No Primary Key o surrogate keys only
«identity» is not the only possible key!
 No Foreign Keys
They’re «awkward»
 No CHECK constraint
The application will guarantee consistency…
 Wrong data types
 Data type is the 1° constraint on the data
 Use of NULL where not appropriate
 Use of «dummy» data (ex: ‘.’ , 0)
CIRCLE 5:
Inconsistent Baptists
Damnation by Namification
 Hungarian Notation (AKA «tibbing»)
 Insanely short names
 Insanely long names
 Mixing languages
 Using the «sp_» prefix
 Using reserved words or illegal characters
 Using system generated constraint names
 No naming convention or multiple naming conventions
Hungary is a nice str_country
CIRCLE 6:
Environment Pollutors
Pollutors will be prosecuted
 Developing in production
 Using the test environment for development
 Using a shared database for development
 No source control
 Developing with sysadmin privileges
 Developing on a different version/edition from production
(less problematic after 2016 SP1)
CIRCLE 7:
Overly Optimistic Testers
Pessimists are Optimists with Experience
 Not testing all the code
Use meaningful data volumes
 Testing in production
Can alter production data
Interferes with production users
 Testing in development environment
Useful at most for unit tests
CIRCLE 8:
Indolent developers
Development Worst Practices
 No transactions
 No error handling
@@ERROR is a thing of the past!
 Wrong isolation levels
NOLOCK = no consistency!
 SELECT *
 Dynamic SQL with concatenated params
 Code vulnerable to SQL injection
 No abstraction layer
Views, Functions, Stored Procedures
It’s all about laziness
CIRCLE 9:
Stingy buyers
HW Worst Practices
 Using inadequate or unbalanced HW
 Reusing decommissioned servers for new installations
 Slower CPUs (license costs the same on fast CPUs)
 Less RAM supported
 Planning storage with capacity in mind
 Choosing the wrong RAID level
CIRCLE 10:
Next next finish installers
Installation Worst Practices
 Installing accepting all the defaults
 Data files on the system drive
 MAXDOP = 0
 Max Server Memory = +∞
 Installing unused components
 Installing multiple services on the same machine
 Giving up easy wins on I/O
 Partition misalignment
 Using the default allocation unit (4Kb)
CIRCLE 11:
Careless caretakers
What does a database need?
Backup and Recovery Worst Practices
 No backup
 With FULL recovery it’s a timebomb
 Ignoring RPO and RTO (it’s not your decision!)
 No test restores
 No consistency checks
 DBCC REPAIR_ALLOW_DATA_LOSS as default response to corruption
Our responsibility is to perform restores,
not backups!
Security Worst Practices
 Too many sysadmins
 Everyone authenticating as ‘sa’
 Using SQL Authentication
 Weak passwords
 123
 P4$$w0rd
 Same as username
 No auditing on sensitive data
Capacity Management Worst Practices
 Not checking disk space
 No space left = database halted!
 FULL recovery and no log backups?
 Relying 100% on autogrowth
 Autoshrink
 Autoclose
 Not presizing tempdb
Different file size = latching (and striping) penalty
Maintenance Worst Practices
 Not maintaining indexes and statistics
 Obsessing over maintaining indexes and statistics
 Using catch-all maintenance plans
CIRCLE 12:
Performance Killers
Performance Tuning
Easier to implementMore effective
Query Optimization Worst Practices
RBAR: Row By Agonizing Row
 Cursors
 WHILE loops
 App-side cursors
 Scalar and multi-statement functions
Query Optimization Worst Practices
Views on views on views…
Might look like a brilliant idea at first (code re-use FTW!)
 You can end up losing control
 Unneeded multiple accesses to the same tables
 Unnecessary JOINs
DEMO:
Nested views
Query Optimization Worst Practices
 One query to rule them all
The optimizer is good, not perfect
«divide et impera» delivers better performance
 DISTINCT in all queries
… because “who wants stinkin’ duplicates?”
 Query HINTs all over the place
Especially index hints
Indexing Worst Practices
 Accepting all suggestions from Tuning Advisor
 Duplicate indexes
 An index for each column
 Indexes are not for free!
 Suboptimal Clustered Index
 Unique
 Small
 Unchanging
 Ever increasing or decreasing
NEWSEQUENTIALID()
NEWID()
Server Tuning Worst Practices
 «Throwing HW» at the problem
 A 2x faster machine might make RBAR code 2x faster
 Using set-based code might make it 500x faster
 Using «advanced» options without testing
 NT Fibers (lightweight pooling)
 Priority Boost
Resources
Detailed blog posts on spaghettidba.com
One post for each circle:
https://spaghettidba.com/category/sql-server/sql-server-infernals/
Resources
Free Tool:
Best Practices Analyzer
 Highlights configuration parameters that don’t comply with
best practices
 Highlights potential problems
 Offers recommendations
http://www.microsoft.com/en-us/download/details.aspx?id=15289
SQL Server Infernals
A Beginner’s Guide to SQL Server Worst Practices
https://groupby.org/go/session5
Contact:
spaghettidba@sqlconsulting.it
More infernal stuff:
https://spaghettidba.com/category/sql-server/sql-server-infernals/

More Related Content

What's hot

Toad tipstricksexpertinsight
Toad tipstricksexpertinsightToad tipstricksexpertinsight
Toad tipstricksexpertinsightRaj esh
 
Slick - The Structured Way
Slick - The Structured WaySlick - The Structured Way
Slick - The Structured WayYennick Trevels
 
SQL Server 2016 Temporal Tables
SQL Server 2016 Temporal TablesSQL Server 2016 Temporal Tables
SQL Server 2016 Temporal TablesDavide Mauri
 
Troubleshooting K1000
Troubleshooting K1000Troubleshooting K1000
Troubleshooting K1000Dell World
 
Indexes: The neglected performance all rounder
Indexes: The neglected performance all rounderIndexes: The neglected performance all rounder
Indexes: The neglected performance all rounderMarkus Winand
 
Inventory Tips & Tricks
Inventory Tips & TricksInventory Tips & Tricks
Inventory Tips & TricksDell World
 
Is there a SQL for NoSQL?
Is there a SQL for NoSQL?Is there a SQL for NoSQL?
Is there a SQL for NoSQL?Arthur Keen
 
Hack your db before the hackers do
Hack your db before the hackers doHack your db before the hackers do
Hack your db before the hackers dofangjiafu
 
Natural Born Killers, Performance issues to avoid
Natural Born Killers, Performance issues to avoid Natural Born Killers, Performance issues to avoid
Natural Born Killers, Performance issues to avoid Richard Douglas
 
SSIS Monitoring Deep Dive
SSIS Monitoring Deep DiveSSIS Monitoring Deep Dive
SSIS Monitoring Deep DiveDavide Mauri
 
Wait Watchers ; Gain SQL Performance Increases Fast!
Wait Watchers; Gain SQL Performance Increases Fast!Wait Watchers; Gain SQL Performance Increases Fast!
Wait Watchers ; Gain SQL Performance Increases Fast!Richard Douglas
 
Campus days 2013 - Instrumentation
Campus days 2013 - InstrumentationCampus days 2013 - Instrumentation
Campus days 2013 - InstrumentationAnders Lybecker
 
Ten query tuning techniques every SQL Server programmer should know
Ten query tuning techniques every SQL Server programmer should knowTen query tuning techniques every SQL Server programmer should know
Ten query tuning techniques every SQL Server programmer should knowKevin Kline
 
Using Angular JS in APEX
Using Angular JS in APEXUsing Angular JS in APEX
Using Angular JS in APEXEnkitec
 
50 Shades of Fail KScope16
50 Shades of Fail KScope1650 Shades of Fail KScope16
50 Shades of Fail KScope16Christian Berg
 
Thick Application Penetration Testing: Crash Course
Thick Application Penetration Testing: Crash CourseThick Application Penetration Testing: Crash Course
Thick Application Penetration Testing: Crash CourseScott Sutherland
 
A lap around microsofts business intelligence platform
A lap around microsofts business intelligence platformA lap around microsofts business intelligence platform
A lap around microsofts business intelligence platformIke Ellis
 
Reduce latency and boost sql server io performance
Reduce latency and boost sql server io performanceReduce latency and boost sql server io performance
Reduce latency and boost sql server io performanceKevin Kline
 

What's hot (20)

Toad tipstricksexpertinsight
Toad tipstricksexpertinsightToad tipstricksexpertinsight
Toad tipstricksexpertinsight
 
Slick - The Structured Way
Slick - The Structured WaySlick - The Structured Way
Slick - The Structured Way
 
SQL Server 2016 Temporal Tables
SQL Server 2016 Temporal TablesSQL Server 2016 Temporal Tables
SQL Server 2016 Temporal Tables
 
Troubleshooting K1000
Troubleshooting K1000Troubleshooting K1000
Troubleshooting K1000
 
Indexes: The neglected performance all rounder
Indexes: The neglected performance all rounderIndexes: The neglected performance all rounder
Indexes: The neglected performance all rounder
 
Inventory Tips & Tricks
Inventory Tips & TricksInventory Tips & Tricks
Inventory Tips & Tricks
 
Is there a SQL for NoSQL?
Is there a SQL for NoSQL?Is there a SQL for NoSQL?
Is there a SQL for NoSQL?
 
Hack your db before the hackers do
Hack your db before the hackers doHack your db before the hackers do
Hack your db before the hackers do
 
Natural Born Killers, Performance issues to avoid
Natural Born Killers, Performance issues to avoid Natural Born Killers, Performance issues to avoid
Natural Born Killers, Performance issues to avoid
 
SSIS Monitoring Deep Dive
SSIS Monitoring Deep DiveSSIS Monitoring Deep Dive
SSIS Monitoring Deep Dive
 
Wait Watchers ; Gain SQL Performance Increases Fast!
Wait Watchers; Gain SQL Performance Increases Fast!Wait Watchers; Gain SQL Performance Increases Fast!
Wait Watchers ; Gain SQL Performance Increases Fast!
 
Campus days 2013 - Instrumentation
Campus days 2013 - InstrumentationCampus days 2013 - Instrumentation
Campus days 2013 - Instrumentation
 
Ten query tuning techniques every SQL Server programmer should know
Ten query tuning techniques every SQL Server programmer should knowTen query tuning techniques every SQL Server programmer should know
Ten query tuning techniques every SQL Server programmer should know
 
Using Angular JS in APEX
Using Angular JS in APEXUsing Angular JS in APEX
Using Angular JS in APEX
 
Plantilla oracle
Plantilla oraclePlantilla oracle
Plantilla oracle
 
50 Shades of Fail KScope16
50 Shades of Fail KScope1650 Shades of Fail KScope16
50 Shades of Fail KScope16
 
Thick Application Penetration Testing: Crash Course
Thick Application Penetration Testing: Crash CourseThick Application Penetration Testing: Crash Course
Thick Application Penetration Testing: Crash Course
 
Understanding indices
Understanding indicesUnderstanding indices
Understanding indices
 
A lap around microsofts business intelligence platform
A lap around microsofts business intelligence platformA lap around microsofts business intelligence platform
A lap around microsofts business intelligence platform
 
Reduce latency and boost sql server io performance
Reduce latency and boost sql server io performanceReduce latency and boost sql server io performance
Reduce latency and boost sql server io performance
 

Viewers also liked

Ευτυχώς ηττηθήκαμε σύντροφοι - Τάκης Λαζαρίδης
Ευτυχώς ηττηθήκαμε σύντροφοι - Τάκης ΛαζαρίδηςΕυτυχώς ηττηθήκαμε σύντροφοι - Τάκης Λαζαρίδης
Ευτυχώς ηττηθήκαμε σύντροφοι - Τάκης ΛαζαρίδηςAgnostosX
 
DevOps 101 for data professionals
DevOps 101 for data professionalsDevOps 101 for data professionals
DevOps 101 for data professionalsAlex Yates
 
Memory management in sql server
Memory management in sql serverMemory management in sql server
Memory management in sql serverPrashant Kumar
 
Rim Based Relational Database Design Tutorial September 2008
Rim Based Relational Database Design Tutorial September 2008Rim Based Relational Database Design Tutorial September 2008
Rim Based Relational Database Design Tutorial September 2008Abdul-Malik Shakir
 
Sql Server 2014 In Memory
Sql Server 2014 In MemorySql Server 2014 In Memory
Sql Server 2014 In MemoryRavi Okade
 
Augmenting RDBMS with MongoDB for ecommerce
Augmenting RDBMS with MongoDB for ecommerceAugmenting RDBMS with MongoDB for ecommerce
Augmenting RDBMS with MongoDB for ecommerceSteven Francia
 
Microsoft SQL Server internals & architecture
Microsoft SQL Server internals & architectureMicrosoft SQL Server internals & architecture
Microsoft SQL Server internals & architectureKevin Kline
 

Viewers also liked (8)

Ευτυχώς ηττηθήκαμε σύντροφοι - Τάκης Λαζαρίδης
Ευτυχώς ηττηθήκαμε σύντροφοι - Τάκης ΛαζαρίδηςΕυτυχώς ηττηθήκαμε σύντροφοι - Τάκης Λαζαρίδης
Ευτυχώς ηττηθήκαμε σύντροφοι - Τάκης Λαζαρίδης
 
SQL 2005 Memory Module
SQL 2005 Memory ModuleSQL 2005 Memory Module
SQL 2005 Memory Module
 
DevOps 101 for data professionals
DevOps 101 for data professionalsDevOps 101 for data professionals
DevOps 101 for data professionals
 
Memory management in sql server
Memory management in sql serverMemory management in sql server
Memory management in sql server
 
Rim Based Relational Database Design Tutorial September 2008
Rim Based Relational Database Design Tutorial September 2008Rim Based Relational Database Design Tutorial September 2008
Rim Based Relational Database Design Tutorial September 2008
 
Sql Server 2014 In Memory
Sql Server 2014 In MemorySql Server 2014 In Memory
Sql Server 2014 In Memory
 
Augmenting RDBMS with MongoDB for ecommerce
Augmenting RDBMS with MongoDB for ecommerceAugmenting RDBMS with MongoDB for ecommerce
Augmenting RDBMS with MongoDB for ecommerce
 
Microsoft SQL Server internals & architecture
Microsoft SQL Server internals & architectureMicrosoft SQL Server internals & architecture
Microsoft SQL Server internals & architecture
 

Similar to Sql server infernals

Getting Unstuck: Working with Legacy Code and Data
Getting Unstuck: Working with Legacy Code and DataGetting Unstuck: Working with Legacy Code and Data
Getting Unstuck: Working with Legacy Code and DataCory Foy
 
Performance By Design
Performance By DesignPerformance By Design
Performance By DesignGuy Harrison
 
Tools and Tips: From Accidental to Efficient Data Warehouse Developer (SQLSat...
Tools and Tips: From Accidental to Efficient Data Warehouse Developer (SQLSat...Tools and Tips: From Accidental to Efficient Data Warehouse Developer (SQLSat...
Tools and Tips: From Accidental to Efficient Data Warehouse Developer (SQLSat...Cathrine Wilhelmsen
 
Domain oriented development
Domain oriented developmentDomain oriented development
Domain oriented developmentrajmundr
 
Elegant and Efficient Database Design
Elegant and Efficient Database DesignElegant and Efficient Database Design
Elegant and Efficient Database DesignBecky Sweger
 
My SQL Skills Killed the Server
My SQL Skills Killed the ServerMy SQL Skills Killed the Server
My SQL Skills Killed the ServerdevObjective
 
No more Three Tier - A path to a better code for Cloud and Azure
No more Three Tier - A path to a better code for Cloud and AzureNo more Three Tier - A path to a better code for Cloud and Azure
No more Three Tier - A path to a better code for Cloud and AzureMarco Parenzan
 
Sql Server Performance Tuning
Sql Server Performance TuningSql Server Performance Tuning
Sql Server Performance TuningBala Subra
 
SELJE_Database_Unit_Testing_Slides.pdf
SELJE_Database_Unit_Testing_Slides.pdfSELJE_Database_Unit_Testing_Slides.pdf
SELJE_Database_Unit_Testing_Slides.pdfEric Selje
 
Mechanisms for Database Intrusion Detection and Response
Mechanisms for Database Intrusion Detection and ResponseMechanisms for Database Intrusion Detection and Response
Mechanisms for Database Intrusion Detection and ResponseAshish Kamra
 
Tutorial databasetestingusingsql
Tutorial databasetestingusingsqlTutorial databasetestingusingsql
Tutorial databasetestingusingsqlRenuka Ballal
 
Quick & Easy SQL Tips
Quick & Easy SQL TipsQuick & Easy SQL Tips
Quick & Easy SQL TipsIke Ellis
 
How To Buy Data Warehouse
How To Buy Data WarehouseHow To Buy Data Warehouse
How To Buy Data WarehouseEric Sun
 
Metric Abuse: Frequently Misused Metrics in Oracle
Metric Abuse: Frequently Misused Metrics in OracleMetric Abuse: Frequently Misused Metrics in Oracle
Metric Abuse: Frequently Misused Metrics in OracleSteve Karam
 
15 Ways to Kill Your Mysql Application Performance
15 Ways to Kill Your Mysql Application Performance15 Ways to Kill Your Mysql Application Performance
15 Ways to Kill Your Mysql Application Performanceguest9912e5
 

Similar to Sql server infernals (20)

Getting Unstuck: Working with Legacy Code and Data
Getting Unstuck: Working with Legacy Code and DataGetting Unstuck: Working with Legacy Code and Data
Getting Unstuck: Working with Legacy Code and Data
 
Performance By Design
Performance By DesignPerformance By Design
Performance By Design
 
Tools and Tips: From Accidental to Efficient Data Warehouse Developer (SQLSat...
Tools and Tips: From Accidental to Efficient Data Warehouse Developer (SQLSat...Tools and Tips: From Accidental to Efficient Data Warehouse Developer (SQLSat...
Tools and Tips: From Accidental to Efficient Data Warehouse Developer (SQLSat...
 
Domain oriented development
Domain oriented developmentDomain oriented development
Domain oriented development
 
Elegant and Efficient Database Design
Elegant and Efficient Database DesignElegant and Efficient Database Design
Elegant and Efficient Database Design
 
My SQL Skills Killed the Server
My SQL Skills Killed the ServerMy SQL Skills Killed the Server
My SQL Skills Killed the Server
 
Sql killedserver
Sql killedserverSql killedserver
Sql killedserver
 
SQL Server 2012 Best Practices
SQL Server 2012 Best PracticesSQL Server 2012 Best Practices
SQL Server 2012 Best Practices
 
10 sql tips
10 sql tips10 sql tips
10 sql tips
 
No more Three Tier - A path to a better code for Cloud and Azure
No more Three Tier - A path to a better code for Cloud and AzureNo more Three Tier - A path to a better code for Cloud and Azure
No more Three Tier - A path to a better code for Cloud and Azure
 
Sql Server Performance Tuning
Sql Server Performance TuningSql Server Performance Tuning
Sql Server Performance Tuning
 
SELJE_Database_Unit_Testing_Slides.pdf
SELJE_Database_Unit_Testing_Slides.pdfSELJE_Database_Unit_Testing_Slides.pdf
SELJE_Database_Unit_Testing_Slides.pdf
 
Mechanisms for Database Intrusion Detection and Response
Mechanisms for Database Intrusion Detection and ResponseMechanisms for Database Intrusion Detection and Response
Mechanisms for Database Intrusion Detection and Response
 
FREE Sql Server syllabus
FREE Sql Server syllabusFREE Sql Server syllabus
FREE Sql Server syllabus
 
Backpack Tools4 Sql Dev
Backpack Tools4 Sql DevBackpack Tools4 Sql Dev
Backpack Tools4 Sql Dev
 
Tutorial databasetestingusingsql
Tutorial databasetestingusingsqlTutorial databasetestingusingsql
Tutorial databasetestingusingsql
 
Quick & Easy SQL Tips
Quick & Easy SQL TipsQuick & Easy SQL Tips
Quick & Easy SQL Tips
 
How To Buy Data Warehouse
How To Buy Data WarehouseHow To Buy Data Warehouse
How To Buy Data Warehouse
 
Metric Abuse: Frequently Misused Metrics in Oracle
Metric Abuse: Frequently Misused Metrics in OracleMetric Abuse: Frequently Misused Metrics in Oracle
Metric Abuse: Frequently Misused Metrics in Oracle
 
15 Ways to Kill Your Mysql Application Performance
15 Ways to Kill Your Mysql Application Performance15 Ways to Kill Your Mysql Application Performance
15 Ways to Kill Your Mysql Application Performance
 

More from Gianluca Sartori

SQL Server 2016 New Security Features
SQL Server 2016 New Security FeaturesSQL Server 2016 New Security Features
SQL Server 2016 New Security FeaturesGianluca Sartori
 
Responding to extended events in near real time
Responding to extended events in near real timeResponding to extended events in near real time
Responding to extended events in near real timeGianluca Sartori
 
Sql server security in an insecure world
Sql server security in an insecure worldSql server security in an insecure world
Sql server security in an insecure worldGianluca Sartori
 
TSQL Advanced Query Techniques
TSQL Advanced Query TechniquesTSQL Advanced Query Techniques
TSQL Advanced Query TechniquesGianluca Sartori
 
My Query is slow, now what?
My Query is slow, now what?My Query is slow, now what?
My Query is slow, now what?Gianluca Sartori
 
SQL Server Benchmarking, Baselining and Workload Analysis
SQL Server Benchmarking, Baselining and Workload AnalysisSQL Server Benchmarking, Baselining and Workload Analysis
SQL Server Benchmarking, Baselining and Workload AnalysisGianluca Sartori
 
A performance tuning methodology
A performance tuning methodologyA performance tuning methodology
A performance tuning methodologyGianluca Sartori
 
SQL Server Worst Practices
SQL Server Worst PracticesSQL Server Worst Practices
SQL Server Worst PracticesGianluca Sartori
 

More from Gianluca Sartori (9)

Benchmarking like a pro
Benchmarking like a proBenchmarking like a pro
Benchmarking like a pro
 
SQL Server 2016 New Security Features
SQL Server 2016 New Security FeaturesSQL Server 2016 New Security Features
SQL Server 2016 New Security Features
 
Responding to extended events in near real time
Responding to extended events in near real timeResponding to extended events in near real time
Responding to extended events in near real time
 
Sql server security in an insecure world
Sql server security in an insecure worldSql server security in an insecure world
Sql server security in an insecure world
 
TSQL Advanced Query Techniques
TSQL Advanced Query TechniquesTSQL Advanced Query Techniques
TSQL Advanced Query Techniques
 
My Query is slow, now what?
My Query is slow, now what?My Query is slow, now what?
My Query is slow, now what?
 
SQL Server Benchmarking, Baselining and Workload Analysis
SQL Server Benchmarking, Baselining and Workload AnalysisSQL Server Benchmarking, Baselining and Workload Analysis
SQL Server Benchmarking, Baselining and Workload Analysis
 
A performance tuning methodology
A performance tuning methodologyA performance tuning methodology
A performance tuning methodology
 
SQL Server Worst Practices
SQL Server Worst PracticesSQL Server Worst Practices
SQL Server Worst Practices
 

Recently uploaded

Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024The Digital Insurer
 
Tech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdfTech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdfhans926745
 
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
 
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
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking MenDelhi Call girls
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdflior mazor
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfEnterprise Knowledge
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdfhans926745
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEarley Information Science
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonAnna Loughnan Colquhoun
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsMaria Levchenko
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slidevu2urc
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘RTylerCroy
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProduct Anonymous
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityPrincipled Technologies
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsJoaquim Jorge
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationSafe Software
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 

Recently uploaded (20)

Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 
Tech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdfTech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdf
 
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
 
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
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 

Sql server infernals

  • 1. SQL Server Infernals A Beginner’s Guide to SQL Server Worst Practices Gianluca Sartori @spaghettidba
  • 2. Gianluca Sartori Independent SQL Server consultant SQL Server MVP, MCTS, MCITP, MCT Works with SQL Server since version 7 DBA @ Scuderia Ferrari Blog: spaghettidba.com Twitter: @spaghettidba
  • 3. Agenda  Best practices or Worst practices?  What can go wrong?  Design  Development  Installation  Administration
  • 4. Disclaimer:  Not everything is black or white  «It depends» is the most likely answer There are edge cases when some of these worst practices are the only possible solution, or not such a bad idea…
  • 5. Best Practices vs. Worst Practices  Why Best Practices are not enough  Too many  No time  Lack of experience  Not always clear what happens if we don’t follow them  Why Worst Practices help  They show the mistakes to avoid  We can learn from someone else’s mistakes
  • 6. Worst Practices Areas Design Development Installation Administration Schema design Naming Data Types Environment HW validation OS configuration SQL installation Recovery Security Capacity Performance Monitoring Code Test
  • 7. SQL Server Infernals  Worst Practices are sins that will put you in the SQL Server hell!!  I will guide you through the circles, as Virgil did with Dante
  • 9. Schema Design  Not normalizing the schema  1NF: A primary key, atomic attributes only  2NF: Every attribute depends on the whole key  3NF: Every attribute depends only on the key «The key, the whole key, nothing but the key, so help me Codd»
  • 10. Clues of denormalization  Repeating data  redundancies  Inconsistent data between tables anomalies  Data separated by «,»  Ex: john@gmail.com, john@business.com  Structured data in «notes» columns  Columns with a numeric suffix  Ex: Zone1, Zone2, Zone3 …
  • 12. Lookup Tables Orders PK order_id int order_date datetime FK2 customer_id int FK1 status_id char(2) FK3 priority_id tinyint Order_Status PK status_id char(2) status_description nvarchar(50) Customers PK customer_id int name varchar(100) address varchar(50) ZIP char(5) city nvarchar(50) FK2 state_id char(2) FK1 country_id char(3) Countries PK country_id char(3) description nvarchar(50) States PK state_id char(2) description nvarchar(50) Order_Priorities PK priority_id tinyint priority_description nvarchar(50) One lookup table for each attribute
  • 13. OTLT: One True Lookup Table Orders PK order_id int order_date datetime FK1 customer_id int status_id char(2) priority_id tinyint Customers PK customer_id int name nvarchar(100) address nvarchar(50) ZIP char(5) city nvarchar(50) state_id char(2) country_id char(3) LookupTable PK table_name sysname PK lookup_code nvarchar(500) lookup_description nvarchar(4000) CREATE TABLE LookupTable ( table_name sysname, lookup_code nvarchar(500), lookup_description nvarchar(4000) ) One lookup table for all attributes
  • 14. OTLT: One True Lookup Table  No Foreign Keys  Generic data types  nvarchar(SomeHighNumber) Implicit Conversions, Incorrect Data, Huge memory grants…  CHECK constraints may help to a point…  Locking CHECK( CASE WHEN lookup_code = 'states' AND lookup_code LIKE '[A-Z][A-Z]' THEN 1 WHEN lookup_code = 'priorities' AND lookup_code LIKE '[0-9]' THEN 1 WHEN lookup_code = 'countries' AND lookup_code LIKE '[0-9][0-9][0-9]' THEN 1 WHEN lookup_code = 'status' AND lookup_code LIKE '[A-Z][A-Z]' THEN 1 ELSE 0 END = 1 )
  • 15. EAV: Entity, Attribute, Value Customers PK customer_id int name nvarchar(100) address nvarchar(50) ZIP char(5) city nvarchar(50) state_id char(2) country_id char(3) AttributeNames PK attribute_id int PK,FK1 entity_id int attribute_name nvarchar(128) AttributeValues PK,FK1 attribute_id int PK,FK1 entity_id int PK,FK2,FK3 id int value nvarchar(4000) Entities PK entity_id int entity_name nvarchar(128) Orders PK order_id int order_date datetime customer_id int status_id char(2) priority_id tinyint
  • 16. EAV: Entity, Attribute, Value Disadvantages:  Generic data types  Ex: varchar(4000)  No Foreign Keys  No CHECK constraints  Multiple accesses to the same table  One access per attribute Advantages  Dynamic schema: no need to alter the database  Replication, distributed environments
  • 17. EAV: Entity, Attribute, Value  Reporting is insanely hard.  Writing to the EAV schema is a mess  Workaround:  Reads: PIVOT / Crosstab  Writes: View + INSTEAD OF triggers  Alternatives:  SPARSE columns  XML/JSON  Key-value store databases  Document-oriented databases
  • 20. Data type Worst Practices  Numeric data types for non-numeric data  Storing data as their human-readable representation  Using deprecated data types  Using larger data types “just in case”  Using variable length data types for fixed size data  Storing durations in date/datetime columns  Getting Unicode wrong  Using different data types for the same data in different tables
  • 22. Chaos Belongs to Hell  No Primary Key o surrogate keys only «identity» is not the only possible key!  No Foreign Keys They’re «awkward»  No CHECK constraint The application will guarantee consistency…  Wrong data types  Data type is the 1° constraint on the data  Use of NULL where not appropriate  Use of «dummy» data (ex: ‘.’ , 0)
  • 24. Damnation by Namification  Hungarian Notation (AKA «tibbing»)  Insanely short names  Insanely long names  Mixing languages  Using the «sp_» prefix  Using reserved words or illegal characters  Using system generated constraint names  No naming convention or multiple naming conventions Hungary is a nice str_country
  • 26. Pollutors will be prosecuted  Developing in production  Using the test environment for development  Using a shared database for development  No source control  Developing with sysadmin privileges  Developing on a different version/edition from production (less problematic after 2016 SP1)
  • 28. Pessimists are Optimists with Experience  Not testing all the code Use meaningful data volumes  Testing in production Can alter production data Interferes with production users  Testing in development environment Useful at most for unit tests
  • 30. Development Worst Practices  No transactions  No error handling @@ERROR is a thing of the past!  Wrong isolation levels NOLOCK = no consistency!  SELECT *  Dynamic SQL with concatenated params  Code vulnerable to SQL injection  No abstraction layer Views, Functions, Stored Procedures It’s all about laziness
  • 32. HW Worst Practices  Using inadequate or unbalanced HW  Reusing decommissioned servers for new installations  Slower CPUs (license costs the same on fast CPUs)  Less RAM supported  Planning storage with capacity in mind  Choosing the wrong RAID level
  • 33. CIRCLE 10: Next next finish installers
  • 34. Installation Worst Practices  Installing accepting all the defaults  Data files on the system drive  MAXDOP = 0  Max Server Memory = +∞  Installing unused components  Installing multiple services on the same machine  Giving up easy wins on I/O  Partition misalignment  Using the default allocation unit (4Kb)
  • 36. What does a database need?
  • 37. Backup and Recovery Worst Practices  No backup  With FULL recovery it’s a timebomb  Ignoring RPO and RTO (it’s not your decision!)  No test restores  No consistency checks  DBCC REPAIR_ALLOW_DATA_LOSS as default response to corruption Our responsibility is to perform restores, not backups!
  • 38. Security Worst Practices  Too many sysadmins  Everyone authenticating as ‘sa’  Using SQL Authentication  Weak passwords  123  P4$$w0rd  Same as username  No auditing on sensitive data
  • 39. Capacity Management Worst Practices  Not checking disk space  No space left = database halted!  FULL recovery and no log backups?  Relying 100% on autogrowth  Autoshrink  Autoclose  Not presizing tempdb Different file size = latching (and striping) penalty
  • 40. Maintenance Worst Practices  Not maintaining indexes and statistics  Obsessing over maintaining indexes and statistics  Using catch-all maintenance plans
  • 42. Performance Tuning Easier to implementMore effective
  • 43. Query Optimization Worst Practices RBAR: Row By Agonizing Row  Cursors  WHILE loops  App-side cursors  Scalar and multi-statement functions
  • 44. Query Optimization Worst Practices Views on views on views… Might look like a brilliant idea at first (code re-use FTW!)  You can end up losing control  Unneeded multiple accesses to the same tables  Unnecessary JOINs
  • 46. Query Optimization Worst Practices  One query to rule them all The optimizer is good, not perfect «divide et impera» delivers better performance  DISTINCT in all queries … because “who wants stinkin’ duplicates?”  Query HINTs all over the place Especially index hints
  • 47. Indexing Worst Practices  Accepting all suggestions from Tuning Advisor  Duplicate indexes  An index for each column  Indexes are not for free!  Suboptimal Clustered Index  Unique  Small  Unchanging  Ever increasing or decreasing NEWSEQUENTIALID() NEWID()
  • 48. Server Tuning Worst Practices  «Throwing HW» at the problem  A 2x faster machine might make RBAR code 2x faster  Using set-based code might make it 500x faster  Using «advanced» options without testing  NT Fibers (lightweight pooling)  Priority Boost
  • 49. Resources Detailed blog posts on spaghettidba.com One post for each circle: https://spaghettidba.com/category/sql-server/sql-server-infernals/
  • 50. Resources Free Tool: Best Practices Analyzer  Highlights configuration parameters that don’t comply with best practices  Highlights potential problems  Offers recommendations http://www.microsoft.com/en-us/download/details.aspx?id=15289
  • 51. SQL Server Infernals A Beginner’s Guide to SQL Server Worst Practices https://groupby.org/go/session5 Contact: spaghettidba@sqlconsulting.it More infernal stuff: https://spaghettidba.com/category/sql-server/sql-server-infernals/

Editor's Notes

  1. Lo scopo non è criticare, ma far capire errori che io per primo ho fatto nella mia carriera
  2. Agganciare la worst practice trascurabile sp_ con Worst Practice tremenda OTLT!!