Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 其中包含多个参数的条件_Sql_Sql Server - Fatal编程技术网

Sql 其中包含多个参数的条件

Sql 其中包含多个参数的条件,sql,sql-server,Sql,Sql Server,我必须在where子句中运行一个大约30列的查询。每列有1000+个值可供比较。我知道第条不是最好的办法。任何人都可以建议如何运行此查询而不出现处理错误。例如下面 select * from table where column1 not in (1,2,3,4......1000+ ) and column2 not in (1,2,3,4......1000+ ) and column3 not in (1,2,3,4......1000+) and so on upto column30

我必须在where子句中运行一个大约30列的查询。每列有1000+个值可供比较。我知道第条不是最好的办法。任何人都可以建议如何运行此查询而不出现处理错误。例如下面

select *
from table
where column1 not in (1,2,3,4......1000+ )
and column2 not in (1,2,3,4......1000+ ) and  column3 not in (1,2,3,4......1000+) and so on upto column30.
我得到一个错误:

SQL Server查询处理器耗尽了内部资源


我探索了其他链接,但没有找到以最佳方式实现它的解决方案或建议。

创建一个包含所有可能值的临时表:

创建表tempColumn1int,column2 int。。。。 插入到临时值中 1,1,1..., 1,2,2..., . . 现在,相应地应用selectqueryfilter

从表中选择*,其中列1不在“从临时文件中选择列1” 和列2不在从临时选择列2中 还有一种使用左外联接的方法

从表中选择*作为t 左侧外部连接温度为t1 在t.column1=t1.column1上 左外连接温度为t2 在t.column2=t1.column2上 . . 其中t.column1为NULL,t.column2为NULL
根据假设,您可以选择几种路线

假设1:1,2,3,4……所有列的1000+值均相同,不进行比较 基于此假设,我认为问题在于SQL Server的资源在以下查询中耗尽:

select *
from MYTABLE
where not exists (select 1 from values_to_compare where comparison_value = MYTABLE.column1)
  and not exists (select 1 from values_to_compare where comparison_value = MYTABLE.column2)
  and not exists (select 1 from values_to_compare where comparison_value = MYTABLE.column3)
  and not exists (select 1 from values_to_compare where comparison_value = MYTABLE.column4)
  and not exists (select 1 from values_to_compare where comparison_value = MYTABLE.column5);
select *
from MYTABLE
where column1 not in (2, 5, 100, 22, 44, ... thousand other values)
and column2 not in (1, 225, 5619, 8, 9000, ... thousand other values)
and column3 not in (2024, 5223, 0, 552, 4564, ... thousand other values)
select *
from MYTABLE
where not exists (select 1 from values_to_compare where comparison_value = MYTABLE.column1 and compare_column = 'column1')
  and not exists (select 1 from values_to_compare where comparison_value = MYTABLE.column2 and compare_column = 'column2')
  and not exists (select 1 from values_to_compare where comparison_value = MYTABLE.column3 and compare_column = 'column3')
  and not exists (select 1 from values_to_compare where comparison_value = MYTABLE.column4 and compare_column = 'column4')
  and not exists (select 1 from values_to_compare where comparison_value = MYTABLE.column5 and compare_column = 'column5');
请注意,我将您的表称为MYTABLE

select *
from MYTABLE -- <---- Remember the name I gave your table
where column1 not in (2, 5, 100, 22, 44, ... thousand other values)
and column2 not in (2, 5, 100, 22, 44, ... thousand other values)
and column3 not in (2, 5, 100, 22, 44, ... thousand other values)
请记住,如果您只是将一个包含值的文本文件导入到比较表中,而不是键入一个巨大的insert语句,那么填充值到比较表中可能会容易得多。但是,如果您选择编写insert语句,下面是您的编写方法。如果SQL Server抱怨insert语句太大,则可能必须将insert语句分成几百个条目的批处理

insert into comparison values (2), (5), (100), (22), (44), ... thousand other values;
insert into comparison values
('column1', 2), ('column1', 5), ('column1', 100), ('column1', 22), ('column1', 44), ... thousand other values ...
, ('column2', 1), ('column2', 225), ('column2', 5619), ('column2', 8), ('column2', 9000), ... thousand other values ...
, ('column3', 2024), ('column3', 5223), ('column3', 0), ('column3', 552), ('column3', 4564) ... thousand other values ...
;
然后,在MYTABLE上创建一个索引,如下所示:

