I need to select certain rows from a mysql table that has VARCHAR Primary Key index ID.
I therefore use a query (but obviously 13,000 ids instead of 3)
SELECT * FROM table1 WHERE ID IN ('id1928383','id3838339','id38383')
For the majority of tables this works well (~0.2s) but for some tables the query takes much longer (~10-11s).
The only thing different about the tables that do not work is they have a larger Data_length? Any ideas how I can speed up the slow query’s?
There are a few things you can do to speed up this query:
INTinstead ofVARCHARfor theIDcolumnComputers in general are much better with numbers than with strings, so this will speed it up considerably.
IDas an index.This will create an ordered table with that indexed column, where the searching and ordering will be a LOT faster.
If you are using this with PHP, I recommend you use PDO, since it has a query caching system, and can significantly improve results if there are many identical queries being run. I suppose other programming languages have similar mechanisms.
For all the software you use it is recommended to use the latest version for many reasons. If I’m not mistaking, there was a performance issue with
INin an older version of MySQL, which was fixed a few version later, but you could be one of the unlucky few that still uses that version.That’s about as much as I can help, if you need more help, you will need to post more info (table structure, database structure, programming language, etc).