SlideShare a Scribd company logo
Lunch + Learn
SQL Tips + Tricks for Developers
Victor Szoltysek
July / 2016
Tables + Indexes
Common Mistakes
Not closing resources
• always using finally block / try-resources
finally{
sql1.close();
sql2.close();
}
@Cleanup
Resource someClosableResource;
Missing Indexes
• Not important for small tables
• Huge potential performance improvements — x10000
• Watch out for incorrect index usage
• MSSQL Server will recommend indexes
• ORMs don’t create indexes!!!!
SELECT *
FROM someTable
WHERE someColumn
LIKE ‘%stringInMiddle%’
SQL Injection
SQL Injection - Mitigation
• Never EVER use concatenated SQL strings in your
prod applications
• Use Parametrized Statements
Not using Transactions when
Needed
• Transaction :
• Logical Atomic Unit of Work
• All or nothing (even if database gets interrupted)
• Solution for A/B
• Spring @Transactional Annotation
Null misunderstandings!
• NULL == NULL <— not true
• NULL != NULL <— also not true
• NULL != “something” <— ALSO not true
• Empty Strings become Null in Oracle!!
N+1 AntiPattern
• Minimize SQL Traffic!! , Let SQL perform joins,
Network is slow
• Use SQL Count instead of getting all rows and
counting at backend
-- Get all Mobile Vendors
SELECT * FROM MobileVendor;
-- For each MobileVendor, get PhoneModel details
SELECT * FROM PhoneModel WHERE MobileVendor.vendorId=?
Stale Statistics
• Database keeps track of statistics about table data
to help to optimize how it executes queries
• Significant changes to data, can make these
statistics temporarily invalid, and can lead to poor
execution
• Only important with large DB changes, i.e. full
refresh
Tools
Spring Data - Auto
Generated Repositories
Intellij Ultimate SQL Support
VirtualBox + Windows + SQL Server
Express + Mangement Studio
Database Versioning
DBDeploy
Different SQL Databases
SQL Server Oracle Postgres MySQL Firebird H2
Single Cross
Platform
Deployable
No No No No No Yes
Embedded
Support
No No No No Yes Yes
InMemory
Support
No No No Yes No Yes
Free No No Yes Yes Yes Yes
Cross Platform No Yes Yes Yes Yes Yes
InMemory / Embedded /
Java Databases
String connectionString =
"jdbc:h2:mem:inMemoryDb;DB_CLOSE_DELAY=-1"
Awesome for Testing!!!
Most commonly used SQL
database?
Explain Plans
Groovy SQL Support
ORMs
• Great for Greenfield / CRUD apps
• Easy to go from Object Model to SQL Model
• Can get difficult to create Object Model from
existing SQL Model
• “ORMS are the Vietnam of Computer Science”
NoSQL vs SQL vs NewSQL
• NoSQL did not replace SQL, but is an alternative
• Relational / Structured Data is still create for SQL
DB’s
• NewSQL!! Modern SQL alternatives
Log4J JDBC Appender
SQL Metadata
SELECT 

    t.NAME AS TableName,

    p.rows AS RowCounts,

    (SUM(a.total_pages) * 8 / 1064) AS TotalSpaceMB

FROM 

    sys.tables t

INNER JOIN 

    sys.schemas s ON s.schema_id = t.schema_id

INNER JOIN      

    sys.indexes i ON t.OBJECT_ID = i.object_id

INNER JOIN 

    sys.partitions p ON i.object_id = p.OBJECT_ID AND i.index_id = p.index_id

INNER JOIN 

    sys.allocation_units a ON p.partition_id = a.container_id

WHERE 

    t.NAME NOT LIKE 'dt%'    -- filter out system tables for diagramming

    AND t.is_ms_shipped = 0

    AND i.OBJECT_ID > 255 

GROUP BY 

    t.Name, s.Name, p.Rows

ORDER BY 

    p.Rows desc
Table Analysis
Table Row Count Size MB
agreement_customer 1039293 103
pricing_results 378520 189
sales_history 378520 40
price_rule 256814 26
base_value 252687 19
customer 32754 3
product 19525 1
product_price 19525 5
agreement_product 15105 1
price_rule_flag 7656 0
price_rule_header 2552 0
min_max_rule 292 0
DATABASECHANGEL
OG
61 0
sub_price_rule 29 0

More Related Content

What's hot

Azure SQL & SQL Server 2016 JSON
Azure SQL & SQL Server 2016 JSONAzure SQL & SQL Server 2016 JSON
Azure SQL & SQL Server 2016 JSON
Davide Mauri
 
