Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/performance/5.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.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 server 当我为列编制索引时,为什么我的视图速度慢?_Sql Server_Performance_Indexing_Views - Fatal编程技术网

Sql server 当我为列编制索引时,为什么我的视图速度慢?

Sql server 当我为列编制索引时,为什么我的视图速度慢?,sql-server,performance,indexing,views,Sql Server,Performance,Indexing,Views,在SQL Server中,我使用LIKE语句将聚集索引置于上,以消除联接的低效性: CREATE VIEW KeywordCount WITH SCHEMABINDING AS SELECT K.ID AS KeywordID ,COUNT_BIG(*) AS KeywordCount FROM dbo.Grants G INNER JOIN dbo.GrantStatuses GS2 ON GS2.ID = G.Statu

在SQL Server中,我使用LIKE语句将聚集索引置于上,以消除联接的低效性

   CREATE VIEW KeywordCount WITH SCHEMABINDING
    AS
    SELECT 
        K.ID AS KeywordID
        ,COUNT_BIG(*) AS KeywordCount
    FROM dbo.Grants G
    INNER JOIN dbo.GrantStatuses GS2 ON GS2.ID = G.StatusID AND GS2.Status NOT IN ('Pre-Submission', 'Awaiting Signatory Approval', 'Modifying', 'Closed')
    INNER JOIN dbo.Keywords K
        ON G.Keywords LIKE '%' + K.Word + '%'                                           --It's one of the grant's keywords
        OR G.Title LIKE '%' + K.Word + '%'                                              --Word appears in the title
        OR Replace(G.Title, '-', ' ') LIKE '%' + Replace(K.Word, '-', ' ') + '%'        --Word with hyphens replaced appears in the title
        OR G.Synopsis LIKE '%' + K.Word  + '%'                                          --Word appears in the Synopsis
        OR Replace(G.Synopsis, '-', ' ') LIKE '%' + Replace(K.Word, '-', ' ')+ '%'      --Word with hyphens replaced appears in the synopsis
    GROUP BY K.ID
    GO

    CREATE UNIQUE CLUSTERED INDEX IX_KeywordCount 
        ON dbo.KeywordCount (KeywordID)
    GO
然后我在KeywordCount列上添加了另一个索引:

    CREATE INDEX IX_KeywordCount_Count 
        ON dbo.KeywordCount (KeywordCount)
    GO
那么,为什么运行下面的查询需要7分钟?指数不应该给我更好的表现吗

    SELECT TOP 10 * FROM KeywordCount ORDER BY KeywordCount DESC
编辑 谢谢大家,但我知道,LIKE语句和REPLACE会使此视图效率低下。这就是我添加聚集索引的原因。我认为将聚集索引放在视图上会将数据具体化到一个表中,这样数据库就不必进行连接。查询计划确实说它正在进行连接。为什么呢

A
如“%”+Replace(K.Word,“-”,“)+“%”
%
搜索词开头的通配符)将永远无法使用任何索引。如果您使用这种语句,不要惊讶于您将一直进行完整的表扫描

如果你真的需要这样的搜索,你要么对你的搜索速度感到满意,要么调查全文搜索

另一个选项是将LIKE语句更改为:
LIKE K.Word+'%'


如果只在结尾使用
%
通配符,SQL Server有机会在
K.Word
上实际使用索引,从而加快查找速度。

我在本文中找到了解决方案:


出于某种原因,查询计划没有使用索引,但我添加了带有(NOEXPAND)的提示,我的查询立即运行-非常感谢Quassnoi指出了正确的操作。

请注意,带有(NOEXPAND)意味着视图应该只查看索引,而不查看表数据。这意味着,如果索引不是最新的,视图也不会是最新的。

查询计划怎么说?在我的脑海中,我看到了5个不同的
JOIN
关键字条件,它们都与
链接。SQL Server正在检查每一行中的每一个条件,您正在对其中一些条件进行多次替换,并且您正在运行的
LIKE
比较无法使用索引,因为您正在使用
%xxx%
,因此它可以出现在字段中的任何点。除了在视图上有索引之外,您的查询还有一些潜在的问题。在源表上索引哪些字段?LIKE和REPLACE都很昂贵。如果我给视图添加一个聚集索引,那么视图就会具体化为一个表,选择应该不受我使用过的通配符和替换的影响?我相信@op所说的是索引视图和
COUNT\u BIG
上的索引,而不是基础表上的索引。什么会导致索引过时?当对基础数据执行插入、更新或删除操作时,SQL Server数据库引擎肯定会自动维护索引吗?
SELECT TOP 10 * FROM KeywordCount WITH (NOEXPAND) ORDER BY KeywordCount DESC