zoukankan      html  css  js  c++  java
  • SQL Server Join

    SQL Server Join

    执行顺序

    ON 定义表连接字段(数据量大的时候添加索引也可以查询速度进行优化

    On.. And.. : And 限定的条件是在 Join之前对目标表的数据进行限定

    Where 对连接后的数据进行过滤筛选

    SELECT * FROM EMPLOY  E LEFT JOIN DEPARTMENT D 
    ON E.DEPTNO=D.DEPTNO AND D.DEPTNO=40
    -- EMPLOY 表不添加限制,所有连接之前获取所有EMPLOY表的数据 相当于Select * From Employ tmp_Employ
    -- DEPARTMENT 表 ,获取DEPTNO 为40的 数据,Select * from Department where Deptno = 40 tmp_Department
    -- 最后结果为 tmp_Department td * tmp_Employ te on te.deptno = td.deptno


    SELECT * FROM EMPLOY E LEFT JOIN DEPARTMENT D
    ON E.DEPTNO=D.DEPTNO Where D.DEPTNO=40
    -- 将 And 换成Where 则变成了两个全表数据关联之后
    -- 再对数据进行筛选 Where Deptno = 40 的数据

     

    MSSQL : 执行顺序 From -> Join -> On -> And ->Left(Right) -> Where -> Select

    Join的逻辑层

    alt text

    详情博文 : SQL SERVER – Better Performance – LEFT JOIN or NOT IN?.

    个人认为 Outer Join 实际上 是在 Inner join 的结果后再对目标集进行筛选

    Join 的物理层

    在写查询语句的时候遇到一种情况,两张表关联的时候,对其中的一张小表加上Where条件反而导致了查询速度更慢的问题,最后排查到是优化器,选择了错误的表数据的关联方式

    没加Where之前

    加了Where之后

    初看感觉所有的消耗都在对表查询,实际而次情况的Neseted Loop(嵌套循环) 是性能消耗最高的地方;

    Hash Map
    Nested Map
    Merge Map

    底层太难了,懒得写,引用下别人的解释

    A nested loop query plan is usually optimal when there are small numbers of rows in one table (think 10s to perhaps 100s in most cases) that can be probed into another table that is either very small or has an index that allows a seek for each input. One thing to watch out for here is when the optimizer THINKS there will be few rows (check the estimated rows in the popup for the estimated query plan graphic) but in reality there are LOTS of rows (thousands or even millions). This is one of the worst things that can happen to a query plan and is usually the result of either out-of-date statistics, a cached query plan or data that is unevenly distributed. Each of these can be addressed to minimize the likelyhood of the problem

    .A hash-match plan is optimal when there is a relatively few rows joined into a relatively large number of rows. Gail's post explains the basics of the mechanism. It can get REALLY slow on machines with insufficient buffer RAM to contain the hash tables in memory because they will have to be laid down to disk at a HUGE relative cost.

     

  • 相关阅读:
    应用开发框架之——业务规则脚本化
    tms脚本演示代码之一
    根据.DFM文件动态生成窗体以及在之前先必须注册窗体中使用到的类
    界面/业务规则脚本化
    delphi 脚本引擎比较
    html5 datalist 选中option选项后的触发事件
    Laravel 5.6 模型关联 user 表后查询 user 表数据只能获取第一条数据,不知道怎么获取第二条...
    小技巧两个感叹号(两个!)连用
    Bootstrap 字体图标(Glyphicons)
    使用withCount后再使用select设置查询的字段。就找不到withCount的数据了
  • 原文地址:https://www.cnblogs.com/Gilfoyle/p/12005143.html
Copyright © 2011-2022 走看看