select UID, T_0_FLD_0, T_0_FLD_1, T_0_FLD_2, T_1_FLD_0, T_1_FLD_1, T_1_FLD_2 --... 
from
(
select ROW_NUMBER() over(order by UID) as pos, UID, T_0_FLD_0, T_0_FLD_1, T_0_FLD_2, T_1_FLD_0, T_1_FLD_1, T_1_FLD_2 --... 
from
(
Select DISTINCT UID, T_0_FLD_0, T_0_FLD_1, T_0_FLD_2, T_1_FLD_0, T_1_FLD_1, T_1_FLD_2 --...
   from VIEW_5
   where T_0_FLD_0='1' or T_1_FLD_1='2' or T_3_FLD_1='3' or T_4_FLD_1='4' or T_5_FLD_1='5'
   ) t
) tt 
where pos>100 and pos<200create view [dbo].[VIEW_5] as 
select T_0.UID, T_0.T_0_FLD_0, T_0.T_0_FLD_1, T_0.T_0_FLD_2,--...
T_1.T_1_FLD_0, T_1.T_1_FLD_1, T_1.T_1_FLD_2,--...
T_2.T_2_FLD_0, T_2.T_2_FLD_1, T_2.T_2_FLD_2,--...
T_3.T_3_FLD_0, T_3.T_3_FLD_1, T_3.T_3_FLD_2,--...
T_4.T_4_FLD_0, T_4.T_4_FLD_1, T_4.T_4_FLD_2,--...
T_5.T_5_FLD_0, T_5.T_5_FLD_1, T_5.T_5_FLD_2--...
from T_0
left join T_1 on T_0.UID=T_1.UID 
left join T_2 on T_0.UID=T_2.UID 
left join T_3 on T_0.UID=T_3.UID 
left join T_4 on T_0.UID=T_4.UID 
left join T_5 on T_0.UID=T_5.UID 所有表的UID字段上都建立了非聚集索引。
由于查询的结果字段和条件不固定,所以预先创建了VIEW_5这个视图。

解决方案 »

  1.   

    将子查询的表先插入到临时表,在临时表上操作SELECT ....
    INTO #T
    FROM 表
      

  2.   

    where T_0_FLD_0='1' or T_1_FLD_1='2' or T_3_FLD_1='3' or T_4_FLD_1='4' or T_5_FLD_1='5'如果条件固定一点,或者看你查询时哪些条件会用得多一点,根据这些条件来建立合适的索引,并包含UID可能会好点,还有根据你返回的数据量,列的数目,直接看你的那视图,很难优化了,没条件限制,基本都是表扫描或(聚集索引扫描),如果有包含索引的话,也许可以索引扫描,节约一些I/O
      

  3.   


    create proc getInfo
    as
    if object_id('#T') is not null drop table #T
    go
    select * into #T from
    (
    create view [dbo].[VIEW_5] as 
    select T_0.UID, T_0.T_0_FLD_0, T_0.T_0_FLD_1, T_0.T_0_FLD_2,--...
    T_1.T_1_FLD_0, T_1.T_1_FLD_1, T_1.T_1_FLD_2,--...
    T_2.T_2_FLD_0, T_2.T_2_FLD_1, T_2.T_2_FLD_2,--...
    T_3.T_3_FLD_0, T_3.T_3_FLD_1, T_3.T_3_FLD_2,--...
    T_4.T_4_FLD_0, T_4.T_4_FLD_1, T_4.T_4_FLD_2,--...
    T_5.T_5_FLD_0, T_5.T_5_FLD_1, T_5.T_5_FLD_2--...
    from T_0
    left join T_1 on T_0.UID=T_1.UID 
    left join T_2 on T_0.UID=T_2.UID 
    left join T_3 on T_0.UID=T_3.UID 
    left join T_4 on T_0.UID=T_4.UID 
    left join T_5 on T_0.UID=T_5.UID 
    ) TT
    goselect UID, T_0_FLD_0, T_0_FLD_1, T_0_FLD_2, T_1_FLD_0, T_1_FLD_1, T_1_FLD_2 --... 
    from
    (
        select ROW_NUMBER() over(order by UID) as pos, UID, T_0_FLD_0, T_0_FLD_1, T_0_FLD_2, T_1_FLD_0, T_1_FLD_1, T_1_FLD_2 --... 
            from
            (
                Select DISTINCT UID, T_0_FLD_0, T_0_FLD_1, T_0_FLD_2, T_1_FLD_0, T_1_FLD_1, T_1_FLD_2 --...
              from #T
              where T_0_FLD_0='1' or T_1_FLD_1='2' or T_3_FLD_1='3' or T_4_FLD_1='4' or T_5_FLD_1='5'
          ) t
    ) tt 
    where pos>100 and pos<200
      

  4.   

    如果只有这些表,,,那就这样了啊
    有必要优化吗?
    不过如果你view5很庞大的话,,建议别做view
    view的作用可不是拿来提高程序效率的
      

  5.   

    不建立这个view的话没法实现该功能啊。
      

  6.   

    OR字句导致SQL Server走不到Index,可以考虑将Or字句修改为UNION起来。将下面的语句:
    Select DISTINCT UID, T_0_FLD_0, T_0_FLD_1, T_0_FLD_2, T_1_FLD_0, T_1_FLD_1, T_1_FLD_2 --...
              from VIEW_5
              where T_0_FLD_0='1' or T_1_FLD_1='2' or T_3_FLD_1='3' or T_4_FLD_1='4' or T_5_FLD_1='5'
    修改为:
    Select UID, T_0_FLD_0, T_0_FLD_1, T_0_FLD_2, T_1_FLD_0, T_1_FLD_1, T_1_FLD_2 --...
    from VIEW_5
    where T_0_FLD_0='1' 
    UNION 
    Select UID, T_0_FLD_0, T_0_FLD_1, T_0_FLD_2, T_1_FLD_0, T_1_FLD_1, T_1_FLD_2 --...
    from VIEW_5
    where T_1_FLD_1='2' 
    UNION 
    Select UID, T_0_FLD_0, T_0_FLD_1, T_0_FLD_2, T_1_FLD_0, T_1_FLD_1, T_1_FLD_2 --...
    from VIEW_5
    where  T_3_FLD_1='3'
    UNION 
    Select UID, T_0_FLD_0, T_0_FLD_1, T_0_FLD_2, T_1_FLD_0, T_1_FLD_1, T_1_FLD_2 --...
    from VIEW_5
    where  T_4_FLD_1='4'
    UNION 
    Select UID, T_0_FLD_0, T_0_FLD_1, T_0_FLD_2, T_1_FLD_0, T_1_FLD_1, T_1_FLD_2 --...
    from VIEW_5
    where  T_5_FLD_1='5'
    试试看看。
      

  7.   

    我暈到底什麽樣的情況才會必須使用,這種中間層一樣的view啊
    你實在要用view,又想提高效率,,
    那你就做多個view吧
    每個view使用不同的
    T_0_FLD_0='1' or T_1_FLD_1='2' or T_3_FLD_1='3' or T_4_FLD_1='4' or T_5_FLD_1='5'
    這樣縂能保證時間和效率了吧
    還不行,,那就寫存儲過程吧,拼接動態SQL總不會出錯