Jump to content

SELECT * FROM table WHERE id IN (....) too slow


phdphd

Recommended Posts

Hi All,

 

I use a query that gets rows according to specified ids. The typical syntax of the query is as follows:

 

SELECT * FROM table WHERE id IN(1,2,3,....,100,...)

The query is very slow (more than 10 seconds) if the IN clause contains thousands of ids. (The same query without the IN clause runs instantaneously.)

 

Note that the number of ids is random and depends of choices from the user.

I read here and there than using an "inner join" might be a solution. I would need some help in converting this query into an inner join-based query. Thanks a lot.

Link to comment
Share on other sites

the link you posted was for a query with a sub-query being changed to a self join. that's not what you are doing, based on your example query. if you are actually first selecting these ids from that same table, you would need to state or show this information before someone could possibly help you with the solution.

Link to comment
Share on other sites

Let me rephrase the context of my issue. I have a PHP processing that ends with the building of a series of ids. Then there is a SQL query based on this series of ids, that I run against a table with an Id column, in order to get all the data in rows matching the ids provided.

Link to comment
Share on other sites

I noticed that PhpMyAdmin reports a processing time of 0.0246 sec. But actually it displays the first set of rows only after more or less 15 seconds.

The Explain reports the following :

id   select_type  table       type possible_keys key    key_len ref     rows  Extra

1    SIMPLE      ma_table ALL PRIMARY        NULL NULL    NULL 8211 Using where

 

(If I run the same query without the IN clause, both the processing and the rendering are immediate.)

Edited by phdphd
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.