MySQL查詢優化一例——也說說 Using intersect

生產上面有一條sql查詢很慢,須要7到8秒左右,簡化以後的sql以下所示:mysql

SELECT
	mingxi.*
FROM
	(
		SELECT
			btjc01.id,			
			department.`name` AS btjc01011,
			btjc01011 AS obj,
			btjc01.inibeginmonth AS beginYearMonth,
			lsbtsj.btje AS btjc01030,			
						
			CASE
		WHEN btjc01.inibeginmonth ='2019-10' THEN
			'是'
		ELSE
			'否'
		END AS isadd
		FROM
			btjc01
		INNER JOIN department ON department.Id = btjc01.btjc01038
		INNER JOIN lsbtsj ON lsbtsj.btrid = btjc01.Id
		WHERE
			btjc01.btjc01037 = '3'
		AND btjc01046 ='江西省'
		AND btjc01047 ='九江市'
		AND btjc01048 ='修水縣'		
		AND lsbtsj.btqsyf <='2019-10'
		AND lsbtsj.btjzyf >='2019-10'
		AND beginYearMonth != inibeginmonth
		UNION ALL
			SELECT
				btjc01.id,
						
				department.`name` AS btjc01011,
				btjc01011 AS obj,
				btjc01.beginYearMonth,
				btjc01.btjc01030,				
				
				CASE
			WHEN btjc01.inibeginmonth ='2019-10' THEN
				'是'
			ELSE
				'否'
			END AS isadd
			FROM
				btjc01 
			INNER JOIN department ON department.Id = btjc01.btjc01038
			INNER JOIN btgg03 ON btgg03.Id = btjc01.btjc01040
			WHERE
				btjc01.btjc01037 = '3'
		AND btjc01046 ='江西省'
		AND btjc01047 ='九江市'
		AND btjc01048 ='修水縣'
		AND (beginYearMonth <='2019-10')
	) AS mingxi

LIMIT 100,200;

 

執行計劃以下所示:sql

  能夠看到 執行計劃裏面出現了 Using intersect,btjc01046 btjc01048 btjc01037 btjc01047 多個索引來進行數據的過濾。mysql優化

另外發現一個問題,裏面的 union all 子查詢 卻只須要 0.4秒就能夠執行完成:優化

SELECT
			btjc01.id,			
			department.`name` AS btjc01011,
			btjc01011 AS obj,
			btjc01.inibeginmonth AS beginYearMonth,
			lsbtsj.btje AS btjc01030,			
						
			CASE
		WHEN btjc01.inibeginmonth ='2019-10' THEN
			'是'
		ELSE
			'否'
		END AS isadd
		FROM
			btjc01
		INNER JOIN department ON department.Id = btjc01.btjc01038
		INNER JOIN lsbtsj ON lsbtsj.btrid = btjc01.Id
		WHERE
			btjc01.btjc01037 = '3'
		AND btjc01046 ='江西省'
		AND btjc01047 ='九江市'
		AND btjc01048 ='修水縣'		
		AND lsbtsj.btqsyf <='2019-10'
		AND lsbtsj.btjzyf >='2019-10'
		AND beginYearMonth != inibeginmonth
		UNION ALL
			SELECT
				btjc01.id,
						
				department.`name` AS btjc01011,
				btjc01011 AS obj,
				btjc01.beginYearMonth,
				btjc01.btjc01030,				
				
				CASE
			WHEN btjc01.inibeginmonth ='2019-10' THEN
				'是'
			ELSE
				'否'
			END AS isadd
			FROM
				btjc01 
			INNER JOIN department ON department.Id = btjc01.btjc01038
			INNER JOIN btgg03 ON btgg03.Id = btjc01.btjc01040
			WHERE
				btjc01.btjc01037 = '3'
		AND btjc01046 ='江西省'
		AND btjc01047 ='九江市'
		AND btjc01048 ='修水縣'
		AND (beginYearMonth <='2019-10')  

 

執行計劃以下:blog

 

 比較一下前面和後面兩個的執行計劃,能夠發現 都是有了 Using intersect 可是呢後面一個卻只須要0.4秒,而在外面加上一層 select * from t,時間卻須要7到8秒。一直沒有明白什麼緣由。索引

最後的優化是去掉 Using interset ,使用 force index 手動指定使用索引 btjc01048:io

SELECT
	mingxi.*
FROM
	(
		SELECT
			btjc01.id,			
			department.`name` AS btjc01011,
			btjc01011 AS obj,
			btjc01.inibeginmonth AS beginYearMonth,
			lsbtsj.btje AS btjc01030,			
						
			CASE
		WHEN btjc01.inibeginmonth ='2019-10' THEN
			'是'
		ELSE
			'否'
		END AS isadd
		FROM
			btjc01 force index(btjc01048)
		INNER JOIN department ON department.Id = btjc01.btjc01038
		INNER JOIN lsbtsj ON lsbtsj.btrid = btjc01.Id
		WHERE
			btjc01.btjc01037 = '3'
		AND btjc01046 ='江西省'
		AND btjc01047 ='九江市'
		AND btjc01048 ='修水縣'		
		AND lsbtsj.btqsyf <='2019-10'
		AND lsbtsj.btjzyf >='2019-10'
		AND beginYearMonth != inibeginmonth
		UNION ALL
			SELECT
				btjc01.id,
						
				department.`name` AS btjc01011,
				btjc01011 AS obj,
				btjc01.beginYearMonth,
				btjc01.btjc01030,				
				
				CASE
			WHEN btjc01.inibeginmonth ='2019-10' THEN
				'是'
			ELSE
				'否'
			END AS isadd
			FROM
				btjc01 force index(btjc01048)
			INNER JOIN department ON department.Id = btjc01.btjc01038
			INNER JOIN btgg03 ON btgg03.Id = btjc01.btjc01040
			WHERE
				btjc01.btjc01037 = '3'
		AND btjc01046 ='江西省'
		AND btjc01047 ='九江市'
		AND btjc01048 ='修水縣'
		AND (beginYearMonth <='2019-10')
	) AS mingxi

LIMIT 100,200;

  執行時間須要1.2秒左右,和0.4秒相比,仍是有差距。執行計劃以下;class

 

 能夠看到沒有了 Using interset. 咱們強制使用索引 btjc01048.select

 

總結:nio

若是出現 Using interset 須要注意是否mysql優化器選擇的是不是最佳的索引方案。是否能夠經過force index來選擇更優的索引。

相關文章
相關標籤/搜索