设为首页 收藏本站
查看: 528|回复: 0

[经验分享] Mysql大数据库测试和一些用法

[复制链接]

尚未签到

发表于 2016-10-19 09:43:27 | 显示全部楼层 |阅读模式
  
  1:全表前100W条的所有数据
  ----------------------------------------------------
  [SQL] select * from e_enterprise limit 1000000
  
  影响的数据栏: 0
  时间: 38.219ms
  
  
  2:全表前100W条的id数据
  ----------------------------------------------------
  [SQL] select id from e_enterprise limit 1000000
  
  影响的数据栏: 0
  时间: 1.187ms
  
  
  3:全表前100W条的count(id)数据
  ----------------------------------------------------
  [SQL] select count(id) from e_enterprise limit 1000000
  
  影响的数据栏: 0
  时间: 0.563ms
  
  
  4:全表前100W条的count(*)数据
  ----------------------------------------------------
  [SQL] select count(*) from e_enterprise limit 1000000
  
  影响的数据栏: 0
  时间: 0.531ms
  
  数据实时的不一致,即使数据不变的查询的时间也不一样
  
  在印象中count(id)总比count(*)要快的,但是我看了一下测试结果竟然count(*)更快,id是一个主键,怎么会这样呢?
  
  下面是测试数据,数据说明了一切
  ----------------------------------------------------
  [SQL] select count(id) from e_enterprise limit 1000000;
  影响的数据栏: 0
  时间: 0.578ms
  
  [SQL] 
  select count(*) from e_enterprise limit 1000000;
  影响的数据栏: 0
  时间: 0.532ms
  ----------------------------------------------------
  [SQL] select count(*) from e_enterprise;
  影响的数据栏: 0
  时间: 0.532ms
  
  [SQL] 
  select ROW_COUNT() from e_enterprise;
  影响的数据栏: 0
  时间: 1.282ms
  --------------------------------------
  [SQL] select count(1) from e_enterprise limit 1000000;
  影响的数据栏: 0
  时间: 0.532ms
  
  [SQL] 
  select count(*) from e_enterprise limit 1000000;
  影响的数据栏: 0
  时间: 0.531ms
  
  测试:500万条数据,表有聚集索引(id+时间字段),还有其它字段 
  
  count(*)与count(id):运行结果一样,速度几乎相同,均为13 " 
  count(*)与count(其他非空字段):非空字段无索引,结果相同,均为13 " 
  count(*)与count(其他有空的字段):有空字段无索引,前者13 ",后者1分多钟 
  
  可见: 
  若id不为空,count(*)与count(id)效率差不多 
  若id有空,count(*)比count(id)记录要多,但效率却要高,原因是count(*)直接统计记录总数,而count(id)还要判断id的值是否为空,因此降低了效率 
  
  下面是一些Mysql的用法(可能很少用)
  
  1. 取得关于 information_schema的基本信息
  查询所有的数据库  show databases;  
  2.use information_schema; 
  3.查询所有的表show tables;  
  4.超过1000行的表 
  select concat(table_schema,'.',table_name) as table_name,table_rows  
  from information_schema.tables where table_rows > 1000 order by table_rows desc; 
  5.没有主键的表
  SELECT CONCAT(t.table_name,".",t.table_schema) as table_name  
  FROM information_schema.TABLES t  
  LEFT JOIN information_schema.TABLE_CONSTRAINTS tc  
  ON t.table_schema = tc.table_schema  
  AND t.table_name = tc.table_name  
  AND tc.constraint_type = 'PRIMARY KEY'  
  WHERE tc.constraint_name IS NULL  
  AND t.table_type = 'BASE TABLE';   
  
  下面是执行多个delete from 表 与 delete from 表 where id in (个数相同) 测试性能比较结果如下
  Mysql5.1 window xp下
  1:数据一千多左右
  
  [SQL] delete  from qa_questions where id  = 100;
  影响的数据栏: 0
  时间: 0.015ms
  
  [SQL] 
  delete  from qa_questions where id  = 101;
  影响的数据栏: 1
  时间: 0.032ms
  
  [SQL] 
  delete  from qa_questions where id  = 102;
  影响的数据栏: 0
  时间: 0.015ms
  
  [SQL] 
  delete  from qa_questions where id  = 103;
  影响的数据栏: 1
  时间: 0.016ms
  
  [SQL] 
  delete  from qa_questions where id  = 104;
  影响的数据栏: 1
  时间: 0.015ms
  
  [SQL] 
  delete  from qa_questions where id  = 105;
  影响的数据栏: 1
  时间: 0.015ms
  
  [SQL] 
  delete  from qa_questions where id  = 106;
  影响的数据栏: 1
  时间: 0.016ms
  
  [SQL] 
  delete  from qa_questions where id  = 107;
  影响的数据栏: 1
  时间: 0.016ms
  
  [SQL] 
  delete  from qa_questions where id  = 108;
  影响的数据栏: 1
  时间: 0.015ms
  
  [SQL] 
  delete  from qa_questions where id  = 109;
  影响的数据栏: 1
  时间: 0.015ms
  
  [SQL] 
  delete  from qa_questions where id in(
  200,201,202,203,204,205,206,207,208,209
  )
  
  
  影响的数据栏: 9
  时间: 0.016ms
