mysql - Most efficient indexes to use for a query using WHERE and GROUP BY? -
mysql - Most efficient indexes to use for a query using WHERE and GROUP BY? -
i have table ~7 1000000 rows continually running queries of sort:
select myfield, count(*) mytable myfield2='constantvalue' , myfield not in ( select field anothertable) , timestamp >= [arbitrarytimestamp] grouping myfield; cardinalities above fields:
myfield = 40,000 distinct values. timestamp = distinct, around 7 1000000 distinct values. myfield2 = 2 distinct values. field anothertable = 50 distinct values.as expected, runs terribly slow, , using explain tells me using where; using temporary; using filesort.
i improve efficiency of these queries adding index table, i'm not sure best way is.
should add together index on myfield? , index on timestamp? both? combined index on both?
also, there else can speed these type of queries?
in add-on suggestions mike , dkamins, having sub-query might bottleneck. restructure left bring together , apply based on null (ensure "anothertable" has index on "field" join
select myfield, count(*) mytable left bring together anothertable on mytable.myfield = anothertable.field myfield2='constantvalue' , anothertable.field null , timestamp >= [arbitrarytimestamp] grouping myfield; i have index based on (myfield2, timestamp, myfield). myfield2 specific qualifier, don't bother including other crud within focus... that, looking @ specific timeframe... that, having myfield help optimize groupby. first 2 parts of key/where, used filter criteria, rest remain in proper order grouping.
mysql sql optimization indexing query-optimization
Comments
Post a Comment