SQL優化--inner、left join替換in、not in、except

来源:https://www.cnblogs.com/zhangdk/archive/2019/04/22/notintoleftjoin.html
-Advertisement-
Play Games

在in\not in\except這種查詢結構時,如果涉及到的數據量較大,建議堅決用連接left join/inner join等替換掉,否則查詢效率十分低下。 ...


新系統上線,用戶基數16萬,各種查詢timeout。打開砂鍋問到底,直接看sql語句吧,都是淚呀,一大堆in\not in\except。這裡總結一下,怎麼替換掉in\not in\except。

1. in/except->left join

查詢目的

根據

  • 客戶表(Customer,按照站點、冊本劃分,16萬數據)
  • 水錶表(Meter,16萬數據)
  • 水錶抄表數據表(Meter_Data,遠傳表每天更新,27萬數據)

關聯查詢,查詢某天某個冊本下水錶未上傳抄表數據的用戶。

原查詢結構

select * 
from Customer cs
where 
cs.Group_No = '冊本編號' and
cs.Customer_No in 
(
    select Customer_No 
    from  Customer cs
    left join Meter me on cs.Customer_No = me.Customer_No
    where cs.Group_No = '冊本編號'
    except
    select Customer_No
    from Customer cs
    left join Meter me on cs.Customer_No = me.Customer_No
    inner join Meter_data md on me.meter_no = md.meter_no and md.date = '2019-04-09'
    where cs.Group_NO='冊本編號'
)

原查詢思路

  1. 查詢出目標冊本已上傳數據的用戶編號
select Customer_No
from Customer cs
left join Meter me on cs.Customer_No = me.Customer_No
inner join Meter_data md on me.meter_no = md.meter_no and md.date = '2019-04-09'
where cs.Group_NO='冊本編號'
  1. 查詢出目標冊本全部用戶編號
select Customer_No 
from  Customer cs
left join Meter me on cs.Customer_No = me.Customer_No
where cs.Group_No = '冊本編號'
  1. 全部用戶編號中排除已上傳數據的用戶編號,即為未上傳數據的用戶編號
全部用戶編號 except 已抄表的用戶編號
  1. 查詢出在未抄表用戶編號集合中的用戶信息。
select * 
from Customer cs
where 
cs.Group_No = '冊本編號' and
cs.Customer_No in 
(全部用戶編號 except 已抄表的用戶編號)

思路倒是沒有問題,但是in+except查詢效率不要太慢了,本來想測試個時間,結果執行了幾分鐘愣是沒出結果,直接終止掉了

優化查詢結構

其實in\not in\except這些語法在查詢中使用,效率不高是公認的事實,但是可能是由於語義比較明顯吧,很多人還是喜歡這樣用。我們這裡使用left join來替代in+except。這裡就來改掉上面的查詢:

select cs.*
from Customer cs
left join Meter me on cs.Customer_No = me.Customer_No
left join Meter_data md on me.meter_no = md.meter_no and md.date = '2019-04-09'
where cs.Group_NO='冊本編號' and md.meter_no is null;

優化查詢思路

  1. 用left join代替in+except,通過left join獲取目標冊本下全部用戶的信息,並與當天上傳的抄表數據進行連接;
  2. 連接中,右表為空即抄表數據為空的,即為當前未上傳數據的客戶信息;

left join on expression where expression 執行時,首先確保左表數據全部返回,然後應用on後指定的條件。因此,on的條件如果是對左表數據的過濾,是無效的;對右表數據的過濾是有效的。對左表數據的過濾條件,需要放到where條件中。

2. not in->left join

上面in+except的寫法,可以使用not in簡化一下,但是一樣效率不高。這裡想要說明的是not in也可以很方便的使用left join替換。

not in結構

select * 
from Customer cs
where 
cs.Group_No = '冊本編號' and
cs.Customer_No not in 
(
    select Customer_No
    from Customer cs
    left join Meter me on cs.Customer_No = me.Customer_No
    inner join Meter_data md on me.meter_no = md.meter_no and md.date = '2019-04-09'
    where cs.Group_NO='冊本編號'
)

left join結構

select cs.*
from Customer cs
left join Meter me on cs.Customer_No = me.Customer_No
left join Meter_data md on me.meter_no = md.meter_no and md.date = '2019-04-09'
where cs.Group_NO='冊本編號' and md.meter_no is null;

3. in->inner join

查詢目的

還是上面的查詢背景,這裡查詢某天某個冊本已經上傳抄表數據的用戶信息。

in結構

