【问题标题】:Performance of VIEW vs. SQL statementVIEW 与 SQL 语句的性能
【发布时间】:2010-12-24 11:37:57
【问题描述】:

我有一个类似于以下内容的查询:

select <field list> 
from <table list>
where <join conditions>
and <condition list>
and PrimaryKey in (select PrimaryKey from <table list>
    where <join list> 
    and <condition list>)
and PrimaryKey not in (select PrimaryKey from <table list>
    where <join list>
    and <condition list>)

子选择查询都有自己的多个子选择查询,我没有显示这些查询,以免语句混乱。

我团队中的一位开发人员认为视图会更好。我不同意 SQL 语句使用程序传入的变量(基于用户的登录 ID)。

对于何时应使用视图与何时使用 SQL 语句是否有任何硬性规定?在针对常规表与针对视图单独运行 SQL 语句时,存在什么样的性能增益问题。 (请注意,所有连接 / where 条件都针对索引列,因此这应该不是问题。)

为澄清而编辑...

这是我正在处理的查询:

select obj_id
from object
where obj_id in( 
(select distinct(sec_id) 
        from security 
        where sec_type_id = 494
        and (
            (sec_usergroup_id = 3278 
            and sec_usergroup_type_id = 230)
            or
            (sec_usergroup_id in (select ug_gi_id 
            from user_group 
            where ug_ui_id = 3278)
            and sec_usergroup_type_id = 231)
        )
        and sec_obj_id in (
        select obj_id from object 
        where obj_ot_id in (select of_ot_id 
            from obj_form 
            left outer join obj_type 
            on ot_id = of_ot_id 
            where ot_app_id = 87
            and of_id in (select sec_obj_id 
                from security
                where sec_type_id = 493
                and (
                    (sec_usergroup_id = 3278 
                    and sec_usergroup_type_id = 230)
                    or
                    (sec_usergroup_id in (select ug_gi_id 
                        from user_group 
                        where ug_ui_id = 3278)
                    and sec_usergroup_type_id = 231)
                    )                
            )   
            and of_usage_type_id  = 131
        )
        )   
        )
)
or 
(obj_ot_id in (select of_ot_id 
        from obj_form
        left outer join obj_type 
        on ot_id = of_ot_id 
        where ot_app_id = 87
        and of_id in (select sec_obj_id 
            from security
            where sec_type_id = 493
            and (
                (sec_usergroup_id = 3278 
                and sec_usergroup_type_id = 230)
                or
                (sec_usergroup_id in (select ug_gi_id 
                    from user_group 
                    where ug_ui_id = 3278)
                and sec_usergroup_type_id = 231)
                )
        )
        and of_usage_type_id  = 131

    )
    and
    obj_id not in (select sec_obj_id 
        from security 
        where sec_type_id = 494)
)

【问题讨论】:

    标签: database performance view sql


    【解决方案1】:

    撇开观点不谈,PrimaryKey AND 子句不是多余的吗?如果 PrimaryKey 值在一个列表中,它不会不在另一个列表中吗?我认为将这两个子句浓缩为一个会提高性能。

    【讨论】:

    • 我编辑了它以进行澄清。在试图总结该陈述时,我错误地犯了一个错误。请参阅我添加我们正在使用的语句的原始帖子中的编辑。
    【解决方案2】:

    根据数据库供应商的不同,一般来说,对视图执行查询会将视图中定义的 SQL 与 Where 子句谓词和 Order By 子句排序表达式结合起来,这些表达式附加到您针对视图传递的 sql 中,以想出一个组合完整的 SQL 查询来执行。然后执行它,就好像它本身已传递给查询进程一样,因此应该没有区别。

    视图是一种组织工具,而不是性能增强工具。

    来自SQL Server View resolution

    当一条 SQL 语句引用一个 非索引视图、解析器和查询 优化器分析两者的来源 SQL 语句和视图和 然后将它们分解为一个 执行计划。没有一个计划 对于 SQL 语句和一个单独的 计划视图。

    【讨论】:

    • 伟大的职位查尔斯!你对此有什么参考吗?提前致谢!
    • “视图是一种组织工具,而不是性能增强工具。” - 很好地总结了它。谢谢:)
    【解决方案3】:

    常规(非索引/物化)视图只是别名;它们不提供任何性能优势。从视图中选择会生成与直接从表中选择完全相同的查询计划。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-08-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-01
      • 2013-09-01
      • 2010-10-07
      • 1970-01-01
      相关资源
      最近更新 更多