• Save
MySQL Query Optimization (Basics)
Upcoming SlideShare
Loading in...5

MySQL Query Optimization (Basics)






Total Views
Views on SlideShare
Embed Views



9 Embeds 153

http://www.linkedin.com 102
https://www.linkedin.com 24
http://remotemysqldba.blogspot.in 11
http://remotemysqldba.blogspot.com 7
https://www.blogger.com 4
http://remotemysqldba.blogspot.jp 2
https://twitter.com 1
https://remotemysqldba.blogspot.com 1
http://remotemysqldba.blogspot.co.uk 1



Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
Post Comment
Edit your comment

MySQL Query Optimization (Basics) MySQL Query Optimization (Basics) Presentation Transcript

  • 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 OptimizationNeed For Query Optimization.• Performance drops as data grows.• Hardware Optimization 5X Performance.• Query Optimization 10X to 100 X .• Reduces load on servers. 8
  • Query OptimizationTechniques To Optimize Queries ?• Indexing .• Rewriting Queries. 9
  • ExampleCREATE 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
  • IndexingMyths 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
  • IndexingTypes of Index :• Primary• Unique• Secondary index.• Full Text Index ( MyISAM ) 15
  • IndexingB – 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
  • IndexingB- 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
  • IndexingExample :Table : EVENT_TRAIL_NEWColumn : event_timestampMin value : 2011-07-20 12:30:40Max Value : 2012-11-27 23:00:00Index name : idx_event_timestampCount of rows : 8560301 18
  • IndexingExample (Range Values ) 19
  • IndexingExample ( Function )Without date()With date() 20
  • IndexingExample (prefix usage).• Table : EVENT_TRAIL_NEW• Column : current_state• Index name : idx_current_state• Count of rows : 8560301Unique Values• COMPLETE• EMITTED• EXCEPTION• RE_EMITTED 21
  • IndexingExample ( Prefix Usage )Search by Prefix :Search by Suffix : 22
  • IndexingExample (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