实验步骤
同一台电脑装了两个mysql,端口号不同,mysql5.6安装好之后只有一个my-default的文件,将其中添加一些错误内容,改mysql依然可以启动成功
,说明使用的不是该文件,将my-default.ini改名为my.ini,再向其中添加一些错误内容,则启动失败,则说明,mysql5.6启动时读取的配置文件
名称默认为my.ini
使用框架springmvc+spring+hibernate,但是插入数据时使用的JdbcDaoSupport中的jdbcTemplete,所以在这里hibernate也算是没有使用到,
由于要获取同一个connection对象,所以在这里封装了个方法

  1. public static final ThreadLocal<Connection> cs = new ThreadLocal<Connection>();
  2.  
  3. public static Connection getConnection(DataSource dataSource) throws SQLException{
  4. Connection c = cs.get();
  5. if(null==c){
  6. c=dataSource.getConnection();
  7. cs.set(c);
  8. }
  9. return c;
  10. }

dao插入数据代码

  1. package com.h3c.itac.alarm.dao;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.PreparedStatement;
  5. import java.sql.ResultSet;
  6. import java.sql.SQLException;
  7.  
  8. import javax.annotation.PostConstruct;
  9. import javax.annotation.Resource;
  10. import javax.sql.DataSource;
  11.  
  12. import org.springframework.jdbc.core.support.JdbcDaoSupport;
  13. import org.springframework.stereotype.Repository;
  14.  
  15. import com.h3c.itac.alarm.po.SyslogAlarm;
  16. import com.h3c.itac.util.ConnectionUtil;
  17.  
  18. @Repository
  19. public class SyslogAlarmDao extends JdbcDaoSupport{
  20. @Resource
  21. public void setDatasource(DataSource dataSource){
  22. this.setDataSource(dataSource);
  23. }
  24.  
  25. private long count = 0L;
  26. String sql1="";
  27. String sql2="";
  28. PreparedStatement ps1 = null;
  29. PreparedStatement ps2 = null;
  30. Connection con = null;
  31. @PostConstruct
  32. public void set() throws SQLException{
  33. sql1 = "insert into alarm(id,adesk_alarm_id,serial_no,level,source,type,device_id,create_time,upload_time,order_id,title,location,customer_id)"
  34. +"values(?,?,?,?,?,?,?,?,?,?,?,?,?)";
  35. sql2 = "insert into syslogalarm values(?,?,?,?,?,?,?,?)";
  36. con = ConnectionUtil.getConnection(this.getDataSource());
  37. // con.setAutoCommit(false);
  38. ps1 = con.prepareStatement(sql1);
  39. ps2 = con.prepareStatement(sql2);
  40. PreparedStatement ps3 = con.prepareStatement("select max(id) from alarm");
  41. ResultSet rs = ps3.executeQuery();
  42. while(rs.next()){
  43. count = rs.getLong();
  44. }
  45. System.out.println("alarm id最大值为 :"+count);
  46. }
  47. public void insert(SyslogAlarm sAlarm) throws SQLException{
  48. count++;
  49. ps1.setLong(,count);
  50. ps1.setLong(,sAlarm.getAlarm().getAdeskAlarmId());
  51. ps1.setString(,sAlarm.getAlarm().getSerialNo());
  52. ps1.setInt(, sAlarm.getAlarm().getLevel());
  53. ps1.setInt(, sAlarm.getAlarm().getSource());
  54. ps1.setInt(,sAlarm.getAlarm().getType());
  55. ps1.setLong(, sAlarm.getAlarm().getDevice().getId());
  56. ps1.setLong(, sAlarm.getAlarm().getCreateTime());
  57. ps1.setLong(, sAlarm.getAlarm().getUploadTime());
  58. ps1.setLong(,count);
  59. ps1.setString(, sAlarm.getAlarm().getTitle());
  60. ps1.setString(, sAlarm.getAlarm().getLocation());
  61. ps1.setLong(,sAlarm.getAlarm().getDevice().getCustomer().getId());
  62.  
  63. ps2.setLong(,count);
  64. ps2.setString(, sAlarm.getDescription());
  65. ps2.setString(,sAlarm.getReason());
  66. ps2.setString(,sAlarm.getAdvise());
  67. ps2.setString(, sAlarm.getVariables());
  68. ps2.setString(, sAlarm.getLogContent());
  69. ps2.setLong(,sAlarm.getPriginalLogId());
  70. ps2.setLong(, count);
  71.  
  72. ps1.addBatch();
  73. ps2.addBatch();
  74. ==){
  75. excuteRemainderBatchSQL();
  76. }
  77. }
  78.  
  79. public void excuteRemainderBatchSQL() throws SQLException{
  80. ps1.executeBatch();
  81. ps1.clearBatch();
  82. ps2.executeBatch();
  83. ps2.clearBatch();
  84. // con.commit();
  85. // con.setAutoCommit(true);
  86. }
  87. }

