Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/77.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
SQL查询速度非常慢,需要改进_Sql_Sql Server - Fatal编程技术网

SQL查询速度非常慢,需要改进

SQL查询速度非常慢,需要改进,sql,sql-server,Sql,Sql Server,我有一个select查询,需要改进以加快查询速度 SELECT st.SigHistUID, st.RelationType, st2.RelationType, case when st.RelationType <> st2.RelationType then 'N' else st.RelationType end as [NewRelationType], st.ToSigUID

我有一个select查询,需要改进以加快查询速度

SELECT 
    st.SigHistUID, st.RelationType, st2.RelationType, 
    case 
       when st.RelationType <> st2.RelationType 
          then 'N' 
          else st.RelationType 
    end as [NewRelationType],
    st.ToSigUID , sh.FullDescription, sh.SigHistUID AS ToSigHistUID
FROM 
    [Stackability] (nolock) st
INNER JOIN 
    [SignatureHistory] (nolock) sh ON sh.SigUID = st.ToSigUID
                                    AND sh.SigHistUID = (SELECT TOP 1 SigHistUID FROM [SignatureHistory] (nolock) tmp where tmp.SigUID = sh.SigUID ORDER BY SigHistUID DESC)
INNER JOIN 
    [SignatureHistory] (nolock) sh2 ON st.SigHistUID = sh2.SigHistUID
INNER JOIN 
    Stackability (nolock) st2 on st2.ToSigUID = sh2.SigUID and st2.SigHistUID = sh.SigHistUID
WHERE 
    st.SigHistUID < 1000 and
    st.RelationType <> st2.RelationType
ORDER BY 
    st.SigHistUID

尝试排除前1个条件:

SELECT 
    st.SigHistUID, st.RelationType, st2.RelationType, 
    case 
       when st.RelationType <> st2.RelationType 
          then 'N' 
          else st.RelationType 
    end as [NewRelationType],
    st.ToSigUID , sh.FullDescription, sh.SigHistUID AS ToSigHistUID
FROM 
    [Stackability] (nolock) st
INNER JOIN 
    (select distinct siguid, max(SigHistUID) OVER(PARTITION BY siguid ) as  SigHistUID from [SignatureHistory] (nolock))  sh ON sh.SigUID = st.ToSigUID

INNER JOIN 
    [SignatureHistory] (nolock) sh2 ON st.SigHistUID = sh2.SigHistUID
INNER JOIN 
    Stackability (nolock) st2 on st2.ToSigUID = sh2.SigUID and st2.SigHistUID = sh.SigHistUID
WHERE 
    st.SigHistUID < 1000 and
    st.RelationType <> st2.RelationType
ORDER BY 
    st.SigHistUID

这信息太少了!桌子的结构是什么?在这些表上定义了什么类型的索引??每个表格都有什么样的数据?至少你应该,你可以使用并分享你问题中的链接。此外,也许您可以通过查询找出性能问题。最后,在执行的查询中包含。根据SignatureHistory的大小,由于子查询结果未被索引,您的子查询可能会在sh连接上减慢速度。也许可以找到一种避免子查询的方法。旁注:我看到您在所有表上都使用nolock。我希望你理解这些含义。如果你不确定,非常感谢大家的回复。我还有最后一个问题,当stack.RelationType在mybatis中为NullStack\u recip.RelationType“-”时,有人知道如何写这个吗?