U-SQL Intro (SQLBits 2016)
U-SQL Intro (SQLBits 2016)U-SQL Intro (SQLBits 2016)
U-SQL Intro (SQLBits 2016)
Michael Rys
 
Rails best practices
Rails best practicesRails best practices
Rails best practices
Achintya Kumar
 
An introduction to mysql Performance Optimization (2008)
An introduction to mysql Performance Optimization (2008)An introduction to mysql Performance Optimization (2008)
An introduction to mysql Performance Optimization (2008)
lmrei
 
SQL Server Worst Practices - EN
SQL Server Worst Practices - ENSQL Server Worst Practices - EN
SQL Server Worst Practices - EN
Gianluca Sartori
 
SQL vs NoSQL
SQL vs NoSQLSQL vs NoSQL
SQL vs NoSQL
Jacinto Limjap
 
Effect systems in scala: beyond flatmap
Effect systems in scala: beyond flatmapEffect systems in scala: beyond flatmap
Effect systems in scala: beyond flatmap
Joost de Vries
 
JavaScript Fundamentals
JavaScript FundamentalsJavaScript Fundamentals
JavaScript Fundamentals
Lasantha Bandara
 
Optimizing Java Performance
Optimizing Java PerformanceOptimizing Java Performance
Optimizing Java Performance
Konstantin Pavlov
 
Data mining tools for excel and sql server
Data mining tools for excel and sql serverData mining tools for excel and sql server
Data mining tools for excel and sql server
Sayed Ahmed
 
Data Abstraction for Large Web Applications
Data Abstraction for Large Web ApplicationsData Abstraction for Large Web Applications
Data Abstraction for Large Web Applicationsbrandonsavage
 
Building 1 million predictions per second using SQL-R
Building 1 million predictions per second using SQL-RBuilding 1 million predictions per second using SQL-R
Building 1 million predictions per second using SQL-R
Amit Banerjee
 
Efficient working with Databases in LabVIEW - Sam Sharp (MediaMongrels Ltd) -...
Efficient working with Databases in LabVIEW - Sam Sharp (MediaMongrels Ltd) -...Efficient working with Databases in LabVIEW - Sam Sharp (MediaMongrels Ltd) -...
Efficient working with Databases in LabVIEW - Sam Sharp (MediaMongrels Ltd) -...
MediaMongrels Ltd
 
Doctrine Data migrations | May 2017
Doctrine Data migrations | May 2017Doctrine Data migrations | May 2017
Doctrine Data migrations | May 2017
Petr Bechyně
 
U-SQL Learning Resources (SQLBits 2016)
U-SQL Learning Resources (SQLBits 2016)U-SQL Learning Resources (SQLBits 2016)
U-SQL Learning Resources (SQLBits 2016)
Michael Rys
 
Unit testing hippo
Unit testing hippoUnit testing hippo
Unit testing hippo
Ebrahim Aharpour
 
Concurrency control
Concurrency controlConcurrency control
Concurrency control
Syed Ashraf Ullah
 
WebObjects Optimization
WebObjects OptimizationWebObjects Optimization
WebObjects OptimizationWO Community
 
Composable Futures with Akka 2.0
Composable Futures with Akka 2.0Composable Futures with Akka 2.0
Composable Futures with Akka 2.0
Mike Slinn
 

What's hot (19)

Azure SQL & SQL Server 2016 JSON
Azure SQL & SQL Server 2016 JSONAzure SQL & SQL Server 2016 JSON
Azure SQL & SQL Server 2016 JSON
 
U-SQL Intro (SQLBits 2016)
U-SQL Intro (SQLBits 2016)U-SQL Intro (SQLBits 2016)
U-SQL Intro (SQLBits 2016)
 
Rails best practices
Rails best practicesRails best practices
Rails best practices
 
An introduction to mysql Performance Optimization (2008)
An introduction to mysql Performance Optimization (2008)An introduction to mysql Performance Optimization (2008)
An introduction to mysql Performance Optimization (2008)
 
SQL Server Worst Practices - EN
SQL Server Worst Practices - ENSQL Server Worst Practices - EN
SQL Server Worst Practices - EN
 
SQL vs NoSQL
SQL vs NoSQLSQL vs NoSQL
SQL vs NoSQL
 
Effect systems in scala: beyond flatmap
Effect systems in scala: beyond flatmapEffect systems in scala: beyond flatmap
Effect systems in scala: beyond flatmap
 
JavaScript Fundamentals
JavaScript FundamentalsJavaScript Fundamentals
JavaScript Fundamentals
 
