mysql两表索引优化
1 | CREATE TABLE `class` ( |
EXPLAIN
1 | mysql> EXPLAIN SELECT * FROM class LEFT JOIN book ON class.card = book.card; |
添加索引优化
1 | mysql> ALTER TABLE book ADD INDEX Y(card); |
EXPLAIN
1 | mysql> EXPLAIN SELECT * FROM class LEFT JOIN book ON class.card = book.card; |
结论:左连接把索引加在了右表上面
把索引加在左连接的左表上
1 | mysql> drop index Y ON book; |
结论
LEFT JOIN 条件用于确定如何从右表搜索行,左边一定都有,所以右边是我们的关键点,一定要建立索引。