如何优化此嵌套查询?

问题描述 投票:0回答:3

我希望就我为工作而写的查询获得一些意见。简而言之,它使用选择和一些联接来显示工厂生产线上组装零件的数据。该查询效果很好,但是执行大约需要40秒,当我查看多个部分(一次一个)时,这几乎没有用。

我在这里可以做些什么来优化它?我无法控制的最大吸引因素是“ item_xref”表未建立索引,这会使事情变慢。

项目表包含零件的状态,模型表保存该项目的模型的信息,如果两个零件相互连接,则item_xref表用于记录。结果是:我扫描了零件上的序列号,显示了该零件的所有组件,然后显示了组件模型的说明,以及添加的日期和添加位置。这用英文显示了附带的内容,而不是大量的序列号。

我还有一些参数可以隐藏某些工位和组件,例如系统记录的螺钉和夹子,但隐藏报表中不需要的绒毛数据。

select 
    i.ITEM_ID as "Components of Target", 
    m.DESCRIPTION as "Component Type",
    ix.LAST_UPDATE as "Time added",
    ix.LAST_USER as "Added by"
from item_xref as ix with (nolock) 
join item as i with (nolock) on ix.COMPONENT_ITEM_ID = i.ITEM_ID 
join model as m with (nolock) on i.MODEL_ID = m.MODEL_ID 
where ix.COMPONENT_ITEM_ID in (
    select component_item_id 
    from item_xref 
    where 
        item_ID = @variable 
        and COMPONENT_ITEM_ID not like '1T%' 
        and COMPONENT_ITEM_ID not like 'T4%' 
        and LAST_USER not like '%IMM%'
        and LAST_USER not like '%HST%'
    )

谢谢您的任何建议!

我希望就我为工作而写的查询获得一些意见。简而言之,它使用选择和一些联接来显示工厂生产线上组装零件的数据。该查询效果很好,但是...

sql sql-server query-performance
3个回答
0
投票

好吧,您可以将其更改为这样的联接


0
投票
select
    x.component_item_id as "Components of Target", 
    m.DESCRIPTION as "Component Type",
    x.LAST_UPDATE as "Time added",
    x.LAST_USER as "Added by"
from 
(
    select top (100000) component_item_id, LAST_UPDATE, LAST_USER
    from item_xref with (nolock)
    where 
        item_ID = @variable 
        and COMPONENT_ITEM_ID not like '1T%' 
        and COMPONENT_ITEM_ID not like 'T4%' 
        and LAST_USER not like '%IMM%'
        and LAST_USER not like '%HST%'
) as x
join item as i with (nolock) on x.component_item_id = i.ITEM_ID 
join model as m with (nolock) on i.MODEL_ID = m.MODEL_ID; 

0
投票

您的查询和数据库设计中的某些问题肯定是固定的。

© www.soinside.com 2019 - 2024. All rights reserved.