Jump to content

use index for IN()


jaymc

Recommended Posts

I have a query which is something like

 

 

SELECT username FROM members WHERE email IN('[email protected]','[email protected]','[email protected]','[email protected]','[email protected]')

There are actually 1000 emails

 

The query is taking around 5 seconds, there are 80,000 records in members.

 

I created an index on the email but it wont use it. I even tried to FORCE INDEX but when using EXPLAIN it tells me its just using WHERE without any index

 

How can I optimise this query using an INDEX or something else..

 

EDIT

Ive just change SELECT username to SELECT email and EXPLAIN now says its using the index. However, the query took 20 seconds even with the index

Link to comment
https://forums.phpfreaks.com/topic/129842-use-index-for-in/
Share on other sites

Structure

CREATE TABLE `msn` (

`username` varchar(20) collate latin1_general_ci NOT NULL default '',

`memberemail` varchar(90) collate latin1_general_ci NOT NULL default '',

`password` varchar(90) collate latin1_general_ci NOT NULL default '',

`contactemail` varchar(90) collate latin1_general_ci NOT NULL default '',

`date` int(15) NOT NULL default '0',

UNIQUE KEY `username` (`username`,`contactemail`)

) ENGINE=InnoDB DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci

 

Query

SELECT username

FROM msn

WHERE contactemail IN('[email protected]', '[email protected]')

 

EXPLAIN

"id","select_type","table","type","possible_keys","key","key_len","ref","rows","Extra"

"1","SIMPLE","msn","index","","username","114","","373209","Using where; Using index"

Link to comment
https://forums.phpfreaks.com/topic/129842-use-index-for-in/#findComment-673376
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.