测试类

  1. package com.h3c.itac.alarm.dao;
  2.  
  3. import java.sql.SQLException;
  4. import java.util.Date;
  5.  
  6. import org.junit.Test;
  7. import org.springframework.context.ApplicationContext;
  8. import org.springframework.context.support.ClassPathXmlApplicationContext;
  9.  
  10. import com.h3c.itac.alarm.po.Alarm;
  11. import com.h3c.itac.alarm.po.SyslogAlarm;
  12. import com.h3c.itac.customer.Customer;
  13. import com.h3c.itac.device.Device;
  14.  
  15. public class TestSyslogAlarmDao {
  16.  
  17. public SyslogAlarmDao getSyslogAlarmDao() {
  18. ApplicationContext ac = new ClassPathXmlApplicationContext(
  19. "applicationContext.xml");
  20. return (SyslogAlarmDao) ac.getBean("syslogAlarmDao");
  21. }
  22. @Test
  23. public void init(){
  24. ApplicationContext ac = new ClassPathXmlApplicationContext(
  25. "applicationContext.xml");
  26. }
  27. /**每次清库1800
  28. * 2,自动提交(2.388,2.388,2.871,2.809,2.683)平均2.6278
  29. * 1,手动提交(2.825,2.717,2.294,2.076,2.169)平均2.4162
  30. * 不清库,每次1800
  31. * 2,自动提交(2.981,3.261,3.433,1.888,1.935)
  32. * 1,手动提交(2.091,2.293,3.354,1.95,2.371,)
  33. * 5000
  34. * 2,自动提交(6.397,5.43,6.584,6.35,4.853) 平均5.9228
  35. * 1,手动提交(4.166,5.913,4.634,2.762,4.962) 平均4.4874
  36. */
  37. @Test
  38. public void testInsert() throws SQLException, ClassNotFoundException{
  39. double start = System.currentTimeMillis();
  40. SyslogAlarmDao sad = this.getSyslogAlarmDao();
  41. ;i<;i++){
  42. Customer c = new Customer();
  43. c.setId(1L);
  44. Device d = new Device();
  45. d.setId(1L);
  46. d.setCustomer(c);
  47.  
  48. Alarm alarm = new Alarm();
  49. alarm.setAdeskAlarmId((long) i);
  50. alarm.setCreateTime(new Date().getTime());
  51. alarm.setDevice(d);
  52. alarm.setLevel();
  53. alarm.setLocation("location "+i);
  54. alarm.setSerialNo("serialno "+i);
  55. alarm.setSource();
  56. alarm.setTitle("title "+i);
  57. alarm.setType();
  58. alarm.setUploadTime(new Date().getTime());
  59.  
  60. SyslogAlarm sa = new SyslogAlarm();
  61. sa.setAdvise("advise "+i);
  62. sa.setAlarm(alarm);
  63. sa.setDescription("description "+i);
  64. sa.setLogContent("logcontent "+i);
  65. sa.setPriginalLogId();
  66. sa.setReason("reason "+i);
  67. sa.setVariables();
  68.  
  69. sad.insert(sa);
  70. }
  71. sad.excuteRemainderBatchSQL();
  72. double end300 = System.currentTimeMillis();
  73. System.);
  74. }
  75.  
  76. }

如果只是将my-default.ini文件改名为my.ini文件,而不该内部配置,则在执行testInsert的时候速度超慢,即便是插入600条也需要将近二十多秒

然后找来其他版本的my.ini文件,替换掉改名后的my.ini文件,则速度比较快,可以想到问题就出在之前版本的my.ini和改名后的my.ini的文件配置内容上

