2012-12-06 62 views
1

我有一个存储过程,它创建一个2x2表的比值比。一个基本的比值比表如下所示: enter image description here存储过程运行速度比查询慢得多

编辑 - 此查询终于没有完成,它做了两分钟,32个单独的函数调用后返回正确的答案。我不明白为什么这是递归运行,任何想法,所以呢?

A - only records that satisfy both thing 1 and thing 2 go here 
B - only records that satisfy thing 1 (people with thing 2 CANNOT go here) 
C - only records that satisfy thing 2 (people with thing 1 CANNOT go here) 
D - people with thing 1 OR thing 2 cannot go here 

表中的所有单元格都将是表示人口总数的整数。

我试图学习一些新的语法,并决定使用intersectexcept。我想制作thing 1thing 2的变量,所以我把下面的查询放到存储过程中。

CREATE PROC Findoddsratio (@diag1 NVARCHAR(5), 
          @diag2 NVARCHAR(5)) 
AS 
    IF Object_id('tempdb..#temp') IS NOT NULL 
     DROP TABLE #temp 

    CREATE TABLE #temp 
     ( 
     squarenumber CHAR(1), 
     counts  FLOAT 
    ) 

    INSERT INTO #temp 
       (squarenumber, 
       counts) 
    SELECT * 
    FROM ( 
      --both + 
      SELECT 'a'         AS squareNumber, 
        Cast(Count(DISTINCT x.counts)AS FLOAT) AS counts 
      FROM (SELECT DISTINCT ic.patid AS counts 
        FROM icdclm AS ic 
        WHERE ic.icd LIKE @diag1 
        INTERSECT 
        SELECT DISTINCT ic.patid AS counts 
        FROM icdclm AS ic 
        WHERE ic.icd LIKE @diag2)x 
      UNION 
      --only 1+ 
      SELECT 'b', 
        Count(DISTINCT x.counts) 
      FROM (SELECT DISTINCT ic.patid AS counts 
        FROM icdclm AS ic 
        WHERE ic.icd LIKE @diag1 
        EXCEPT 
        SELECT DISTINCT ic.patid AS counts 
        FROM icdclm AS ic 
        WHERE ic.icd LIKE @diag2)AS x 
      UNION 
      --only 2+ 
      SELECT 'c', 
        Count(DISTINCT x.counts) 
      FROM (SELECT DISTINCT ic.patid AS counts 
        FROM icdclm AS ic 
        WHERE ic.icd LIKE @diag2 
        EXCEPT 
        SELECT DISTINCT ic.patid AS counts 
        FROM icdclm AS ic 
        WHERE ic.icd LIKE @diag1)AS x 
      UNION 
      --both - 
      SELECT 'd', 
        Count(DISTINCT x.counts) 
      FROM (SELECT DISTINCT ic.patid AS counts 
        FROM icdclm AS ic 
        EXCEPT 
        SELECT DISTINCT ic.patid AS counts 
        FROM icdclm AS ic 
        WHERE ic.icd LIKE @diag2 
        EXCEPT 
        SELECT DISTINCT ic.patid AS counts 
        FROM icdclm AS ic 
        WHERE ic.icd LIKE @diag1) AS x)y 

    --i used a pivot table to make the math work out easier 
    SELECT Round(Cast((a * d)/(b * c) AS FLOAT), 2) AS OddsRatio 
    FROM (SELECT [a], 
        [b], 
        [c], 
        [d] 
      FROM (SELECT [squarenumber], 
          [counts] 
        FROM #temp) p 
        PIVOT (Sum(counts) 
         FOR [squarenumber] IN ([a], 
               [b], 
               [c], 
               [d])) AS pvt)t 

ICDCLM是一个结构类似patid=int, icd=varchar(5)

有〜在ICDCLM一个百万行。当我运行这个查询而不使它成为存储过程时,它会在几秒钟内运行。如果我尝试exec FindsOddsRation 'thing1%','thing2%'。它运行并运行,但从不返回任何内容(> 2分钟)。存储过程需要这么长时间才有什么不同? SQL Server 2008 R2 小提琴here

+1

我会查找参数嗅探,如果我是你 – HLGEM

+0

嘿,这是堆栈溢出,而不是红管!我不嗅探什么'(但是,谢谢,我会研究它) – wootscootinboogie

+0

我不知道如果我正确使用它,但我没有看到任何长时间运行的查询:http:// sqlfiddle。 com /#!3/4a883/10 – Shmiddty

回答

5

如果您正在运行与存储过程相同的确切SQL并且时间不同,那么您的存储过程可能依赖于过时的元数据。尝试更新统计信息或重新编译存储过程。

+0

你有什么想法,为什么这个东西会运行32次而没有'with recompile'并且正常运行呢? – wootscootinboogie

+0

@wootscootinboogie:在删除'WITH RECOMPILE'之后,它是否仍然运行32次,现在它已重新编译? – zimdanen

+0

不,它运行得非常好。尽管如此,我确实改变了类似的运算符到'='加快了速度。 – wootscootinboogie

相关问题