iT邦幫忙

0

SQL SERVER SELECT 問題請教

  • 分享至 

  • xImage

您好:

同一個TABLE, 類別不一樣

typex,barcode, ordno , barcode2
11 ,XXXX , 45446787-XDD ,YYY
12 ,YYY , 4544SDDD-XDD ,XXX

select *
from (
select typex,barcode, ordno , barcode2
from TABLE_A
where type=11 and ...
) T11 left join (
select typex,barcode, ordno , barcode2
from TABLE_A
where type=12 and ...
) T12 on T11.barcode=T12.barcode

目前 2個分開抓,都很快
LEFT JOIN 抓 ,只要 T12.ordno 不select 也很快

只要甚至 單獨select T12.ORDNO ,就變慢很多 快10秒

用實際執行計畫 看,也沒有出現 索引遺漏
查詢成本 兩個比較 都是50%

請問這該如何去DEBUG?

看更多先前的討論...收起先前的討論...
select * 不慢才有鬼
原則上你需要哪些欄位一個一個放進來,不要直接用 *
這麼說好了 * 是逐欄查詢,同樣十各欄位,有精準目標的話就是直接讀取
沒有精準目標就是逐欄、遞迴、迴圈,怎麼會不慢
rain_yu iT邦新手 1 級 ‧ 2023-10-05 08:25:43 檢舉
十分同意樓上觀點
dennislin iT邦新手 4 級 ‧ 2023-10-06 16:14:18 檢舉
窮大觀點+1
只有最一開始要做表或查詢才會用select *看有什麼 ,真正開始要寫指令請把需要的欄位分別加進去
noway iT邦研究生 2 級 ‧ 2023-10-06 16:31:04 檢舉
不好意思,沒寫清楚
我只是先 抓幾個欄位出來 說明 typex,barcode, ordno , barcode2
我SELECT 都有 先把需要的欄位抓出來
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

1 個回答

0
優質野林豬
iT邦新手 5 級 ‧ 2023-10-04 11:43:28

直接join

select *
  from tabA as t1
  join tabA as t2
    on t1.barcode = t2.barcode
 where t1.typex = 11
   and t2.typex = 12
   and 其他條件

我要發表回答

立即登入回答