然后用Compare对比改名前后的my.ini文件内容使用Compare辅助工具进行测试,(使用Compare对比在改名的my.ini文件中不存在的内容,将之前版本的my.ini文件内容逐行添加到改名后文件中,停止mysql服务,保存修改后文件,清空数据库,运行testInsert方法,观察运行时间,如果这条没有效果,继续添加下条配置)

后来就发现有一个配置起了作用

# If set to 1, InnoDB will flush (fsync) the transaction logs to the
# disk at each commit, which offers full ACID behavior. If you are
# willing to compromise this safety, and you are running small
# transactions, you may set this to 0 or 2 to reduce disk I/O to the
# logs. Value 0 means that the log is only written to the log file and
# the log file flushed to disk approximately once per second. Value 2
# means the log is written to the log file at each commit, but the log
# file is only flushed to disk approximately once per second.
innodb_flush_log_at_trx_commit = 2

一、当该配置为2的时候,代码还是如上,自动提交事务,则插入数据速度明显提升,和使用之前版本的my.ini文件几乎是相同的,

二、当该配置为1的时候,代码依然如上,自动提交事务,则插入数据速度又变得明显超慢

三、当该配置依然为1的时候,手动提交事务,即将上述代码con.setAutoCommit(false);和con.commit();的注释去掉,则插入数据也有明显提升,和第一种情况差不多,当插入数据过多时比第一种要快多一些

四、当该配置为2的时候,手动提交事务,即将上述代码con.setAutoCommit(false);和con.commit();的注释去掉,插入速度也会有明显提升。

五、去掉innodb_flush_log_at_trx_commit = 2配置,设置为手动提交事务,插入速度也会明显提升

六、去掉innodb_flush_log_at_trx_commit = 2配置,自动提交事务,速度又是明显超慢。

综上,如果用的是mysql5.6版本,要更改默认配置,目前我们采用的是将my-default.ini文件名改成,my.ini文件名,这样就可以在其中进行文件配置修改

通过上面测试结果也可看出,不管是配置为1或者2,只要是手动提交事务,则都可一提高插入速度,这个是在jdbc中进行的测试,至于hibernate需要就不清楚了

my.ini

  1. # For advice on how to change settings please see
  2. # http://dev.mysql.com/doc/refman/5.6/en/server-configuration-defaults.html
  3. # *** DO NOT EDIT THIS FILE. It's a template which will be copied to the
  4. # *** default location during install, and will be replaced if you
  5. # *** upgrade to a newer version of MySQL.
  6. [mysqld]
  7.  
  8. # generic configuration options
  9. port =
  10. socket = /tmp/mysql.sock
  11. character-set-server=utf8
  12.  
  13. innodb_print_all_deadlocks=
  14.  
  15. innodb_locks_unsafe_for_binlog=
  16.  
  17. expire_logs_days=
  18.  
  19. event_scheduler=
  20.  
  21. # If , InnoDB will flush (fsync) the transaction logs to the
  22. # disk at each commit, which offers full ACID behavior. If you are
  23. # willing to compromise this safety, and you are running small
  24. # transactions, you may or to reduce disk I/O to the
  25. # logs. Value means that the log is only written to the log file and
  26. # the log file flushed to disk approximately once per second. Value
  27. # means the log is written to the log file at each commit, but the log
  28. # file is only flushed to disk approximately once per second.
  29. innodb_flush_log_at_trx_commit =

