使用索引优化存储过程

时间:2014-05-04 05:49:57

标签: sql-server sql-server-2008 stored-procedures

我有一个这样的存储过程:

ALTER PROCEDURE [dbo].[IBS_fetchrequested]
   @tid integer = null,
   @Tbaroced dbo.TBarcode readonly
as 
begin
   SET NOCOUNT ON;

   if (select COUNT(*) from  Khanger_tbl 
       where tid = @tid and requested = 1 and delivered = 0) > 0
   begin
      select 
         t.TBarcode, k.HBarcode as keyloc 
      from 
         Khanger_tbl k
      inner join 
         transaction_tbl t on k.transactid = t.transactID
      where 
         tid = @tid 
         and requested = 1 
         and delivered = 0  
         and t.Tbarcode not in (select carid from @Tbaroced) 
         and t.status = 3
    end
end

在我的数据库中,我有大约2个缺少记录..如果我在tid Khanger_tbl添加索引,我的存储过程性能会提高吗?如何增加这些存储过程的性能?还有其他技术吗?

感谢任何帮助!

如果我添加索引,这将如何影响我的插入和更新? 我怎么能优化这个?

1 个答案:

答案 0 :(得分:0)

为什么在运行查询之前检查是否存在数据? 只需运行查询,如果没有数据,您将获得一个空的记录集。您正在这样做的方式是两次读取数据表。

...而且,是的,如果没有索引,在tid上放置索引会加快速度。它会轻微减慢插入速度。

对于data distribution,对于表中的任何特定列,这与表中有多少记录包含该列中的每个值有关。  例如,对于像requested这样的布尔coluimn,如果表中的数据分布只有一小部分记录具有requested值= 1,那么将索引放在`请求'会加快速度。

 ALTER PROCEDURE [dbo].[IBS_fetchrequested]
 @tid integer = null,
 @Tbaroced dbo.TBarcode readonly
 as 
 SET NOCOUNT ON;

   Select t.TBarcode, k.HBarcode as keyloc 
   From   Khanger_tbl k
      Join transaction_tbl t 
         on t.transactid = k.transactID
   Where tid = @tid 
     and requested = 1 
     and delivered = 0  
     and t.Tbarcode not in 
            (select carid from @Tbaroced) 
     and t.status = 3
相关问题