mysql 查询优化

数据库有100万条数据,至于怎么插入,参看前一篇文章。

执行简单一句:select * from cardtotalinfo where  networkid=1004

需要15.11秒

没有建索引,有主键为自增id。


在networkid建立索引:

CREATE INDEX idx_cardtotalinfo_networkid ON cardtotalinfo(networkid); 

更新表

update cardtotalinfo set purchaseprice = 12 where networkid=1004

受影响的行: 93

时间: 0.953秒

查询表

select * from  cardtotalinfo  where networkid=1004

受影响的行: 0
时间: 0.000秒

插入表

INSERT INTO `cardtotalinfo`(`code`,`companyid`, `faceprice`,`purchaseprice` ,`purchasetime`  ,`networkid`,`isfirst` , `sendtime`, `issaled`,`saletime` ,`networkmoney` ,`nwmoneytime` , `isspended`,`companymoney` , `cpmoneytime`) VALUES ('ADFE3993', '1', '10.5', '6', '2011-11-30 12:43:03', '1004', '', '2011-11-30 12:43:12', '', '2011-11-30 12:43:19', '7', '2011-11-30 12:43:29', '', '6', '2011-11-30 12:43:37');

受影响的行: 1
时间: 0.062秒

 删除表记录

delete from  cardtotalinfo  where networkid=1004

受影响的行: 95
时间: 0.078秒


再建立一个索引

CREATE INDEX idx_cardtotalinfo_companyid ON cardtotalinfo(companyid); 

花费5分50秒。

查询

select * from  cardtotalinfo  where companyid=734

受影响的行: 0
时间: 7.781秒

1011条记录

select * from  cardtotalinfo  where networkid=5566

受影响的行: 0
时间: 0.578秒

92条记录

select * from  cardtotalinfo  where companyid=734 and networkid = 2045

时间: 0.047秒

31条记录

看来查询效率和返回的记录条数有关系

select * from  cardtotalinfo  where companyid=794 and code = 'Abc13'

时间: 7.250秒

91条记录

select * from (
select * from  cardtotalinfo  where companyid=384 ) as c
 where code = 'Abc13'

时间: 6.719秒

91条记录

看来where条件的字段不建索引,怎么查都慢



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值