mysql实验的更多相关文章

  1. MySQL实验准备(二)--Python模拟数据(MySQL数据库)

    Python模拟数据(MySQL数据库) 数据模拟 目的:模拟多个表的插入和查询数据的模拟,再通过基准测试脚本测试服务器性能和收集数据,仿真模拟. 备注: 如果需要基础的python环境,可以查看&l ...

  2. MySQL实验准备(一)--环境准备

    MySQL实验脚本准备(一) python操纵数据库的实验环境 安装pip 1.下载脚本文件 [root@db01 .vnc]# wget https://bootstrap.pypa.io/get- ...

  3. MySQL实验 内连接优化order by+limit 以及添加索引再次改进

    MySQL实验 内连接优化order by+limit 以及添加索引再次改进 在进行子查询优化双参数limit时我萌生了测试更加符合实际生产需要的ORDER BY + LIMIT的想法,或许我们也可以 ...

  4. MySQL实验 子查询优化双参数limit

    MySQL实验 子查询优化双参数limit 没想到双参数limit还有优化的余地,为了亲眼见到,今天来亲自实验一下.   实验准备 使用MySQL官方的大数据库employees进行实验,导入该示例库 ...

  5. Centos7+lnmp+zabbix4+分离mysql实验

    一.简介 1.什么是zabbix zabbix是一个企业级的.开源的.分布式的监控套件. zabbix可以对网络和服务进行完整性,健康性的监控.zabbix利用灵活的告警机制,可以实验微信,短信和邮件 ...

  6. JDBC入门之一--连接Mysql实验

    工具:mysql-connector-java-5.1.40.eclipse 1)首先要将mysql-connector-java包整合到eclipse中,右击项目,然后选择build path,出现 ...

  7. mysql 实验论证 innodb表级锁与行级锁

    innodb 的行锁是在有索引的情况下,没有索引的表是锁定全表的. 表锁演示(无索引) Session1: mysql> set autocommit=0; mysql> select * ...

  8. MySQL实验1: 新建一个名为 library 的数据库,包含 book、reader 两张表,根据自己的理解安排表的内容并插入数据。

    数据表(table)简称表,它是数据库最重要的组成部分之一.数据库只是一个框架,表才是实质内容. 实验: 新建一个名为 library的数据库,包含 book.reader两张表,根据自己的理解安排表 ...

  9. mysql 实验

    http://yangyaru0108.blog.51cto.com/6616699/1205001

随机推荐

  1. 仿IOS圆形下载进度条

    /** * Created by C058 on 2016/5/25. */ public class MyHoriztalProgressBar extends ProgressBar { priv ...

  2. PHPExcel设置数据格式的几种方法

    转自:http://www.cnblogs.com/guangxiaoluo/archive/2013/11/19/3431846.html 解决 PHPExcel 长数字串显示为科学计数 在exce ...

  3. Jmeter之录制脚本(二)

    上一节已经已经介绍过Jmeter的安装,对于web测试的话,经常会用到一些脚本去执行某些功能,也就是所谓的半自动化测试, 对于不懂代码的童鞋来说,脚本是一个很头疼的概念,badboy的录制是一个对于刚 ...

  4. Loadrunner参数化连接oracle、mysql数据源报错及解决办法

    Loadrunner参数化连接oracle.mysql数据源报错及解决办法 (本人系统是Win7 64,  两位小伙伴因为是默认安装lr,安装在 最终参数化的时候,出现连接字符串无法自动加载出来: 最 ...

  5. 通过jquery.transit.min.js插件,实现图片的移动

    首先给出插件:jquery.transit.min.js (function(t,e){if(typeof define==="function"&&define. ...

  6. express-5 质量保证(2)

    跨页测试 跨页测试更有挑战性,因为需要你控制和观测浏览器. 现在设置一个跨页测试情境的例子.比如,你的网站上有一个包含联系表单的Request Group Rate页面.营销部门想知道客户是从哪个页面 ...

  7. JavaScript 笔记 ( Prototype )

    这阵子实在好忙 ( 这样说好像也不是一两个月了... ),然后因为工作伙伴都是 JavaScript 神之等级的工程师,从中也学到不少知识,毕竟就是要和强者工作才会成长呀!为了想好好瞭解他们写的程式码 ...

  8. UVa12092 Paint the Roads(最小费用最大流)

    题目大概说一个n个点m条带权有向边的图,要给边染色,染色的边形成若干个回路且每个点都恰好属于其中k个回路.问最少要染多少边权和的路. 一个回路里面各个点的入度=出度=1,那么可以猜想知道各个点如果都恰 ...

  9. WPF – pass multiple parameters to a Command

    public class SendCommand : ICommand { public void Execute(object parameter) { var labels = ((object[ ...

  10. Oracle中Cursor的用法

    关键字 概念 类型 异常处理 一 概念 游标是SQL的一个内存工作区,由系统或用户以变量的形式定义.游标的作用就是用于临时存储从数据库中提取的数据块.在某些情况下,需要把数据从存放在磁 盘的表中 ...