3

table:

CREATE TABLE `deal` (
  `id` int(11) NOT NULL default '0',
  `site` int(11) NOT NULL default '0',
  `time` bigint(13) NOT NULL default '0',
  PRIMARY KEY  (`id`),
  KEY `site` (`site`),
  KEY `time` (`time`,`site`)
) TYPE=MyISAM

sql query:

select * from `deal` where time>0 && site=8

I create index:time for this query,

but why this query always using index: site?

explain select * from `deal` where time>0 && site=8

output:

table    type    possible_keys   key    key_len    ref   rows   Extra

deal     ref     site,time       site   4          const 1      Using where

1 Answer 1

5

You need to create composite index site + time (yes, order matters).

So delete both indexes site and time now and create 2:

  1. KEY site (site, time)
  2. KEY time (time)
Sign up to request clarification or add additional context in comments.

2 Comments

thanks zerkms, KEY site (site, time) can completely replace the KEY site (site)? (I also need select * from deal where site=123)
@Zenofo: yes, it is called "leftmost". So with index a+b+c you don't need to have indexes a and a+b, since it covers them with its left part

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.