Optimizing Java Performance
Optimizing Java PerformanceOptimizing Java Performance
Optimizing Java Performance
 
Data mining tools for excel and sql server
Data mining tools for excel and sql serverData mining tools for excel and sql server
Data mining tools for excel and sql server
 
Data Abstraction for Large Web Applications
Data Abstraction for Large Web ApplicationsData Abstraction for Large Web Applications
Data Abstraction for Large Web Applications
 
Building 1 million predictions per second using SQL-R
Building 1 million predictions per second using SQL-RBuilding 1 million predictions per second using SQL-R
Building 1 million predictions per second using SQL-R
 
Efficient working with Databases in LabVIEW - Sam Sharp (MediaMongrels Ltd) -...
Efficient working with Databases in LabVIEW - Sam Sharp (MediaMongrels Ltd) -...Efficient working with Databases in LabVIEW - Sam Sharp (MediaMongrels Ltd) -...
Efficient working with Databases in LabVIEW - Sam Sharp (MediaMongrels Ltd) -...
 
Doctrine Data migrations | May 2017
Doctrine Data migrations | May 2017Doctrine Data migrations | May 2017
Doctrine Data migrations | May 2017
 
U-SQL Learning Resources (SQLBits 2016)
U-SQL Learning Resources (SQLBits 2016)U-SQL Learning Resources (SQLBits 2016)
U-SQL Learning Resources (SQLBits 2016)
 
Unit testing hippo
Unit testing hippoUnit testing hippo
Unit testing hippo
 
Concurrency control
Concurrency controlConcurrency control
Concurrency control
 
WebObjects Optimization
WebObjects OptimizationWebObjects Optimization
WebObjects Optimization
 
Composable Futures with Akka 2.0
Composable Futures with Akka 2.0Composable Futures with Akka 2.0
Composable Futures with Akka 2.0
 

Similar to SQL Tips + Tricks for Developers

Building scalable application with sql server
Building scalable application with sql serverBuilding scalable application with sql server
Building scalable application with sql serverChris Adkin
 
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
Kevin Kline
 
Querying_with_T-SQL_-_01.pptx
Querying_with_T-SQL_-_01.pptxQuerying_with_T-SQL_-_01.pptx
Querying_with_T-SQL_-_01.pptx
QuyVo27
 
Enterprise NoSQL: Silver Bullet or Poison Pill
Enterprise NoSQL: Silver Bullet or Poison PillEnterprise NoSQL: Silver Bullet or Poison Pill
Enterprise NoSQL: Silver Bullet or Poison Pill
Billy Newport
 
White Paper for OMG! Identifying and Refactoring Common SQL...
White Paper for OMG! Identifying and Refactoring Common SQL...White Paper for OMG! Identifying and Refactoring Common SQL...
White Paper for OMG! Identifying and Refactoring Common SQL...
Jeff Jacobs
 
Schema less table & dynamic schema
Schema less table & dynamic schemaSchema less table & dynamic schema
Schema less table & dynamic schema
Davide Mauri
 
Oracle SQL Tuning for Day-to-Day Data Warehouse Support
Oracle SQL Tuning for Day-to-Day Data Warehouse SupportOracle SQL Tuning for Day-to-Day Data Warehouse Support
Oracle SQL Tuning for Day-to-Day Data Warehouse Support
nkarag
 
Dapper: the microORM that will change your life
Dapper: the microORM that will change your lifeDapper: the microORM that will change your life
Dapper: the microORM that will change your life
Davide Mauri
 
Conquering "big data": An introduction to shard query
Conquering "big data": An introduction to shard queryConquering "big data": An introduction to shard query
Conquering "big data": An introduction to shard query
Justin Swanhart
 
Solid And Sustainable Development in Scala
Solid And Sustainable Development in ScalaSolid And Sustainable Development in Scala
Solid And Sustainable Development in Scala
Kazuhiro Sera
 
Geek Sync | Field Medic’s Guide to Database Mirroring
Geek Sync | Field Medic’s Guide to Database MirroringGeek Sync | Field Medic’s Guide to Database Mirroring
Geek Sync | Field Medic’s Guide to Database Mirroring
IDERA Software
 
A. Sarkissian Death of Relational Databases
A. Sarkissian Death of Relational DatabasesA. Sarkissian Death of Relational Databases
A. Sarkissian Death of Relational DatabasesMediabistro
 
PHP - Introduction to Advanced SQL
PHP - Introduction to Advanced SQLPHP - Introduction to Advanced SQL
PHP - Introduction to Advanced SQL
Vibrant Technologies & Computers
 
