项目上遇到一个郁闷的事,我有一个表 车辆通行 有两个字段 经过时间和记录类型
我为这两个字段都加了索引。
单独以这两个字段为条件的查询都很快 在1秒内
但两个字段联合作为记录时 却要20多秒
我的查询语句是 select count (id) from cltx where jgsj>'2010-11-17' and jgsj<'2010-11-18' and jllx='2'
总数据量不到50万条,这条语句的结果5000+条
按说不应该要20多秒的。
哎。。 高手,小弟先在此拜谢
我为这两个字段都加了索引。
单独以这两个字段为条件的查询都很快 在1秒内
但两个字段联合作为记录时 却要20多秒
我的查询语句是 select count (id) from cltx where jgsj>'2010-11-17' and jgsj<'2010-11-18' and jllx='2'
总数据量不到50万条,这条语句的结果5000+条
按说不应该要20多秒的。
哎。。 高手,小弟先在此拜谢
select count(id) from
(
select * from cltx where jgsj>'2010-11-17' and jgsj<'2010-11-18'
)
where jllx='2'
select count (id) from cltx where jgsj>'2010-11-17' and jgsj<'2010-11-18' and jllx='2'
发现执行速度也是12秒。。而且还有
我执行select count (id) from cltx where jllx='2'的时候,按F5 可以看到执行了jllx的索引
但是执行select count (id) from cltx where jgsj>'2010-11-17' and jgsj<'2010-11-18' and jllx='2'
的时候 只看到执行jgsj的索引
介四为嘛呀~~!!!
建了个索引叫jllx 字段是jllx
还一个是jgsj 字段是jgsj
我现在count(id) count(jgsj) count(jllx)
还有那三个条件的顺序 不论如何
查询结果都是那该死的12秒
where jgsj>'2010-11-17' and jgsj<'2010-11-18' and jllx='2'
哪个条件的记录少些 就优先前面过滤id 是否为主键
--试试下面的看看能
select count(主键) from cltx where jgsj>'2010-11-17' and jgsj<'2010-11-18' and jllx='2'
--or
select count(主键)
from (select 主键 from cltx where jgsj>'2010-11-17' and jgsj<'2010-11-18' and jllx='2'
)
开始是索引A on colum A
B on colum B
现在是C on colum A,B 就好了
建个联合索引试试
create index idx_name on cltx(jgsj,jllx)
,用联合索引,太合适了