Otis wrote:
MarcoP wrote:
Otis wrote:
Do you have indices defined in the DB on the fields you sort on? It's likely the slowness comes from the fact you order on a field which doesn't have its own index, which means it's a table scan.
I do. The problem is I am writing an application (denormalized database) that contains anywhere from 500K to 8 millions records per table, thus my application is built around paging. So when the LIMIT happens on the outer query, I guess that is making it perform poorly.
Any other thoughts, ideas?
The limit can't be applied on the inner query, as that would make 1) no sense (it would order the page, not all data) and 2) it's not always possible to do this in all DQEs, and the architecture of the query producer produces 1 query per level. Paging directives are therefore moved to the outer query, as that's exactly what you want: you want to obtain the page X on the resultset to return.
What I think happens is that mysql isn't capable of sorting the resultset fast enough, which causes the slowness. After all, it has to sort 8million rows. The only way to overcome this IMHO is to drop the sorting, but then you get random rows back.
It performs great if I remove the inner select and just do the following over the table:
select column1, column2 from table order by column1 where ... LIMIT 1, 25
When I look at the execution plan in the my sql query window, the order by does take like 30% BUT the Sending Data part takes the rest. I would think that would mean the amount of data, but when i run the same query with and without the inner select, performance is night and day different.
Also, MySql does allow you to using a LIMIT on the inner select (also peform an order by here) and thus performance is great again....sig