SQL查询 - 包含300列和1.2亿条记录的表

时间:2017-07-18 09:11:50

标签: sql sql-server-2012

我有一个包含300列和超过1.2亿条记录的表。我想执行像

这样的查询
select * 
from tablename 
where code in ('aa','bb', ..) 
  and column1 in ('a11','a22',..) 

大约有50多个列需要应用条件。

执行此查询需要两个多小时。有没有办法改善这个?

enter image description here

选择查询是:

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)

因此,您希望处理至少存在一个列中的列的记录,并且至少存在一个列,该列不在集合中并且提供了其中一个代码。

我认为您不想仅仅评估一次这样的查询,因此重组或重新编制数据索引是合理的。

正常索引无济于事,索引和数据之间的跳转会使它们无法使用。 Bitmap-Indices可以工作,因为许多索引的逻辑运算速度非常快。但是,只有没有太多不同的代码和列名 - 值?但是Sql-Server没有提供这些。

这些列存储索引可能会有所帮助,sqlserver正在提供。我承认,我对这些没有经验。

或者你可以切换到oracle或postgres。但只有经过广泛的测试!!