0

我已经为小型数据库编写了这段代码,但知道数据库大小已经增加,它显示超时错误。请帮助优化它下面是代码:-

IF OBJECT_ID('Temp_expo') is not null
begin 
    drop table Temp_expo
end

set @query3 =  'SELECT SPCT_ID_REL_LOW,SPCT_ID_REL_HIGH,ROW_NUMBER() over (order by PDBC_PFX) as TempId  

INTO Temp_expo  
FROM ['+ @FCTServer +'].['+@FCTDBName+'].dbo.CMC_SPCT_SUPP_CONV  
where  SPCT_ID_REL_LOW <> '''' and SPCT_ID_REL_HIGH <> '''''

exec (@query3)

Select @minCount= min(TempId) from Temp_expo   
Select @maxCount= max(TempId) from Temp_expo  

create table #ICD9SPCT
(
    ICD9Code varchar(200)
} 

while @minCount<=@maxCount 
begin
    select @low=SPCT_ID_REL_LOW,@high=SPCT_ID_REL_HIGH 
    from Temp_expo 
    where TempId=@minCount 
    group by SPCT_ID_REL_LOW,SPCT_ID_REL_HIGH

    set  @loworder = (select ISNULL(OrderId,0) from FCT_ICD9_Diag_ORDER where ICD9=@low)
    set  @highorder = (select ISNULL(OrderId,0) from FCT_ICD9_Diag_ORDER where ICD9=@high)


    insert into #ICD9SPCT 
    select ICD9 from FCT_ICD9_Diag_ORDER ordert
    left join #ICD9SPCT icdorder on ordert.ICD9 = icdorder.ICD9Code
    where OrderId between @loworder and @highorder and icdorder.ICD9Code is null

    set @minCount = @minCount+1;
end
4

1 回答 1

0

如果这是针对 SQL SERVER 的,您可以尝试一些基本技巧: USE: WITH (NOLOCK) 在您使用的每个选择之后。即选择ICD9 from FCT_ICD9_Diag_ORDER ordert WITH (NOLOCK) left join #ICD9SPCT icdorder on ordert.ICD9 = icdorder.ICD9Code where OrderId between @loworder and @highorder and icdorder.ICD9Code is null

您还可以尝试将临时表更改为变量表,只需将 # 更改为 @ like :

创建表 @ICD9SPCT ( ICD9Code varchar(200) }

尽管如此,您使用的 WHILE 循环可能是您的问题的主要原因。

于 2013-03-28T15:47:11.677 回答