dbTalk Databases Forums  

Index selection issue

comp.databases.mysql comp.databases.mysql


Discuss Index selection issue in the comp.databases.mysql forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
AT
 
Posts: n/a

Default Index selection issue - 07-19-2006 , 11:43 AM






Hi,
I have an innodb table with about 8 million rows of data that has
several indexes defined. While performing a select MySQL almost always
selects the most efficient index but it will occasionally select an
index that is not efficient. How can I determine what is going on
behind the scenes in MySQL that makes it determine which index to use.
I have been using explain with my select statement and I can see that
the index used is changing but I do not know why. Here is an example.

Efficient index:
mysql> explain SELECT count(*) from mytable WHERE recdate BETWEEN
20060701000000 AND 20060710235959 AND cust=999999\G
*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: mytable
type: range
possible_keys: recdate_i,cust_i
key: recdate_i
key_len: 3
ref: NULL
rows: 67884
Extra: Using where
1 row in set (0.00 sec)

Not efficient index:
mysql> explain SELECT count(*) from mytable WHERE recdate BETWEEN
20060601000000 AND 20060610235959 AND cust=999999\G
*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: qcal
type: ref
possible_keys: recdate_i,cust_i
key: cust_i
key_len: 3
ref: const
rows: 719680
Extra: Using where
1 row in set (0.00 sec)

The only difference in the queries is the date range (July in one and
June in another). Both date ranges have very similar total records to
sort through as well but as you can see, the 2nd query decides to look
through 700k + rows while the 1st only looks through 67k + rows.

Thanks


Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.