Active Record PowerPoint
Active Record PowerPointActive Record PowerPoint
Active Record PowerPointElizabeth Cruz
 
Breaking data
Breaking dataBreaking data
Breaking data
Terry Bunio
 
Building better SQL Server Databases
Building better SQL Server DatabasesBuilding better SQL Server Databases
Building better SQL Server Databases
ColdFusionConference
 
Solid and Sustainable Development in Scala
Solid and Sustainable Development in ScalaSolid and Sustainable Development in Scala
Solid and Sustainable Development in Scala
scalaconfjp
 
Golden Hammer - Shawn Oden
Golden Hammer - Shawn OdenGolden Hammer - Shawn Oden
Golden Hammer - Shawn Oden
Ortus Solutions, Corp
 
Stumbling stones when migrating from Oracle
 Stumbling stones when migrating from Oracle Stumbling stones when migrating from Oracle
Stumbling stones when migrating from Oracle
EDB
 

Similar to SQL Tips + Tricks for Developers (20)

Building scalable application with sql server
Building scalable application with sql serverBuilding scalable application with sql server
Building scalable application with sql server
 
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
 
Querying_with_T-SQL_-_01.pptx
Querying_with_T-SQL_-_01.pptxQuerying_with_T-SQL_-_01.pptx
Querying_with_T-SQL_-_01.pptx
 
Revision
RevisionRevision
Revision
 
Enterprise NoSQL: Silver Bullet or Poison Pill
Enterprise NoSQL: Silver Bullet or Poison PillEnterprise NoSQL: Silver Bullet or Poison Pill
Enterprise NoSQL: Silver Bullet or Poison Pill
 
White Paper for OMG! Identifying and Refactoring Common SQL...
White Paper for OMG! Identifying and Refactoring Common SQL...White Paper for OMG! Identifying and Refactoring Common SQL...
White Paper for OMG! Identifying and Refactoring Common SQL...
 
Schema less table & dynamic schema
Schema less table & dynamic schemaSchema less table & dynamic schema
Schema less table & dynamic schema
 
Oracle SQL Tuning for Day-to-Day Data Warehouse Support
Oracle SQL Tuning for Day-to-Day Data Warehouse SupportOracle SQL Tuning for Day-to-Day Data Warehouse Support
Oracle SQL Tuning for Day-to-Day Data Warehouse Support
 
Dapper: the microORM that will change your life
Dapper: the microORM that will change your lifeDapper: the microORM that will change your life
Dapper: the microORM that will change your life
 
Conquering "big data": An introduction to shard query
Conquering "big data": An introduction to shard queryConquering "big data": An introduction to shard query
Conquering "big data": An introduction to shard query
 
Solid And Sustainable Development in Scala
Solid And Sustainable Development in ScalaSolid And Sustainable Development in Scala
Solid And Sustainable Development in Scala
 
Geek Sync | Field Medic’s Guide to Database Mirroring
Geek Sync | Field Medic’s Guide to Database MirroringGeek Sync | Field Medic’s Guide to Database Mirroring
Geek Sync | Field Medic’s Guide to Database Mirroring
 
A. Sarkissian Death of Relational Databases
A. Sarkissian Death of Relational DatabasesA. Sarkissian Death of Relational Databases
A. Sarkissian Death of Relational Databases
 
PHP - Introduction to Advanced SQL
PHP - Introduction to Advanced SQLPHP - Introduction to Advanced SQL
PHP - Introduction to Advanced SQL
 
Active Record PowerPoint
Active Record PowerPointActive Record PowerPoint
Active Record PowerPoint
 
Breaking data
Breaking dataBreaking data
Breaking data
 
Building better SQL Server Databases
Building better SQL Server DatabasesBuilding better SQL Server Databases
Building better SQL Server Databases
 
Solid and Sustainable Development in Scala
Solid and Sustainable Development in ScalaSolid and Sustainable Development in Scala
Solid and Sustainable Development in Scala
 
Golden Hammer - Shawn Oden
Golden Hammer - Shawn OdenGolden Hammer - Shawn Oden
Golden Hammer - Shawn Oden
 
Stumbling stones when migrating from Oracle
 Stumbling stones when migrating from Oracle Stumbling stones when migrating from Oracle
Stumbling stones when migrating from Oracle
 

More from VictorSzoltysek

