数据库执行存储过程优化

Posted mjxxsc

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了数据库执行存储过程优化相关的知识,希望对你有一定的参考价值。

 

 下面的old 是原来的查询, 大概花了 58秒, 下面经过优化后的, 执行了22秒.

 

核心思想:  先缩小数据范围, 然后再和其他的表做 关联查询.

 

  ----new 2018-07-03 
  insert into @tab_xmxkxx
    select  DISTINCT CONVERT(NVARCHAR(50),GETDATE(),112) AS jhrq,
    tab_sgxkdwxx.wsslbh as wsslbh,
    tab_xmxkxx.xmmc as xmmc,
    tab_sgxkdwxx.dwmc AS zbdwmc,
    CONVERT(NVARCHAR(50),tab_xmxkxx.sdrq,112) AS hfxkzrq,
    tab_xmxkxx.fzdwmc AS hfxkzbm

    from 
       (select bjbh,bdh,wsslbh,dwmc FROM [SgXk].[tab_zbb_xmxkxx_dwxx] AS sgxkdwxx where ISNULL(sgxkdwxx.wsslbh,‘‘) <> ‘‘  AND sgxkdwxx.dwlb = sg) tab_sgxkdwxx,
       (select bjbh,bdh,xmmc,fzdwmc,sdrq from sgxk.tab_zbb_xmxkxx xmxkxx where  xmxkxx.state=SdApproved and xmxkxx.fzrq BETWEEN @startTime AND @endTime) tab_xmxkxx,
          @tab_htxx as tab_htxx
    where tab_sgxkdwxx.bjbh = tab_xmxkxx.bjbh  COLLATE Chinese_PRC_CS_AS and tab_sgxkdwxx.bdh = tab_xmxkxx.bdh   COLLATE Chinese_PRC_CS_AS
     and tab_sgxkdwxx.wsslbh = tab_htxx.wsslbh;
     
-----------------------------------------------------------------------------------------------------------------------------------
   
/*
-- old 2018-06-12
insert into @tab_xmxkxx
    SELECT DISTINCT CONVERT(NVARCHAR(50),GETDATE(),112) AS jhrq,
        sgxkdwxx.wsslbh AS wsslbh,
        xmxkxx.xmmc AS xmmc,
        sgxkdwxx.dwmc AS zbdwmc,
        CONVERT(NVARCHAR(50),xmxkxx.sdrq,112) AS hfxkzrq,
        xmxkxx.fzdwmc AS hfxkzbm FROM [SgXk].[tab_zbb_xmxkxx_dwxx] AS sgxkdwxx
       LEFT JOIN sgxk.tab_zbb_xmxkxx xmxkxx ON xmxkxx.bjbh COLLATE Chinese_PRC_CS_AS = sgxkdwxx.bjbh COLLATE Chinese_PRC_CS_AS AND sgxkdwxx.bdh COLLATE Chinese_PRC_CS_AS = xmxkxx.bdh COLLATE Chinese_PRC_CS_AS 
    WHERE ISNULL(sgxkdwxx.wsslbh,‘‘) <> ‘‘ AND xmxkxx.state=‘SdApproved‘ AND sgxkdwxx.dwlb = ‘sg‘ 
    
    AND sgxkdwxx.wsslbh IN (SELECT wsslbh FROM @tab_htxx)
*/    
-------------------------------------------------------------------------------------

 

以上是关于数据库执行存储过程优化的主要内容,如果未能解决你的问题,请参考以下文章

数据库执行存储过程优化

SQL Server——存储过程

SQL Server T—SQL 存储过程 触发器

MYSQL数据太大时怎么优化 视图?存储过程? - 技术问答

数据库基础详解:存储过程、视图、游标、SQL语句优化以及索引

oracle中的存储过程,有啥作用,以及怎么在代码中使用?