mysql profiling的使用与sql分析

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/ypp91zr/article/details/85159032

mysql在5.0.3版本之后添加了profiling来进行性能分析

首先进入数据库

show profiles;

mysql默认是关闭的,也就是OFF

需要设置成1开启,也就是ON,注意设置以后退出窗口之后会还原,要永久生效貌似需要重启mysql才行

查看是否开启:

开启:set profiling=1

在这之后每次执行的sql语句都会被记录。

然后可以查看具体每个sql的具体情况,执行时间、CPU消耗等

添加测试数据测试:

我自己添加了测试100W+的数据,数据内容乱写的。本来想一次添加1000W的,电脑内存不够异常了。

测试数据代码:

@Test
	public void add() {
		java.sql.Connection conn = null;
		java.sql.PreparedStatement pstm = null;
		ResultSet rt = null;
		try {
			Class.forName("com.mysql.jdbc.Driver");
			conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/mypinyu?useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=true&useSSL=false",
					"root", "Goodlan@123");
			String sql = "INSERT INTO sys_operation_log"
					+ "(clazz,function,level,logger,message,create_date,ip,user_id,create_user_name,creater,type) "
					+ "VALUES(?,?,?,?,?,?,?,?,?,?,?)";
			pstm = conn.prepareStatement(sql);
			conn.setAutoCommit(false);
			Long startTime = System.currentTimeMillis();
			Random rand = new Random();
			int a, b, c, d;
			for (int i = 1; i <= 1000000; i++) {
				a = rand.nextInt(10);
				b = rand.nextInt(10);
				c = rand.nextInt(10);
				d = rand.nextInt(10);
				pstm.setString(1, "com.pinyu.system.web.controller.LoginController");
				pstm.setString(2, "login"+i);
				pstm.setString(3, "OPERATING");
				pstm.setString(4, "com.pinyu.system.web.controller.LoginController");
				pstm.setString(5, "xxxxxxxxxx_" + "188" + a + "88" + b + c + "66" + d);
				pstm.setDate(6, new java.sql.Date(System.currentTimeMillis()));
				pstm.setString(7, "171.221.144.197"+"."+a+b+c+d);
				pstm.setString(8, "4");
				pstm.setString(9, "admin"+i);
				pstm.setString(10, "ypp哈哈"+i);
				pstm.setString(11, "OPERATING");
				pstm.addBatch();
			}
			pstm.executeBatch();
			conn.commit();
			Long endTime = System.currentTimeMillis();
			System.err.println("OK,用时:" + (endTime - startTime));
		} catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException(e);
		} finally {
			if (pstm != null) {
				try {
					pstm.close();
				} catch (SQLException e) {
					e.printStackTrace();
					throw new RuntimeException(e);
				}
			}
			if (conn != null) {
				try {
					conn.close();
				} catch (SQLException e) {
					e.printStackTrace();
					throw new RuntimeException(e);
				}
			}
		}
	}

上面一次添加100W数据用了150秒,还能接受。表字段在10个左右

现在查询这张表数据,加上之前添加的测试数据,一共有120W+的测试数据。没有用字段名,直接*查询。看看sql的具体执行情况

执行了sql语句:select * from sys_operation_log where message like '%admin%';

接下来肯定是个漫长的等待过程,没用到索引,全表扫描等,数据量百万级,肯定不会快,等吧,目的主要分析profiles

再次执行命令:show profiles;

4是执行的sql语句

show profile all for query 4查看该sql所有执行信息

里面每一项,网上资料很多这里不一一介绍了。

测试完毕,关闭参数

set profiling=0

退出命令窗口。

猜你喜欢

转载自blog.csdn.net/ypp91zr/article/details/85159032
今日推荐