MySQL Query Optimization.

28
1

description

This presentation speaks about the ways to optimize queries and the best practices

Transcript of MySQL Query Optimization.

Page 1: MySQL Query Optimization.

1

Page 2: MySQL Query Optimization.

MySQL Query Optimization

• MySQL Introduction

• Storage Engines

• InnoDB

• Optimization Types

• Query Optimization

• Explain Plan

• Indexing

• Rewriting Queries

2

Page 3: MySQL Query Optimization.

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

Page 4: MySQL Query Optimization.

Storage Engines

• Prominent Feature of MySQL

• Depends on Needs

• MyISAM and INNODB

• MyISAM – Non Transactional

• Innodb - Transactional

• Others – Memory, CSV, Merge , NDB Cluster

4

Page 5: MySQL Query Optimization.

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

Page 6: MySQL Query Optimization.

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

Page 7: MySQL Query Optimization.

Optimization Types

• Hardware & OS Optimization

• Query Optimization

• Server Optimization

• Application Optimization

• Network Optimization

7

Page 8: MySQL Query Optimization.

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

Page 9: MySQL Query Optimization.

Query Optimization

Techniques To Optimize Queries ?

• Indexing .

• Rewriting Queries.

9

Page 10: MySQL Query Optimization.

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

Page 11: MySQL Query Optimization.

Explain Plan

• An inbuilt tool to see query performance.

• Shows estimated query performance.

• Works only for select queries.

11

Page 12: MySQL Query Optimization.

Explain Plan

• Complex Query Example.

12

Page 13: MySQL Query Optimization.

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

Page 14: MySQL Query Optimization.

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

Page 15: MySQL Query Optimization.

Indexing

Types of Index :

• Primary

• Unique

• Secondary index.

• Full Text Index ( MyISAM )

15

Page 16: MySQL Query Optimization.

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

Page 17: MySQL Query Optimization.

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

Page 18: MySQL Query Optimization.

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

Page 19: MySQL Query Optimization.

Indexing

Example (Range Values )

19

Page 20: MySQL Query Optimization.

Indexing

Example ( Function )

Without date()

With date()

20

Page 21: MySQL Query Optimization.

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

Page 22: MySQL Query Optimization.

Indexing

Example ( Prefix Usage )

Search by Prefix :

Search by Suffix :

22

Page 23: MySQL Query Optimization.

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

Page 24: MySQL Query Optimization.

Rewriting Queries

• Make Use Of Index .

• Poorly Written .

• Fetching Too Many Rows.

• Fetching Unwanted Column.

• Improperly Arranged.

• Sort on Primary Key.

24

Page 25: MySQL Query Optimization.

Rewriting Queries

• In Query Optimization

25

Page 26: MySQL Query Optimization.

Rewriting Queries

• In Query Optimization ( Use Joins)

26

Page 27: MySQL Query Optimization.

Rewriting Queries

• Removing Functions.

Date() is removed on Right side.

27

Page 28: MySQL Query Optimization.

THANKS …

28