Learn how to Analyze & Tune MySQL Queries for Better Performance · Learn how to Analyze & Tune...

Post on 05-Sep-2020

14 views 0 download

Transcript of Learn how to Analyze & Tune MySQL Queries for Better Performance · Learn how to Analyze & Tune...

©2016Percona©2016Percona©2016Percona©2016Percona©2016Percona©2016Percona©2016Percona�1

BradleyMickel

LearnhowtoAnalyze&TuneMySQLQueriesforBetterPerformanceToolsandtechniquesforfasterqueries

MySQLDBAThursdayJune21st

©2016Percona©2016Percona©2016Percona©2016Percona©2016Percona©2016Percona©2016Percona�2

ToolsOverviewofcommontoolsusedtoidentifyqueryoptimizations

©2018Percona

EXPLAIN

�3

EXPLAINSELECT*FROMstaff;

EXPLAINSELECT*FROMstaff\G

©2018Percona

JSONFORMATEXPLAIN

�4

EXPLAINFORMAT=JSONSELECTcount(1)FROMsakila.rentalWHERElast_update>‘2006-02-1800:00:00'\G

©2018Percona

EXPLAINFields

�5

Field JSONFIELD Descriptionid select_id ThesequentialnumberoftheSELECTwithinthequeryselect_type TheTypeofselectbeingperformed.table table_name Thename,oraliasofthetablerelatedtothisstepofthequeries

partitions partitions Thepartitionsfromwhichrecordswouldbematchedbythequery.NULLfornonpartitionedtables

type access_type Thetypeofaccessbeingusedtoretrievedataorjointablespossible_keys possible_keys Whichindexesarebeingreviewedforuseinthequerykey key Theindexwhichwillbeusedforthequery

key_len key_length ThelengthofthekeythatMySQLhasdecidedtouse.Thisenablesyoutodeterminehowmanypartsofacompositeindexarebeingused.*

ref ref Shows which columns or constants are compared to the index named in the key column to select rows from the table

rows rows The number of rows MySQL estimates will be need to be reviewedfiltered filtered Anestimatedpercentageoftablerowsthatwillbefilteredbythecondition.100%meansall

therowsreturnedbytheconditionwillbejoinedwithprevioustablesextra Additionalinformationrelatedtotheoptimizer’schosenpath.

©2018Percona

JSONFORMATEXPLAIN

�6

▪AdditionalFields:• cost_Info

• query_cost• used_key_parts

©2018Percona

JSONFORMATEXPLAIN

�7

▪AdditionalFields:• rows_examined_per_scan• rows_examined_per_join• cost_info

• read_cost• eval_cost• prefix_cost• data_read_per_join

• used_columns

©2018Percona

AccessTypes

�8

Type Descriptionconstant Thetablehasatmostonematchingrowwhichisreadatthestartofthequerysystem Atypeofconstanttableusedforsystemstableseq_ref Usedfortablejoinswitha1:1relationship.RequiresthejoiningindexbyaPrimaryorUniqueKey

ref Allrowswithmatchingindexvaluesarereadfromthistableforeachcombinationofrowsfromtheprevioustables

fulltext AfulltextIndexisbeingusedref_or_null Similartoref,butwillfindnullvaluesinadditiontotheselectedvalueindex_merge IndexMergeisbeingusedforthejoinunique_subquery Replaceseq_refforcertainsubqueriesusingINindex_subquery Similartounique_subquerybutforwhentheindexdoesnothaveauniqueconstraint

rangeOnly rows that are in a given range are retrieved, using an index to select the rows. The key column in the output row indicates which index is used. The key_len contains the longest key part that was used. The ref column is NULL for this type.

index Theentireindexisscannedall Afulltablescanisrequired

©2018Percona

PerformanceSchemaProfilingSELECTevent_id,sql_textFROMperformance_schema.events_statements_history_longWHEREsql_textlike‘%select*fromcity%’;

�9

©2018Percona

PerformanceSchemaProfilingcont.

SELECTevent_nameASStage,round(timer_wait/pow(10,12),6)ASDurationFROMevents_stages_history_longWHEREnesting_event_id=54;

