sql语句

EXPLAIN SELECT `wl`.`get_user_id`, `el`.`level` FROM `winning` `wl`  LEFT JOIN `turntable` `el`  ON wl.level_id=el.id     WHERE ((wl.`activity_id`=1) AND (`el`.`item_id`='318') AND (`level_id` <> 0) AND (`el`.`level` <> 9));

发现没有走索引 activity_id,找了下原因发现winning 表基数太小了,activity_id总共只有2条不同的记录,mysql认为这样走全表扫描更快。

强制走索引的防范

EXPLAIN SELECT `wl`.`get_user_id`, `el`.`level` FROM `winning` `wl` force index ( `activity_id`) LEFT JOIN `turntable` `el`  ON wl.level_id=el.id     WHERE ((wl.`activity_id`=1) AND (`el`.`item_id`='318') AND (`level_id` <> 0) AND (`el`.`level` <> 9));