0

I have a simple query that is way too slow considering my needs. The query is:

SELECT a FROM tableA WHERE b IN ("SOME_IDS_LIST") ORDER BY a

tableA has about 300 000 rows and contains index (a,b). The longer "SOME_IDS_LIST" list the slower the query. Is there any way of speeding up this kind of query? I was looking for some generic solution over the internet but with no luck.

Jacek Francuz
  • 2,420
  • 8
  • 45
  • 61

2 Answers2

1

put on yout table an index on b field.

If the result is too slow.

Instead use IN clause, create a temporary table with your ID ad link your temporary table with your tableA using INNER JOIN statement.

Your script:

CREATE TEMPORARY TABLE myTempTable (id int)
INSERT INTO myTempTable (all your ID)

SELECT *
FROM tableA
JOIN myTempTable
ON tableA.b = myTempTable.id

About creation of temporary table see here

Joe Taras
  • 15,166
  • 7
  • 42
  • 55
1

It would be worthwhile to test an EXISTS clause. These can often have significant performance boosts over an IN clause. Again this would require a temp table

CREATE TEMPORARY TABLE myTempTable (id int)
INSERT INTO myTempTable (all your ID)

SELECT  TA.a 
FROM    tableA TA
WHERE   EXISTS 
        (SELECT *
        FROM    myTempTable TT
        WHERE   TT.id = TA.b
        )
ORDER BY
        TA.a
Declan_K
  • 6,726
  • 2
  • 19
  • 30