Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/86.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 update语句的索引扫描_Sql_Performance_Tsql_Indexing - Fatal编程技术网

SQL update语句的索引扫描

SQL update语句的索引扫描,sql,performance,tsql,indexing,Sql,Performance,Tsql,Indexing,我有下面的SQL语句,我想提高效率。通过查看执行计划,我可以看到@newWebcastEvents上有一个聚集索引扫描。有没有办法让这变成一个寻找?或者有没有其他方法可以让下面的内容更有效 declare @newWebcastEvents table ( webcastEventId int not null primary key clustered (webcastEventId) with (ignore_dup_key=off) ) insert into @newWebca

我有下面的SQL语句,我想提高效率。通过查看执行计划,我可以看到@newWebcastEvents上有一个聚集索引扫描。有没有办法让这变成一个寻找?或者有没有其他方法可以让下面的内容更有效

declare @newWebcastEvents table (
    webcastEventId int not null primary key clustered (webcastEventId) with (ignore_dup_key=off)
)

insert into @newWebcastEvents
select wel.WebcastEventId
from WebcastChannelWebcastEventLink wel with (nolock)
where wel.WebcastChannelId = 1178

Update WebcastEvent
set WebcastEventTitle = LEFT(WebcastEventTitle, CHARINDEX('(CLONE)',WebcastEventTitle,0) - 2)
where
WebcastEvent.WebcastEventId in (select webcastEventId from @newWebcastEvents)

@newWebcastEvents
表格变量只包含一列,您在where子句中要求该表格变量的所有行

where
   WebcastEvent.WebcastEventId in (select webcastEventId from @newWebcastEvents)
因此,在此聚集索引上执行搜索通常是毫无意义的-SQL Server查询优化器无论如何都需要该表变量的所有列、所有行。。。。。所以它选择了索引扫描

无论如何,我不认为这是一个性能问题

如果您需要选择非常少的行,则索引搜索非常有用(搜索毫无意义,只需在一次扫描中读取15个
int
值并使用它,搜索速度将快得多

更新:不确定它在性能方面是否有任何不同,但我个人通常更喜欢使用连接而不是子选择来“连接”两个表:

UPDATE we
SET we.WebcastEventTitle = LEFT(we.WebcastEventTitle, CHARINDEX('(CLONE)', we.WebcastEventTitle, 0) - 2)
FROM dbo.WebcastEvent we
INNER JOIN @newWebcastEvents nwe ON we.WebcastEventId = nwe.webcastEventId

它只是一个临时表,而不仅仅是主键。表很小,最多只能有15行。但是我正在更新的表可能有数千行。WebcastChannelWebcastVentLink中大约有5000行,这个表中大约有15行被选入@newWebcastVentsoh,好的,干杯。这个查询没有意义从长远来看,我只是想提高我的知识水平。我被告知在我的查询中尽量避免索引扫描,所以我只是想纠正这一点,但上面的说法是有道理的。你认为这是我尝试做的最有效的方法吗?@user3284707:是的,我还建议尝试避免索引扫描-在非常大的表上s、 例如,100000行或更多行-如果您只需要提取少数行(占总数的1-2%)从这些表中。但是15
int
值-这是60个字节,比单个页面的8K小得多-SQL Server总是基于页面加载…非常感谢,如果这被认为是最佳实践,我将考虑将我的更新更改为此。感谢您的帮助。