Вы находитесь на странице: 1из 2

Aworkaroundfortheperformanceproblemsof

TEMPTABLEviews
Postedon:
May19,2010|
By:

MySQLsupportstwodifferentalgorithmsforviews:theMERGEalgorithmandtheTEMPTABLE
algorithm.Thesetwoalgorithmsdiffergreatly.AviewwhichusestheMERGEalgorithmcanmerge
filterconditionsintotheviewqueryitself.Thishassignificantperformanceadvantagesover
TEMPTABLEviews.AviewwhichusestheTEMPTABLEalgorithmwillhavetocomputetheentire
contentsoftheviewbeforeanyfilterisapplied.Computingtheentirecontentsrequiresatemporary
tableandmanymorerowsmaybeaccessedthanotherwisewouldhadthefilterbeenmergedinto
theviewquery.
Aslongasaviewavoidsaggregation,DISTINCT,GROUPBY,ORDERBY,etc,thenitcanusethe
MERGEalgorithm.Unfortunately,thismeansthatviewsofanysignificantcomplexitywillalmost
alwaysusetheTEMPTABLEalgorithm.
Thisblogpostdemonstratesaworkaroundthatallowstheconvenienceofcomplexviews,including
thosewithaggregation(thatisviewswhichusetheTEMPTABLEalgorithm)withsomeofthe
performanceadvantagesofMERGEalgorithmviews.
Asademonstration,considerthefollowingtablewhichcontainsacombinationofintegervalues.
Thereareenoughvaluesinthetablesuchthatascanofallrowstakesasecondortwo.
Shell
mysql>explainselect*fromv2wherec1=100
+++++++++++
|id|select_type|table|type|possible_keys|key|key_len|ref|rows|Extra|
+++++++++++
|1|PRIMARY|<derived2>|ALL|NULL|NULL|NULL|NULL|1310720|Usingwhere
|
|2|DERIVED|t2|index|NULL|c1|5|NULL|2785914|Usingindex|
+++++++++++
2rowsinset(1.76sec)
Nowtotheactualworkaround.MySQLwontmergequeryfiltersintotheviewdefinition,butwecan

modifythephysicaldefinitionoftheviewinawaywhichprovidesadifferentmeansofprovidingfilter
conditionstothequery.
Letsstartbycreatingaparametertable.Thistablemightbeviewspecificoryoucouldconstructitin
suchawaythatitcanbesharedbetweenviews.Thisexampleissomewhereinbetweenthosetwo
extremes:
Shell
createalorithm=mergeviewv2as
selectc1,count(*)
fromv1
joinparamsponp.connection_id=connection_id()
andp.view_name='test.v2'
andp.param1_val=v1.c1
groupbyc1
QueryOK,0rowsaffected,1warning(0.01sec)
Therearetwoimportantthingstoconsiderhere.First,noticetheuseofconnection_id()intheview
definition.Thismakessurethattheonlyrowsexaminedintheparamstablearethosewhichbelong
tothisconnection.Whenthetableisinsertedtoo(seebelow)theconnection_id()functionwillbe
usedtogeneratethevaluefortheconnection_idcolumn.Alsonoticetherestrictionthatv1.c1=
p.param1_val.Thisrestrictsthequerytoonlythoserowswherev1.c1matchesthevalueinthe
paramstable.
PlaceparametersintotheparamstableusingtheREPLACEstatement:
Shell
mysql>explainselect*fromv2wherec1=10
+++++++++++
|id|select_type|table|type|possible_keys|key|key_len|ref|rows|Extra|
+++++++++++
|1|PRIMARY|<derived2>|system|NULL|NULL|NULL|NULL|1
||
|2|DERIVED|p|const|PRIMARY|PRIMARY|260||1||
|2|DERIVED|t2|ref|c1|c1|5|const|130|UsingwhereUsingindex|
+++++++++++
3rowsinset(0.01sec)
Inreview,IlikeviewsfortheirconvenientencapsulationofSQLlogicthatwouldusuallyhavetobe
repeatedinSQLstatementsthroughouttheapplicationotherwise.Theconveniencesometimes
comeswithhighcostthough,particularlywhentheTEMPTABLEalgorithmisused.Thetechnique
outlinedinthispostcanbeusedtomakeatradeoffbetweenincreasedquerycomplexityfor
improvedviewperformance.

Вам также может понравиться