查询的性能调优

时间:2018-04-11 05:56:38

标签: sql sql-server query-performance

我有一个需要很长时间才能运行的查询。这可能是因为我在连接条件中使用了太多isnulls。如何通过删除isnull

来优化它

有没有更新表的替代方法?查询如下:

 select pos.C_id
    ,pos.s_id
    ,pos.A_id
    ,pos.Ad_id
    ,pos.Pr_id
    ,pos.prog_id
    ,pos.port_id
    ,pos.o_type
    ,pos.o_id
    ,pos.s_id
    ,pos.c_id
    ,pos.s_type_id
    ,pos.s_type
    ,pos.e_date
    ,pos.mv
    ,0 is_pub
    , 1 is_adj           
    ,pos.is_unsup
    ,getdate() date
    ,getdate() timestamp
    from #temp pos
    left join acc c with(nolock) ON pos._id = c.c_id
    AND pos.account_id = c.account_id
    AND isnull(pos.Pr_id,0) = isnull(c.pr_id,0)
    AND isnull(pos.prog_id,0) = isnull(c.prog_id,0)
    AND isnull(pos.port_id,0) = isnull(c.port_id,0)
    and isnull(pos.style_type_id,0)=isnull(c.s_type_id,0)
    AND pos.s_id = c._id
    AND pos.c_id = c.c_id
    AND pos.s_type = c.s_type
    AND pos.is_unsup = c.is_uns
    AND pos.is_pub = 1
    where c.a_id is null

3 个答案:

答案 0 :(得分:1)

下面的查询怎么样

 select pos.C_id
    ,pos.s_id
    ,pos.A_id
    ,pos.Ad_id
    ,pos.Pr_id
    ,pos.prog_id
    ,pos.port_id
    ,pos.o_type
    ,pos.o_id
    ,pos.s_id
    ,pos.c_id
    ,pos.s_type_id
    ,pos.s_type
    ,pos.e_date
    ,pos.mv
    ,0 is_pub
    , 1 is_adj           
    ,pos.is_unsup
    ,getdate() date
    ,getdate() timestamp
    from #temp pos
    left join acc c with(nolock) ON pos._id = c.c_id
    AND pos.account_id = c.account_id
    AND pos.Pr_id = c.pr_id
    AND pos.prog_id = c.prog_id
    AND pos.port_id = c.port_id
    and pos.style_type_id=c.s_type_id
    AND pos.s_id = c._id
    AND pos.c_id = c.c_id
    AND pos.s_type = c.s_type
    AND pos.is_unsup = c.is_uns
    AND pos.is_pub = 1
    where c.a_id is null
union all
 select pos.C_id
    ,pos.s_id
    ,pos.A_id
    ,pos.Ad_id
    ,pos.Pr_id
    ,pos.prog_id
    ,pos.port_id
    ,pos.o_type
    ,pos.o_id
    ,pos.s_id
    ,pos.c_id
    ,pos.s_type_id
    ,pos.s_type
    ,pos.e_date
    ,pos.mv
    ,0 is_pub
    , 1 is_adj           
    ,pos.is_unsup
    ,getdate() date
    ,getdate() timestamp
    from #temp pos
    left join acc c with(nolock) ON pos._id = c.c_id
    AND pos.account_id = c.account_id
    AND pos.s_id = c._id
    AND pos.c_id = c.c_id
    AND pos.s_type = c.s_type
    AND pos.is_unsup = c.is_uns
    AND pos.is_pub = 1
    where c.a_id is null
    AND pos.Pr_id is null AND  c.pr_id is null
    AND pos.prog_id is null AND  c.prog_id is null
    AND pos.port_id is null AND  c.port_id is null
    and pos.style_type_id is null AND c.s_type_id is null

答案 1 :(得分:0)

尝试使用

AND (pos.Pr_id = c.pr_id OR (pos.Pr_id IS NULL AND c.pr_id IS NULL))

而不是

AND ISNULL(pos.Pr_id,0) = ISNULL(c.pr_id,0)

IS NULL比ISNULL

更有效

答案 2 :(得分:0)

您确实需要在临时表#temp上设置聚簇索引,如果不存在,sql-server将为连接提供默认的基于哈希的索引,并且它不适用于大型表。

如果您#temp表中的所有id列都是唯一键,则确实需要在其上创建聚簇索引:

CREATE CLUSTERED INDEX cx_temp ON #temp (YourIDColumn);

如果#temp中没有关键列,请以这种方式为其添加标识主键:

1)如果您使用SELECT INTO方法创建#temp,请更改

select *
into #temp
from YourQuery

select IDENTITY (int, 1,1) ThisIsTheKey, *
into #temp
from YourQuery

-- and then add the index
CREATE CLUSTERED INDEX cx_temp ON #temp (ThisIsTheKey);

1)如果您使用CREATE TABLE + INSERT INTO方法,只需添加如下列:

CREATE TABLE #TEMP (
    ThisIsTheKey INT IDENTITY(1,1) NOT NULL PRIMARY KEY,
    ..
    ..
    -- ALL YOUR OTHER COLUMNS
    ..
    ..
)

不要依赖于覆盖非聚集索引,因为优化程序可能不会使用它们,因为您选择了太多列。

首先使用聚簇索引进行测试,然后如果需要进一步优化,可以尝试添加一些特定的非聚簇索引。