Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Improving Performance With Better Indexes

Improving Performance With Better Indexes

In this presentation, I discuss how to identify, review and analyze SQL statements in order to create better indexes for your queries. This includes understanding the EXPLAIN syntax and how to create and identify covering and partial column indexes.

download pdf of Improving Performance With Better Indexes
Download PDF Presentation
This presentation is based on the work with a customer showing the 95% improvement of a key 15 table join query running 15,000 QPS in a 25 billion SQL statements per day infrastructure.

Ronald Bradford

October 06, 2016
Tweet

More Decks by Ronald Bradford

Other Decks in Technology

Transcript

  1. Improving MySQL Performance with Better Indexes EffectiveMySQL.com - Performance, Scalability,

    Site Reliability @RonaldBradford #PerconaLive Ronald Bradford http://ronaldbradford.com 2016.10 Saturday, October 1, 16
  2. EffectiveMySQL.com - Performance, Scalability, Site Reliability @RonaldBradford #PerconaLive OBJECTIVE Improve

    query performance, therefore Increase server throughput, therefore Reduce infrastructure (and cost) to run your application Actual customer case study Saturday, October 1, 16
  3. EffectiveMySQL.com - Performance, Scalability, Site Reliability @RonaldBradford #PerconaLive REAL RESULTS

    Already well indexed schema Reduced query time from 175ms to 10ms (17x, 94% faster) 15,000+ requests per sec Eliminated 100 AWS EC2 servers Actual customer case study Saturday, October 1, 16
  4. EffectiveMySQL.com - Performance, Scalability, Site Reliability @RonaldBradford #PerconaLive ABOUT Author

    17 years with MySQL / 27 years with RDBMS Senior Consultant at MySQL Inc (06-08) Consultant for Oracle Corporation (96-99) 10th year presenting MySQL content Published author of 4 books Oracle ACE Director All time top MySQL blogger (2011) Ronald BRADFORD http://ronaldbradford.com/presentations/ Saturday, October 1, 16
  5. EffectiveMySQL.com - Performance, Scalability, Site Reliability @RonaldBradford #PerconaLive SQL REVIEW

    1. Capture 2. Identify 3. Confirm 4. Analyze 5. Optimize 6. Verify Six step process Saturday, October 1, 16
  6. 1 EffectiveMySQL.com - Performance, Scalability, Site Reliability @RonaldBradford #PerconaLive Capture

    General Query Log TCP/IP Connectors Application Proxy Plugin ... Not discussed in detail this presentation 1. Capture 2. Identify 3. Confirm 4. Analyze 5. Optimize 6. Verify Saturday, October 1, 16
  7. 1 EffectiveMySQL.com - Performance, Scalability, Site Reliability @RonaldBradford #PerconaLive Capture

    General Query Log TCP/IP Connectors Application Proxy Plugin ... Not discussed in detail this presentation 1. Capture 2. Identify 3. Confirm 4. Analyze 5. Optimize 6. Verify All Queries? or Sample Queries? Saturday, October 1, 16
  8. 1 EffectiveMySQL.com - Performance, Scalability, Site Reliability @RonaldBradford #PerconaLive Capture

    5.6+ Upgrade to MySQL 5.6 Use Performance Schema events_statements_... https://dev.mysql.com/doc/refman/5.6/en/performance-schema-statement-tables.html Saturday, October 1, 16
  9. EffectiveMySQL.com - Performance, Scalability, Site Reliability @RonaldBradford #PerconaLive SELECT EVENT_ID,

    TRUNCATE(TIMER_WAIT/1000000000000,6) AS duration, current_schema, sql_text FROM performance_schema.events_statements_history_long WHERE sql_text IS NOT NULL AND current_schema != 'performance_schema' ORDER BY duration DESC LIMIT 10; UPDATE performance_schema.setup_consumers SET enabled='YES' WHERE name='events_statements_history_long'; #/etc/my.cnf performance_schema=ON Saturday, October 1, 16
  10. EffectiveMySQL.com - Performance, Scalability, Site Reliability @RonaldBradford #PerconaLive Capture <

    5.6 TCP/IP Sample queries for a period of time No impact to existing code Application Logging All queries for a given function Shows full sequential path of SQL 1 Example Customer example Saturday, October 1, 16
  11. 1 EffectiveMySQL.com - Performance, Scalability, Site Reliability @RonaldBradford #PerconaLive $

    sudo tcpdump -i any port 3306 -s 65535 -x -nn -q -tttt -c 10000 | \ pt-query-digest --type tcpdump # Profile # Rank Query ID Response time Calls R/Call Apdx V/M Item # ==== ================== ============= ===== ====== ==== ===== ====== # 1 0xE5C8D4B9F7BDCCAF 0.5044 18.7% 1 0.5044 1.00 0.00 SELECT .. # 2 0x813031B8BBC3B329 0.4893 18.2% 23 0.0213 1.00 0.01 COMMIT # 3 0x04AB3BC1A33D3B48 0.4107 15.2% 1 0.4107 1.00 0.00 SELECT .. # 4 0xD15CA257BAF77DAF 0.3356 12.5% 321 0.0010 1.00 0.00 SELECT .. # 5 0x228B1F36C5EBCF1F 0.2177 8.1% 2 0.1089 1.00 0.22 SELECT .. TCP/IP Example http://effectiveMySQL.com/article/mk-query-digest Captured SQL queries executed in a sample 1 second Capture Saturday, October 1, 16
  12. EffectiveMySQL.com - Performance, Scalability, Site Reliability @RonaldBradford #PerconaLive <?php mysql_query('SELECT

    col1, col2 FROM table1 ...'); ... ... mysql_query('SELECT a,b,c FROM x INNER JOIN y ... ... mysql_query('UPDATE table SET ... ?> 1 Application Logging Example // Create wrapper function function db_query($sql) { $rs = mysql_query($sql); return $rs; } // Global replace mysql_query with db_query PHP example Capture Saturday, October 1, 16
  13. 1 EffectiveMySQL.com - Performance, Scalability, Site Reliability @RonaldBradford #PerconaLive function

    db_query($sql) { $start_time = microtime(true); $rs = mysql_query($sql); $exec_time = microtime(true) - $start_time; debug(format_time($exec_time) . ' ' . $sql . "\n"); return $rs; } function debug($str) { if (DEBUG) echo $str; return 0; } function format_time($time) { return number_format($time, 8, '.', ''); } Application Logging Example Capture Saturday, October 1, 16
  14. EffectiveMySQL.com - Performance, Scalability, Site Reliability @RonaldBradford #PerconaLive 0.00010109 SELECT

    ip FROM ... 0.00005198 SELECT name, value FROM ... 0.00005984 SELECT id, status, ... 0.17592907 SELECT g.id, c.id, ... 0.00047803 SELECT DISTINCT id_c FROM camp... 0.00741315 SELECT DISTINCT id_c FROM camp.. 0.00058198 SELECT id_c FROM cr ... 0.00161815 SELECT id_c FROM cr ... 0.00032806 SELECT id_c FROM cr ... 0.00007200 SELECT DISTINCT id_a FROM arb ... 0.00005412 SELECT DISTINCT id_a FROM asw ... 0.00004697 SELECT id_adv FROM arw 0.00004601 SELECT id_adv FROM arw 0.00009012 SELECT gk.id, k.value ... 0.00009084 SELECT gk.id, k.value ... 0.00006318 SELECT gk.id, k.value ... 0.00005794 SELECT gk.id, k.value ... 0.00005603 SELECT gk.id, k.value ... 1 24 statements for single API call Easy to spot worst query Application Logging Example Capture Saturday, October 1, 16
  15. EffectiveMySQL.com - Performance, Scalability, Site Reliability @RonaldBradford #PerconaLive 0.00010109 SELECT

    ip FROM ... 0.00005198 SELECT name, value FROM ... 0.00005984 SELECT id, status, ... 0.17592907 SELECT g.id, c.id, ... 0.00047803 SELECT DISTINCT id_c FROM camp... 0.00741315 SELECT DISTINCT id_c FROM camp.. 0.00058198 SELECT id_c FROM cr ... 0.00161815 SELECT id_c FROM cr ... 0.00032806 SELECT id_c FROM cr ... 0.00007200 SELECT DISTINCT id_a FROM arb ... 0.00005412 SELECT DISTINCT id_a FROM asw ... 0.00004697 SELECT id_adv FROM arw 0.00004601 SELECT id_adv FROM arw 0.00009012 SELECT gk.id, k.value ... 0.00009084 SELECT gk.id, k.value ... 0.00006318 SELECT gk.id, k.value ... 0.00005794 SELECT gk.id, k.value ... 0.00005603 SELECT gk.id, k.value ... 1 24 statements for single API call Easy to spot worst query Application Logging Example 7 ms 175 ms Capture Saturday, October 1, 16
  16. 2 EffectiveMySQL.com - Performance, Scalability, Site Reliability @RonaldBradford #PerconaLive IDENTIFY

    By Duration of time By Frequency of execution Not discussed in detail this presentation 1. Capture 2. Identify 3. Confirm 4. Analyze 5. Optimize 6. Verify Saturday, October 1, 16
  17. EffectiveMySQL.com - Performance, Scalability, Site Reliability @RonaldBradford #PerconaLive //TODO #

    Profile # Rank Query ID Response time Calls R/Call Apdx V/M Item # ==== ================== ============= ===== ====== ==== ===== ====== # 1 0xE5C8D4B9F7BDCCAF 0.5044 18.7% 1 0.5044 1.00 0.00 SELECT .. # 2 0x813031B8BBC3B329 0.4893 18.2% 23 0.0213 1.00 0.01 COMMIT # 3 0x04AB3BC1A33D3B48 0.4107 15.2% 1 0.4107 1.00 0.00 SELECT .. # 4 0xD15CA257BAF77DAF 0.3356 12.5% 321 0.0010 1.00 0.00 SELECT .. # 5 0x228B1F36C5EBCF1F 0.2177 8.1% 2 0.1089 1.00 0.22 SELECT .. TCP/IP Example 2 Frequency Duration Which query is more important to improve? IDENTIFY Saturday, October 1, 16
  18. 3 EffectiveMySQL.com - Performance, Scalability, Site Reliability @RonaldBradford #PerconaLive CONFIRM

    Via Application Logging mysql client SHOW PROFILES (deprecated) Performance Schema Not discussed in detail this presentation 1. Capture 2. Identify 3. Confirm 4. Analyze 5. Optimize 6. Verify Saturday, October 1, 16
  19. 3 EffectiveMySQL.com - Performance, Scalability, Site Reliability @RonaldBradford #PerconaLive CONFIRM

    Why confirm? Watch & Isolate other factors e.g. Locking Load Caching Not discussed in detail this presentation Saturday, October 1, 16
  20. 3 EffectiveMySQL.com - Performance, Scalability, Site Reliability @RonaldBradford #PerconaLive mysql>

    SELECT ... 1 row in set (0.00 sec) mysql client 10 millisecond precision mysql client Example Saturday, October 1, 16
  21. 3 EffectiveMySQL.com - Performance, Scalability, Site Reliability @RonaldBradford #PerconaLive mysql>

    SELECT ... 1 row in set (0.00 sec) # What you really want is? mysql> SELECT ... 1 row in set (0.008110 sec) mysql client microsecond precision http://j.mp/gM3Hb3 mysql client Example Saturday, October 1, 16
  22. 3 EffectiveMySQL.com - Performance, Scalability, Site Reliability @RonaldBradford #PerconaLive mysql>

    SET PROFILING=1; mysql> SELECT SLEEP(1.0); mysql> SELECT a,b,c FROM table LIMIT 1; mysql> SELECT SLEEP(0.2); mysql> SHOW PROFILES; +----------+------------+-----------------------------+ | Query_ID | Duration | Query | +----------+------------+-----------------------------+ | 1 | 1.00135300 | SELECT SLEEP(1) | | 2 | 0.00026700 | SELECT a,b,c FROM table ... | | 3 | 0.20215600 | SELECT SLEEP(0.2) | +----------+------------+-----------------------------+ Quick verification microsecond precision PROFILING Example Saturday, October 1, 16
  23. EffectiveMySQL.com - Performance, Scalability, Site Reliability @RonaldBradford #PerconaLive ANALYZE <

    5.6 EXPLAIN SHOW CREATE TABLE SHOW INDEXES FROM INFORMATION_SCHEMA.TABLES SHOW TABLE STATUS LIKE EXPLAIN EXTENDED 4 1. Capture 2. Identify 3. Confirm 4. Analyze 5. Optimize 6. Verify Saturday, October 1, 16
  24. EffectiveMySQL.com - Performance, Scalability, Site Reliability @RonaldBradford #PerconaLive ANALYZE 5.6+

    Optimizer trace EXPLAIN FORMAT=JSON Performance Schema ‘sys’ schema Optimizer Hints /*+ */ (5.7.7) 4 https://dev.mysql.com/doc/internals/en/optimizer-tracing.html https://github.com/MarkLeith/mysql-sys Saturday, October 1, 16
  25. EffectiveMySQL.com - Performance, Scalability, Site Reliability @RonaldBradford #PerconaLive mysql> EXPLAIN

    SELECT p.amt, p.reference FROM payment p JOIN invoice i ON i.inv_id = p.inv_id WHERE i.due_date = '2009-10-14' AND i.user_id = 1 AND i.account_id = 10 AND p.amt > 0 +----+-------------+-------+------+---------------+------+---------+------+-------------+ | id | select_type | table | type | possible_keys | key | key_len | rows | Extra | +----+-------------+-------+------+---------------+------+---------+------+-------------+ | 1 | SIMPLE | i | ref | PRIMARY,u,a,d | u | 4 | 1 | Using where | | 1 | SIMPLE | p | ref | i | i | 4 | 6 | Using where | +----+-------------+-------+------+---------------+------+---------+------+-------------+ 4 Example Format modified for display purposes Saturday, October 1, 16
  26. EffectiveMySQL.com - Performance, Scalability, Site Reliability @RonaldBradford #PerconaLive mysql> SHOW

    CREATE TABLE wp_users \G CREATE TABLE `wp_users` ( `ID` bigint(20) unsigned NOT NULL AUTO_INCREMENT, `user_login` varchar(60) NOT NULL DEFAULT '', `user_pass` varchar(64) NOT NULL DEFAULT '', `user_nicename` varchar(50) NOT NULL DEFAULT '', `user_email` varchar(100) NOT NULL DEFAULT '', `user_url` varchar(100) NOT NULL DEFAULT '', `user_registered` datetime NOT NULL DEFAULT '0000-00-00 00:00:00', `user_activation_key` varchar(60) NOT NULL DEFAULT '', `user_status` int(11) NOT NULL DEFAULT '0', `display_name` varchar(250) NOT NULL DEFAULT '', PRIMARY KEY (`ID`), KEY `user_login_key` (`user_login`), KEY `user_nicename` (`user_nicename`) ) ENGINE=MyISAM DEFAULT CHARSET=utf8 4 Example Shows column definitions and index definitions Saturday, October 1, 16
  27. EffectiveMySQL.com - Performance, Scalability, Site Reliability @RonaldBradford #PerconaLive mysql> SHOW

    INDEXES FROM wp_posts; ...+------------+------------------+--------------+-------------+... ...| Non_unique | Key_name | Seq_in_index | Cardinality |... ...+------------+------------------+--------------+-------------+... ...| 0 | PRIMARY | 1 | 2606 |... ...| 1 | post_name | 1 | 2606 |... ...| 1 | type_status_date | 1 | 4 |... ...| 1 | type_status_date | 2 | 6 |... ...| 1 | type_status_date | 3 | 2606 |... ...| 1 | type_status_date | 4 | 2606 |... ...| 1 | post_parent | 1 | 217 |... ...| 1 | post_author | 1 | 1 |... ...+------------+------------------+--------------+-------------+... 4 Example Saturday, October 1, 16
  28. 4 EffectiveMySQL.com - Performance, Scalability, Site Reliability @RonaldBradford #PerconaLive mysql>

    SHOW INDEXES FROM ...; +------------+--------------+--------------+-----------------+-----------+-------------+ | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | +------------+--------------+--------------+-----------------+-----------+-------------+ | 0 | PRIMARY | 1 | id | A | 100085 | | 1 | is_available | 1 | is_available | A | 4 | | 1 | is_active | 1 | is_active | A | 4 | | 1 | market | 1 | is_active | A | 36 | | 1 | market | 2 | is_available | A | 36 | | 1 | market | 3 | is_proposed | A | 238 | +------------+--------------+--------------+-----------------+-----------+-------------+ Example Shows uniqueness of values in index Saturday, October 1, 16
  29. EffectiveMySQL.com - Performance, Scalability, Site Reliability @RonaldBradford #PerconaLive mysql> SELECT

    table_schema,table_name,engine, row_format,table_rows, avg_row_length, (data_length+index_length)/1024/1024 as total_mb, (data_length)/1024/1024 as data_mb, (index_length)/1024/1024 as index_mb FROM INFORMATION_SCHEMA.TABLES WHERE table_schema=DATABASE() AND table_name = 'example'\G table_schema: example table_name: example engine: MyISAM row_format: Dynamic table_rows: 260006 avg_row_length: 2798 total_mb: 700.20174026 data_mb: 600.95564651 index_mb: 99.24609375 4 Example Size of data on disk Saturday, October 1, 16
  30. EffectiveMySQL.com - Performance, Scalability, Site Reliability @RonaldBradford #PerconaLive mysql> SHOW

    TABLE STATUS LIKE 'wp_posts'\G Name: wp_posts Engine: MyISAM Version: 10 Row_format: Dynamic Rows: 2649 Avg_row_length: 2850 Data_length: 7550800 Max_data_length: 281474976710655 Index_length: 259072 Data_free: 0 Auto_increment: 3586 Create_time: 2011-02-02 12:04:21 Update_time: 2011-03-19 11:42:12 Check_time: 2011-02-02 12:04:21 Collation: utf8_general_ci Checksum: NULL 4 Example Saturday, October 1, 16
  31. 4 EffectiveMySQL.com - Performance, Scalability, Site Reliability @RonaldBradford #PerconaLive mysql>

    EXPLAIN SELECT x.uuid, ..., y.uniqueid FROM table1 x, table2 y WHERE x.uuid = y.uniqueid; +----+---------+-------+--------+-----------+---------+---------+------+----------+--------------------------+ | id | select_ | table | type | possible_ | key | key_len | ref | rows | Extra | +----+---------+-------+--------+-----------+---------+---------+------+----------+--------------------------+ | 1 | SIMPLE | x | ALL | NULL | NULL | NULL | NULL | 29960165 | | | 1 | SIMPLE | y | eq_ref | PRIMARY | PRIMARY | 194 | func | 1 | Using where; Using index | +----+---------+-------+--------+-----------+---------+---------+------+----------+--------------------------+ Example Saturday, October 1, 16
  32. 4 EffectiveMySQL.com - Performance, Scalability, Site Reliability @RonaldBradford #PerconaLive mysql>

    EXPLAIN EXTENDED SELECT x.uuid, ..., y.uniqueid FROM table1 x, table2 y WHERE x.uuid = y.uniqueid; mysql> SHOW WARNINGS; Level: Note Code: 10031 Message: select `schema`.`x`.`uuid` AS `uuid`, ... `schema`.`y`.`uniqueid` AS `uniqueid` from `schema`.`table1` `x` join `schema`.`table2` `y` where (convert(`schema`.`x`.`uuid` using utf8) = `test`.`y`.`uniqueid` Example Saturday, October 1, 16
  33. 5 EffectiveMySQL.com - Performance, Scalability, Site Reliability @RonaldBradford #PerconaLive OPTIMIZE

    Adding Indexes Query simplification Eliminate queries MySQL configuration Schema design ... 1. Capture 2. Identify 3. Confirm 4. Analyze 5. Optimize 6. Verify Not discussed in detail this presentation Saturday, October 1, 16
  34. EffectiveMySQL.com - Performance, Scalability, Site Reliability @RonaldBradford #PerconaLive CREATE TABLE

    posts ( id INT UNSIGNED NOT NULL AUTO_INCREMENT, author INT UNSIGNED NOT NULL, status VARCHAR(20) NOT NULL DEFAULT 'publish', name VARCHAR(100) NOT NULL, content TEXT NOT NULL, comment_count INT UNSIGNED NOT NULL, PRIMARY KEY (id), KEY name (name) ) ENGINE=MyISAM DEFAULT CHARSET=utf8; Example 5 THEORY Saturday, October 1, 16
  35. EffectiveMySQL.com - Performance, Scalability, Site Reliability @RonaldBradford #PerconaLive mysql> EXPLAIN

    SELECT COUNT(*) FROM posts WHERE author=2 AND status='draft' \G id: 1 select_type: SIMPLE table: posts type: ALL possible_keys: NULL key: NULL key_len: NULL ref: NULL rows: 2649 Extra: Using where 5 Example No Index used i.e. Full Table Scan See my presentation on Explaining the MySQL explain http://effectivemysql.com/presentation/explaining-the-mysql-explain/ Saturday, October 1, 16
  36. 5 EffectiveMySQL.com - Performance, Scalability, Site Reliability @RonaldBradford #PerconaLive mysql>

    ALTER TABLE posts ADD INDEX (author); mysql> EXPLAIN SELECT COUNT(*) FROM posts WHERE author=2 AND status='draft' \G id: 1 select_type: SIMPLE table: posts type: ref possible_keys: author key: author key_len: 4 ref: const rows: 373 Extra: Using where Using an index Example Less rows compared Saturday, October 1, 16
  37. 5 EffectiveMySQL.com - Performance, Scalability, Site Reliability @RonaldBradford #PerconaLive mysql>

    ALTER TABLE posts ADD INDEX (author, status); mysql> EXPLAIN SELECT COUNT(*) FROM posts WHERE author=2 AND status='draft' \G id: 1 select_type: SIMPLE table: posts type: ref possible_keys: author key: author key_len: 66 ref: const,const rows: 31 Extra: Using where; Using index Using a better index Example Even less rows compared Saturday, October 1, 16
  38. EffectiveMySQL.com - Performance, Scalability, Site Reliability @RonaldBradford #PerconaLive mysql> ALTER

    TABLE posts ADD INDEX (author); mysql> EXPLAIN SELECT COUNT(*) FROM posts WHERE author=2 AND status='draft' \G id: 1 select_type: SIMPLE table: posts type: ref possible_keys: author key: author key_len: 4 ref: const rows: 373 Extra: Using where mysql> ALTER TABLE posts ADD INDEX (author,status); mysql> EXPLAIN SELECT COUNT(*) FROM posts WHERE author=2 AND status='draft' \G id: 1 select_type: SIMPLE table: posts type: ref possible_keys: author key: author key_len: 66 ref: const,const rows: 31 Extra: Using where; Using index Example More columns used in index Less rows processed Saturday, October 1, 16
  39. 5 EffectiveMySQL.com - Performance, Scalability, Site Reliability @RonaldBradford #PerconaLive mysql>

    ALTER TABLE posts ADD INDEX (author, status); mysql> EXPLAIN SELECT COUNT(*) FROM posts WHERE author=2 AND status='draft' \G id: 1 select_type: SIMPLE table: posts type: ref possible_keys: author key: author key_len: 66 ref: const,const rows: 31 Extra: Using where; Using index Using a better index Example Even less rows compared Saturday, October 1, 16
  40. 5 EffectiveMySQL.com - Performance, Scalability, Site Reliability @RonaldBradford #PerconaLive mysql>

    ALTER TABLE posts ADD INDEX (author, status); mysql> EXPLAIN SELECT COUNT(*) FROM posts WHERE author=2 AND status='draft' \G id: 1 select_type: SIMPLE table: posts type: ref possible_keys: author key: author key_len: 66 ref: const,const rows: 31 Extra: Using where; Using index Using a better index Example Even less rows compared Additional Information Saturday, October 1, 16
  41. 5 EffectiveMySQL.com - Performance, Scalability, Site Reliability @RonaldBradford #PerconaLive A

    better INDEX What is: Extra: Using Index ? Saturday, October 1, 16
  42. 5 EffectiveMySQL.com - Performance, Scalability, Site Reliability @RonaldBradford #PerconaLive A

    better INDEX What is: Extra: Using Index ? Does not mean using index (key column) Saturday, October 1, 16
  43. 5 EffectiveMySQL.com - Performance, Scalability, Site Reliability @RonaldBradford #PerconaLive A

    better INDEX What is: Extra: Using Index ? Does not mean using index (key column) Means ONLY using the index Saturday, October 1, 16
  44. 5 EffectiveMySQL.com - Performance, Scalability, Site Reliability @RonaldBradford #PerconaLive A

    better INDEX What is: Extra: Using Index ? Does not mean using index (key column) Means ONLY using the index All query columns are satisfied by index Saturday, October 1, 16
  45. 5 EffectiveMySQL.com - Performance, Scalability, Site Reliability @RonaldBradford #PerconaLive A

    better INDEX What is: Extra: Using Index ? Does not mean using index (key column) Means ONLY using the index All query columns are satisfied by index This is a covering index Saturday, October 1, 16
  46. 5 EffectiveMySQL.com - Performance, Scalability, Site Reliability @RonaldBradford #PerconaLive COVERING

    INDEX Storage Engines matter MyISAM Indexes B-tree (PK & Secondary) InnoDB Indexes B+tree/Clustered (PK) B-tree (secondary) Saturday, October 1, 16
  47. EffectiveMySQL.com - Performance, Scalability, Site Reliability @RonaldBradford #PerconaLive mysql> ALTER

    TABLE posts ENGINE=InnoDB; mysql> EXPLAIN SELECT id FROM posts WHERE author=2 AND status='draft' \G id: 1 select_type: SIMPLE table: posts type: ref possible_keys: author key: author key_len: 66 ref: const,const rows: 34 Extra: Using where; Using index 5 Example Covering Index Saturday, October 1, 16
  48. EffectiveMySQL.com - Performance, Scalability, Site Reliability @RonaldBradford #PerconaLive mysql> ALTER

    TABLE posts ENGINE=MyISAM; mysql> EXPLAIN SELECT id FROM posts WHERE author=2 AND status='draft' \G id: 1 select_type: SIMPLE table: posts type: ref possible_keys: author key: author key_len: 66 ref: const,const rows: 31 Extra: Using where 5 Example Not a covering Index Saturday, October 1, 16
  49. EffectiveMySQL.com - Performance, Scalability, Site Reliability @RonaldBradford #PerconaLive CREATE TABLE

    invoice ( inv_id INT UNSIGNED NOT NULL, user_id INT UNSIGNED NOT NULL, account_id INT UNSIGNED NOT NULL, invoice_date DATE NOT NULL, due_date DATE NOT NULL, PRIMARY KEY pk (inv_id), INDEX u (user_id), INDEX a (account_id), INDEX d (due_date) ) ENGINE=InnoDB; CREATE TABLE payment ( pay_id INT UNSIGNED NOT NULL AUTO_INCREMENT, inv_id INT UNSIGNED NOT NULL, amt DECIMAL (10,2) NOT NULL, reference VARCHAR(20) NOT NULL, PRIMARY KEY pk (pay_id), INDEX i (inv_id) ) ENGINE=InnoDB; Example 5 Parent/Child Master/Child Relationship Saturday, October 1, 16
  50. EffectiveMySQL.com - Performance, Scalability, Site Reliability @RonaldBradford #PerconaLive EXPLAIN SELECT

    p.amt, p.reference FROM payment p JOIN invoice i ON i.inv_id = p.inv_id WHERE i.due_date = '2009-10-14' AND i.user_id = 1 AND i.account_id = 10 AND p.amt > 0 +----+-------------+-------+------+---------------+------+---------+------+-------------+ | id | select_type | table | type | possible_keys | key | key_len | rows | Extra | +----+-------------+-------+------+---------------+------+---------+------+-------------+ | 1 | SIMPLE | i | ref | PRIMARY,u,a,d | u | 4 | 1 | Using where | | 1 | SIMPLE | p | ref | i | i | 4 | 6 | Using where | +----+-------------+-------+------+---------------+------+---------+------+-------------+ SHOW CREATE TABLE payment \G ... PRIMARY KEY pk (pay_id), INDEX i (inv_id) ) ENGINE=InnoDB; Example 5 Important start Saturday, October 1, 16
  51. EffectiveMySQL.com - Performance, Scalability, Site Reliability @RonaldBradford #PerconaLive SELECT p.amt,

    p.reference FROM payment p JOIN invoice i ON i.inv_id = p.inv_id WHERE i.due_date = '2009-10-14' AND i.user_id = 1 AND i.account_id = 10 AND p.amt > 0 ALTER TABLE payment DROP INDEX i, ADD INDEX i (inv_id, amt); Example 5 Identify additional columns used in query Add column to index Saturday, October 1, 16
  52. EffectiveMySQL.com - Performance, Scalability, Site Reliability @RonaldBradford #PerconaLive Before +----+-------------+-------+------+---------------+------+---------+------+-------------+

    | id | select_type | table | type | possible_keys | key | key_len | rows | Extra | +----+-------------+-------+------+---------------+------+---------+------+-------------+ | 1 | SIMPLE | i | ref | PRIMARY,u,a,d | u | 4 | 1 | Using where | | 1 | SIMPLE | p | ref | i | i | 4 | 6 | Using where | +----+-------------+-------+------+---------------+------+---------+------+-------------+ After +----+-------------+-------+------+---------------+------+---------+------+-------------+ | id | select_type | table | type | possible_keys | key | key_len | rows | Extra | +----+-------------+-------+------+---------------+------+---------+------+-------------+ | 1 | SIMPLE | i | ref | PRIMARY,u,a,d | u | 4 | 1 | Using where | | 1 | SIMPLE | p | ref | i | i | 4 | 6 | Using where | +----+-------------+-------+------+---------------+------+---------+------+-------------+ Example 5 Zero improvement Saturday, October 1, 16
  53. EffectiveMySQL.com - Performance, Scalability, Site Reliability @RonaldBradford #PerconaLive SELECT p.amt,

    p.reference FROM payment p JOIN invoice i ON i.inv_id = p.inv_id WHERE i.due_date = '2009-10-14' AND i.user_id = 1 AND i.account_id = 10 AND p.amt > 0 ALTER TABLE payment DROP INDEX i, ADD INDEX i (inv_id, amt, reference); Example 5 Identify additional columns in query Add column to index Saturday, October 1, 16
  54. 5 EffectiveMySQL.com - Performance, Scalability, Site Reliability @RonaldBradford #PerconaLive Before

    +----+-------------+-------+------+---------------+------+---------+------+-------------+ | id | select_type | table | type | possible_keys | key | key_len | rows | Extra | +----+-------------+-------+------+---------------+------+---------+------+-------------+ | 1 | SIMPLE | i | ref | PRIMARY,u,a,d | u | 4 | 1 | Using where | | 1 | SIMPLE | p | ref | i | i | 4 | 6 | Using where | +----+-------------+-------+------+---------------+------+---------+------+-------------+ After +----+-------------+-------+------+---------------+------+---------+------+--------------------------+ | id | select_type | table | type | possible_keys | key | key_len | rows | Extra | +----+-------------+-------+------+---------------+------+---------+------+--------------------------+ | 1 | SIMPLE | i | ref | PRIMARY,u,a,d | u | 4 | 1 | Using where | | 1 | SIMPLE | p | ref | i | i | 4 | 6 | Using where; Using index | +----+-------------+-------+------+---------------+------+---------+------+--------------------------+ Example Covering Index Saturday, October 1, 16
  55. EffectiveMySQL.com - Performance, Scalability, Site Reliability @RonaldBradford #PerconaLive SELECT p.amt,

    p.reference FROM payment p JOIN invoice i ON i.inv_id = p.inv_id WHERE i.due_date = '2009-10-14' AND i.user_id = 1 AND i.account_id = 10 AND p.amt > 0 +----+-------------+-------+------+---------------+------+---------+------+-------------+ | id | select_type | table | type | possible_keys | key | key_len | rows | Extra | +----+-------------+-------+------+---------------+------+---------+------+-------------+ | 1 | SIMPLE | i | ref | PRIMARY,u,a,d | u | 4 | 1 | Using where | | 1 | SIMPLE | p | ref | i | i | 4 | 6 | Using where | +----+-------------+-------+------+---------------+------+---------+------+-------------+ CREATE TABLE invoice ( ... PRIMARY KEY pk (inv_id), INDEX u (user_id), INDEX a (account_id), INDEX d (due_date) ) ENGINE=InnoDB; Example 5 Second table in query Saturday, October 1, 16
  56. EffectiveMySQL.com - Performance, Scalability, Site Reliability @RonaldBradford #PerconaLive SELECT p.amt,

    p.reference FROM payment p JOIN invoice i ON i.inv_id = p.inv_id WHERE i.due_date = '2009-10-14' AND i.user_id = 1 AND i.account_id = 10 AND p.amt > 0 ALTER TABLE invoice DROP INDEX u, ADD INDEX u (user_id, account_id); Example 5 Identify additional columns in query Is account_id index needed now? Saturday, October 1, 16
  57. 5 EffectiveMySQL.com - Performance, Scalability, Site Reliability @RonaldBradford #PerconaLive Before

    +----+-------------+-------+------+---------------+------+---------+------+--------------------------+ | id | select_type | table | type | possible_keys | key | key_len | rows | Extra | +----+-------------+-------+------+---------------+------+---------+------+--------------------------+ | 1 | SIMPLE | i | ref | PRIMARY,u,a,d | u | 4 | 1 | Using where | | 1 | SIMPLE | p | ref | i | i | 4 | 6 | Using where; Using index | +----+-------------+-------+------+---------------+------+---------+------+--------------------------+ After +----+-------------+-------+------+---------------+------+---------+------+--------------------------+ | id | select_type | table | type | possible_keys | key | key_len | rows | Extra | +----+-------------+-------+------+---------------+------+---------+------+--------------------------+ | 1 | SIMPLE | i | ref | u | u | 8 | 1 | Using where | | 1 | SIMPLE | p | ref | i | i | 4 | 6 | Using where; Using index | +----+-------------+-------+------+---------------+------+---------+------+--------------------------+ Example Better index usage (4 + 4 bytes) Saturday, October 1, 16
  58. EffectiveMySQL.com - Performance, Scalability, Site Reliability @RonaldBradford #PerconaLive SELECT p.amt,

    p.reference FROM payment p JOIN invoice i ON i.inv_id = p.inv_id WHERE i.due_date = '2009-10-14' AND i.user_id = 1 AND i.account_id = 10 AND p.amt > 0 ALTER TABLE invoice DROP INDEX u, ADD INDEX u (user_id, account_id, due_date); Example 5 Identify additional columns in query Saturday, October 1, 16
  59. 5 EffectiveMySQL.com - Performance, Scalability, Site Reliability @RonaldBradford #PerconaLive Before

    +----+-------------+-------+------+---------------+------+---------+------+--------------------------+ | id | select_type | table | type | possible_keys | key | key_len | rows | Extra | +----+-------------+-------+------+---------------+------+---------+------+--------------------------+ | 1 | SIMPLE | i | ref | u | u | 8 | 1 | Using where | | 1 | SIMPLE | p | ref | i | i | 4 | 6 | Using where; Using index | +----+-------------+-------+------+---------------+------+---------+------+--------------------------+ After +----+-------------+-------+------+---------------+------+---------+------+--------------------------+ | id | select_type | table | type | possible_keys | key | key_len | rows | Extra | +----+-------------+-------+------+---------------+------+---------+------+--------------------------+ | 1 | SIMPLE | i | ref | u | u | 11 | 1 | Using index | | 1 | SIMPLE | p | ref | i | i | 4 | 6 | Using where; Using index | +----+-------------+-------+------+---------------+------+---------+------+--------------------------+ Example Better index usage (4 + 4 + 3 bytes) Covering index Saturday, October 1, 16
  60. 6 EffectiveMySQL.com - Performance, Scalability, Site Reliability @RonaldBradford #PerconaLive VERIFY

    Quantifiable Reproducible 1. Capture 2. Identify 3. Confirm 4. Analyze 5. Optimize 6. Verify Saturday, October 1, 16
  61. 6 EffectiveMySQL.com - Performance, Scalability, Site Reliability @RonaldBradford #PerconaLive +----+---------+----+--------+------+------+-------------+

    | id | select_t| tab| key | key_ | rows | Extra | +----+---------+----+--------+------+------+-------------+ | 1 | PRIMARY | c | statu | 1 | 74 | Using where | | 1 | PRIMARY | e | PRIMA | 4 | 1 | Using where | | 1 | PRIMARY | g | campa | 4 | 1 | Using where | | 10 | DEPENDEN| crb| id_ca | 4 | 253 | Using where | | 9 | DEPENDEN| csb| pub_s | 98 | 1 | Using where | | 8 | DEPENDEN| arb| id_ad | 4 | 901 | Using where | | 7 | DEPENDEN| asb| pub_s | 34 | 1 | Using where | | 6 | DEPENDEN| pm | id_adr | 4 | 42 | Using where | | 5 | DEPENDEN| tgv| searc | 4 | 2 | Using where | | 4 | DEPENDEN| st | id_sc | 4 | 7 | Using where | | 4 | DEPENDEN| t | PRIMA | 4 | 1 | Using where | | 3 | DEPENDEN| k2 | keywo | 302 | 4 | Using where | | 3 | DEPENDEN| gk2| PRIMA | 100 | 1 | Using where | | 2 | DEPENDEN| k1 | keywo | 302 | 3 | Using where | | 2 | DEPENDEN| gk1| PRIMA | 100 | 1 | Using where | +----+---------+----+--------+------+------+-------------+ 15 table join Example 175ms REAL WORLD BEFORE Saturday, October 1, 16
  62. 6 EffectiveMySQL.com - Performance, Scalability, Site Reliability @RonaldBradford #PerconaLive +----+---------+----+--------+------+------+--------------------------+

    | id | select_t| tab| key | key_ | rows | Extra | +----+---------+----+--------+------+------+--------------------------+ | 1 | PRIMARY | e | statu | 1 | 33 | Using where; Using index | | 1 | PRIMARY | c | adver | 4 | 7 | Using where | | 1 | PRIMARY | g | campa | 4 | 1 | Using where | | 10 | DEPENDEN| crb| id_ca | 66 | 1 | Using where | | 9 | DEPENDEN| csb| pub_s | 98 | 1 | Using where | | 8 | DEPENDEN| arb| id_ad | 26 | 1 | Using where | | 7 | DEPENDEN| asb| id_ad | 40 | 1 | Using where; Using index | | 6 | DEPENDEN| pm | id_adr | 12 | 1 | Using index | | 5 | DEPENDEN| tgv| searc | 10 | 1 | Using where; Using index | | 4 | DEPENDEN| st | id_sc | 4 | 7 | Using where; Using index | | 4 | DEPENDEN| t | PRIMA | 4 | 1 | Using where | | 3 | DEPENDEN| k2 | keywo | 302 | 3 | Using where; Using index | | 3 | DEPENDEN| gk2| PRIMA | 100 | 1 | Using where | | 2 | DEPENDEN| k1 | keywo | 302 | 2 | Using where; Using index | | 2 | DEPENDEN| gk1| PRIMA | 100 | 1 | Using where | +----+---------+----+--------+------+------+--------------------------+ 15 table join Example 10ms REAL WORLD AFTER Saturday, October 1, 16
  63. 6 EffectiveMySQL.com - Performance, Scalability, Site Reliability @RonaldBradford #PerconaLive +----+---------+----+--------+------+------+-------------+

    | id | select_t| tab| key | key_ | rows | Extra | +----+---------+----+--------+------+------+-------------+ | 1 | PRIMARY | c | statu | 1 | 74 | Using where | | 1 | PRIMARY | e | PRIMA | 4 | 1 | Using where | | 1 | PRIMARY | g | campa | 4 | 1 | Using where | | 10 | DEPENDEN| crb| id_ca | 4 | 253 | Using where | | 9 | DEPENDEN| csb| pub_s | 98 | 1 | Using where | | 8 | DEPENDEN| arb| id_ad | 4 | 901 | Using where | | 7 | DEPENDEN| asb| pub_s | 34 | 1 | Using where | | 6 | DEPENDEN| pm | id_adr | 4 | 42 | Using where | | 5 | DEPENDEN| tgv| searc | 4 | 2 | Using where | | 4 | DEPENDEN| st | id_sc | 4 | 7 | Using where | | 4 | DEPENDEN| t | PRIMA | 4 | 1 | Using where | | 3 | DEPENDEN| k2 | keywo | 302 | 4 | Using where | | 3 | DEPENDEN| gk2| PRIMA | 100 | 1 | Using where | | 2 | DEPENDEN| k1 | keywo | 302 | 3 | Using where | | 2 | DEPENDEN| gk1| PRIMA | 100 | 1 | Using where | +----+---------+----+--------+------+------+-------------+ 15 table join Example 10ms +----+---------+----+--------+------+--------------------------+ | id | select_t| tab| key | key_ | Extra | +----+---------+----+--------+------+--------------------------+ | 1 | PRIMARY | e | adver | 4 | Using where | | 1 | PRIMARY | c | statu | 1 | Using where; Using index | | 1 | PRIMARY | g | campa | 4 | Using where | | 10 | DEPENDEN| crb| id_ca | 66 | Using where | | 9 | DEPENDEN| csb| pub_s | 98 | Using where | | 8 | DEPENDEN| arb| id_ad | 26 | Using where | | 7 | DEPENDEN| asb| id_ad | 40 | Using where; Using index | | 6 | DEPENDEN| pm | id_adr | 12 | Using index | | 5 | DEPENDEN| tgv| searc | 10 | Using where; Using index | | 4 | DEPENDEN| st | id_sc | 4 | Using where; Using index | | 4 | DEPENDEN| t | PRIMA | 4 | Using where | | 3 | DEPENDEN| k2 | keywo | 302 | Using where; Using index | | 3 | DEPENDEN| gk2| PRIMA | 100 | Using where | | 2 | DEPENDEN| k1 | keywo | 302 | Using where; Using index | | 2 | DEPENDEN| gk1| PRIMA | 100 | Using where | +----+---------+----+--------+------+--------------------------+ REAL WORLD Only added columns to existing indexes. No Code Changes. No Configuration Changes. Saturday, October 1, 16
  64. EffectiveMySQL.com - Performance, Scalability, Site Reliability @RonaldBradford #PerconaLive +----+---------+----+--------+------+------+-------------+ |

    id | select_t| tab| key | key_ | rows | Extra | +----+---------+----+--------+------+------+-------------+ ... | 10 | DEPENDEN| crb| id_ca | 4 | 253 | Using where | .... 6 Example +----+---------+----+--------+------+------+--------------------------+ | id | select_t| tab| key | key_ | rows | Extra | +----+---------+----+--------+------+------+--------------------------+ ... | 10 | DEPENDEN| crb| id_ca | 66 | 1 | Using where | ... Saturday, October 1, 16
  65. EffectiveMySQL.com - Performance, Scalability, Site Reliability @RonaldBradford #PerconaLive ... NOT

    EXISTS ( SELECT 1 FROM crb WHERE crb.id = p.id AND crb.value = 'xyz') ... CREATE TABLE crb( id INT UNSIGNED NOT NULL, value VARCHAR(100) NOT NULL, .... INDEX (id, value(20)) ) .. CHARSET=utf8; Example 6 66 bytes = 4 + (20*3) + 2 partial column index Saturday, October 1, 16
  66. 5 EffectiveMySQL.com - Performance, Scalability, Site Reliability @RonaldBradford #PerconaLive Re-OPTIMIZE

    Change data types Simplify query Removed normalization Other secrets ... Optimizing SQL is an iterative process Saturday, October 1, 16
  67. EffectiveMySQL.com - Performance, Scalability, Site Reliability @RonaldBradford #PerconaLive Repeat as

    necessary 6 VERIFY 175ms to 10ms to 3ms Now 98% faster for 15,000+ qps Client is now more happy for multiple reasons Saturday, October 1, 16
  68. 6 EffectiveMySQL.com - Performance, Scalability, Site Reliability @RonaldBradford #PerconaLive VERIFY

    Cardinal Sin Indexes affect all queries on table i.e. slows down writes VERY, VERY IMPORTANT Saturday, October 1, 16
  69. EffectiveMySQL.com - Performance, Scalability, Site Reliability @RonaldBradford #PerconaLive CONCLUSION 6

    steps to successful SQL review Optimization is an iterative process Table rows, storage engines, configuration & MySQL version affect results over time Indexes are not the only optimization Indexes are not the best optimization C Saturday, October 1, 16
  70. EffectiveMySQL.com - Performance, Scalability, Site Reliability @RonaldBradford #PerconaLive 0.00010109 SELECT

    ip FROM ... 0.00005198 SELECT name, value FROM ... 0.00005984 SELECT id, status, ... 0.17592907 SELECT g.id, c.id, ... 0.00047803 SELECT DISTINCT id_c FROM camp... 0.00741315 SELECT DISTINCT id_c FROM camp.. 0.00058198 SELECT id_c FROM cr ... 0.00161815 SELECT id_c FROM cr ... 0.00032806 SELECT id_c FROM cr ... 0.00007200 SELECT DISTINCT id_a FROM arb ... 0.00005412 SELECT DISTINCT id_a FROM asw ... 0.00004697 SELECT id_adv FROM arw 0.00004601 SELECT id_adv FROM arw 0.00009012 SELECT gk.id, k.value ... 0.00009084 SELECT gk.id, k.value ... 0.00006318 SELECT gk.id, k.value ... 0.00005794 SELECT gk.id, k.value ... 0.00005603 SELECT gk.id, k.value ... 1 architecture 3 statements Professional re-architecture consulting 24 statements Saturday, October 1, 16
  71. EffectiveMySQL.com - Performance, Scalability, Site Reliability @RonaldBradford #PerconaLive This success

    story ? One day of consulting Saved client 50% in daily hosting $500 p.d = $180,000 p.y. CONCLUSION Saturday, October 1, 16
  72. [email protected] #Hire Me Now available for professional consulting MySQL /

    Linux / PHP / Apache / Cloud Audit / Performance / DBA / Scalability / B&R Planning Improving performance – A full stack problem From 2,000ms to 35ms response time and no impact with100x load in Flash Sales Recent blog post from client work (Mar 2015) -- http://j.mp/FullStackPerf Saturday, October 1, 16