view plaincopy to clipboardprint?
explain SELECT sc.*
FROM pcom_story_content sc INNER JOIN pcom_object_to_tag ot ON sc.story_id = ot.obj_id
WHERE ot.tag_id =24 AND ot.obj_type=1 AND sc.language='zh_CN'
ORDER BY sc.create_time DESC
LIMIT 0, 20;
修改的方式, 就是增加ignore index, 这样时间降低到原先的1/15.
view plaincopy to clipboardprint?
explain SELECT sc.*
FROM pcom_story_content sc IGNORE INDEX (PRIMARY, story_id) INNER JOIN pcom_object_to_tag ot ON sc.story_id = ot.obj_id
WHERE ot.tag_id =24 AND ot.obj_type=1 AND sc.language='zh_CN'
ORDER BY sc.create_time DESC
LIMIT 0, 20;
另外还有一个和排序相关的优化. 因为这个查询使用到了sc的language和create_time, 两个字段都各自建有索引, 但是MySQL在查询中只对一个表使用一个索引, 所以效果并不好. 这种情况需要建立这两者的联合索引, 这样在查询中就可以完全用上了.