tags:

views:

51

answers:

1

I have a myisam table with 2.5M rows, I use an union all to get my results as following:

(SELECT t.id FROM t WHERE type=1 LIMIT 10)
UNION ALL
(SELECT t.id FROM t WHERE type=2 LIMIT 10)
...
UNION ALL
(SELECT t.id FROM t WHERE type=25 LIMIT 10)

the time to opening the table t is about 6ms.

With a single request:

SELECT t.id FROM t WHERE type=1 LIMIT 10

the time is about 1ms.

What I don't understand is why mysql spend more time to the same table in union all. It should recognize that is the same table and so just opening at the first union.

Does anybody can help me to decrease the time for opening table in a "union all"?

+2  A: 

6ms is pretty fast. You're fetching 25 times as much data and it's only taking 6 times as long.

I'd stop worrying about it and find where the real bottlenecks in your application are. If 6ms per query is a problem you might want to try reducing the number of queries you are performing per second.

Mark Byers
I agree with you Mark. 6ms is pretty fast but this request is often used by users which mean I have to make this request as fast as possible.
parm.95
@parm.95: Perhaps you could cache the result?
Mark Byers
@parm.95: By the way, LIMIT without ORDER BY is usually an error.
Mark Byers
in fact ORDER BY is killing my request about 12s for a single "union".I wonder if there is a way to keep opening table as long as possible, maybe some parameters to change in my.ini? What is the inconvenient to keep table open? Does the table open is always close after insert, update, delete?
parm.95
@parm.95: If ORDER BY is slow it's because you're missing an index.
Mark Byers