2017-07-18 65 views
-1

我有一個包含300列和超過1.2億條記錄的表。我想執行一個查詢,如SQL查詢 - 包含300列和1.2億條記錄的表

select * 
from tablename 
where code in ('aa','bb', ..) 
    and column1 in ('a11','a22',..) 

有大約50多列需要應用條件。

執行此查詢需要兩個多小時。有沒有辦法改善這一點?

enter image description here

Select查詢語句是:

SELECT Number, COUNT(Code) AS Volume 
FROM TableNameTest 
WHERE LEN(Number) >= 5 
    AND Code IN ('851','852','853','870','871','872','002','004','006','013','017') 
    AND (ColumnName IN ('N179','R9002','R40.2240','R40.2241','R40.2242','R40.2243','R40.2244','R40.2245','R40.2246','R40.2247','R40.2248','R40.2249','R40.2230','R40.2231','R40.2232','R40.2233','R40.2234','R40.2235','R40.2236','R40.2237','R40.2238','R40.2239','N289','R410','R34','N178','J80') 
     OR ColumnName2 IN ('N179','R9002','R40.2240','R40.2241','R40.2242','R40.2243','R40.2244','R40.2245','R40.2246','R40.2247','R40.2248','R40.2249','R40.2230','R40.2231','R40.2232','R40.2233','R40.2234','R40.2235','R40.2236','R40.2237','R40.2238','R40.2239','N289','R410','R34','N178','J80') 
     OR ColumnName3 IN ('N179','R9002','R40.2240','R40.2241','R40.2242','R40.2243','R40.2244','R40.2245','R40.2246','R40.2247','R40.2248','R40.2249','R40.2230','R40.2231','R40.2232','R40.2233','R40.2234','R40.2235','R40.2236','R40.2237','R40.2238','R40.2239','N289','R410','R34','N178','J80') 
     OR ColumnName4 IN ('N179','R9002','R40.2240','R40.2241','R40.2242','R40.2243','R40.2244','R40.2245','R40.2246','R40.2247','R40.2248','R40.2249','R40.2230','R40.2231','R40.2232','R40.2233','R40.2234','R40.2235','R40.2236','R40.2237','R40.2238','R40.2239','N289','R410','R34','N178','J80') 
OR ColumnName5 IN (
'N179','R9002','R40.2240','R40.2241','R40.2242','R40.2243','R40.2244','R40.2245','R40.2246','R40.2247','R40.2248','R40.2249','R40.2230','R40.2231','R40.2232','R40.2233','R40.2234','R40.2235','R40.2236','R40.2237','R40.2238','R40.2239','N289','R410','R34','N178','J80') 
OR ColumnName6 IN (
'N179','R9002','R40.2240','R40.2241','R40.2242','R40.2243','R40.2244','R40.2245','R40.2246','R40.2247','R40.2248','R40.2249','R40.2230','R40.2231','R40.2232','R40.2233','R40.2234','R40.2235','R40.2236','R40.2237','R40.2238','R40.2239','N289','R410','R34','N178','J80') 
OR ColumnName7 IN (
'N179','R9002','R40.2240','R40.2241','R40.2242','R40.2243','R40.2244','R40.2245','R40.2246','R40.2247','R40.2248','R40.2249','R40.2230','R40.2231','R40.2232','R40.2233','R40.2234','R40.2235','R40.2236','R40.2237','R40.2238','R40.2239','N289','R410','R34','N178','J80') 
OR ColumnName8 IN (
'N179','R9002','R40.2240','R40.2241','R40.2242','R40.2243','R40.2244','R40.2245','R40.2246','R40.2247','R40.2248','R40.2249','R40.2230','R40.2231','R40.2232','R40.2233','R40.2234','R40.2235','R40.2236','R40.2237','R40.2238','R40.2239','N289','R410','R34','N178','J80') 
OR ColumnName9 IN (
'N179','R9002','R40.2240','R40.2241','R40.2242','R40.2243','R40.2244','R40.2245','R40.2246','R40.2247','R40.2248','R40.2249','R40.2230','R40.2231','R40.2232','R40.2233','R40.2234','R40.2235','R40.2236','R40.2237','R40.2238','R40.2239','N289','R410','R34','N178','J80') 
OR ColumnName10 IN (
'N179','R9002','R40.2240','R40.2241','R40.2242','R40.2243','R40.2244','R40.2245','R40.2246','R40.2247','R40.2248','R40.2249','R40.2230','R40.2231','R40.2232','R40.2233','R40.2234','R40.2235','R40.2236','R40.2237','R40.2238','R40.2239','N289','R410','R34','N178','J80') 
OR ColumnName11 IN (
'N179','R9002','R40.2240','R40.2241','R40.2242','R40.2243','R40.2244','R40.2245','R40.2246','R40.2247','R40.2248','R40.2249','R40.2230','R40.2231','R40.2232','R40.2233','R40.2234','R40.2235','R40.2236','R40.2237','R40.2238','R40.2239','N289','R410','R34','N178','J80') 
OR ColumnName12 IN (
'N179','R9002','R40.2240','R40.2241','R40.2242','R40.2243','R40.2244','R40.2245','R40.2246','R40.2247','R40.2248','R40.2249','R40.2230','R40.2231','R40.2232','R40.2233','R40.2234','R40.2235','R40.2236','R40.2237','R40.2238','R40.2239','N289','R410','R34','N178','J80') 
OR ColumnName13 IN (
'N179','R9002','R40.2240','R40.2241','R40.2242','R40.2243','R40.2244','R40.2245','R40.2246','R40.2247','R40.2248','R40.2249','R40.2230','R40.2231','R40.2232','R40.2233','R40.2234','R40.2235','R40.2236','R40.2237','R40.2238','R40.2239','N289','R410','R34','N178','J80') 
OR ColumnName14 IN (
'N179','R9002','R40.2240','R40.2241','R40.2242','R40.2243','R40.2244','R40.2245','R40.2246','R40.2247','R40.2248','R40.2249','R40.2230','R40.2231','R40.2232','R40.2233','R40.2234','R40.2235','R40.2236','R40.2237','R40.2238','R40.2239','N289','R410','R34','N178','J80') 
OR ColumnName15 IN (
'N179','R9002','R40.2240','R40.2241','R40.2242','R40.2243','R40.2244','R40.2245','R40.2246','R40.2247','R40.2248','R40.2249','R40.2230','R40.2231','R40.2232','R40.2233','R40.2234','R40.2235','R40.2236','R40.2237','R40.2238','R40.2239','N289','R410','R34','N178','J80') 
OR ColumnName16 IN (
'N179','R9002','R40.2240','R40.2241','R40.2242','R40.2243','R40.2244','R40.2245','R40.2246','R40.2247','R40.2248','R40.2249','R40.2230','R40.2231','R40.2232','R40.2233','R40.2234','R40.2235','R40.2236','R40.2237','R40.2238','R40.2239','N289','R410','R34','N178','J80') 
OR ColumnName17 IN (
'N179','R9002','R40.2240','R40.2241','R40.2242','R40.2243','R40.2244','R40.2245','R40.2246','R40.2247','R40.2248','R40.2249','R40.2230','R40.2231','R40.2232','R40.2233','R40.2234','R40.2235','R40.2236','R40.2237','R40.2238','R40.2239','N289','R410','R34','N178','J80') 
OR ColumnName18 IN (
'N179','R9002','R40.2240','R40.2241','R40.2242','R40.2243','R40.2244','R40.2245','R40.2246','R40.2247','R40.2248','R40.2249','R40.2230','R40.2231','R40.2232','R40.2233','R40.2234','R40.2235','R40.2236','R40.2237','R40.2238','R40.2239','N289','R410','R34','N178','J80') 
OR ColumnName19 IN (
'N179','R9002','R40.2240','R40.2241','R40.2242','R40.2243','R40.2244','R40.2245','R40.2246','R40.2247','R40.2248','R40.2249','R40.2230','R40.2231','R40.2232','R40.2233','R40.2234','R40.2235','R40.2236','R40.2237','R40.2238','R40.2239','N289','R410','R34','N178','J80') 
OR ColumnName20 IN (
'N179','R9002','R40.2240','R40.2241','R40.2242','R40.2243','R40.2244','R40.2245','R40.2246','R40.2247','R40.2248','R40.2249','R40.2230','R40.2231','R40.2232','R40.2233','R40.2234','R40.2235','R40.2236','R40.2237','R40.2238','R40.2239','N289','R410','R34','N178','J80') 
OR ColumnName21 IN (
'N179','R9002','R40.2240','R40.2241','R40.2242','R40.2243','R40.2244','R40.2245','R40.2246','R40.2247','R40.2248','R40.2249','R40.2230','R40.2231','R40.2232','R40.2233','R40.2234','R40.2235','R40.2236','R40.2237','R40.2238','R40.2239','N289','R410','R34','N178','J80') 
OR ColumnName22 IN (
'N179','R9002','R40.2240','R40.2241','R40.2242','R40.2243','R40.2244','R40.2245','R40.2246','R40.2247','R40.2248','R40.2249','R40.2230','R40.2231','R40.2232','R40.2233','R40.2234','R40.2235','R40.2236','R40.2237','R40.2238','R40.2239','N289','R410','R34','N178','J80') 
OR ColumnName23 IN (
'N179','R9002','R40.2240','R40.2241','R40.2242','R40.2243','R40.2244','R40.2245','R40.2246','R40.2247','R40.2248','R40.2249','R40.2230','R40.2231','R40.2232','R40.2233','R40.2234','R40.2235','R40.2236','R40.2237','R40.2238','R40.2239','N289','R410','R34','N178','J80') 
OR ColumnName24 IN (
'N179','R9002','R40.2240','R40.2241','R40.2242','R40.2243','R40.2244','R40.2245','R40.2246','R40.2247','R40.2248','R40.2249','R40.2230','R40.2231','R40.2232','R40.2233','R40.2234','R40.2235','R40.2236','R40.2237','R40.2238','R40.2239','N289','R410','R34','N178','J80') 
OR ColumnName25 IN (
'N179','R9002','R40.2240','R40.2241','R40.2242','R40.2243','R40.2244','R40.2245','R40.2246','R40.2247','R40.2248','R40.2249','R40.2230','R40.2231','R40.2232','R40.2233','R40.2234','R40.2235','R40.2236','R40.2237','R40.2238','R40.2239','N289','R410','R34','N178','J80') 
) AND (ColumnName NOT IN (
'N179','R9002','R40.2240','R40.2241','R40.2242','R40.2243','R40.2244','R40.2245','R40.2246','R40.2247','R40.2248','R40.2249','R40.2230','R40.2231','R40.2232','R40.2233','R40.2234','R40.2235','R40.2236','R40.2237','R40.2238','R40.2239','N289','R410','R34','N178','J80') 
OR ColumnName2 NOT IN (
'N179','R9002','R40.2240','R40.2241','R40.2242','R40.2243','R40.2244','R40.2245','R40.2246','R40.2247','R40.2248','R40.2249','R40.2230','R40.2231','R40.2232','R40.2233','R40.2234','R40.2235','R40.2236','R40.2237','R40.2238','R40.2239','N289','R410','R34','N178','J80') 
OR ColumnName3 NOT IN (
'N179','R9002','R40.2240','R40.2241','R40.2242','R40.2243','R40.2244','R40.2245','R40.2246','R40.2247','R40.2248','R40.2249','R40.2230','R40.2231','R40.2232','R40.2233','R40.2234','R40.2235','R40.2236','R40.2237','R40.2238','R40.2239','N289','R410','R34','N178','J80') 
OR ColumnName4 NOT IN (
'N179','R9002','R40.2240','R40.2241','R40.2242','R40.2243','R40.2244','R40.2245','R40.2246','R40.2247','R40.2248','R40.2249','R40.2230','R40.2231','R40.2232','R40.2233','R40.2234','R40.2235','R40.2236','R40.2237','R40.2238','R40.2239','N289','R410','R34','N178','J80') 
OR ColumnName5 NOT IN (
'N179','R9002','R40.2240','R40.2241','R40.2242','R40.2243','R40.2244','R40.2245','R40.2246','R40.2247','R40.2248','R40.2249','R40.2230','R40.2231','R40.2232','R40.2233','R40.2234','R40.2235','R40.2236','R40.2237','R40.2238','R40.2239','N289','R410','R34','N178','J80') 
OR ColumnName6 NOT IN (
'N179','R9002','R40.2240','R40.2241','R40.2242','R40.2243','R40.2244','R40.2245','R40.2246','R40.2247','R40.2248','R40.2249','R40.2230','R40.2231','R40.2232','R40.2233','R40.2234','R40.2235','R40.2236','R40.2237','R40.2238','R40.2239','N289','R410','R34','N178','J80') 
OR ColumnName7 NOT IN (
'N179','R9002','R40.2240','R40.2241','R40.2242','R40.2243','R40.2244','R40.2245','R40.2246','R40.2247','R40.2248','R40.2249','R40.2230','R40.2231','R40.2232','R40.2233','R40.2234','R40.2235','R40.2236','R40.2237','R40.2238','R40.2239','N289','R410','R34','N178','J80') 
OR ColumnName8 NOT IN (
'N179','R9002','R40.2240','R40.2241','R40.2242','R40.2243','R40.2244','R40.2245','R40.2246','R40.2247','R40.2248','R40.2249','R40.2230','R40.2231','R40.2232','R40.2233','R40.2234','R40.2235','R40.2236','R40.2237','R40.2238','R40.2239','N289','R410','R34','N178','J80') 
OR ColumnName9 NOT IN (
'N179','R9002','R40.2240','R40.2241','R40.2242','R40.2243','R40.2244','R40.2245','R40.2246','R40.2247','R40.2248','R40.2249','R40.2230','R40.2231','R40.2232','R40.2233','R40.2234','R40.2235','R40.2236','R40.2237','R40.2238','R40.2239','N289','R410','R34','N178','J80') 
OR ColumnName10 NOT IN (
'N179','R9002','R40.2240','R40.2241','R40.2242','R40.2243','R40.2244','R40.2245','R40.2246','R40.2247','R40.2248','R40.2249','R40.2230','R40.2231','R40.2232','R40.2233','R40.2234','R40.2235','R40.2236','R40.2237','R40.2238','R40.2239','N289','R410','R34','N178','J80') 
OR ColumnName11 NOT IN (
'N179','R9002','R40.2240','R40.2241','R40.2242','R40.2243','R40.2244','R40.2245','R40.2246','R40.2247','R40.2248','R40.2249','R40.2230','R40.2231','R40.2232','R40.2233','R40.2234','R40.2235','R40.2236','R40.2237','R40.2238','R40.2239','N289','R410','R34','N178','J80') 
OR ColumnName12 NOT IN (
'N179','R9002','R40.2240','R40.2241','R40.2242','R40.2243','R40.2244','R40.2245','R40.2246','R40.2247','R40.2248','R40.2249','R40.2230','R40.2231','R40.2232','R40.2233','R40.2234','R40.2235','R40.2236','R40.2237','R40.2238','R40.2239','N289','R410','R34','N178','J80') 
OR ColumnName13 NOT IN (
'N179','R9002','R40.2240','R40.2241','R40.2242','R40.2243','R40.2244','R40.2245','R40.2246','R40.2247','R40.2248','R40.2249','R40.2230','R40.2231','R40.2232','R40.2233','R40.2234','R40.2235','R40.2236','R40.2237','R40.2238','R40.2239','N289','R410','R34','N178','J80') 
OR ColumnName14 NOT IN (
'N179','R9002','R40.2240','R40.2241','R40.2242','R40.2243','R40.2244','R40.2245','R40.2246','R40.2247','R40.2248','R40.2249','R40.2230','R40.2231','R40.2232','R40.2233','R40.2234','R40.2235','R40.2236','R40.2237','R40.2238','R40.2239','N289','R410','R34','N178','J80') 
OR ColumnName15 NOT IN (
'N179','R9002','R40.2240','R40.2241','R40.2242','R40.2243','R40.2244','R40.2245','R40.2246','R40.2247','R40.2248','R40.2249','R40.2230','R40.2231','R40.2232','R40.2233','R40.2234','R40.2235','R40.2236','R40.2237','R40.2238','R40.2239','N289','R410','R34','N178','J80') 
OR ColumnName16 NOT IN (
'N179','R9002','R40.2240','R40.2241','R40.2242','R40.2243','R40.2244','R40.2245','R40.2246','R40.2247','R40.2248','R40.2249','R40.2230','R40.2231','R40.2232','R40.2233','R40.2234','R40.2235','R40.2236','R40.2237','R40.2238','R40.2239','N289','R410','R34','N178','J80') 
OR ColumnName17 NOT IN (
'N179','R9002','R40.2240','R40.2241','R40.2242','R40.2243','R40.2244','R40.2245','R40.2246','R40.2247','R40.2248','R40.2249','R40.2230','R40.2231','R40.2232','R40.2233','R40.2234','R40.2235','R40.2236','R40.2237','R40.2238','R40.2239','N289','R410','R34','N178','J80') 
OR ColumnName18 NOT IN (
'N179','R9002','R40.2240','R40.2241','R40.2242','R40.2243','R40.2244','R40.2245','R40.2246','R40.2247','R40.2248','R40.2249','R40.2230','R40.2231','R40.2232','R40.2233','R40.2234','R40.2235','R40.2236','R40.2237','R40.2238','R40.2239','N289','R410','R34','N178','J80') 
OR ColumnName19 NOT IN (
'N179','R9002','R40.2240','R40.2241','R40.2242','R40.2243','R40.2244','R40.2245','R40.2246','R40.2247','R40.2248','R40.2249','R40.2230','R40.2231','R40.2232','R40.2233','R40.2234','R40.2235','R40.2236','R40.2237','R40.2238','R40.2239','N289','R410','R34','N178','J80') 
OR ColumnName20 NOT IN (
'N179','R9002','R40.2240','R40.2241','R40.2242','R40.2243','R40.2244','R40.2245','R40.2246','R40.2247','R40.2248','R40.2249','R40.2230','R40.2231','R40.2232','R40.2233','R40.2234','R40.2235','R40.2236','R40.2237','R40.2238','R40.2239','N289','R410','R34','N178','J80') 
OR ColumnName21 NOT IN (
'N179','R9002','R40.2240','R40.2241','R40.2242','R40.2243','R40.2244','R40.2245','R40.2246','R40.2247','R40.2248','R40.2249','R40.2230','R40.2231','R40.2232','R40.2233','R40.2234','R40.2235','R40.2236','R40.2237','R40.2238','R40.2239','N289','R410','R34','N178','J80') 
OR ColumnName22 NOT IN (
'N179','R9002','R40.2240','R40.2241','R40.2242','R40.2243','R40.2244','R40.2245','R40.2246','R40.2247','R40.2248','R40.2249','R40.2230','R40.2231','R40.2232','R40.2233','R40.2234','R40.2235','R40.2236','R40.2237','R40.2238','R40.2239','N289','R410','R34','N178','J80') 
OR ColumnName23 NOT IN (
'N179','R9002','R40.2240','R40.2241','R40.2242','R40.2243','R40.2244','R40.2245','R40.2246','R40.2247','R40.2248','R40.2249','R40.2230','R40.2231','R40.2232','R40.2233','R40.2234','R40.2235','R40.2236','R40.2237','R40.2238','R40.2239','N289','R410','R34','N178','J80') 
OR ColumnName24 NOT IN (
'N179','R9002','R40.2240','R40.2241','R40.2242','R40.2243','R40.2244','R40.2245','R40.2246','R40.2247','R40.2248','R40.2249','R40.2230','R40.2231','R40.2232','R40.2233','R40.2234','R40.2235','R40.2236','R40.2237','R40.2238','R40.2239','N289','R410','R34','N178','J80') 
OR ColumnName25 NOT IN (
'N179','R9002','R40.2240','R40.2241','R40.2242','R40.2243','R40.2244','R40.2245','R40.2246','R40.2247','R40.2248','R40.2249','R40.2230','R40.2231','R40.2232','R40.2233','R40.2234','R40.2235','R40.2236','R40.2237','R40.2238','R40.2239','N289','R410','R34','N178','J80') 
)) group by Number Order by Number 

`

+1

請發佈架構和查詢的解釋。 –

+0

已添加有問題的執行計劃 –

+0

執行計劃中的查詢看起來並不相同 - 它顯然使用了聚合函數。另外,如果沒有模式,我們只是猜測 - 表中是否有索引? –

回答

0

所以,你想其中至少一列存在這是集合和至少一個列上存在這是處理記錄不在集合中並且提供一個代碼。

我假設你不想僅僅評估一次這樣的查詢,所以重構或重新索引你的數據是合理的。

正常索引不起作用,索引和數據之間的跳轉將使它們不可用。位圖索引可以工作,因爲在很多索引上的邏輯運算非常快。但是,只有當不存在太多不同的代碼和列名稱 - 值時?但Sql-Server不提供這些。

可能這些列存儲索引會幫助,sqlserver提供。我承認,我對這些沒有經驗。

或者你可以切換到oracle或postgres。但只有經過廣泛的測試!

相關問題