ChatGPT and Beyond - Elevating DevOps Productivity
ChatGPT and Beyond - Elevating DevOps ProductivityChatGPT and Beyond - Elevating DevOps Productivity
ChatGPT and Beyond - Elevating DevOps Productivity
VictorSzoltysek
 
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM TechniquesAI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
VictorSzoltysek
 
Harnessing ChatGPT - Elevating Productivity in Today's Agile Environment
Harnessing ChatGPT  - Elevating Productivity in Today's Agile EnvironmentHarnessing ChatGPT  - Elevating Productivity in Today's Agile Environment
Harnessing ChatGPT - Elevating Productivity in Today's Agile Environment
VictorSzoltysek
 
Simplified DevOps Bliss -with OpenAI API
Simplified DevOps Bliss -with OpenAI APISimplified DevOps Bliss -with OpenAI API
Simplified DevOps Bliss -with OpenAI API
VictorSzoltysek
 
From SpaceX Launch Pads to Rapid Deployments
From SpaceX Launch Pads to Rapid DeploymentsFrom SpaceX Launch Pads to Rapid Deployments
From SpaceX Launch Pads to Rapid Deployments
VictorSzoltysek
 
The Future of JVM Languages
The Future of JVM Languages The Future of JVM Languages
The Future of JVM Languages
VictorSzoltysek
 
Driving Process Improvements - A Guided Approach to Running Effective Retrosp...
Driving Process Improvements - A Guided Approach to Running Effective Retrosp...Driving Process Improvements - A Guided Approach to Running Effective Retrosp...
Driving Process Improvements - A Guided Approach to Running Effective Retrosp...
VictorSzoltysek
 
Spaceships, Pull Requests and Feature Branching - A Principles-Based approac...
Spaceships, Pull Requests and Feature Branching  - A Principles-Based approac...Spaceships, Pull Requests and Feature Branching  - A Principles-Based approac...
Spaceships, Pull Requests and Feature Branching - A Principles-Based approac...
VictorSzoltysek
 
Real-World DevOps — 20 Practical Developers Tips for Tightening Your Operatio...
Real-World DevOps — 20 Practical Developers Tips for Tightening Your Operatio...Real-World DevOps — 20 Practical Developers Tips for Tightening Your Operatio...
Real-World DevOps — 20 Practical Developers Tips for Tightening Your Operatio...
VictorSzoltysek
 
Real-World Application Observability - 11 Practical Developer Focused Tips
Real-World Application Observability - 11 Practical Developer Focused TipsReal-World Application Observability - 11 Practical Developer Focused Tips
Real-World Application Observability - 11 Practical Developer Focused Tips
VictorSzoltysek
 
Victor's Awesome Retro Deck
Victor's Awesome Retro DeckVictor's Awesome Retro Deck
Victor's Awesome Retro Deck
VictorSzoltysek
 
Software Development in Internet Memes
Software Development in Internet MemesSoftware Development in Internet Memes
Software Development in Internet Memes
VictorSzoltysek
 
Big Bangs, Monorails and Microservices - Feb 2020
Big Bangs, Monorails and Microservices - Feb 2020Big Bangs, Monorails and Microservices - Feb 2020
Big Bangs, Monorails and Microservices - Feb 2020
VictorSzoltysek
 
Less is more the 7 wastes of lean software development
Less is more   the 7 wastes of lean software developmentLess is more   the 7 wastes of lean software development
Less is more the 7 wastes of lean software development
VictorSzoltysek
 
Modern day jvm controversies
Modern day jvm controversiesModern day jvm controversies
Modern day jvm controversies
VictorSzoltysek
 
The Future of Java - and a look at the evolution of programming languages
The Future of Java - and a look at the evolution of programming languagesThe Future of Java - and a look at the evolution of programming languages
The Future of Java - and a look at the evolution of programming languages
VictorSzoltysek
 
Client Technical Analysis of Legacy Software and Future Replacement
Client Technical Analysis of Legacy Software and Future ReplacementClient Technical Analysis of Legacy Software and Future Replacement
Client Technical Analysis of Legacy Software and Future Replacement
VictorSzoltysek
 
Improving velocity through abstraction
Improving velocity through abstractionImproving velocity through abstraction
Improving velocity through abstraction
VictorSzoltysek
 

More from VictorSzoltysek (18)

ChatGPT and Beyond - Elevating DevOps Productivity
ChatGPT and Beyond - Elevating DevOps ProductivityChatGPT and Beyond - Elevating DevOps Productivity
ChatGPT and Beyond - Elevating DevOps Productivity
 
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM TechniquesAI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
 
