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('email1@test.com','email2@test.com','email15@test.com','email3@test.com','email4@test.com')

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
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('test@test.com', 'test2@test.com')

 

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.