How to create an effective Index with this query?

how can I create index for this SQL query?

  • 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

  • Answer:

    You need to create composite index site + time (yes, order matters). So delete both indexes site and time now and create 2: KEY site (site, time) KEY time (time)

Zenofo at Stack Overflow Visit the source

Was this solution helpful to you?

Related Q & A:

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.