Harnessing ChatGPT - Elevating Productivity in Today's Agile Environment
Harnessing ChatGPT  - Elevating Productivity in Today's Agile EnvironmentHarnessing ChatGPT  - Elevating Productivity in Today's Agile Environment
Harnessing ChatGPT - Elevating Productivity in Today's Agile Environment
 
Simplified DevOps Bliss -with OpenAI API
Simplified DevOps Bliss -with OpenAI APISimplified DevOps Bliss -with OpenAI API
Simplified DevOps Bliss -with OpenAI API
 
From SpaceX Launch Pads to Rapid Deployments
From SpaceX Launch Pads to Rapid DeploymentsFrom SpaceX Launch Pads to Rapid Deployments
From SpaceX Launch Pads to Rapid Deployments
 
The Future of JVM Languages
The Future of JVM Languages The Future of JVM Languages
The Future of JVM Languages
 
Driving Process Improvements - A Guided Approach to Running Effective Retrosp...
Driving Process Improvements - A Guided Approach to Running Effective Retrosp...Driving Process Improvements - A Guided Approach to Running Effective Retrosp...
Driving Process Improvements - A Guided Approach to Running Effective Retrosp...
 
Spaceships, Pull Requests and Feature Branching - A Principles-Based approac...
Spaceships, Pull Requests and Feature Branching  - A Principles-Based approac...Spaceships, Pull Requests and Feature Branching  - A Principles-Based approac...
Spaceships, Pull Requests and Feature Branching - A Principles-Based approac...
 
Real-World DevOps — 20 Practical Developers Tips for Tightening Your Operatio...
Real-World DevOps — 20 Practical Developers Tips for Tightening Your Operatio...Real-World DevOps — 20 Practical Developers Tips for Tightening Your Operatio...
Real-World DevOps — 20 Practical Developers Tips for Tightening Your Operatio...
 
Real-World Application Observability - 11 Practical Developer Focused Tips
Real-World Application Observability - 11 Practical Developer Focused TipsReal-World Application Observability - 11 Practical Developer Focused Tips
Real-World Application Observability - 11 Practical Developer Focused Tips
 
Victor's Awesome Retro Deck
Victor's Awesome Retro DeckVictor's Awesome Retro Deck
Victor's Awesome Retro Deck
 
Software Development in Internet Memes
Software Development in Internet MemesSoftware Development in Internet Memes
Software Development in Internet Memes
 
Big Bangs, Monorails and Microservices - Feb 2020
Big Bangs, Monorails and Microservices - Feb 2020Big Bangs, Monorails and Microservices - Feb 2020
Big Bangs, Monorails and Microservices - Feb 2020
 
Less is more the 7 wastes of lean software development
Less is more   the 7 wastes of lean software developmentLess is more   the 7 wastes of lean software development
Less is more the 7 wastes of lean software development
 
Modern day jvm controversies
Modern day jvm controversiesModern day jvm controversies
Modern day jvm controversies
 
The Future of Java - and a look at the evolution of programming languages
The Future of Java - and a look at the evolution of programming languagesThe Future of Java - and a look at the evolution of programming languages
The Future of Java - and a look at the evolution of programming languages
 
Client Technical Analysis of Legacy Software and Future Replacement
Client Technical Analysis of Legacy Software and Future ReplacementClient Technical Analysis of Legacy Software and Future Replacement
Client Technical Analysis of Legacy Software and Future Replacement
 
Improving velocity through abstraction
Improving velocity through abstractionImproving velocity through abstraction
Improving velocity through abstraction
 

Recently uploaded

20240605 QFM017 Machine Intelligence Reading List May 2024
20240605 QFM017 Machine Intelligence Reading List May 202420240605 QFM017 Machine Intelligence Reading List May 2024
20240605 QFM017 Machine Intelligence Reading List May 2024
Matthew Sinclair
 
Alt. GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using ...
Alt. GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using ...Alt. GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using ...
Alt. GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using ...
James Anderson
 
Pushing the limits of ePRTC: 100ns holdover for 100 days
Pushing the limits of ePRTC: 100ns holdover for 100 daysPushing the limits of ePRTC: 100ns holdover for 100 days
Pushing the limits of ePRTC: 100ns holdover for 100 days
Adtran
 
Building RAG with self-deployed Milvus vector database and Snowpark Container...
Building RAG with self-deployed Milvus vector database and Snowpark Container...Building RAG with self-deployed Milvus vector database and Snowpark Container...
Building RAG with self-deployed Milvus vector database and Snowpark Container...
Zilliz
 
Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !
KatiaHIMEUR1
 