create index on MYTABLE (column1);
create index on MYTABLE (column1);
然后,将您的查询分块编写。首先,这样做:

select *
from MYTABLE
where not exists (select 1 from values_to_compare where comparison_value = MYTABLE.column1);
select *
from MYTABLE
where not exists (select 1 from values_to_compare where comparison_value = MYTABLE.column1 and compare_column = 'column1');
希望这会运行得很快。如果运行速度足够快,那么是时候再添加4个索引了:

create index on MYTABLE (column2);
create index on MYTABLE (column3);
create index on MYTABLE (column4);
create index on MYTABLE (column5);
create index on MYTABLE (column2);
create index on MYTABLE (column3);
create index on MYTABLE (column4);
create index on MYTABLE (column5);
然后,再添加4行,使查询如下所示:

select *
from MYTABLE
where not exists (select 1 from values_to_compare where comparison_value = MYTABLE.column1)
  and not exists (select 1 from values_to_compare where comparison_value = MYTABLE.column2)
  and not exists (select 1 from values_to_compare where comparison_value = MYTABLE.column3)
  and not exists (select 1 from values_to_compare where comparison_value = MYTABLE.column4)
  and not exists (select 1 from values_to_compare where comparison_value = MYTABLE.column5);
select *
from MYTABLE
where column1 not in (2, 5, 100, 22, 44, ... thousand other values)
and column2 not in (1, 225, 5619, 8, 9000, ... thousand other values)
and column3 not in (2024, 5223, 0, 552, 4564, ... thousand other values)
select *
from MYTABLE
where not exists (select 1 from values_to_compare where comparison_value = MYTABLE.column1 and compare_column = 'column1')
  and not exists (select 1 from values_to_compare where comparison_value = MYTABLE.column2 and compare_column = 'column2')
  and not exists (select 1 from values_to_compare where comparison_value = MYTABLE.column3 and compare_column = 'column3')
  and not exists (select 1 from values_to_compare where comparison_value = MYTABLE.column4 and compare_column = 'column4')
  and not exists (select 1 from values_to_compare where comparison_value = MYTABLE.column5 and compare_column = 'column5');
如果效果良好,请在MYTABLE中的每一列上再添加25个索引。然后,展开上面的查询以比较所有30列。我相信SQL Server会表现良好

假设2:与列1..30比较的值都不同 基于此假设,我认为问题在于SQL Server的资源在以下查询中耗尽:

select *
from MYTABLE
where not exists (select 1 from values_to_compare where comparison_value = MYTABLE.column1)
  and not exists (select 1 from values_to_compare where comparison_value = MYTABLE.column2)
  and not exists (select 1 from values_to_compare where comparison_value = MYTABLE.column3)
  and not exists (select 1 from values_to_compare where comparison_value = MYTABLE.column4)
  and not exists (select 1 from values_to_compare where comparison_value = MYTABLE.column5);
select *
from MYTABLE
where column1 not in (2, 5, 100, 22, 44, ... thousand other values)
and column2 not in (1, 225, 5619, 8, 9000, ... thousand other values)
and column3 not in (2024, 5223, 0, 552, 4564, ... thousand other values)
select *
from MYTABLE
where not exists (select 1 from values_to_compare where comparison_value = MYTABLE.column1 and compare_column = 'column1')
  and not exists (select 1 from values_to_compare where comparison_value = MYTABLE.column2 and compare_column = 'column2')
  and not exists (select 1 from values_to_compare where comparison_value = MYTABLE.column3 and compare_column = 'column3')
  and not exists (select 1 from values_to_compare where comparison_value = MYTABLE.column4 and compare_column = 'column4')
  and not exists (select 1 from values_to_compare where comparison_value = MYTABLE.column5 and compare_column = 'column5');
注意我们正在比较的值。将所有列与不同的值集进行比较

解决方案

创建一个名为values_to_compare的表,如下所示:

create table values_to_compare (comparison_value int primary key);
create table values_to_compare (compare_column as varchar(50), comparison_value int, primary key (compare_column, compare_value));
请记住,如果您只是将一个包含值的文本文件导入到比较表中,而不是键入一个巨大的insert语句,那么填充值到比较表中可能会容易得多。但是,如果您选择编写insert语句,下面是您的编写方法。如果SQL Server抱怨insert语句太大,则可能必须将insert语句分成几百个条目的批处理

insert into comparison values (2), (5), (100), (22), (44), ... thousand other values;
insert into comparison values
('column1', 2), ('column1', 5), ('column1', 100), ('column1', 22), ('column1', 44), ... thousand other values ...
, ('column2', 1), ('column2', 225), ('column2', 5619), ('column2', 8), ('column2', 9000), ... thousand other values ...
, ('column3', 2024), ('column3', 5223), ('column3', 0), ('column3', 552), ('column3', 4564) ... thousand other values ...
;
然后,在MYTABLE上创建一个索引,如下所示:

create index on MYTABLE (column1);
create index on MYTABLE (column1);
然后,将您的查询分块编写。首先,这样做:

select *
from MYTABLE
where not exists (select 1 from values_to_compare where comparison_value = MYTABLE.column1);
select *
from MYTABLE
where not exists (select 1 from values_to_compare where comparison_value = MYTABLE.column1 and compare_column = 'column1');
希望这会运行得很快。如果运行速度足够快,那么是时候再添加4个索引了:

create index on MYTABLE (column2);
create index on MYTABLE (column3);
create index on MYTABLE (column4);
create index on MYTABLE (column5);
create index on MYTABLE (column2);
create index on MYTABLE (column3);
create index on MYTABLE (column4);
create index on MYTABLE (column5);
然后,再添加4行,使查询如下所示:

select *
from MYTABLE
where not exists (select 1 from values_to_compare where comparison_value = MYTABLE.column1)
  and not exists (select 1 from values_to_compare where comparison_value = MYTABLE.column2)
  and not exists (select 1 from values_to_compare where comparison_value = MYTABLE.column3)
  and not exists (select 1 from values_to_compare where comparison_value = MYTABLE.column4)
  and not exists (select 1 from values_to_compare where comparison_value = MYTABLE.column5);
select *
from MYTABLE
where column1 not in (2, 5, 100, 22, 44, ... thousand other values)
and column2 not in (1, 225, 5619, 8, 9000, ... thousand other values)
and column3 not in (2024, 5223, 0, 552, 4564, ... thousand other values)
select *
from MYTABLE
where not exists (select 1 from values_to_compare where comparison_value = MYTABLE.column1 and compare_column = 'column1')
  and not exists (select 1 from values_to_compare where comparison_value = MYTABLE.column2 and compare_column = 'column2')
  and not exists (select 1 from values_to_compare where comparison_value = MYTABLE.column3 and compare_column = 'column3')
  and not exists (select 1 from values_to_compare where comparison_value = MYTABLE.column4 and compare_column = 'column4')
  and not exists (select 1 from values_to_compare where comparison_value = MYTABLE.column5 and compare_column = 'column5');
如果效果良好,请在MYTABLE中的每一列上再添加25个索引。然后,展开上面的查询以比较所有30列。我相信SQL Server会表现良好

试试看

编辑 根据SQL Server能够进行多达20列比较的新信息,我们可以执行拆分操作

select * into MYTABLE_TEMP from MYTABLE where 1=2;
我们现在有一个临时表来存储数据。然后,执行仅比较15列的查询。获取输出并将其转储到MYTABLE_TEMP中

insert into MYTABLE_TEMP
select *
from MYTABLE
where not exists (select 1 from values_to_compare where comparison_value = MYTABLE.column1)
  and not exists (select 1 from values_to_compare where comparison_value = MYTABLE.column2)
  and not exists (select 1 from values_to_compare where comparison_value = MYTABLE.column3)
  ...
  ...
  and not exists (select 1 from values_to_compare where comparison_value = MYTABLE.column15);
