PDA

View Full Version : MySQL Index Question



Yeldarb
March 15th, 2010, 05:33 AM
Hey guys, I am running into a problem in my database with queries like this:

SELECT fbid FROM users WHERE fbid IN (a, b, c, ..., z) AND active=1
There could be hundreds or even thousands of ids in the IN() clause.

Right now I just have an index on "fbid", would adding an index on "active, fbid" significantly speed things up? I was thinking that maybe if I did this it would be able to serve the query exclusively from the index rather than having to retrieve each individual row to check "active". Is this true? And does anyone know by how much that would speed up the queries?

I would just try it but I'm on vacation in Europe and can't deal with things if this doesn't speed it up; for now I'm limiting the number of elements in the IN() clause but that's not a valid long-term solution.

Yeldarb
March 15th, 2010, 09:37 PM
I did a few benchmarks. The answer is yes, it does only use the index. But no, it's not faster (at least in my case). In fact, it was about 8x slower to restructure the queries [the above is a bit of a simplification of what I was trying to do].