当谈到超越MySQL的基础时,我并不精彩,但是,我正在尝试优化查询:
SELECT DATE_FORMAT(t.completed, '%H') AS hour, t.orderId, t.completed as stamp, t.deadline as deadline, t.completedBy as user, p.largeFormat as largeFormat FROM tasks t JOIN orders o ON o.id=t.orderId JOIN products p ON p.id=o.productId WHERE DATE(t.completed) = '2013-09-11' AND t.type = 7 AND t.completedBy IN ('user1', 'user2') AND t.suspended = '0' AND o.shanleys = 0 LIMIT 0,100 +----+-------------+-------+--------+----------------------------+-----------+---------+-----------------+-------+-------------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +----+-------------+-------+--------+----------------------------+-----------+---------+-----------------+-------+-------------+ | 1 | SIMPLE | o | ref | PRIMARY,productId,shanleys | shanleys | 2 | const | 54464 | Using where | | 1 | SIMPLE | p | eq_ref | PRIMARY | PRIMARY | 4 | sfp.o.productId | 1 | | | 1 | SIMPLE | t | ref | NewIndex1 | NewIndex1 | 5 | sfp.o.id | 6 | Using where | +----+-------------+-------+--------+----------------------------+-----------+---------+-----------------+-------+-------------+
在添加一些索引之前,它在p
表和o
表上执行全表扫描.
基本上,我认为MySQL会:
使用where子句限制任务表中的行(应该是没有连接的84行)
然后通过orders表到products表获取一个flag(largeFormat
).
我的问题是为什么当MySQL只有84个不同的ID要查找时,它会查找50000多行,有没有办法可以优化查询?
我无法添加新字段或新表.
先感谢您!