VIEW与SQL语句的性能

Mat*_* W. 16 sql database performance view

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

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>)
Run Code Online (Sandbox Code Playgroud)

子选择查询都有自己的多个子选择查询,我没有显示,以免混淆语句.

我的团队中的一位开发人员认为视图会更好.我不同意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)
)
Run Code Online (Sandbox Code Playgroud)

Cha*_*ana 52

通常,根据数据库供应商,对视图执行查询会将View中定义的SQL与附加到您针对View传递的sql的Where子句谓词和Order By子句排序表达式相结合,从而得出要执行的组合完整SQL查询.然后执行它,好像它本身已传递给查询processsor,因此应该没有区别.

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

SQL Server View解析

当SQL语句引用非索引视图时,解析器和查询优化器会分析SQL语句和视图的来源,然后将它们解析为单个执行计划.SQL语句没有一个计划,视图也有单独的计划.

  • "视图是一种组织工具,而不是性能增强工具." - 完美地总结了它.谢谢 :) (10认同)

Ric*_*kNZ 8

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


sim*_*nks 0

除了视图之外,PrimaryKey AND 子句不是多余的吗?如果 PrimaryKey 值在一个列表中,那么它不会在另一个列表中吗?我认为将这两个子句压缩为一个子句可以提高性能。