2. MySQL Query Optimization
• MySQL Introduction
• Storage Engines
• InnoDB
• Optimization Types
• Query Optimization
• Explain Plan
• Indexing
• Rewriting Queries
2
3. MySQL Introduction
• Open Source
• RDBMS
• Released in 1995
• Founded by Monty
• Current Version 5.5
• Most Widely Used DB
• Forks - Percona, MariaDB , Drizzle
• Facebook, Twitter , Wikipedia, Walmart, ebay.
3
4. Storage Engines
• Prominent Feature of MySQL
• Depends on Needs
• MyISAM and INNODB
• MyISAM – Non Transactional
• Innodb - Transactional
• Others – Memory, CSV, Merge , NDB Cluster
4
5. InnoDB
• Founded in 1995 by Innobase.
• Transactional Support
• Row level locking
• Foreign Key support
• Clustered Index
• Current Version 1.1
• Forks – XtraDB , Falcon , MariaDB , TokuDB
5
6. InnoDB
Primary Key
• Clustered index needs a Primary Key.
• If not InnoDB creates a hidden Primary key.
• Column must be small .
• It is better to have auto-increment columns.
6
8. Query Optimization
Need For Query Optimization.
• Performance drops as data grows.
• Hardware Optimization 5X Performance.
• Query Optimization 10X to 100 X .
• Reduces load on servers.
8
13. Indexing
• Index (Key) are used to find rows quickly.
• Powerful way to tune queries.
• Increases the performance of select, update and
delete queries.
• Reduces disk IO .
• Optimal index requires query rewrite
13
14. Indexing
Myths of Index :
• Increases the database size.
• Indexing will decrease the insert query
performance.
• One index per table query.
• Poor performance with wild card characters.(%,$).
• Make index length small ( 767 bytes InnoDB).
• Do not work on full table scans.
14
15. Indexing
Types of Index :
• Primary
• Unique
• Secondary index.
• Full Text Index ( MyISAM )
15
16. Indexing
B – Tree:
• Balanced Tree (not Binary Tree).
• Used in all databases.
• All the secondary indexes.
• Suitable for most needs.
• Re-indexing is not required.
• Used when queries scans less than 30% of available
data approximately
16
17. Indexing
B- Tree Works On:
• Full value.
• Range values.
• Perfix values. Ex) Mysql% , M%.
• Use of functions on indexed columns disables it
usage in most cases.
17
18. Indexing
Example :
Table : EVENT_TRAIL_NEW
Column : event_timestamp
Min value : 2011-07-20 12:30:40
Max Value : 2012-11-27 23:00:00
Index name : idx_event_timestamp
Count of rows : 8560301
18
23. Indexing
Example (One index per table query )
• Table : EVENT_TRAIL_NEW
• Column : TARGET_SYSTEM_ID
• Index name : idx_TARGET_SYSTEM_ID
• Count of rows : 8560301
23
24. Rewriting Queries
• Make Use Of Index .
• Poorly Written .
• Fetching Too Many Rows.
• Fetching Unwanted Column.
• Improperly Arranged.
• Sort on Primary Key.
24