1
May 10, 2015
1
MySQL Query Optimization
• MySQL Introduction
• Storage Engines
• InnoDB
• Optimization Types
• Query Optimization
• Explain Plan
• Indexing
• Rewriting Queries
2
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
Storage Engines
• Prominent Feature of MySQL
• Depends on Needs
• MyISAM and INNODB
• MyISAM – Non Transactional
• Innodb - Transactional
• Others – Memory, CSV, Merge , NDB Cluster
4
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
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
Optimization Types
• Hardware & OS Optimization
• Query Optimization
• Server Optimization
• Application Optimization
• Network Optimization
7
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
Query Optimization
Techniques To Optimize Queries ?
• Indexing .
• Rewriting Queries.
9
Example
CREATE TABLE `EVENT_TRAIL_NEW` (
`ID` bigint(20) NOT NULL AUTO_INCREMENT,
`SOURCE_SYSTEM_ID` bigint(20) NOT NULL,
`TARGET_SYSTEM_ID` bigint(20) NOT NULL,
`EVENT_SITUATION_CATALOG_ID` bigint(20) NOT NULL,
`PREVIOUS_EVENT_ID` bigint(20) DEFAULT NULL,
`secure_token` varchar(100) DEFAULT NULL,
`event_timestamp` datetime DEFAULT NULL,
`current_state` varchar(25) NOT NULL,
`event_correlation_id` bigint(20) DEFAULT NULL,
PRIMARY KEY (`ID`)
) ENGINE=InnoDB AUTO_INCREMENT=8561335 DEFAULT CHARSET=ucs2
10
Explain Plan
• An inbuilt tool to see query performance.
• Shows estimated query performance.
• Works only for select queries.
11
Explain Plan
• Complex Query Example.
12
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
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
Indexing
Types of Index :
• Primary
• Unique
• Secondary index.
• Full Text Index ( MyISAM )
15
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
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
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
Indexing
Example (Range Values )
19
Indexing
Example ( Function )
Without date()
With date()
20
Indexing
Example (prefix usage).
• Table : EVENT_TRAIL_NEW
• Column : current_state
• Index name : idx_current_state
• Count of rows : 8560301
Unique Values
• COMPLETE
• EMITTED
• EXCEPTION
• RE_EMITTED
21
Indexing
Example ( Prefix Usage )
Search by Prefix :
Search by Suffix :
22
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
Rewriting Queries
• Make Use Of Index .
• Poorly Written .
• Fetching Too Many Rows.
• Fetching Unwanted Column.
• Improperly Arranged.
• Sort on Primary Key.
24
Rewriting Queries
• In Query Optimization
25
Rewriting Queries
• In Query Optimization ( Use Joins)
26
Rewriting Queries
• Removing Functions.
Date() is removed on Right side.
27
THANKS …
28