加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > MsSql教程 > 正文

数据库 – VIEW与SQL语句的性能

发布时间:2020-12-12 16:37:07 所属栏目:MsSql教程 来源:网络整理
导读:我有一个查询,如下所示: select field list from table listwhere join conditionsand condition listand PrimaryKey in (select PrimaryKey from table list where join list and condition list)and PrimaryKey not in (select PrimaryKey from table list
我有一个查询,如下所示:
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语句与常规表相对于视图. (请注意,所有联接/条件与索引列相关,因此不应该是一个问题.)

编辑澄清…

这是我正在使用的查询:

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)
)

解决方法

根据数据库供应商,一般来说,针对视图执行查询会将视图中定义的SQL与Where子句谓词和Order By子句排序表达式相结合,该表达式附加到您传递给View的sql中,以提出一个组合的完整SQL查询执行.然后执行它就好像它已经被传递给查询processsor,所以应该没有区别.

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

从SQL Server View resolution

When an SQL statement references a nonindexed view,the parser and query optimizer analyze the source of both the SQL statement and the view and then resolve them into a single execution plan. There is not one plan for the SQL statement and a separate plan for the view.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读