I found and odd performance while writing a query, this was the original query (BTW I am using MySQL 5.5.8)
What I need to do is to select id, a, b c,d
attributes from all elements (and all ocurrences of that elements) that have new tuples inserted in a time (time
) between now and 2012-03-13 10:41:34.8431
(e.g.) but also, these elements must have had a number of ocurrences HAVING COUNT(id) >= '5'
SELECT id, a, b, c,d FROM table1 WHERE
id IN (SELECT id FROM table1 WHERE id IN (SELECT id FROM
table1 WHERE time >= '2012-03-13 10:41:34.8431' AND a = '1') HAVING COUNT(id) >= '5')
In table1
there are aprox 700 tuples. This query lasts 14.5s
Since it was not acceptable to take this long, I separated both querys just to test:
SELECT id FROM table1 WHERE id IN (SELECT id FROM
table1 WHERE time >= '2012-03-13 10:41:34.8431' AND a = '1') HAVING COUNT(id) >= '5'
Takes 0.025s and returns one tuple with id = 6
Since i knew the result, and just for testing I executed the other part of the query as:
SELECT id, a, b, c,d FROM table1 WHERE id IN (6)
And it took 0.0012s
Now, what happens there? I have no clue, just some unfounded speculations. Why there is so much difference between them?
And if this is normal, are there any known workarrounds?