select * 
from Customer cs
where 
cs.Group_No = '冊本編號' and
cs.Customer_No in 
(
    select Customer_No
    from Customer cs
    left join Meter me on cs.Customer_No = me.Customer_No
    inner join Meter_data md on me.meter_no = md.meter_no and md.date = '2019-04-09'
    where cs.Group_NO='冊本編號'
)

這裡使用in不夠高效,但是我們使用left join是否可以呢?

left join結構

select cs.*
from Customer cs
left join Meter me on cs.Customer_No = me.Customer_No
left join Meter_data md on me.meter_no = md.meter_no and md.date = '2019-04-09'
where cs.Group_NO='冊本編號' and md.meter_no is not null;

left join結構的話,這裡需要使用is not null作為篩選條件。但是is not null同樣非常低效。因此我們使用inner join

inner join結構

select cs.*
from Customer cs
left join Meter me on cs.Customer_No = me.Customer_No
inner join Meter_data md on me.meter_no = md.meter_no and md.date = '2019-04-09'
where cs.Group_NO='冊本編號';

inner join通過連接操作,直接獲取到已上傳抄表數據的用戶信息。

4. not in -> in -> inner join

前面的查詢場景中,我們預設的條件是未上傳抄表數據的用戶,當天在meter_data表是沒有記錄的。現在假設我們每天凌晨初始化meter_data表,設置抄表數值預設為零,抄表數據上傳預設為state=0未上傳。上傳後,更新抄表數值和抄表狀態state=1。

這時,我們來優化上面的not in查詢結構還有另外一種思路。

not in結構

select * 
from Customer cs
where 
cs.Group_No = '冊本編號' and
cs.Customer_No not in 
(
    select Customer_No
    from Customer cs
    left join Meter me on cs.Customer_No = me.Customer_No
    inner join Meter_data md on me.meter_no = md.meter_no and md.date = '2019-04-09'
    where cs.Group_NO='冊本編號' and meter.state=1
)

in結構

通過篩選條件取反,變換not in->in

select * 
from Customer cs
where 
cs.Group_No = '冊本編號' and
cs.Customer_No in 
(
    select Customer_No
    from Customer cs
    left join Meter me on cs.Customer_No = me.Customer_No
    inner join Meter_data md on me.meter_no = md.meter_no and md.date = '2019-04-09'
    where cs.Group_NO='冊本編號' and meter.state=0
)

inner join結構

select cs.*
from Customer cs
left join Meter me on cs.Customer_No = me.Customer_No
inner join Meter_data md on me.meter_no = md.meter_no and md.date = '2019-04-09'
where cs.Group_NO='冊本編號' and meter.state=0;

5. 總結如下

上面的查詢結構拆分出來後,大家可能覺得這麼簡單的sql怎麼可能寫成這個沙雕。其實真實業務系統,還有關聯其他將近10張表。這裡想說的是,在in\not in\except這種查詢結構時,如果涉及到的數據量較大,建議堅決用連接替換。

  • ... in (all except sub)... 查詢結構可以轉換為->left join
  • ... not in ... 查詢結構可以轉換為->left join
  • ... not in ... 查詢也可以轉換為 in -> inner join,這裡需要確認轉換查詢條件時,是否有對應的數據
  • ... in 查詢結構可以轉換為->inner join

您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • SQL操作符 算術操作符:+加, 減, 乘,/除 比較操作符: ,=,!=,,= 常用的判斷,和!=相同 between $lower_val$ and $hight_val$ between .. and.. 包括兩端 查詢20 ...
  • 這一篇筆記的mysql優化是註重於查詢優化,根據mysql的執行情況,判斷mysql什麼時候需要優化,關於資料庫開始階段的資料庫邏輯、物理結構的設計結構優化不是本文重點,下次再談 查看mysql語句的執行情況,判斷是否需要進行優化 以下分別通過java程式員可分析的前三個方面來討論mysql語句的查 ...
  • 1.按姓氏筆畫排序:Select * From TableName Order By CustomerName Collate Chinese_PRC_Stroke_ci_as 2.資料庫加密:select encrypt('原始密碼')select pwdencrypt('原始密碼')select ...
  • mysql優化二之鎖機制 mysql提供了鎖機制和MVCC機制來保證併發操作的安全性,這裡主要討論鎖機制, MVCC見下篇文章 mysql的鎖按照鎖粒度可分為行鎖與表鎖,按照操作類型劃分可讀鎖和寫鎖 InnoDB存儲引擎支持表鎖和行鎖,預設鎖為行鎖,MyIsam只支持表鎖 鎖粒度越高則併發性越好 表 ...
  • SQL中只有兩列數據(欄位1,欄位2),將其相同欄位1的行轉列 轉換前: 轉換後: 轉自:https://bbs.csdn.net/topics/392320974 ...
  • 一.Spark2.0的新特性Spark讓我們引以為豪的一點就是所創建的API簡單、直觀、便於使用,Spark 2.0延續了這一傳統,併在兩個方面凸顯了優勢: 1、標準的SQL支持; 2、數據框(DataFrame)/Dataset (數據集)API的統一。 在SQL方面,我們已經對Spark的SQL ...
  • 前言 關於資料庫鎖,是一個很重要的知識點; 不少人在開發的時候,應該 很少會註意到 這些鎖的問題,也很少會給程式加鎖(除了 庫存 這些對數量準確性要求極高的情況下); 一般也就聽過常說的樂觀鎖和悲觀鎖,瞭解過基本的含義之後就沒了,沒有去實際的操作過,本文將簡單的整理一下資料庫鎖的知識,希望對大家有所 ...
  • 數據導出時,出現錯誤: 一臉懵逼,百度了下,是導出數量有格式有限制。一開始導出為excel表格式,後改為文本格式,不會報錯。 ...
