MySQL Forums
Forum List  »  Optimizer & Parser

sometimes, mysql don't use an existing index
Posted by: Bart Peeters
Date: September 04, 2006 12:17PM

Hello,

I'm running a mysql 4.0.8 database. It is a production database.
We have a table with more than 50 miljon records in it. And we have slow queries. (minutes)

In the first sql, you can see that no index is used, type ALL.
But I have a combined index, timestamp, customerid.

mysql> explain SELECT...... FROM trace LEFT JOIN terminal ON trace.terminalID = terminal.terminalID LEFT JOIN traceproperty ON trace.id = traceproperty.trace AND traceproperty.propkey = 'ATY' WHERE trace.customerID = 'A2808080808080808080808080808A80' AND trace.timestamp >= 1118872800000;
+---------------+--------+----------------------------+----------------------------+---------+------------------+--------+-------------+
| table | type | possible_keys | key | key_len | ref | rows | Extra |
+---------------+--------+----------------------------+----------------------------+---------+------------------+--------+-------------+
| trace | ALL | timeStamp_customerID_idx | NULL | NULL | NULL | 199713 | Using where |
| terminal | eq_ref | PRIMARY | PRIMARY | 32 | trace.terminalID | 1 | Using index |
| traceproperty | ref | fk_traceproperty_trace_idx | fk_traceproperty_trace_idx | 33 | trace.id | 2 | |
+---------------+--------+----------------------------+----------------------------+---------+------------------+--------+-------------+

The second sql, the only difference, the timestamp between two values.
And now a index is used. (timeStamp_customerID_idx)

mysql> explain SELECT...... FROM trace LEFT JOIN terminal ON trace.terminalID = terminal.terminalID LEFT JOIN traceproperty ON trace.id = traceproperty.trace AND traceproperty.propkey = 'ATY' WHERE trace.customerID = 'A2808080808080808080808080808A80' AND trace.timestamp >= 1118872800000 AND trace.timestamp < 1119564000000;
+---------------+--------+----------------------------+----------------------------+---------+------------------+------+-------------+
| table | type | possible_keys | key | key_len | ref | rows | Extra |
+---------------+--------+----------------------------+----------------------------+---------+------------------+------+-------------+
| trace | range | timeStamp_customerID_idx | timeStamp_customerID_idx | 41 | NULL | 10 | Using where |
| terminal | eq_ref | PRIMARY | PRIMARY | 32 | trace.terminalID | 1 | Using index |
| traceproperty | ref | fk_traceproperty_trace_idx | fk_traceproperty_trace_idx | 33 | trace.id | 2 | |
+---------------+--------+----------------------------+----------------------------+---------+------------------+------+-------------+

Tables:

| trace | CREATE TABLE `trace` (
`id` char(32) NOT NULL default '',
`latitude` int(11) default NULL,
`longitude` int(11) default NULL,
`timeStamp` bigint(20) NOT NULL default '0',
`terminalID` char(32) default NULL,
`customerID` char(32) default NULL,
`mileage` int(11) default NULL,
`creationtime` bigint(20) NOT NULL default '0',
`type` int(11) default NULL,
`heading` smallint(6) default NULL,
`speed` smallint(6) default NULL,
PRIMARY KEY (`id`),
KEY `terminalID_timestamp_idx` (`terminalID`,`timeStamp`),
KEY `timeStamp_customerID_idx` (`timeStamp`,`customerID`),
KEY `fk_trace_type_idx` (`type`)
) TYPE=InnoDB |

| terminal |CREATE TABLE `terminal` (
`terminalID` varchar(32) NOT NULL default '',
`serialid` varchar(64) default NULL,
`name` varchar(100) binary default NULL,
`ref` varchar(100) binary default NULL,
`terminaltype` int(11) NOT NULL default '0',
`partner` varchar(32) default NULL,
`customer` varchar(32) default NULL,
`timestamp` bigint(20) NOT NULL default '0',
`phone` varchar(64) default NULL,
`configurationgroup` varchar(32) default NULL,
`configured` tinyint(4) NOT NULL default '0',
`secondserialid` varchar(64) default NULL,
PRIMARY KEY (`terminalID`),
KEY `terminal_idx21` (`partner`),
KEY `terminal_idx22` (`customer`),
KEY `terminal_idx4` (`ref`),
KEY `fk_terminal_configurationgroup_idx` (`configurationgroup`),
CONSTRAINT `fk_terminal_configurationgroup` FOREIGN KEY (`configurationgroup`) REFERENCES `configurationgroup` (`id`),
CONSTRAINT `FK_terminal_customer` FOREIGN KEY (`customer`) REFERENCES `customer` (`id`) ON DELETE SET NULL,
CONSTRAINT `FK_terminal_partner` FOREIGN KEY (`partner`) REFERENCES `partner` (`id`) ON DELETE SET NULL

| traceproperty | CREATE TABLE `traceproperty` (
`id` varchar(32) NOT NULL default '',
`propkey` varchar(32) default NULL,
`value` longblob,
`trace` varchar(32) default NULL,
PRIMARY KEY (`id`),
KEY `fk_traceproperty_trace_idx` (`trace`),
CONSTRAINT `fk_traceproperty_trace_new` FOREIGN KEY (`trace`) REFERENCES `trace` (`id`)

I have no idee how to fix it.

Thanks
Bart

Options: Reply• Quote


Subject
Views
Written By
Posted
sometimes, mysql don't use an existing index
6607
September 04, 2006 12:17PM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.