分析:执行一条的where id = 与where id in(xx,xx,xx,.......) 时间差 千分之一
  2:数据量32万左右
  [SQL] delete  from e_template where id  = 100;
  影响的数据栏: 1
  时间: 0.078ms
  
  [SQL] 
  delete  from e_template where id  = 101;
  影响的数据栏: 1
  时间: 0.031ms
  
  [SQL] 
  delete  from e_template where id  = 102;
  影响的数据栏: 1
  时间: 0.016ms
  
  [SQL] 
  delete  from e_template where id  = 103;
  影响的数据栏: 1
  时间: 0.032ms
  
  [SQL] 
  delete  from e_template where id  = 104;
  影响的数据栏: 1
  时间: 0.016ms
  
  [SQL] 
  delete  from e_template where id  = 105;
  影响的数据栏: 1
  时间: 0.031ms
  
  [SQL] 
  delete  from e_template where id  = 106;
  影响的数据栏: 1
  时间: 0.016ms
  
  [SQL] 
  delete  from e_template where id  = 107;
  影响的数据栏: 1
  时间: 0.000ms
  
  [SQL] 
  delete  from e_template where id  = 108;
  影响的数据栏: 1
  时间: 0.032ms
  
  [SQL] 
  delete  from e_template where id  = 109;
  影响的数据栏: 1
  时间: 0.000ms
  
  [SQL] 
  delete  from e_template where id in(
  200,201,202,203,204,205,206,207,208,209
  )
  
  影响的数据栏: 10
  时间: 0.016ms
  
  分析:从三十多万的数据可以看到  id  = 100;时间: 0.078ms 而 where id in(
200,201,202,203,204,205,206,207,208,209) 只用了 0.016ms,此时有的where id  = 是where id in(xx,xx,xx,....)消耗时间的4倍多
所有的删除加起来执行效率id=是in的  78+31+16+32+16+31+16+0+32+0/ 16倍,id=消耗的时间大约是where in的15倍多时间
3:我感觉数据量越大1000万以上越能体现 where id in 的优势。数据量大id = 反而没有id in(xx,xx,xx,...)删除多个性能好了。(其实数据1000多和三十二万的效率来讲 id = 差不多都是 id in的十几倍耗费时间,三十二万这种差距相比数据量1k又大多了。)
 
 
  

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-288285-1-1.html 上篇帖子: 在jsp中对mysql数据库分页的方法 下篇帖子: MySQL数据库性能优化之硬件优化
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表