20240607 QFM018 Elixir Reading List May 2024
20240607 QFM018 Elixir Reading List May 202420240607 QFM018 Elixir Reading List May 2024
20240607 QFM018 Elixir Reading List May 2024
Matthew Sinclair
 
Uni Systems Copilot event_05062024_C.Vlachos.pdf
Uni Systems Copilot event_05062024_C.Vlachos.pdfUni Systems Copilot event_05062024_C.Vlachos.pdf
Uni Systems Copilot event_05062024_C.Vlachos.pdf
Uni Systems S.M.S.A.
 
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
Neo4j
 
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
SOFTTECHHUB
 
RESUME BUILDER APPLICATION Project for students
RESUME BUILDER APPLICATION Project for studentsRESUME BUILDER APPLICATION Project for students
RESUME BUILDER APPLICATION Project for students
KAMESHS29
 
Unlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdf
Unlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdfUnlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdf
Unlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdf
Malak Abu Hammad
 
Enchancing adoption of Open Source Libraries. A case study on Albumentations.AI
Enchancing adoption of Open Source Libraries. A case study on Albumentations.AIEnchancing adoption of Open Source Libraries. A case study on Albumentations.AI
Enchancing adoption of Open Source Libraries. A case study on Albumentations.AI
Vladimir Iglovikov, Ph.D.
 
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
James Anderson
 
Climate Impact of Software Testing at Nordic Testing Days
Climate Impact of Software Testing at Nordic Testing DaysClimate Impact of Software Testing at Nordic Testing Days
Climate Impact of Software Testing at Nordic Testing Days
Kari Kakkonen
 
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Albert Hoitingh
 
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
名前 です男
 
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
DanBrown980551
 
Video Streaming: Then, Now, and in the Future
Video Streaming: Then, Now, and in the FutureVideo Streaming: Then, Now, and in the Future
Video Streaming: Then, Now, and in the Future
Alpen-Adria-Universität
 
Monitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR EventsMonitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR Events
Ana-Maria Mihalceanu
 
Artificial Intelligence for XMLDevelopment
Artificial Intelligence for XMLDevelopmentArtificial Intelligence for XMLDevelopment
Artificial Intelligence for XMLDevelopment
Octavian Nadolu
 

Recently uploaded (20)

20240605 QFM017 Machine Intelligence Reading List May 2024
20240605 QFM017 Machine Intelligence Reading List May 202420240605 QFM017 Machine Intelligence Reading List May 2024
20240605 QFM017 Machine Intelligence Reading List May 2024
 
Alt. GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using ...
Alt. GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using ...Alt. GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using ...
Alt. GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using ...
 
Pushing the limits of ePRTC: 100ns holdover for 100 days
Pushing the limits of ePRTC: 100ns holdover for 100 daysPushing the limits of ePRTC: 100ns holdover for 100 days
Pushing the limits of ePRTC: 100ns holdover for 100 days
 
Building RAG with self-deployed Milvus vector database and Snowpark Container...
Building RAG with self-deployed Milvus vector database and Snowpark Container...Building RAG with self-deployed Milvus vector database and Snowpark Container...
Building RAG with self-deployed Milvus vector database and Snowpark Container...
 
Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !
 
20240607 QFM018 Elixir Reading List May 2024
20240607 QFM018 Elixir Reading List May 202420240607 QFM018 Elixir Reading List May 2024
20240607 QFM018 Elixir Reading List May 2024
 
Uni Systems Copilot event_05062024_C.Vlachos.pdf
Uni Systems Copilot event_05062024_C.Vlachos.pdfUni Systems Copilot event_05062024_C.Vlachos.pdf
Uni Systems Copilot event_05062024_C.Vlachos.pdf
 
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
 
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
 
RESUME BUILDER APPLICATION Project for students
RESUME BUILDER APPLICATION Project for studentsRESUME BUILDER APPLICATION Project for students
RESUME BUILDER APPLICATION Project for students
 
Unlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdf
Unlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdfUnlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdf
Unlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdf
 
Enchancing adoption of Open Source Libraries. A case study on Albumentations.AI
Enchancing adoption of Open Source Libraries. A case study on Albumentations.AIEnchancing adoption of Open Source Libraries. A case study on Albumentations.AI
Enchancing adoption of Open Source Libraries. A case study on Albumentations.AI
 
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
 
Climate Impact of Software Testing at Nordic Testing Days
Climate Impact of Software Testing at Nordic Testing DaysClimate Impact of Software Testing at Nordic Testing Days
Climate Impact of Software Testing at Nordic Testing Days
 
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
 
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
 
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
 
