mysql性能优化及 Comparison method violates its general contract

  项目上嵌套结果集查询,查询的列表再根据每个id进行查询计算,嵌套的sql如下:

SELECT SUM(IFNULL(t.out_rate,0)) totalOutRate, SUM(IF(IFNULL(t.pre_power,0)-IFNULL(t.power,0)<0,0,IFNULL(t.pre_power,0)-IFNULL(t.power,0))) totalPower
FROM ( SELECT i.id id,i.`station_id` station_id,i.`out_rate` out_rate,i.`upload_datetime` upload_datetime,i.power 'power',@i.power pre_power,@i.power:=i.power tem FROM data_inverter i,
(SELECT @i.power:=0) s WHERE i.`upload_datetime`='201712131020' AND i.`del_flag`=0 AND i.`station_id`='f45ca4ba3xxxxxx28003f5ca83137f6c' ORDER BY i.`upload_datetime` DESC) t
WHERE t.station_id='f45ca4xxxxxxx03f5ca83137f6c'

当数据库数据较少时候都没问题,但是数据库数据到达一定数量后,请求服务总是超时,查看日志发现sql语句正常,但是总是报服务响应超时,然后就觉得是sql的性能,于是对上面语句进行explain分析如下:

对上面的station_id创建索引后

在对upload_datetime创建索引后如下:

这样一对比,性能明显好转多了,但是又遇到其他问题,我程序中用到了map排序,现在报错为:

 Comparison method violates its general contract!也就是说比较排序违反了一般规律。

然后网上查询最后更改代码如下解决

 1 Collections.sort(monitorList, new Comparator<Stu>() {
 2 @Override
 3 public int compare(Stu p1,Stu p2) {
 4 return p1.getWd().compareTo(p2.getWd());
 5 /*if(p1.getWd()>p2.getWd()){
 6 return 1;
 7 }
 8 else if(p1.getWd()==p2.gd()){
 9 return 0;
10 }
11 else{
12 return -1 ; // 调用String中的compareTo()方法
13 }*/
14 }
15 }

 之后,这个问题得到解决,然而当我将项目部署上以后,查询跨度拉大以后,又出现了服务器的问题错误信息主要包含:Last error is: Invoke remote method timeout.因为我们这个需求中没有关于分页的查询,所以,我怀疑是没有分页导致的问题,于是我就测试一下分页对查询性能的影响,首先,对mysql进行设置,查询后查看耗时

1 show profiles; 
2 show variables;查看profiling 是否是on状态; 
3 如果是off,则 set profiling = 1; 
4 执行自己的sql语句; 
5 show profiles;就可以查到sql语句的执行时间;

执行几次 后结果如下:

所以页容量越小查询效率也就越高。

原文地址:https://www.cnblogs.com/xiaoyao-001/p/10160414.html