一周排行
    -Advertisement-
    Play Games
  • 示例項目結構 在 Visual Studio 中創建一個 WinForms 應用程式後,項目結構如下所示: MyWinFormsApp/ │ ├───Properties/ │ └───Settings.settings │ ├───bin/ │ ├───Debug/ │ └───Release/ ...
  • [STAThread] 特性用於需要與 COM 組件交互的應用程式,尤其是依賴單線程模型(如 Windows Forms 應用程式)的組件。在 STA 模式下,線程擁有自己的消息迴圈,這對於處理用戶界面和某些 COM 組件是必要的。 [STAThread] static void Main(stri ...
  • 在WinForm中使用全局異常捕獲處理 在WinForm應用程式中,全局異常捕獲是確保程式穩定性的關鍵。通過在Program類的Main方法中設置全局異常處理,可以有效地捕獲並處理未預見的異常,從而避免程式崩潰。 註冊全局異常事件 [STAThread] static void Main() { / ...
  • 前言 給大家推薦一款開源的 Winform 控制項庫,可以幫助我們開發更加美觀、漂亮的 WinForm 界面。 項目介紹 SunnyUI.NET 是一個基於 .NET Framework 4.0+、.NET 6、.NET 7 和 .NET 8 的 WinForm 開源控制項庫,同時也提供了工具類庫、擴展 ...
  • 說明 該文章是屬於OverallAuth2.0系列文章,每周更新一篇該系列文章(從0到1完成系統開發)。 該系統文章,我會儘量說的非常詳細,做到不管新手、老手都能看懂。 說明:OverallAuth2.0 是一個簡單、易懂、功能強大的許可權+可視化流程管理系統。 有興趣的朋友,請關註我吧(*^▽^*) ...
  • 一、下載安裝 1.下載git 必須先下載並安裝git,再TortoiseGit下載安裝 git安裝參考教程:https://blog.csdn.net/mukes/article/details/115693833 2.TortoiseGit下載與安裝 TortoiseGit,Git客戶端,32/6 ...
  • 前言 在項目開發過程中,理解數據結構和演算法如同掌握蓋房子的秘訣。演算法不僅能幫助我們編寫高效、優質的代碼,還能解決項目中遇到的各種難題。 給大家推薦一個支持C#的開源免費、新手友好的數據結構與演算法入門教程:Hello演算法。 項目介紹 《Hello Algo》是一本開源免費、新手友好的數據結構與演算法入門 ...
  • 1.生成單個Proto.bat內容 @rem Copyright 2016, Google Inc. @rem All rights reserved. @rem @rem Redistribution and use in source and binary forms, with or with ...
  • 一:背景 1. 講故事 前段時間有位朋友找到我,說他的窗體程式在客戶這邊出現了卡死,讓我幫忙看下怎麼回事?dump也生成了,既然有dump了那就上 windbg 分析吧。 二:WinDbg 分析 1. 為什麼會卡死 窗體程式的卡死,入口門檻很低,後續往下分析就不一定了,不管怎麼說先用 !clrsta ...
  • 前言 人工智慧時代,人臉識別技術已成為安全驗證、身份識別和用戶交互的關鍵工具。 給大家推薦一款.NET 開源提供了強大的人臉識別 API,工具不僅易於集成,還具備高效處理能力。 本文將介紹一款如何利用這些API,為我們的項目添加智能識別的亮點。 項目介紹 GitHub 上擁有 1.2k 星標的 C# ...