0

您好我正在使用 SQL 管理工作室 2008R2。

我有一个查询来获取 tblScan 的扫描 ID,其中一些 varbinary(max) 字段为空。我的查询是:

select SCANID 
from tblScan 
where scanFileFace is null 
  and scanFileAvatar is null 
  and hair is null

当我在 SQL 中执行查询时,第一次运行此查询需要一分半钟。在我的客户端,这给出了超时异常。如果我没有在 SQL 管理工作室中运行过 1 次查询。

优化此查询的最佳方法是什么?或者可以只增加我的连接字符串中的超时时间吗?

编辑:

这是我的桌子设计:

SCANID - int
scanDate - datetime
scanFileMeasurements - varbinary(MAX)
MEMBERID - int
scanFileFace - varbinary(MAX)
scanFileAvatar - varbinary(MAX)
头发- varbinary(MAX)

提前致谢!

4

3 回答 3

2

请在 scanFileFace 、scanFileAvatar 和 hair 字段上使用索引。

创建一个计算列,该列将在目标字段中更改值时自动计算,并在此计算字段上创建索引。我会大大提高查询性能。

alter table tblScan
add ContentLength as ISNULL(DATALENGTH(scanFileFace ),0) persisted

CREATE NONCLUSTERED INDEX [IX_tblScan_ContentLength] ON [dbo].[tblScan] 
(
    [ContentLength] ASC
)

select scanid from tblScan where ContentLength > 0
于 2012-05-07T08:40:27.690 回答
1

也许您想要一个带有索引的计算布尔字段。

插入时,让布尔字段成为您现有的条件;更新时,您可以使用触发器执行此操作

于 2012-05-07T08:59:20.207 回答
1

您可能会尝试使用物化视图。简而言之,它是一个索引视图,其行为类似于表并随着基础数据的变化而变化,但它不需要执行 select,因为在基础表的 CRUD 操作期间已经准备好数据。

create view EmptyScans with schemabinding
as
    select SCANID 
      from dbo.tblScan 
     where scanFileFace is null 
       and scanFileAvatar is null 
       and hair is null
GO
create unique clustered index ix_empty_scans on EmptyScans (ScanID)
GO

select scanid 
  from EmptyScans (noexpand)

从物化视图中选择时不要忘记添加 noexpand 提示,否则它将充当普通视图(至少在我使用 Sql Server 2005 的经验中)。

于 2012-05-07T09:58:27.763 回答