DB Optimization
The framework:
- Optimize server setting
- Archiving/purging (for huge database)
- Optimize connection (connection pool)
- Optimize query
- Optimize application
- Optimize data structure
Database Problem Patterns:
Excessive SQLs
: Executing a lot (> 500) different SQL StatementsN+1 Query Problem
: Executing the same SQL statement multiple times (>20):Slow Single SQL Issue
: Executing a single SQL that contributes > 80% of response timeData-Driven
Issue: Same request executes different SQL depending on input parametersDatabase Heavy
: Database Contribution Time is > 60% of overall response timeUnprepared Statements
: Executing the same SQL without preparing the statementPool Exhaustion
: Impacted by High Connection Acquisition Time (getConnection time > executeStatement)Inefficient Pool Access
: Excessive access to connection pool (calling getConnection > 50% of executeStatement count)Overloaded Database Server
: Database server is simply overloaded with too many requests from different apps
Query Optimizer:
Rule-based Optimization
: by how to execute a queryCost-based Optimization
: by cheapest execution plan
Query Optimization
- Make sure that the
JOIN
columns are indexed in both tables - In most cases, many small query is faster than complex query
- In most cases,
LEFT JOIN
/RIGHT JOIN
is slower thanINNER JOIN
(source) - Avoiding joins examples
- use
WHERE
statement to make smaller dataset before processing a IN (b,c,d)
translate to(a=b OR a=c OR a=d)
a NOT IN (b, c, d)
translates to(a != b AND a != c AND a != d)
EXISTS
is much faster thanIN
, when the sub-query results is very large (source)IN
is faster thanEXISTS
, when the sub-query results is very smallGROUP BY
on the smallest dataset you can (use where before grouping)- Avoid
ORDER BY
if ordering column not indexed (ordering in application instead) - Postgres can use an index when doing
some_string LIKE 'pattern%'
but not forsome_string LIKE '%pattern%'
- Use
BEGIN
andROLLBACK
for explain statement
Archiving
Reference:
- https://www.microfocus.com/media/flyer/top_four_data_retention_concerns_facing_records_managers_flyer.pdf
- https://www.freeitdata.com/5-steps-to-crafting-a-data-archiving-strategy/
Concern:
- Regulation Compliance
- Litigation Preparation
- Storage Management
- Security