mysql - Is it possible to select by range on leftmost multiple-index? -
i have index m_idx on (k1,k2,k3)
if do
select c1,c2,c3... tb force index (m_idx) k1=500 , k2 in(...) , k2>2000 order k1 limit 1000;
or
select c1,c2,c3... tb force index (m_idx) k1 in (500,1000,1500 ...) , k2 in(...) , k2>2000 order k1 limit 1000;
handler_read_next = 999
but if try use range on k1:
select c1,c2,c3... tb force index (m_idx) k1>=500 , k2 in(...) , k2>2000 order k1 limit 1000;
handler_read_next = 58035
in cases explain says key used m_idx
think in third case m_idx it's not used (i have index on k1).
otherwise don't understand why it'is reading more 1000 rows.
expecting scan m_idx index, , only first 1000 rows meet conditions read table.
in fact think third case scans index , rows meet k1 condition read tb , k2 , k3 conditions checked after rows read tb.
use: mysql myisam, windows 7 64, tb has 1 mil rows;
questions are:
possible select range on leftmost multiple-index?
or
i'm doing else wrong?
thank you.
- no not.
- no, you're doing right
http://dev.mysql.com/doc/refman/5.1/en/range-optimization.html ("7.3.1.3.2. range access method multiple-part indexes" part)
so cannot optimizer perform query faster.
and avoid using force index, since optimizer knows better index use.
also:
k1>=500 , k2 in(...) , k2>2000 depending of part returns fewer records, either k1>=500 or k2 in(...) , k2>2000 (i'm not sure why need > 2000 here, due can compare manually, before add in()), try create index k2 (if k2 part returns less amount of records).
Comments
Post a Comment