iI have three tables defined:
C, CT, T
- C
- CT
- T
The query i'mI'm trying to optimize is of the following kind:
There are secondary indices defined on T.colT.col
and on CC.pop
.pop
C (*using C.pop_idx*)
join CT (c.id == ct.cid)
join T (ct.tid == t.id)
filter (T.col == 8)
There is no doubt that this is the optimal query plan (because, because of the ORDER BYORDER BY
and LIMITLIMIT
clauses).
CT (table-scan)
join T (using T.primary)
filter T.col == 8
join C (using C.primary)
I've tried everything I can think of: using FORCE INDEX, query optimizer hints
- using FORCE INDEX
- query optimizer hints
But to no avail.
The frustrating thing is that I've seen query plans where MySQL is smart enough to do exactly what I want it to do, viz. use the index defined on the ORDER BY/LIMIT to "filter" the rows in the outermost table as the outermost loop of a nested loop IJ, but I simply can't induce it to do so in this case Any.
Any ideas?
For those interested (not necessary to answer question)
for those interested (not necessary to answer question): theThe calculations as to why said query plan would be optimal: pseudo in pseudo-code:
evenEven if the % of T rows with T.col == 8T.col == 8
is small (say, 1%), on average only 20 * 100 C rows would need to be read (which is way less than all the rows in CT).