MySQL Query Optimization.

819 views

Published on

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

Published in: Technology
0 Comments
3 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
819
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
30
Comments
0
Likes
3
Embeds 0
No embeds

No notes for slide

MySQL Query Optimization.

  1. 1. 1
  2. 2. MySQL Query Optimization • • • • • • • • MySQL Introduction Storage Engines InnoDB Optimization Types Query Optimization Explain Plan Indexing Rewriting Queries 2
  3. 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. 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. 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. 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
  7. 7. Optimization Types • • • • • Hardware & OS Optimization Query Optimization Server Optimization Application Optimization Network Optimization 7
  8. 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
  9. 9. Query Optimization Techniques To Optimize Queries ? • Indexing . • Rewriting Queries. 9
  10. 10. 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
  11. 11. Explain Plan • An inbuilt tool to see query performance. • Shows estimated query performance. • Works only for select queries. 11
  12. 12. Explain Plan • Complex Query Example. 12
  13. 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. 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. 15. Indexing Types of Index : • • • • Primary Unique Secondary index. Full Text Index ( MyISAM ) 15
  16. 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. 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. 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
  19. 19. Indexing Example (Range Values ) 19
  20. 20. Indexing Example ( Function ) Without date() With date() 20
  21. 21. 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
  22. 22. Indexing Example ( Prefix Usage ) Search by Prefix : Search by Suffix : 22
  23. 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. 24. Rewriting Queries • • • • • • Make Use Of Index . Poorly Written . Fetching Too Many Rows. Fetching Unwanted Column. Improperly Arranged. Sort on Primary Key. 24
  25. 25. Rewriting Queries • In Query Optimization 25
  26. 26. Rewriting Queries • In Query Optimization ( Use Joins) 26
  27. 27. Rewriting Queries • Removing Functions. Date() is removed on Right side. 27
  28. 28. THANKS … 28

×