mysql optimization

449 views

Published on

Published in: Technology, Business
0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

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

No notes for slide

mysql optimization

  1. 1. MySQL Optimization Ha noi php day 2009 vanchinh@gmail.com19/12/2009 Ha Noi - 2009 1
  2. 2. Content Introduction Example Indexing Principles Considerations Explain General discussion19/12/2009 Ha Noi - 2009 2
  3. 3. Introduction Ha noi php day 2009 vanchinh@gmail.com19/12/2009 Ha Noi - 2009 3
  4. 4. Who am i I working in NaisCorp (socbay.com) I’ve been using MySQL personally for 2 years and professionally for over 1 years. Contact info:  Personal: vanchinh@gmail.com  work: chinhnv@socbay.com  Web: http://ffx.socbay.com19/12/2009 Ha Noi - 2009 4
  5. 5. Example Ha noi php day 2009 vanchinh@gmail.com19/12/2009 Ha Noi - 2009 5
  6. 6. Example SELECT * FROM news WHERE cate_id =1 ORDER BY createdate DESC LIMIT 0,10; SELECT * FROM news WHERE cate_id =1 or cate_id=2 ORDER BY createdate DESC LIMIT 0,10; SELECT * FROM news WHERE id < 12345678 ORDER BY createdate DESC LIMIT 0, 1019/12/2009 Ha Noi - 2009 6
  7. 7. Example Query Parsing and Optimizing Query Parse Thread 1 Thread 2 Thread 3 Optimize Thread 4 MyISAM Key Buffer Engine API MyISAM … Thread 5 Storage Execute (ha_myisam) (fetch) *.MYD, *.MYI Thread n (update) InnoDB Key Buffer InnoDB (ha_innodb) ibdataN Results Group By Having Order ByGroup By and Order By can be done with indexes instead.19/12/2009 Ha Noi - 2009 7
  8. 8. Example Query Parsing and Optimizing  All parsing and optimizing steps are the same for all storage engines  All ORDER BY / GROUP BY steps are the same  The only difference for a given storage engine is in actually fetching the rows  Some types of indexes don’t exist on some table types: e.g. FULLTEXT and SPATIAL indexes don’t exist for InnoDB19/12/2009 Ha Noi - 2009 8
  9. 9. Example Explain SELECT * FROM news WHERE cate_id =1 ORDER BY createdate DESC LIMIT 0,10; Run time: 0.9724s19/12/2009 Ha Noi - 2009 9
  10. 10. Example WHERE cate_id =1 Order By createdate Limit 0,10 2.000.000 2.000.000 1019/12/2009 Ha Noi - 2009 10
  11. 11. Indexing Principles Ha noi php day 2009 vanchinh@gmail.com19/12/2009 Ha Noi - 2009 11
  12. 12. Optimization – Indexing Principles Indexing principles  Single-column index  Multi-column index  Table news19/12/2009 Ha Noi - 2009 12
  13. 13. Optimization – Indexing Principles Single index Table news Multi index19/12/2009 Ha Noi - 2009 13
  14. 14. Optimization – Indexing Principles Basic Index Principles  Only one index can be used, per table, per query  Index definitions must match your real-world use  You don’t have to do anything special to maintain indexes after creation Creating/Dropping Indexes  CREATE INDEX cate ON news (cate_id)  DROP INDEX cate ON news;  ALTER TABLE news ADD INDEX cate (cate_id);  ALTER TABLE news DROP INDEX cate;19/12/2009 Ha Noi - 2009 14
  15. 15. Optimization – Indexing Principles CREATE INDEX cate_date ON news (cate_id, createdate) Explain SELECT * FROM news WHERE cate_id =1 ORDER BY createdate DESC LIMIT 0,10 Run time: 0.0007 s19/12/2009 Ha Noi - 2009 15
  16. 16. Optimization – Indexing Principles WHERE cate_id =1 Order By createdate Limit 0,10 2.000.000 2.000.000 1019/12/2009 Ha Noi - 2009 16
  17. 17. Optimization – Indexing Principles Explain SELECT * FROM news WHERE cate_id =1 or cate_id=2 ORDER BY createdate DESC LIMIT 0,10 Run time: 1.0067S19/12/2009 Ha Noi - 2009 17
  18. 18. Optimization – Indexing Principles Cate_id=1 Order by Limit 0,10 1.000.000 Cate_id=2 3.000.000 10 2.000.00019/12/2009 Ha Noi - 2009 18
  19. 19. Optimization – Indexing Principles SELECT news.* FROM ( (SELECT id FROM news WHERE cate_id=1 ORDER BY createdate LIMIT 0,10) UNION (SELECT id FROM news WHERE cate_id=2 ORDER BY createdate LIMIT 0,10)) as temp, news where news.id=temp.id ORDER BY createdate DESC LIMIT 0,10;19/12/2009 Ha Noi - 2009 19
  20. 20. Optimization – Indexing Principles Run time: 0.00928s19/12/2009 Ha Noi - 2009 20
  21. 21. Optimization – Indexing Principles Cate_id=1 Order by Limit 10 20 10 Cate_id=2 1019/12/2009 Ha Noi - 2009 21
  22. 22. Optimization – Indexing Principles MyISAM indexes19/12/2009 Ha Noi - 2009 22
  23. 23. Optimization – Indexing Principles MyISAM indexes19/12/2009 Ha Noi - 2009 23
  24. 24. Optimization – Indexing Principles MyISAM indexes19/12/2009 Ha Noi - 2009 24
  25. 25. Optimization – Indexing Principles InnoDB indexes19/12/2009 Ha Noi - 2009 25
  26. 26. Optimization – Indexing Principles MyISAM vs InnoDB19/12/2009 Ha Noi - 2009 26
  27. 27. Optimization – Considerations When can indexes be used?  Simple PRIMARY KEY lookups  PRIMARY KEY(i) :: WHERE i = 5  Secondary KEY (index) lookups  INDEX(i) :: WHERE i = 7  Prefix lookups  INDEX(s) :: WHERE s LIKE “foo%”  INDEX(i, j) :: WHERE i = 5  Joins – index either table19/12/2009 Ha Noi - 2009 27
  28. 28. Optimization – Considerations When can’t indexes be used?  LIKE that starts with a wildcard  INDEX(s) :: WHERE s LIKE “%foo”  Bitwise operations  INDEX(i) :: WHERE i & 4  Non-prefix lookups  INDEX(i, j) :: WHERE j = 5  Using a function containing column ref  INDEX(s) :: WHERE foo(s) = “FOO”  INDEX(dt) :: WHERE unix_timestamp(dt) = 119/12/2009 Ha Noi - 2009 28
  29. 29. Optimization – Explain Explain SELECT * FROM news WHERE cate_id = 218103808 ORDER BY id DESC LIMIT 0,10;19/12/2009 Ha Noi - 2009 29
  30. 30. Optimization – Explain Fields in EXPLAIN  id – (4.1+) The ID number of this SELECT  select_type – (4.1+) The type of this SELECT  table – The name of the table in question  type – The type of reading done on this table (system, const, eq_ref, ref, ref_or_null, index_merge, unique_subquery, index_subquery, range, index, ALL)  possible_keys – The keys which were examined for possible use  key – The key that was finally chosen for use19/12/2009 Ha Noi - 2009 30
  31. 31. Optimization – Explain Fields in EXPLAIN  key_len – The length of the part of the key that was finally chosen for use  ref – Row reference; What is used to look up row values from the index  rows – The approximate number of rows which must be examined from this table using the chosen optimizations  Extra – Any extra comments the server wanted to add about this optimization path (free form)19/12/2009 Ha Noi - 2009 31
  32. 32. General Discussion Got any questions for me? Ha noi php day 2009 vanchinh@gmail.com19/12/2009 Ha Noi - 2009 32

×