�10

useperformance_schema;

©2018Percona

Pt-query-digest

�11

▪AnalysesMySQLqueriesfromtheslow,general,andbinarylogs▪Groupsqueriesbyfingerprint▪providesaverageruntimeinformation

©2016Percona©2016Percona©2016Percona©2016Percona©2016Percona©2016Percona©2016Percona�12

IndexesIndexconceptsandlimitations

©2018Percona

BalancedTreeIndex

�13

©2018Percona

CompositeBalancedTreeIndex

�14

©2018Percona

PrimaryKeyRequirementsandconstraints▪Field(s)MUSTbeUNIQUE▪NULLisnotvalid▪Only1pertable▪Canbeasingleormultiplefields

Controlshowdataisstoredphysically

IsutilizedaspartofALLsecondaryindexes�15

©2018Percona�16

IndexImpactSELECTSQL_NO_CACHEcount(1)FROMsakila.rentalWHERElast_update>'2006-02-1800:00:00'\G

©2018Percona�17

IndexImpactcontSELECTcount(1)FROMsakila.rentalWHERElast_update>'2006-02-1800:00:00'\G

©2018Percona�18

MultipleTablesSELECTf.rating,sum(p.amount)asincomeFROMfilmfJOINinventoryiONi.film_id=f.film_idJOINrentalrONr.inventory_id=i.inventory_idJOINpaymentpONp.rental_id=r.rental_idGROUPBYf.ratingORDERBYincomeDESC;

©2018Percona�19

MultipleTablesSELECTf.rating,sum(p.amount)asincomeFROMfilmfJOINinventoryiONi.film_id=f.film_idJOINrentalrONr.inventory_id=i.inventory_idJOINpaymentpONp.rental_id=r.rental_idGROUPBYf.ratingORDERBYincomeDESC;

©2018Percona

IndexLimitationsFunctions▪Aggregates

• SUM,AVG• DoesnotincludeMIN,MAX▪MultipleRangescans

• multipleORstatements• multiplelessthanorgreaterthan▪CharacterSetDifferences▪DataTypeDifferences

�20

©2018Percona�21

MultipleRangeScansSELECTcount(1)FROMrentalWHEREcustomer_id=148ANDreturn_dateBETWEEN'2005-08-2900:00:00'AND'2005-09-0523:59:59'ANDlast_updateBETWEEN'2006-02-1500:00:00'AND'2006-02-1523:59:59';

Addacompositeindexoncustomer_id,return_date,last_update

©2018Percona�22

MultipleRangeScans

©2018Percona�23

MultipleRangeScansSELECTCOUNT(1)FROMrentalWHEREcustomer_id=148AND(return_dateBETWEEN'2005-08-2900:00:00'AND'2005-09-0523:59:59'ORlast_updateBETWEEN'2006-02-1500:00:00'AND'2006-02-1523:59:59');

©2018Percona�24

MultipleRangeScansSELECTCOUNT(1)FROMrentalWHEREcustomer_id=148ANDreturn_dateBETWEEN'2005-08-2900:00:00’AND'2005-09-0523:59:59'UNIONSELECTCOUNT(1)FROMrentalWHEREcustomer_id=148ANDlast_updateBETWEEN'2006-02-1500:00:00'AND'2006-02-1523:59:59';

Indexesarereadlefttorightsoanewindexisneededoncustomer_id,last_update

©2018Percona�25

MultipleRangeScans

OR

UNION

©2018Percona�26

Referenceshttps://dev.mysql.com/doc/sakila/en/sakila-installation.htmlhttps://dev.mysql.com/doc/refman/5.7/en/explain-output.html#explain-join-typeshttps://dev.mysql.com/doc/mysql-perfschema-excerpt/5.7/en/performance-schema-query-profiling.htmlhttps://dev.mysql.com/doc/refman/5.7/en/general-thread-states.htmlhttps://www.cs.usfca.edu/~galles/visualization/BPlusTree.htmlhttps://www.percona.com/software/database-tools/percona-toolkithttps://bugs.mysql.com/bug.php?id=83062