表有三个字段,ID(主键,自增长),DATA(MediumText,用来保存大的纯文本,因为不确定文本长度是否会超过64K),UPDATE(INT 1,用来标记是否更新过)
索引已建立:ID、UPDATEselect count(id) from cn显示结果为237549记录
耗时:113469 ms另外,DATA改为TEXT是否会好一些呢?救救啊
索引已建立:ID、UPDATEselect count(id) from cn显示结果为237549记录
耗时:113469 ms另外,DATA改为TEXT是否会好一些呢?救救啊
or
CREATE TEMPORARY TABLE tt
select id from cnselect count(id) from ttor
直接查询系统表此表的记录数
select count(id) from cn where `update`=1
总表大概是26万条记录,`update`=1是23万条
加了where后速度下降明显,不加的时候是0ms
试试
OR
select count(id) from (SELECT ID FROM cn where `update`=1) A group by id
OR
select count(id) from (SELECT ID FROM cn where `update`=1) A
select count(id) from (SELECT ID FROM cn where `update`=1) A group by id
OR
select count(id) from (SELECT ID FROM cn where `update`=1) A
select sum(iif(update=1,1,0)) from cn group by id
http://www.mysqlperformanceblog.com/2007/04/10/count-vs-countcol/
不加的话全表扫描快。因为你的UPDATE列此时的索引是一个烂索引。也就是比FULL TABLE SCAN 的效率更低下的索引。这样的列根本就不应该建立索引,直接分表得了。当一个索引要扫描的行数多于总表记录数50%的时候,就是烂索引!
declare rows_update int;
declare rows_all int;
select count(id) into rows_all from cn;
select count(id) into rows_not_update from cn where 'update'=0; -- 寻找未更新的,假设其状态=0
set rows_update=rows_all-rows_notupdate;
select rows_update;
你刚才的sql语句
select count(id) from cn 应该很快。
select count(id) from cn where `update`=1 由于采用的全表扫描,因此会慢。
但是,innodb类型的表比myisam类型的表还要慢。
如果不用事物的话,建议不建成 innodb 类型的表。
用TINYINT(1)不是更好吗?
而且根本不用为UPDATE建索引.因为只有0和1的可能性
2、DATA改为TEXT,在数据量大的时候,会好一些,但是,不建议这样做。不能为了提升速度而牺牲功能,并且这种提升也是很有限的。