一次SQL查询语句的优化
1.项目中之前的"我关注的拍品列表"需要添加筛选功能,因为目前显示的关注的拍品太多没有进行分类,用户体验差。
2.添加筛选条件之后,可以筛选出“未开始”“进行中”“已结束”三种情况的拍品。
其中
“未开始”--->状态为 1
“进行中”---->状态为 2
“已结束”---->状态为 3 or 4 or 5 or 6 or 7
3.优化之前,每一个条件都书写了一个SQL 语句。 功能能够实现,但是代码比较冗余。
//未开始状态
@Query(value="SELECT a.id,a.auc_id,a.crt_time,a.crt_user_id,a.crt_user_name,a.customer_id,a.upd_time,a.upd_user_id,a.upd_user_name FROM auc_attention a,auc_lot b WHERE a.auc_id = b.id "
+ "AND a.customer_id = ?1 AND ( b.STATUS = 1) order by a.crt_time desc limit ?2,?3",nativeQuery=true)
List<AucAttention> findAllByCustomerIdAndAucLotStatus1(Long customerId,
int i, int rows); @Query(value="select count(*) from (SELECT a.id FROM auc_attention a,auc_lot b WHERE a.auc_id = b.id "
+ "AND a.customer_id = ?1 AND ( b.STATUS = 1)) as temp",nativeQuery=true)
int findAllCountByCustomerIdAndAucLotStatus1(Long customerId); //进行中状态
@Query(value="SELECT a.id,a.auc_id,a.crt_time,a.crt_user_id,a.crt_user_name,a.customer_id,a.upd_time,a.upd_user_id,a.upd_user_name FROM auc_attention a,auc_lot b WHERE a.auc_id = b.id "
+ "AND a.customer_id = ?1 AND ( b.STATUS = 2) order by a.crt_time desc limit ?2,?3",nativeQuery=true)
List<AucAttention> findAllByCustomerIdAndAucLotStatus2(Long customerId,
int i, int rows); @Query(value="select count(*) from (SELECT a.id FROM auc_attention a,auc_lot b WHERE a.auc_id = b.id "
+ "AND a.customer_id = ?1 AND ( b.STATUS = 2)) as temp",nativeQuery=true)
int findAllCountByCustomerIdAndAucLotStatus2(Long customerId); //已结束状态
@Query(value="SELECT a.id,a.auc_id,a.crt_time,a.crt_user_id,a.crt_user_name,a.customer_id,a.upd_time,a.upd_user_id,a.upd_user_name FROM auc_attention a,auc_lot b WHERE a.auc_id = b.id "
+ "AND a.customer_id = ?1 AND ( b.STATUS != 1 AND b.STATUS != 2) order by a.crt_time desc limit ?2,?3",nativeQuery=true)
List<AucAttention> findAllByCustomerIdAndAucLotStatus3(Long customerId,
int i, int rows); @Query(value="select count(*) from (SELECT a.id FROM auc_attention a,auc_lot b WHERE a.auc_id = b.id "
+ "AND a.customer_id = ?1 AND ( b.STATUS != 1 AND b.STATUS != 2)) as temp",nativeQuery=true)
int findAllCountByCustomerIdAndAucLotStatus3(Long customerId); //默认状态
@Query(value="SELECT a.id,a.auc_id,a.crt_time,a.crt_user_id,a.crt_user_name,a.customer_id,a.upd_time,a.upd_user_id,a.upd_user_name FROM auc_attention a,auc_lot b WHERE a.auc_id = b.id "
+ "AND a.customer_id = ?1 order by a.crt_time desc limit ?2,?3",nativeQuery=true)
List<AucAttention> findAllByCustomerIdAndAucLotStatus(Long customerId,
int i, int rows); @Query(value="select count(*) from (SELECT a.id FROM auc_attention a,auc_lot b WHERE a.auc_id = b.id "
+ "AND a.customer_id = ?1 ) as temp",nativeQuery=true)
int findAllCountByCustomerIdAndAucLotStatus(Long customerId);
4.优化之后,公用一个SQL语句即可。
控制器方法:
/**
* 返回所有的我关注的拍品列表 筛选条件 未开始,进行中,已结束
*
* 1 未开始 2 进行中 3 已结束
*
* @param request
* @return
*/
@RequestMapping(value = "/auctionFocusOnList", method = RequestMethod.POST)
@ResponseBody
public ResultDTO<Map<String, Object>> findAllMyFocusOn(Integer checkStatus, int page, int rows,
HttpServletRequest request) {
if (checkStatus == null) {
checkStatus = 0;
} Map<String, Object> retMap = null;
List<Map<String, Object>> focusOnList = new ArrayList<>(); // 获取session中的登陆对象
Customer user = (Customer) request.getSession().getAttribute("user"); List<AucAttention> aucAttentionList = null; // 获取所有的关注的拍品的条件
if (null != user) {
Long customerId = Long.valueOf(user.id());
int total = 0; if (checkStatus == 1) {// 未开始
aucAttentionList = aucAttentionRepository.findAllByCustomerIdAndAucLotStatus(customerId,
(page - 1) * rows, rows, 1, 0, 0, 0, 0, 0, 0);
total = aucAttentionRepository.findAllCountByCustomerIdAndAucLotStatus(customerId, 1, 0, 0, 0, 0, 0, 0); } else if (checkStatus == 2) {// 进行中 aucAttentionList = aucAttentionRepository.findAllByCustomerIdAndAucLotStatus(customerId,
(page - 1) * rows, rows, 2, 0, 0, 0, 0, 0, 0);
total = aucAttentionRepository.findAllCountByCustomerIdAndAucLotStatus(customerId, 2, 0, 0, 0, 0, 0, 0);
} else if (checkStatus == 3) {// 已结束 aucAttentionList = aucAttentionRepository.findAllByCustomerIdAndAucLotStatus(customerId,
(page - 1) * rows, rows, 3, 4, 5, 6, 7, 0, 0);
total = aucAttentionRepository.findAllCountByCustomerIdAndAucLotStatus(customerId, 3, 4, 5, 6, 7, 0, 0);
} else {// 默认情况 aucAttentionList = aucAttentionRepository.findAllByCustomerIdAndAucLotStatus(customerId,
(page - 1) * rows, rows, 1, 2, 3, 4, 5, 6, 7);
total = aucAttentionRepository.findAllCountByCustomerIdAndAucLotStatus(customerId, 1, 2, 3, 4, 5, 6, 7);
} Map<String, Object> map = null;
AucLot aucLot = null;
int auctionStatus = 4;
String startTime = null;
String endTime = null;
String aucLotTime = null;
SimpleDateFormat dateFormater = new SimpleDateFormat("yyyy.MM.dd");// 格式化时间 for (AucAttention aucAttention : aucAttentionList) {
map = new HashMap<String, Object>(); aucLot = aucLotRepository.findOne(aucAttention.aucId()); if (aucLot != null) {
startTime = dateFormater.format(aucLot.startTime());// 拍卖开始时间
endTime = dateFormater.format(aucLot.endTime());// 拍卖结束时间 // 定义拍卖时间,开始时间和结束时间的拼接
if (startTime.equals(endTime)) {
aucLotTime = startTime;
} else {
aucLotTime = startTime + "-" + endTime;
} map.put("aucLotId", aucLot.id());// 拍卖id
map.put("goodsName", aucLot.goodsName());// 拍品名称
map.put("aucLotTime", aucLotTime);// 拍品时间
map.put("status", aucLot.status());// 拍卖状态 // 关注的拍品的拍卖资质对象的条件
Specification<AucBrand> aucBrandSpec = (root, query, cb) -> {
List<Predicate> predicates = new ArrayList<Predicate>();
if (null != aucAttention) {
Predicate predicate = cb.equal(root.get(AucBrand_.customerId), aucAttention.customerId());// 竞买人id
predicates.add(predicate);
}
if (null != aucAttention) {
Predicate predicate = cb.equal(root.get(AucBrand_.aucLot).get(AucLot_.id),
aucAttention.aucId());// 拍品id
predicates.add(predicate);
}
if (!predicates.isEmpty()) {
return cb.and(predicates.toArray(new Predicate[0]));
} else {
return null;
}
}; List<AucBrand> aucBrandList = aucBrandRepository.findAll(aucBrandSpec);
if (aucBrandList != null && aucBrandList.size() > 0) {
AucBrand aucBrand = aucBrandList.get(0);
if (aucBrand != null) {
boolean bailPayed = aucBrand.isBailPayed();// 保证金是否支付
int isDeal = aucBrand.isDeal();// 成交状态
int auditType = aucBrand.auditType();// 审核状态
map.put("bailPayed", bailPayed);
map.put("auditType", auditType);
// 获取保证金主键id值
List<SettlementBail> settlementBails = settlementBailRepository
.findAllByBrandId(aucBrand.id());
if (settlementBails != null && settlementBails.size() > 0) {
SettlementBail settlementBail = settlementBails.get(0);
if (settlementBail != null) {
map.put("settlementBailId", settlementBail.id());
} else {
map.put("settlementBailId", null);
}
} // 判断竞拍情况
if (bailPayed == true) {
// 成交
if (isDeal == 1) {
auctionStatus = 1;// "已成交"
} else {
auctionStatus = 2;// "已参拍"
}
} else {
// 保证金未支付
auctionStatus = 3;// "已报名"
}
map.put("auctionStatus", auctionStatus);
} else {
// 没有参拍资质
auctionStatus = 4;// "未报名"
map.put("bailPayed", null);
map.put("auditType", null);
map.put("settlementBailId", null);
map.put("auctionStatus", auctionStatus);// 竞拍情况
}
} else {
auctionStatus = 4;// "未报名"
map.put("bailPayed", null);
map.put("auditType", null);
map.put("settlementBailId", null);
map.put("auctionStatus", auctionStatus);// 竞拍情况
}
focusOnList.add(map);
} else {
continue;
}
}
retMap = new HashMap<String, Object>();
retMap.put("focusOnList", focusOnList);
retMap.put("total", total);
return new ResultDTO<Map<String, Object>>(retMap);
} else {
return new ResultDTO<Map<String, Object>>(700, "用户未登录", null);
}
}
SQL语句:
@Query(value="SELECT a.id,a.auc_id,a.crt_time,a.crt_user_id,a.crt_user_name,a.customer_id,a.upd_time,a.upd_user_id,a.upd_user_name FROM auc_attention a,auc_lot b WHERE a.auc_id = b.id "
+ "AND a.customer_id = ?1 AND ( b.STATUS = ?4 or b.STATUS = ?5 or b.STATUS = ?6 or b.STATUS = ?7 or b.STATUS = ?8 or b.STATUS = ?9 or b.STATUS = ?10) order by a.crt_time desc limit ?2,?3",nativeQuery=true)
List<AucAttention> findAllByCustomerIdAndAucLotStatus(Long customerId,
int i, int rows,int checkStatus1,int checkStatus2,int checkStatus3,int checkStatus4,int checkStatus5,int checkStatus6,int checkStatus7); @Query(value="select count(*) from (SELECT a.id FROM auc_attention a,auc_lot b WHERE a.auc_id = b.id "
+ "AND a.customer_id = ?1 AND ( b.STATUS = ?2 or b.STATUS = ?3 or b.STATUS = ?4 or b.STATUS = ?5 or b.STATUS = ?6 or b.STATUS = ?7 or b.STATUS = ?8)) as temp",nativeQuery=true)
int findAllCountByCustomerIdAndAucLotStatus(Long customerId,int checkStatus1,int checkStatus2,int checkStatus3,int checkStatus4,int checkStatus5,int checkStatus6,int checkStatus7);
一次SQL查询语句的优化的更多相关文章
- 深入MySQL(四):MySQL的SQL查询语句性能优化概述
关于SQL查询语句的优化,有一些一般的优化步骤,本节就介绍一下通用的优化步骤. 一条查询语句是如何执行的 首先,我们如果要明白一条查询语句所运行的过程,这样我们才能针对过程去进行优化. 参考我之前画的 ...
- SQL查询语句优化的实用方法
查询语句的优化是SQL效率优化的一个方式,可以通过优化sql语句来尽量使用已有的索引,避免全表扫描,从而提高查询效率.最近在对项目中的一些sql进行优化,总结整理了一些方法. 1.在表中建立索引,优先 ...
- 浅谈SQL优化入门:1、SQL查询语句的执行顺序
1.SQL查询语句的执行顺序 (7) SELECT (8) DISTINCT <select_list> (1) FROM <left_table> (3) <join_ ...
- sql查询语句优化
http://www.cnblogs.com/dubing/archive/2011/12/09/2278090.html 最近公司来一个非常虎的dba 10几年的经验 这里就称之为蔡老师吧 在征得 ...
- 高性能MySql进化论(十一):常见查询语句的优化
总结一下常见查询语句的优化方式 1 COUNT 1. COUNT的作用 · COUNT(table.filed)统计的该字段非空值的记录行数 · ...
- oracle中sql查询语句的执行顺序
查询语句的处理过程主要包含3个阶段:编译.执行.提取数据(sql查询语句的处理主要是由用户进程和服务器进程完成的,其他进程辅助配合) 一.编译parse 在进行编译时服务器进程会将sql语句的正文放入 ...
- MySQL 笔记整理(1) --基础架构,一条SQL查询语句如何执行
最近在学习林晓斌(丁奇)老师的<MySQL实战45讲>,受益匪浅,做一些笔记整理一下,帮助学习.如果有小伙伴感兴趣的话推荐原版课程,很不错. 1) --基础架构,一条SQL查询语句如何执行 ...
- 【转】同一个SQL查询语句,为什么使用到的索引不同?
问: 同一个SQL查询语句,只是修改where条件中的一个值,为什么使用到的索引情况也会不同?谢谢! 1) explain执行结果,如下图: 2) 表中的数据如下图: 3) 表结构如下图: 4) 创建 ...
- Oracle执行SQL查询语句的步骤
Oracle执行SQL查询语句的步骤 如果用户在SQL*Plus下输入了如下查询语句:SELECT * FROM dept: 查询语句的处理主要包括三个过程:编译(parse).执行(execute) ...
随机推荐
- AndroidStudio升到最新版本(3.1.2)之后
暂时发现的需要大家注意的地方 1.androidstudio3无法导入moudle? 例如:我写了一个简单的项目,需要导入一个第三方的moudle,我导入: 因为AS升级之后,没有突出颜色的变化(变黑 ...
- Python爬虫-正则表达式基础
import re #常规匹配 content = 'Hello 1234567 World_This is a Regex Demo' #result = re.match('^Hello\s\d\ ...
- spring_150907_sqlmapclientdaosupport_getSqlMapClient
1.新建java工程:spring_150907_sqlmapclientdaosupport_getSqlMapClient,如下图所示: 2.工程里添加spring.hibernate.ibati ...
- 【面试总结-编程】多行两列数据,实现同key的value求和并输出
一个文件,两列,多行. 第一列是字母,第二列是数字,同列数据之间通过空格分割. 统计首列字母相同的第二列之和. 样例输入: A 5 B 6 OO 7 A 6 A 2 OO 2 输出: A:13 B:6 ...
- 在phpWeChat里生成一个临时二维码(非微信二维码)
phpWeChat作为支持Pc+H5开发的管理系统,内置一套二维码生成API,访问地址: 您的域名/api/qrcode/index.php?data=二维码数据 以下为使用示例 使用时有一点需要注意 ...
- python 写一个贪吃蛇游戏
#!usr/bin/python #-*- coding:utf-8 -*- import random import curses s = curses.initscr() curses.curs_ ...
- [BZOJ2281][SDOI2011]黑白棋(K-Nim博弈)
2281: [Sdoi2011]黑白棋 Time Limit: 3 Sec Memory Limit: 512 MBSubmit: 626 Solved: 390[Submit][Status][ ...
- HDU1251 统计难题 trie树 简单
http://acm.hdu.edu.cn/showproblem.php?pid=1251 题意: 找前缀数量 裸模板 #include<cstdio> #include<cstr ...
- 【洛谷】P1631: 序列合并
P1631 序列合并 题目描述 有两个长度都是N的序列A和B,在A和B中各取一个数相加可以得到N2个和,求这N2个和中最小的N个. 输入输出格式 输入格式: 第一行一个正整数N: 第二行N个整数Ai ...
- 数组中找出最小的K个数
题目 给出一个数组,找出K个最小的值 例如给出数组{5,2,4,3,1},给定K值3,则输出结果为{2,3,1} 程序 先给出第一个版本的程序 public static void printKNum ...