MS Sql 優化步驟及優化not in一例

2022-02-21 13:59:09 字數 4432 閱讀 5162

今天接到客戶投訴說系統卡死了,經過一翻努力,終於解決了。現將解決步驟記錄一下,以便下次參考:

因為客戶系統集中在阿里雲上面,使用的是ms sql2008資料庫,上面有n個客戶,一下子無法知道是哪個客戶。

第一步,先開啟任務管理器,看看cpu使用情況,

一看就知道是 ms sql server有大查詢占用了所有的cpu時間,所以卡死系統 。

第二步,開啟ms sql server 的活動監控器,檢視是哪條語句卡死。

開啟活動監控器的方法。在 

中的物件資源管理器,找到伺服器,右擊。可以看到「活動監控器」,或者用快捷鍵ctrl+alt+a  .。

第三步,找到有問題 的語句。點開程序, 通過任務狀態,篩選 running的程序 。

逐個檢視執行中的語句,分析最有可能卡住 系統的語句,去執行一下。就可以查到是哪條語句卡住 了。

我的情況就是下面這句:

select a.ccrm, a.ccode as cordercode, a.drequire, a.dsubmit, b.*

, c.ccode

ascproductcode, b.cproductspec bcproductspec, a.dconfirm,

a.dcheck1, c.cparamter, c.cspec

ascproductspec, c.ccolor, a.ccreator

from orders a with

( nolock )

left

join orders_product b with ( nolock ) on a.cid =

b.cordersid

left

join product c with ( nolock ) on b.cproductid =

c.cid

left

join

(

--生產的產品id

select

distinct

a1.cproductid

from

dbo.product_item a1

left

join dbo.orders_productitem a2 on a1.cproductid =

a2.cproductid

where a1.iproduct !=

0) d

on b.cproductid =

d.cproductid

where1=

1and b.cproductid =

d.cproductid

and a.icancel ='0

'and ( istatus =

30or ( istatus =

20and inewcrm !=

1and

notexists ( select

1from orders_productitem with

( nolock )

where icustom =

1and cordersid =

a.cid )))

and ( a.istatuspp =

0or a.istatuspp =1)

andnot

exists ( select

1from

morders_product

left

join dbo.morders on morders.cid =

morders_product.cmordersid

where cordersproductid =

b.csubid

and dbo.morders.istatus !=2)

and b.icancelm =

0and b.csubid notin(

select

b.cordersproductid

from

dorders a

left

join dorders_sub b on a.cid =

b.cdordersid

where istatus =3)

order

by a.dudate desc;

經過分析定位到:not in 導至系統卡頓:

b.csubid notin(

select

b.cordersproductid

from

dorders a

left

join dorders_sub b on a.cid =

b.cdordersid

where istatus =

3

將not in 改為 not exists問題得以解決,系統正常運作。

改後的**為:

select a.ccrm, a.ccode as cordercode, a.drequire, a.dsubmit, b.*

, c.ccode

ascproductcode, b.cproductspec bcproductspec, a.dconfirm,

a.dcheck1, c.cparamter, c.cspec

ascproductspec, c.ccolor, a.ccreator

from orders a with

( nolock )

left

join orders_product b with ( nolock ) on a.cid =

b.cordersid

left

join product c with ( nolock ) on b.cproductid =

c.cid

left

join

(

--生產的產品id

select

distinct

a1.cproductid

from

dbo.product_item a1

left

join dbo.orders_productitem a2 on a1.cproductid =

a2.cproductid

where a1.iproduct !=

0) d

on b.cproductid =

d.cproductid

where1=

1and b.cproductid =

d.cproductid

and a.icancel ='0

'and ( istatus =

30or ( istatus =

20and inewcrm !=

1and

notexists ( select

1from orders_productitem with

( nolock )

where icustom =

1and cordersid =

a.cid )))

and ( a.istatuspp =

0or a.istatuspp =1)

andnot

exists ( select

1from

morders_product

left

join dbo.morders on morders.cid =

morders_product.cmordersid

where cordersproductid =

b.csubid

and dbo.morders.istatus !=2)

and b.icancelm =

0and

notexists

(

select

1from

dorders aa

left

join dorders_sub bb on aa.cid =

bb.cdordersid

where istatus =

3and b.csubid=

bb.cordersproductid)

order

by a.dudate desc;

MSSQL優化之索引優化

1。用count 統計比用count 字段 快。用count 主鍵 速度最快!2。select cid,title,cnt from table where cid 9999999 or riqi 2004 9 16 select cid,title,cnt from table where cid...

nginx 優化步驟

1 優化nginx 需要硬體與軟體上的優化,在保證硬體上的要求下,在軟體上的優化 2 先進行ab 壓強測試 3 資料大小的定義 壓力測試 模擬 前0 10 萬是熱資料,10 20萬是冷門資料 4 優化思路 nginx響應請求 1 建立 socket連線 2 開啟檔案,並沿 socket返回.排查問題...

SEO優化步驟

2 架構分析 3 目錄和頁面優化 seo不止是讓 首頁在搜尋引擎有好的排名,更重要的是讓 的每個頁面都帶來流量。4 內容發布和鏈結布置 5 與搜尋引擎對話 向各大搜尋引擎登陸入口提交尚未收錄站點。在搜尋引擎看seo的效果,通過site 你的網域名稱,知道站點的收錄和更新情況。通過domain 你的網...