Video Streaming: Then, Now, and in the Future
Video Streaming: Then, Now, and in the FutureVideo Streaming: Then, Now, and in the Future
Video Streaming: Then, Now, and in the Future
 
Monitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR EventsMonitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR Events
 
Artificial Intelligence for XMLDevelopment
Artificial Intelligence for XMLDevelopmentArtificial Intelligence for XMLDevelopment
Artificial Intelligence for XMLDevelopment
 

SQL Tips + Tricks for Developers

  • 1. Lunch + Learn SQL Tips + Tricks for Developers Victor Szoltysek July / 2016
  • 4. Not closing resources • always using finally block / try-resources finally{ sql1.close(); sql2.close(); } @Cleanup Resource someClosableResource;
  • 5. Missing Indexes • Not important for small tables • Huge potential performance improvements — x10000 • Watch out for incorrect index usage • MSSQL Server will recommend indexes • ORMs don’t create indexes!!!! SELECT * FROM someTable WHERE someColumn LIKE ‘%stringInMiddle%’
  • 6.
  • 8. SQL Injection - Mitigation • Never EVER use concatenated SQL strings in your prod applications • Use Parametrized Statements
  • 9. Not using Transactions when Needed • Transaction : • Logical Atomic Unit of Work • All or nothing (even if database gets interrupted) • Solution for A/B • Spring @Transactional Annotation
  • 10. Null misunderstandings! • NULL == NULL <— not true • NULL != NULL <— also not true • NULL != “something” <— ALSO not true • Empty Strings become Null in Oracle!!
  • 11. N+1 AntiPattern • Minimize SQL Traffic!! , Let SQL perform joins, Network is slow • Use SQL Count instead of getting all rows and counting at backend -- Get all Mobile Vendors SELECT * FROM MobileVendor; -- For each MobileVendor, get PhoneModel details SELECT * FROM PhoneModel WHERE MobileVendor.vendorId=?
  • 12. Stale Statistics • Database keeps track of statistics about table data to help to optimize how it executes queries • Significant changes to data, can make these statistics temporarily invalid, and can lead to poor execution • Only important with large DB changes, i.e. full refresh
  • 13. Tools
  • 14. Spring Data - Auto Generated Repositories
  • 16. VirtualBox + Windows + SQL Server Express + Mangement Studio
  • 18. Different SQL Databases SQL Server Oracle Postgres MySQL Firebird H2 Single Cross Platform Deployable No No No No No Yes Embedded Support No No No No Yes Yes InMemory Support No No No Yes No Yes Free No No Yes Yes Yes Yes Cross Platform No Yes Yes Yes Yes Yes
  • 19. InMemory / Embedded / Java Databases String connectionString = "jdbc:h2:mem:inMemoryDb;DB_CLOSE_DELAY=-1" Awesome for Testing!!!
  • 20. Most commonly used SQL database?
  • 23.
  • 24. ORMs • Great for Greenfield / CRUD apps • Easy to go from Object Model to SQL Model • Can get difficult to create Object Model from existing SQL Model • “ORMS are the Vietnam of Computer Science”
  • 25. NoSQL vs SQL vs NewSQL • NoSQL did not replace SQL, but is an alternative • Relational / Structured Data is still create for SQL DB’s • NewSQL!! Modern SQL alternatives
  • 27. SQL Metadata SELECT      t.NAME AS TableName,     p.rows AS RowCounts,     (SUM(a.total_pages) * 8 / 1064) AS TotalSpaceMB FROM      sys.tables t INNER JOIN      sys.schemas s ON s.schema_id = t.schema_id INNER JOIN           sys.indexes i ON t.OBJECT_ID = i.object_id INNER JOIN      sys.partitions p ON i.object_id = p.OBJECT_ID AND i.index_id = p.index_id INNER JOIN      sys.allocation_units a ON p.partition_id = a.container_id WHERE      t.NAME NOT LIKE 'dt%'    -- filter out system tables for diagramming     AND t.is_ms_shipped = 0     AND i.OBJECT_ID > 255  GROUP BY      t.Name, s.Name, p.Rows ORDER BY      p.Rows desc
  • 28. Table Analysis Table Row Count Size MB agreement_customer 1039293 103 pricing_results 378520 189 sales_history 378520 40 price_rule 256814 26 base_value 252687 19 customer 32754 3 product 19525 1 product_price 19525 5 agreement_product 15105 1 price_rule_flag 7656 0 price_rule_header 2552 0 min_max_rule 292 0 DATABASECHANGEL OG 61 0 sub_price_rule 29 0