Embed presentation
























































![Slow Log StatisticsSET GLOBAL long_query_time = 0;SET GLOBAL log_slow_verbosity = ‘full’; This was executed on a machine with entirely cold# Time: 100924 13:58:47# User@Host: root[root] @ localhost [] caches.# Thread_id: 10 Schema: imdb Last_errno: 0 Killed: 0# Query_time: 399.563977 Lock_time: 0.000110 Rows_sent: 1 Rows_examined: 46313608Rows_affected: 0 Rows_read: 1# Bytes_sent: 131 Tmp_tables: 1 Tmp_disk_tables: 1 Tmp_table_sizes: 25194923# InnoDB_trx_id: 1403# QC_Hit: No Full_scan: Yes Full_join: No Tmp_table: Yes Tmp_table_on_disk: Yes# Filesort: Yes Filesort_on_disk: Yes Merge_passes: 5# InnoDB_IO_r_ops: 1064749 InnoDB_IO_r_bytes: 17444847616 InnoDB_IO_r_wait: 26.935662# InnoDB_rec_lock_wait: 0.000000 InnoDB_queue_wait: 0.000000# InnoDB_pages_distinct: 65329SET timestamp=1285336727;select STRAIGHT_JOIN count(*) as c, person_id FROM cast_info FORCE INDEX(person_id)INNER JOIN title ON (cast_info.movie_id=title.id) WHERE title.kind_id = 1 GROUP BYcast_info.person_id ORDER by c DESC LIMIT 1; www.percona.com 57](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fpldc2012-mysql-query-optimization-120203025134-phpapp01%2f75%2fPercona-Live-2012PPT-MySQL-Query-optimization-57-2048.jpg&f=jpg&w=240)











































































![pt-query-digest# Query 1: 17.06 QPS, 1.92x concurrency, ID 0x3928FBFF36663F33 at byte 141746# This item is included in the report because it matches --limit.# Scores: Apdex = 1.00 [1.0], V/M = 0.03# Time range: 2010-11-29 09:14:30.052415 to 09:26:11.914796# Attribute pct total min max avg 95% stddev median# ============ === ======= ======= ======= ======= ======= ======= =======# Count 1 11976# Exec time 62 1350s 25ms 395ms 113ms 219ms 54ms 91ms# Rows affecte 0 39 0 35 0.00 0 0.32 0# Query size 23 28.75M 2.46k 2.46k 2.46k 2.38k 0 2.38k# Warning coun 11 51.51k 0 12.80k 4.40 0 233.99 0# Boolean:# No index use 99% yes, 0% no# String:# Databases# Errors none (273/99%), #1064 (1/0%)# Hosts 172.20.101.178# Users dbuser www.percona.com 122](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fpldc2012-mysql-query-optimization-120203025134-phpapp01%2f75%2fPercona-Live-2012PPT-MySQL-Query-optimization-133-2048.jpg&f=jpg&w=240)

![# Item 1: 3.41 QPS, 0.97x concurrency, ID 0xABCE5AD2A2DD1BA1 at byte 28812466# This item is included in the report because it matches --limit.# Scores: Apdex = 0.97 [1.0], V/M = 19.02## mk-query-digest Query_time sparkline: | ^______| Time range: 2011-04-05 16:12:13 to 16:14:45# Attribute pct total min max avg 95% stddev median# ============ === ======= ======= ======= ======= ======= ======= =======# Count 0 519# Exec time 2 148s 11us 33s 285ms 53ms 2s 26us# Lock time 0 5ms 0 334us 9us 66us 32us 0# Rows sent 0 41 0 1 0.08 0.99 0.27 0# Rows examine 1 4.97M 0 445.49k 9.80k 5.73k 49.33k 0# Rows affecte 0 2 0 1 0.00 0 0.06 0# Rows read 1 2.01M 0 250.47k 3.96k 1.96 27.94k 0.99# Bytes sent 0 241.20k 11 8.01k 475.89 918.49 689.98 258.32# Merge passes 0 0 0 0 0 0 0 0# Tmp tables 0 15 0 1 0.03 0 0.17 0# Tmp disk tbl 0 3 0 1 0.01 0 0.08 0# Tmp tbl size 0 4.78k 0 4.78k 9.43 0 211.60 0# Query size 0 100.95k 19 2.71k 199.17 363.48 206.60 151.03# InnoDB:# IO r bytes 0 0 0 0 0 0 0 0# IO r ops 0 0 0 0 0 0 0 0# IO r wait 0 0 0 0 0 0 0 0# pages distin 1 67.99k 0 10.64k 1.26k 3.88k 2.47k 31.70# queue wait 0 0 0 0 0 0 0 0# rec lock wai 0 0 0 0 0 0 0 0# Boolean:# Filesort 0% yes, 99% no# Full scan 7% yes, 92% no# QC Hit 78% yes, 21% no# Tmp table 2% yes, 97% no# Tmp table on 0% yes, 99% no www.percona.com 124](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fpldc2012-mysql-query-optimization-120203025134-phpapp01%2f75%2fPercona-Live-2012PPT-MySQL-Query-optimization-135-2048.jpg&f=jpg&w=240)




The document discusses techniques for optimizing MySQL queries. It begins by explaining how to use EXPLAIN to view a query's execution plan and identify opportunities for improvement. Examples demonstrate how adding appropriate indexes can speed up queries by reducing the number of rows examined. The use of composite indexes, covering indexes, and index column order are also addressed. More advanced profiling techniques are presented to further analyze query performance beyond what EXPLAIN shows.
























































![Slow Log StatisticsSET GLOBAL long_query_time = 0;SET GLOBAL log_slow_verbosity = ‘full’; This was executed on a machine with entirely cold# Time: 100924 13:58:47# User@Host: root[root] @ localhost [] caches.# Thread_id: 10 Schema: imdb Last_errno: 0 Killed: 0# Query_time: 399.563977 Lock_time: 0.000110 Rows_sent: 1 Rows_examined: 46313608Rows_affected: 0 Rows_read: 1# Bytes_sent: 131 Tmp_tables: 1 Tmp_disk_tables: 1 Tmp_table_sizes: 25194923# InnoDB_trx_id: 1403# QC_Hit: No Full_scan: Yes Full_join: No Tmp_table: Yes Tmp_table_on_disk: Yes# Filesort: Yes Filesort_on_disk: Yes Merge_passes: 5# InnoDB_IO_r_ops: 1064749 InnoDB_IO_r_bytes: 17444847616 InnoDB_IO_r_wait: 26.935662# InnoDB_rec_lock_wait: 0.000000 InnoDB_queue_wait: 0.000000# InnoDB_pages_distinct: 65329SET timestamp=1285336727;select STRAIGHT_JOIN count(*) as c, person_id FROM cast_info FORCE INDEX(person_id)INNER JOIN title ON (cast_info.movie_id=title.id) WHERE title.kind_id = 1 GROUP BYcast_info.person_id ORDER by c DESC LIMIT 1; www.percona.com 57](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fpldc2012-mysql-query-optimization-120203025134-phpapp01%2f75%2fPercona-Live-2012PPT-MySQL-Query-optimization-57-2048.jpg&f=jpg&w=240)











































































![pt-query-digest# Query 1: 17.06 QPS, 1.92x concurrency, ID 0x3928FBFF36663F33 at byte 141746# This item is included in the report because it matches --limit.# Scores: Apdex = 1.00 [1.0], V/M = 0.03# Time range: 2010-11-29 09:14:30.052415 to 09:26:11.914796# Attribute pct total min max avg 95% stddev median# ============ === ======= ======= ======= ======= ======= ======= =======# Count 1 11976# Exec time 62 1350s 25ms 395ms 113ms 219ms 54ms 91ms# Rows affecte 0 39 0 35 0.00 0 0.32 0# Query size 23 28.75M 2.46k 2.46k 2.46k 2.38k 0 2.38k# Warning coun 11 51.51k 0 12.80k 4.40 0 233.99 0# Boolean:# No index use 99% yes, 0% no# String:# Databases# Errors none (273/99%), #1064 (1/0%)# Hosts 172.20.101.178# Users dbuser www.percona.com 122](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fpldc2012-mysql-query-optimization-120203025134-phpapp01%2f75%2fPercona-Live-2012PPT-MySQL-Query-optimization-133-2048.jpg&f=jpg&w=240)

![# Item 1: 3.41 QPS, 0.97x concurrency, ID 0xABCE5AD2A2DD1BA1 at byte 28812466# This item is included in the report because it matches --limit.# Scores: Apdex = 0.97 [1.0], V/M = 19.02## mk-query-digest Query_time sparkline: | ^______| Time range: 2011-04-05 16:12:13 to 16:14:45# Attribute pct total min max avg 95% stddev median# ============ === ======= ======= ======= ======= ======= ======= =======# Count 0 519# Exec time 2 148s 11us 33s 285ms 53ms 2s 26us# Lock time 0 5ms 0 334us 9us 66us 32us 0# Rows sent 0 41 0 1 0.08 0.99 0.27 0# Rows examine 1 4.97M 0 445.49k 9.80k 5.73k 49.33k 0# Rows affecte 0 2 0 1 0.00 0 0.06 0# Rows read 1 2.01M 0 250.47k 3.96k 1.96 27.94k 0.99# Bytes sent 0 241.20k 11 8.01k 475.89 918.49 689.98 258.32# Merge passes 0 0 0 0 0 0 0 0# Tmp tables 0 15 0 1 0.03 0 0.17 0# Tmp disk tbl 0 3 0 1 0.01 0 0.08 0# Tmp tbl size 0 4.78k 0 4.78k 9.43 0 211.60 0# Query size 0 100.95k 19 2.71k 199.17 363.48 206.60 151.03# InnoDB:# IO r bytes 0 0 0 0 0 0 0 0# IO r ops 0 0 0 0 0 0 0 0# IO r wait 0 0 0 0 0 0 0 0# pages distin 1 67.99k 0 10.64k 1.26k 3.88k 2.47k 31.70# queue wait 0 0 0 0 0 0 0 0# rec lock wai 0 0 0 0 0 0 0 0# Boolean:# Filesort 0% yes, 99% no# Full scan 7% yes, 92% no# QC Hit 78% yes, 21% no# Tmp table 2% yes, 97% no# Tmp table on 0% yes, 99% no www.percona.com 124](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fpldc2012-mysql-query-optimization-120203025134-phpapp01%2f75%2fPercona-Live-2012PPT-MySQL-Query-optimization-135-2048.jpg&f=jpg&w=240)



