一个文章库,里面有两个表:category和article。category里面有10条分类数据。article里面有 20万条。article里面有一个"article_category"字段是与category里的"category_id"字段相对应的。 article表里面已经把 article_category字义为了索引。数据库大小为1.3G。 问题描述:
执行一个很普通的查询: Select * FROM `article` Where article_category=11 ORDER BY article_id DESC LIMIT 5 。执行时间大约要5秒左右解决方案:
建一个索引:create index idx_u on article (article_category,article_id);
Select * FROM `article` Where article_category=11 ORDER BY article_id DESC LIMIT 5 减少到0.0027秒继续问题:
Select * FROM `article` Where article_category IN (2,3) ORDER BY article_id DESC LIMIT 5 执行时间要11.2850秒。
使用OR:
select * from article
where article_category=2
or article_category=3
order by article_id desc
limit 5
执行时间:11.0777解决方案:避免使用in 或者 or (or会导致扫表),使用union all使用UNION ALL:
(select * from article where article_category=2 order by article_id desc limit 5)
UNION ALL (select * from article where article_category=3 order by article_id desc limit 5)
ORDER BY article_id desc
limit 5
执行时间:0.0261EXP-00056: ORACLE error 29275 encountered修改Oracle实例名相关资讯 MySQL索引
- MySQL中索引和优化的用法总结 (今 14:24)
- MySQL 全索引扫描的bug (11/11/2015 14:04:56)
- MySQL索引和优化查询 (05/17/2015 14:16:03)
| - MySQL索引下推技术 (02月23日)
- MySQL (ICP) 索引条件下推对比 (09/17/2015 19:11:44)
- MySQL理解索引、添加索引的原则 (05/17/2015 14:15:05)
|
本文评论 查看全部评论 (0)