Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/74.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 选择“查询太慢”,即使I';我在使用索引_Sql_Sql Server_Indexing - Fatal编程技术网

Sql 选择“查询太慢”,即使I';我在使用索引

Sql 选择“查询太慢”,即使I';我在使用索引,sql,sql-server,indexing,Sql,Sql Server,Indexing,我不确定我在这里是否做错了什么,但我有一个查询在一个有数百万行的表上运行 查询如下所示: select * from dbo.table with (index (index_1), nolock) where col1 = 15464 and col2 not in ('X', 'U') and col3 is null and col4 = 'E'

我不确定我在这里是否做错了什么,但我有一个查询在一个有数百万行的表上运行

查询如下所示:

select * 
  from    dbo.table with (index (index_1), nolock)
          where   col1 = 15464
                  and col2 not in ('X', 'U')
                  and col3 is null
                  and col4 = 'E'
CREATE NONCLUSTERED INDEX [index_1] ON [dbo].[table] ([col1], [col2], [col3], [col4]) WITH (FILLFACTOR=90) ON [PRIMARY]
GO
索引如下所示:

select * 
  from    dbo.table with (index (index_1), nolock)
          where   col1 = 15464
                  and col2 not in ('X', 'U')
                  and col3 is null
                  and col4 = 'E'
CREATE NONCLUSTERED INDEX [index_1] ON [dbo].[table] ([col1], [col2], [col3], [col4]) WITH (FILLFACTOR=90) ON [PRIMARY]
GO
此选择仍需要一分钟的运行时间。我缺少什么?

对于此查询:

select * 
from table 
where col1 = 15464 and
      col2 not in ('X', 'U') and
      col3 is null and
      col4 = 'E';
最好的索引是
表(col1、col4、col3、col2)
。查询应自动使用索引,无需任何提示

当根据
where
子句选择索引时,应首先输入相等条件,然后是一列带不等项的索引。出于索引的目的,中的
中的非
通常是不等式条件

此外,如果混合使用数据类型,则有时不使用索引。因此,这假设
col1
是数字。

对于此查询:

select * 
from table 
where col1 = 15464 and
      col2 not in ('X', 'U') and
      col3 is null and
      col4 = 'E';
最好的索引是
表(col1、col4、col3、col2)
。查询应自动使用索引,无需任何提示

当根据
where
子句选择索引时,应首先输入相等条件,然后是一列带不等项的索引。出于索引的目的,
中的
中的非
通常是不等式条件


此外,如果混合使用数据类型,则有时不使用索引。因此,这假设
col1
是数字。

注意NOLOCK提示。在你的情况下,这可能没问题,但大多数人并不真正理解这一暗示的所有后果。尝试将综合指数放在col1、col4上。将where重写为col1,然后是col4,然后是其余部分。如果索引顺序正确,where子句的顺序是否重要?@Rj。向我们展示执行计划……据我所知,是的。请尝试并提供反馈。注意NOLOCK提示。在你的情况下,这可能没问题,但大多数人并不真正理解这一暗示的所有后果。尝试将综合指数放在col1、col4上。将where重写为col1,然后是col4,然后是其余部分。如果索引顺序正确,where子句的顺序是否重要?@Rj。向我们展示执行计划……据我所知,是的。请尝试提供反馈。啊,我明白为什么了。这完全有道理。抱歉,仍在学习索引:)。。我可以在9分钟内接受你的回答!谢谢你,我的朋友。嗯,它仍然在运行超过1分钟。我需要“刷新”SQL以实现更改吗?@Rj。如果查询位于存储过程中或以某种方式缓存,则可能需要
选项(重新编译)
。否则,问题可能只是太多行匹配。啊,我明白为什么了。这完全有道理。抱歉,仍在学习索引:)。。我可以在9分钟内接受你的回答!谢谢你,我的朋友。嗯,它仍然在运行超过1分钟。我需要“刷新”SQL以实现更改吗?@Rj。如果查询位于存储过程中或以某种方式缓存,则可能需要
选项(重新编译)
。否则,问题可能只是太多行匹配。