create index on MYTABLE_TEMP (column16);
create index on MYTABLE_TEMP (column17);
...
...
create index on MYTABLE_TEMP (column30);
select *
from MYTABLE_TEMP
where not exists (select 1 from values_to_compare where comparison_value = MYTABLE.column16)
  and not exists (select 1 from values_to_compare where comparison_value = MYTABLE.column17)
  and not exists (select 1 from values_to_compare where comparison_value = MYTABLE.column18)
  ...
  ...
  and not exists (select 1 from values_to_compare where comparison_value = MYTABLE.column30);
在MYTABLE_TEMP上创建15个索引

insert into MYTABLE_TEMP
select *
from MYTABLE
where not exists (select 1 from values_to_compare where comparison_value = MYTABLE.column1)
  and not exists (select 1 from values_to_compare where comparison_value = MYTABLE.column2)
  and not exists (select 1 from values_to_compare where comparison_value = MYTABLE.column3)
  ...
  ...
  and not exists (select 1 from values_to_compare where comparison_value = MYTABLE.column15);
create index on MYTABLE_TEMP (column16);
create index on MYTABLE_TEMP (column17);
...
...
create index on MYTABLE_TEMP (column30);
select *
from MYTABLE_TEMP
where not exists (select 1 from values_to_compare where comparison_value = MYTABLE.column16)
  and not exists (select 1 from values_to_compare where comparison_value = MYTABLE.column17)
  and not exists (select 1 from values_to_compare where comparison_value = MYTABLE.column18)
  ...
  ...
  and not exists (select 1 from values_to_compare where comparison_value = MYTABLE.column30);
然后,在MYTABLE_TEMP上运行查询

insert into MYTABLE_TEMP
select *
from MYTABLE
where not exists (select 1 from values_to_compare where comparison_value = MYTABLE.column1)
  and not exists (select 1 from values_to_compare where comparison_value = MYTABLE.column2)
  and not exists (select 1 from values_to_compare where comparison_value = MYTABLE.column3)
  ...
  ...
  and not exists (select 1 from values_to_compare where comparison_value = MYTABLE.column15);
create index on MYTABLE_TEMP (column16);
create index on MYTABLE_TEMP (column17);
...
...
create index on MYTABLE_TEMP (column30);
select *
from MYTABLE_TEMP
where not exists (select 1 from values_to_compare where comparison_value = MYTABLE.column16)
  and not exists (select 1 from values_to_compare where comparison_value = MYTABLE.column17)
  and not exists (select 1 from values_to_compare where comparison_value = MYTABLE.column18)
  ...
  ...
  and not exists (select 1 from values_to_compare where comparison_value = MYTABLE.column30);

看看这是否有帮助。

不要在中使用。在表中存储1…1000+个项目,并在该字段上放置索引。然后使用选择。。。从t。。。如果不存在,请在newfield=t.col1且不存在的newtable中选择1。。。newfield=t.col2。。。;还应该考虑在COL1中通过COL30创建索引,至少在运行该查询时是暂时的。一旦运行,删除这些索引。如果值为字符串,则索引将是一个好选项,而不是1,2,3?是的,索引仍然是字符串的一个好选项。我假设您使用的是varchar50之类的东西,而不是nvarcharmax。我创建了一个临时表,并在其中插入了30列,但当我在每个列中只选择一个值时,会出现相同的错误,从[ID]中选择col1、col2、Countcol2作为卷,从[table1]中选择col1、col2、Countcol2作为卷a.id=b.id上的内部联接表2 b,其中LENcol1>=5,col2='023',从MyTestablePXInclusion中选择col中的col和从MyTestablePXInclusion中选择col2中的col2,以及。。。col25在通过a.[ID]从MyTestablePX包含组中选择col25,通过col1、col2从b.col1、b.col2 aa组中选择col25,但如果我有
不同的值,即使我尝试使用或代替,它仍然有效-如果我们有3000+in子句,请提供任何帮助/建议。如果我们有3000+in子句,则临时表逻辑将有效?或者我们需要为此改变什么?就像2100年后闯入加州一样。临时表就像另一个表一样。您可以在其中插入值。如果您正在为insert使用VALUES子句,则需要为每1000个相同的问题进行拆分,最多19-20列comparisons@AnkeshKumar我将很快用新的启示更新答案