I’m receiving a sequence of product IDs from external system. I have to show the product information preserving the sequence.
I’m using the following select to do so:
SELECT * FROM products
WHERE prodid in (10331,11639,12127..) ORDER BY Field(prodid, 10331,11639,12127...);
Sequence can consist of 20 IDs. prodid has b-tree index.
It’s very frequent query, and I’m trying to find ways to improve performance of that part of the system. Now the average time for this query is 0.14-0.2 sec
I would like decrease time to 0.01-0.05 sec.
What is the best way to do so? MySQL HASH index, store product id in memcached, or something else?
First put an index on
prodidsee @Anthony’s answer.Than change the query to read:
If you make sure your
INlist is sorted ascending before offering it to theINclause, theorder by prodidwill yield the same result alsorder by field(...select *will fetch data you may not need, causing extra disk access, and extra memory usage and extra network traffic.selectindexed fields, MySQL will never read the table, but only the index saving time (in your case this is probably not an issue though)There are a few tricks you can use.
memorytable, which is stored in RAM. Don’t do this for big tables, it will slow other things down.You can only use
hashindexes on memory tables.BETWEEN 1000 AND 1019instead ofIN (1000, 1001 ..., 1019)