I have the following MSQL query, that is very slow
SELECT a.id, a.date, a.Title, a.Text, a.Text2, a.Text3,
b.xxx, b.yyyy,
c.aaaa, c.bbbb
FROM table_a a
JOIN table_b b on a.Id = b.Id
JOIN table_c c on a.eeee = c.id
ORDER BY a.date DESC
LIMIT 200
The table_a has more that 100.000 rows. I need the last 200 rows in reverse order. What MySQL does is to select all rows, made the JOIN of all rows, then reverse the full table and finally select 200 record. The full process takes about 30-40 seconds.
Question: since table_a is ordered by a.id (it is a progressive number) and also by date (new articles are always added in the last position), is it possible to rewrite the query so that the JOIN is performed only on the latest 200 records and not the full table ? In this way the query will be much faster!
In general, how can be optimized ??
2
Answers
You can try this, but i am not realy sure if
LIMIT is allowed.
This may be even faster than Bernd’s suggestion:
together with
This is because the inner query can be run very efficiently in that index instead of bouncing between the index BTree and the data BTree. After that 200 rows to deal with for the rest.
Another issue: If
table_b
andtable_a
are 1:1, why are they separate tables?Yet another issue: If
table_b
andtable_c
are used for filtering ("1:0") or expanding (1:many) this won’t work (and nor will Bernd’s). You may get less than or more than 200 rows.