2014-11-08 72 views
2

嗨,大家请指教我这个。MySQL的优化和索引

我有一个包含百万条记录的tablemasterlist。 我使用了CycleDate和ProductType的索引。 我期待的记录数为100,132,但sql scan 的行数太多。

explain SELECT * FROM tblmasterlist where CycleDate BETWEEN '2014-08-01' AND '2014-08-30' 

enter image description here


+0

如果你使用cycledate> = 2014-08-01和cycledate的老派方法<= 2014年8月30日 – Christian 2014-11-08 07:31:20

+0

是您的一个类型B树索引? – Jaylen 2014-11-08 07:34:22

+0

尝试运行ANALYZE TABLE; MRR在服务器上?你可以查看'select @@ optimizer_switch;' – Jaylen 2014-11-08 07:42:00

回答

1

你的指数CycleDate是确定的,并在此查询使用。像Mike说的那样,确保它是一个Typ=BTREE,因为您选择了一系列值(请参阅here)。

EXPLAIN给出的记录数是行数不是真正的量MySQL会扫描的估计(见here):

The rows column indicates the number of rows MySQL believes 
it must examine to execute the query. 
For InnoDB tables, this number is an estimate, and may not always be exact. 

您可以通过更新索引统计增加accurancy:

OPTIMIZE TABLE tblmasterlist