Mysql index

1,511 views
1,388 views

Published on

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

No Downloads
Views
Total views
1,511
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
54
Comments
0
Likes
5
Embeds 0
No embeds

No notes for slide

Mysql index

  1. 1. MySQL Index Andy YaoFriday, December 30, 11
  2. 2. MySQL BasicFriday, December 30, 11
  3. 3. MySQL ArchitectureFriday, December 30, 11
  4. 4. Storage Engine: MyISAM Table lock No automated data recovery No transactions Only indexes are cached in memory Compact storageFriday, December 30, 11
  5. 5. Storage Engine: InnoDB Transactional Foreign keys Multiversioning Clustered by primary key No cached count(*) Blocking AUTO_INCREMENT Optimized cacheFriday, December 30, 11
  6. 6. InnoDB Storage Architecture Tablespaces Segment leaf node segment extent extent non-leaf node segment extent extent rollback node segment ... Extent Row Page trx id row row row id row row roll pointer ... col 1 ... col n 64 pagesFriday, December 30, 11
  7. 7. InnoDB and File system  File  system                            -­‐>  InnoDB  -­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐  disk  partition                      -­‐>  tablespace  file                                          -­‐>  segment  inode                                        -­‐>  fsp0fsp.c  inode  fs  space  allocation  unit  -­‐>  extent  disk  block                              -­‐>  page  (16  kB)Friday, December 30, 11
  8. 8. Types of IndexesFriday, December 30, 11
  9. 9. Types of Indexes B+Tree indexes Hash indexes R-Tree indexes Full-text indexesFriday, December 30, 11
  10. 10. B+Tree IndexesFriday, December 30, 11
  11. 11. Binary search tree Binary search tree AVL tree 2 6 3 3 7 5 7 2 5 8 6 8Friday, December 30, 11
  12. 12. B Tree 25 50 75 ... 5 10 15 20 25 30 50 55 60 65 75 80 85 90Friday, December 30, 11
  13. 13. B+Tree 25 50 75 ... 5 10 15 20 25 30 50 55 60 65 75 80 85 90Friday, December 30, 11
  14. 14. B+Tree IndexesFriday, December 30, 11
  15. 15. B+tree indexes: Demo CREATE TABLE People ( last_name varchar(50) not null, first_name varchar(50) not null, dob date not null, gender enum(m, f) not null, key(last_name, first_name, dob) );Friday, December 30, 11
  16. 16. B+tree indexes: DemoFriday, December 30, 11
  17. 17. B+Tree: Types of queries Match the full value where last_name=? and first_name=? and bod=? Match a leftmost prefix where last_name=? where last_name=?Friday, December 30, 11
  18. 18. B+Tree: Types of queries Match a column prefix where last_name like ”Liang%” Match a range of values where last_name > ?Friday, December 30, 11
  19. 19. B+Tree: Types of queries Match one part exactly and match a range on another where last_name=? and first_name>? Index only queries select first_name where last_name=?Friday, December 30, 11
  20. 20. B+Tree: Limitations Doesn’t start from leftmost where first_name=? Skip columns in the index where last_name=? and bod=?Friday, December 30, 11
  21. 21. B+Tree: Limitations More than one range conditions where last_name>? and first_name>? Can’t optimize to the right of the first range conditon where last_name>? and first_name=?Friday, December 30, 11
  22. 22. Clustered IndexFriday, December 30, 11
  23. 23. Clustered Index Non Clustered Clustered Index organized table Heap tableFriday, December 30, 11
  24. 24. Friday, December 30, 11
  25. 25. Useful CommandsFriday, December 30, 11
  26. 26. Useful commands show index from TABLE; explain [extended] SELECT * FROM TABLE; UPDATE, DELETE convert to SELECTFriday, December 30, 11
  27. 27. Explain select_tpe simple, primary, subquery, derived, union type all < index < range < ref < eq_ref < const, system < nullFriday, December 30, 11
  28. 28. Explain Extra using where using index using filesort using temporaryFriday, December 30, 11
  29. 29. Indexing StrategiesFriday, December 30, 11
  30. 30. Indexing Strategies Isolate the column Prefix Indexes and Index Selectivity Covering Indexes Use Index Scan For SortsFriday, December 30, 11
  31. 31. Isolate the column where last_name=”Fred” where a+1=5 where md5(a)=”45c48cce2e2d7fbdea1afc5”Friday, December 30, 11
  32. 32. Prefix index & Index selectivity Prefix index KEY index_on_sum(`sum`(5)) Index Selectivity Cardinality/Count(*) 0..1Friday, December 30, 11
  33. 33. Covering Index select first_name from people where last_name=”fred” Extra: Using index Not support “Like” in queryFriday, December 30, 11
  34. 34. Using Index for Sorts select * from people where last_name=? and first_name =? order by dobFriday, December 30, 11
  35. 35. Redundant/Duplicate index Duplicate Redundant primary key(id) key(a, b, c) key(id) key(a, b) unique(id) key(a) key(b, a)Friday, December 30, 11
  36. 36. Others Index merge Or Sub-query/Join/Union Group/Order Locking Query optimizationFriday, December 30, 11
  37. 37. References http://www.mysqlperformanceblog.com/ http://www.percona.com/Friday, December 30, 11
  38. 38. Questions?Friday, December 30, 11

×