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

SQL Tips + Tricks for Developers

  • 1.
    Lunch + Learn SQLTips + Tricks for Developers Victor Szoltysek July / 2016
  • 2.
  • 3.
  • 4.
    Not closing resources •always using finally block / try-resources finally{ sql1.close(); sql2.close(); } @Cleanup Resource someClosableResource;
  • 5.
    Missing Indexes • Notimportant 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%’
  • 7.
  • 8.
    SQL Injection -Mitigation • Never EVER use concatenated SQL strings in your prod applications • Use Parametrized Statements
  • 9.
    Not using Transactionswhen 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 • MinimizeSQL 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 • Databasekeeps 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.
  • 14.
    Spring Data -Auto Generated Repositories
  • 15.
  • 16.
    VirtualBox + Windows+ SQL Server Express + Mangement Studio
  • 17.
  • 18.
    Different SQL Databases SQLServer 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 usedSQL database?
  • 21.
  • 22.
  • 24.
    ORMs • Great forGreenfield / 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 SQLvs NewSQL • NoSQL did not replace SQL, but is an alternative • Relational / Structured Data is still create for SQL DB’s • NewSQL!! Modern SQL alternatives
  • 26.
  • 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 RowCount 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