SELECT b.CallerNum, b.Duration duration, min(b.CRT_TIME_) abnormalTime, count(b.CallerNum) abnormalCallNumber, count(b.CallerNum) abnormalCallTotal FROM basebill b left join billdetails b2 on b.base_bill_id =b2.base_bill_id where b2.area_code_ != 86 and b.billType = 1 and b.IS_DELETE_ = 0 and b.CRT_TIME_ >= '2023-06-06 13:00:00' and b.CRT_TIME_ <= '2023-06-06 15:00:00' and ( (b.Duration >=900) or (b.CallerNum in (SELECT b.CallerNum FROM basebill b left join billdetails b2 on b.base_bill_id =b2.base_bill_id where b2.area_code_ != 86 and b.billType = 1 and b.IS_DELETE_ = 0 and b.CRT_TIME_ >= '2023-06-06 13:00:00' and b.CRT_TIME_ <= '2023-06-06 15:00:00' GROUP BY b.CallerNum HAVING COUNT(b.CallerNum) >= 5)) or ((SELECT COUNT(*) FROM basebill b1 left join billdetails b2 on b1.base_bill_id =b2.base_bill_id where b2.area_code_ != 86 and b1.billType = 1 and b1.IS_DELETE_ = 0 and b1.CRT_TIME_ >= '2023-06-06 13:00:00' and b1.CRT_TIME_ <= '2023-06-06 15:00:00' and b1.CallerNum = b.CallerNum ) >= 10) ) group by b.CallerNum优化这条sql的查询速度,具体做法
时间: 2024-03-23 12:42:47 浏览: 283
SQL_select.rar_sql 语法
具体做法如下:
1. 对where条件中的字段建立索引:针对b2.area_code_、b.billType、b.IS_DELETE_、b.CRT_TIME_、b.Duration、b.CallerNum等字段建立相应的索引,可以提高查询效率。
2. 避免使用子查询:将子查询改写成join操作,可以提高查询速度。例如:
```
SELECT b.CallerNum, b.Duration duration, min(b.CRT_TIME_) abnormalTime, count(b.CallerNum) abnormalCallNumber, count(b.CallerNum) abnormalCallTotal
FROM basebill b
LEFT JOIN billdetails b2 ON b.base_bill_id = b2.base_bill_id
LEFT JOIN (SELECT CallerNum, COUNT(*) AS cnt FROM basebill WHERE IS_DELETE_ = 0 AND CRT_TIME_ >= '2023-06-06 13:00:00' AND CRT_TIME_ <= '2023-06-06 15:00:00' GROUP BY CallerNum HAVING COUNT(*) >= 5) AS t1 ON b.CallerNum = t1.CallerNum
LEFT JOIN (SELECT CallerNum, COUNT(*) AS cnt FROM basebill WHERE IS_DELETE_ = 0 AND CRT_TIME_ >= '2023-06-06 13:00:00' AND CRT_TIME_ <= '2023-06-06 15:00:00' GROUP BY CallerNum HAVING COUNT(*) >= 10) AS t2 ON b.CallerNum = t2.CallerNum
WHERE b2.area_code_ != 86 AND b.billType = 1 AND b.IS_DELETE_ = 0 AND b.CRT_TIME_ >= '2023-06-06 13:00:00' AND b.CRT_TIME_ <= '2023-06-06 15:00:00' AND (b.Duration >= 900 OR t1.CallerNum IS NOT NULL OR t2.CallerNum IS NOT NULL)
GROUP BY b.CallerNum
```
3. 避免重复查询:在where条件和select语句中都有重复查询的情况,可以将重复查询的部分提取出来,减少查询次数。例如:
```
SELECT b.CallerNum, b.Duration duration, min(b.CRT_TIME_) abnormalTime, count(b.CallerNum) abnormalCallNumber, count(b.CallerNum) abnormalCallTotal
FROM basebill b
LEFT JOIN billdetails b2 ON b.base_bill_id = b2.base_bill_id
LEFT JOIN (SELECT CallerNum, COUNT(*) AS cnt FROM basebill WHERE IS_DELETE_ = 0 AND CRT_TIME_ >= '2023-06-06 13:00:00' AND CRT_TIME_ <= '2023-06-06 15:00:00' GROUP BY CallerNum HAVING COUNT(*) >= 5) AS t1 ON b.CallerNum = t1.CallerNum
LEFT JOIN (SELECT CallerNum, COUNT(*) AS cnt FROM basebill WHERE IS_DELETE_ = 0 AND CRT_TIME_ >= '2023-06-06 13:00:00' AND CRT_TIME_ <= '2023-06-06 15:00:00' GROUP BY CallerNum HAVING COUNT(*) >= 10) AS t2 ON b.CallerNum = t2.CallerNum
WHERE b2.area_code_ != 86 AND b.billType = 1 AND b.IS_DELETE_ = 0 AND b.CRT_TIME_ BETWEEN '2023-06-06 13:00:00' AND '2023-06-06 15:00:00' AND (b.Duration >= 900 OR t1.CallerNum IS NOT NULL OR t2.CallerNum IS NOT NULL)
GROUP BY b.CallerNum, b.Duration, abnormalTime
```
4. 分批查询:将查询时间段分成多个小段,避免一次查询过多数据,导致查询速度变慢。
5. 对于大表使用分区表:如果表中数据量较大,可以将表进行分区,以提高查询效率。
6. 定期清理无用数据:定期清理已经无用的数据,可以减少查询时的数据量,提高查询速度。
综上所述,以上优化方案可以针对不同的情况进行选择,以提高SQL语句的查询速度。
阅读全文