select * from notice fid in (一个几百长度的数组) or tid in (同一个几百长度的数组) and ltype<>11 order by updated_at目前notice这个表里面大概有36W条记录,并且每天增长量很大,这条语句现在成为了我整个项目的瓶颈,里面用到的四个字段我全有做索引,但是运行时间居然达到5秒甚至8秒
但这个功能又不可缺少有没有办法能加快速度啊?
但这个功能又不可缺少有没有办法能加快速度啊?
谢谢啦,问题解决后立即给分
select * from notice where ltype<>11 and fid in(数组) or tid in(数组) order by updated_at
估测一下 fid 和 tid 的数组,看哪个数组长度短些,放前面。
2.采用联合
select * from notice where ltype<>11 and fid in(数组) union select * from notice where ltype<>11 and fid in(数组) order by update_at你自己测试下,看看有不有用
ltype<> 11是FULL TABLE SCAN.
所以你这条语句对于表NOTICE而言是FULL TABLE SCAN。没有用到任何索引!你这里面还有OR操作!如果换成UNION他会对主表多扫描一次,浪费资源!所以还是保留的好!
建议对NOTCIE进行分表。条件为ltype 是否等于11.这样查询条件就只要查询LTYPE<>11的那张小表了。
on lsb b on (a.fid=b.id or a.tid=b.id) order by updated_at 要fid、tid、ltype上建立索引
在fid、tid、ltype上建立索引
select * from (select * from notice where ltype <>11) a inner join
on lsb b on (a.fid=b.id or a.tid=b.id) order by updated_at
在相关字段上建立索引,试试速度吧
select * into 临时表 from notic where ltype>11 UNION ALL select * from notic where ltype <11
select * from 临时表 where fid in (一个几百长度的数组) or tid in (同一个几百长度的数组)
EXPLAIN一下SQL语句,贴结果出来看看
on lsb b on (a.fid=b.id or a.tid=b.id) order by updated_at
在fid、tid、ltype、LSB的ID上建立索引
http://blog.chinaunix.net/u/29134/showart_1332349.html