我的页面上有一个搜索按钮。如果我没有指定任何搜索过滤器并单击搜索按钮,那么它应显示所有邮政编码。 这里的每个id都有许多邮政编码,长度从3到6不等。
我编写了一个很大的存储过程,因为每个条件都有不同的条件。
我的问题是整个存储过程的片段。执行它的时间占总执行时间的近60%,因此我的应用程序显示以下错误:
超时已过期。操作完成之前经过的超时时间或服务器没有响应
代码:
declare @id int
set @id= 21
select distinct T.PostalCode from Table T
where SUBSTRING(T.PostalCode,0,7) IN
(select PostalCode from Ref_Table where
ID=@id and LEN(PostalCode)=6)
UNION ALL
select distinct T.PostalCode from Table T
where SUBSTRING(T.PostalCode,0,6) IN
(select PostalCode from Ref_Table where
ID=@id and LEN(PostalCode)=5)
UNION ALL
select distinct T.PostalCode from Table T
where SUBSTRING(T.PostalCode,0,5) IN
(select PostalCode from Ref_Table where
ID=@id and LEN(PostalCode)=4)
UNION ALL
select distinct T.PostalCode from Table T
where SUBSTRING(T.PostalCode,0,4) IN
(select PostalCode from Ref_Table where
ID=@id and LEN(PostalCode)=3)
我已经尝试过所有可能的选项。任何人都可以建议我为此减少执行时间。
由于
Shivani