Why isn't my index being used?

This is a discussion on Why isn't my index being used? within the MySQL Database forums, part of the Database Forums category; Maybe this is a naïve question but I'm trying to figure out why my index is not being ...


Go Back   Usenet Forums > Database Forums > MySQL Database

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 03-03-2008
Israel
 
Posts: n/a
Default Why isn't my index being used?


Maybe this is a naïve question but I'm trying to figure out why my
index is not being used during one query but it is on another with
slightly different numbers. Here are the table definitions in question
(with only the important information shown for brevity):

CREATE TABLE `parameter` (
`Id` int(10) unsigned NOT NULL auto_increment,
PRIMARY KEY (`Id`),
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

CREATE TABLE `parameterlog` (
`ParameterId` int(10) unsigned NOT NULL default '0',
`SampleTimestamp` double NOT NULL default '0',
`Data` varchar(50) NOT NULL default '',
KEY `FK_parameterdata_Parameter` (`ParameterId`),
KEY `ParameterData_TimestampIndex` USING BTREE (`SampleTimestamp`),
CONSTRAINT `FK_parameterdata_Parameter` FOREIGN KEY (`ParameterId`)
REFERENCES `parameter` (`Id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;


If I run this query:
select pl.*
from parameterlog pl
where
pl.ParameterId = 59 and
pl.SampleTimestamp >= 39505.2280861227 and
pl.SampleTimestamp <= 39505.9780861227;

It executes quickly (0.03 seconds) and explain outputs:
select_type = SIMPLE
table = pl
type = ref
possible_keys = FK_parameterdata_Parameter,
ParameterLog_TimeStampIndex
key = ParameterLog_TimeStampIndex
key_len = 8
ref = null
rows = 4332
extra = Using where

If I run this query:
select pl.*
from parameterlog pl
where
pl.ParameterId = 59 and
pl.SampleTimestamp >= 39504.2280861227 and
pl.SampleTimestamp <= 39504.9780861227;

It executes VERY slowly (6.6 seconds) and explain outputs:
select_type = SIMPLE
table = pl
type = ref
possible_keys = FK_parameterdata_Parameter,
ParameterLog_TimeStampIndex
key = FK_parameterdata_Parameter
key_len = 4
ref = const
rows = 28832
extra = Using where

So in the first case it decided to use the index on the timestamp but
in the second case it decided to use the FK on the parameter index. I
don't quite get how it's making this decision or what I need to do
always use the timestamp index in this query.

Reply With Quote
  #2 (permalink)  
Old 03-03-2008
Israel
 
Posts: n/a
Default Re: Why isn't my index being used?

I should also note that if I remove the FK from parameterlog to
parameter then it uses the other index both times.
Reply With Quote
  #3 (permalink)  
Old 03-03-2008
Rik Wasmus
 
Posts: n/a
Default Re: Why isn't my index being used?

On Mon, 03 Mar 2008 18:16:05 +0100, Israel <israeldiperi@hotmail.com>
wrote:
> Maybe this is a naïve question but I'm trying to figure out why my
> index is not being used during one query but it is on another with
> slightly different numbers. Here are the table definitions in question
> (with only the important information shown for brevity):
>
> CREATE TABLE `parameter` (
> `Id` int(10) unsigned NOT NULL auto_increment,
> PRIMARY KEY (`Id`),
> ) ENGINE=InnoDB DEFAULT CHARSET=latin1;
>
> CREATE TABLE `parameterlog` (
> `ParameterId` int(10) unsigned NOT NULL default '0',
> `SampleTimestamp` double NOT NULL default '0',
> `Data` varchar(50) NOT NULL default '',
> KEY `FK_parameterdata_Parameter` (`ParameterId`),
> KEY `ParameterData_TimestampIndex` USING BTREE (`SampleTimestamp`),
> CONSTRAINT `FK_parameterdata_Parameter` FOREIGN KEY (`ParameterId`)
> REFERENCES `parameter` (`Id`)
> ) ENGINE=InnoDB DEFAULT CHARSET=latin1;
>
>
> If I run this query:
> select pl.*
> from parameterlog pl
> where
> pl.ParameterId = 59 and
> pl.SampleTimestamp >= 39505.2280861227 and
> pl.SampleTimestamp <= 39505.9780861227;
>
> It executes quickly (0.03 seconds) and explain outputs:
> select_type = SIMPLE
> table = pl
> type = ref
> possible_keys = FK_parameterdata_Parameter,
> ParameterLog_TimeStampIndex
> key = ParameterLog_TimeStampIndex
> key_len = 8
> ref = null
> rows = 4332
> extra = Using where
>
> If I run this query:
> select pl.*
> from parameterlog pl
> where
> pl.ParameterId = 59 and
> pl.SampleTimestamp >= 39504.2280861227 and
> pl.SampleTimestamp <= 39504.9780861227;
>
> It executes VERY slowly (6.6 seconds) and explain outputs:
> select_type = SIMPLE
> table = pl
> type = ref
> possible_keys = FK_parameterdata_Parameter,
> ParameterLog_TimeStampIndex
> key = FK_parameterdata_Parameter
> key_len = 4
> ref = const
> rows = 28832
> extra = Using where
>
> So in the first case it decided to use the index on the timestamp but
> in the second case it decided to use the FK on the parameter index. I
> don't quite get how it's making this decision or what I need to do
> always use the timestamp index in this query.


As to the why, I can't help you, however, you can tell MySQL which
index/key to use:
<http://dev.mysql.com/doc/refman/5.1/en/index-hints.html>
--
Rik Wasmus
Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
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

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +1. The time now is 07:02 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0