对于大量left join 的表查询,可以在关键的 连接节点字段上创建索引。

问题:

大量的left join 怎么优化
select a.id,a.num,b.num,b.pcs,c.num,
c.pcs,d.num,d.pcs,e.num,e.pcs,a.x,
a.y from a left join b.id=a.id and b.time=a.time
left join c.id=a.id and b.time=a.time
left join d.id=a.id and b.time=a.time
left join e.id=a.id and b.time=a.time
where a.time='2013-10-1'
这样查询效率很低,要几十分钟才能出laugh数据~每张表有上千万条数据,求问该如何优化?

答案:

在各个表的id和time属性上创建索引,而且把其中除了第一次left join中的 b.time=a.time外,其余的 b.time=a.time去掉,并先对b表执行    b.time='2013-10-1'的查询。
如果各表都需要判断时间的话,那么请先在各表上执行基于时间的选择操作,在参加左外连接。因此,时间字段上的索引很重要。

实例:自己实践经验:

select  --------------------------首先各个物料在各个期间的返利金额,然后根据每个期间求出对应每个月的返利总和-----------------------------------
    GZB.customer,GZB.company,GZB.jt,GZB.pz,
    ' then GZB.sl*GZB.fldj end) 二月,
    ' then GZB.sl*GZB.fldj end) 四月,
    ' then GZB.sl*GZB.fldj end) 六月,
    ' then GZB.sl*GZB.fldj end) 八月,
    ' then GZB.sl*GZB.fldj end) 十月,
     ,)), ,))
    from
    (        ----------------------------先把每个物料的返利单价,和对应的期间算出来------------------------
    select
    wldw.lswldw_dwmc customer,dw.lsbzdw_dwmc company,org.organizationname jt,ms.materialsortname pz,
    gs.issfiscalperiod qj,nvl(gm.actoutquantity,) sl,))))))) fldj
    from goodsmovement gs
    left join goodsmovementitem gm on gs.goodsmovementid=gm.goodsmovementid
    left join lswldw wldw on wldw.lswldw_wldwbh=gs.customerid
    left join lsbzdw dw on dw.lsbzdw_dwbh=gs.isscompanyid
    left join organizations org on gs.issorgid=org.organizationid                       --生产机台
    left join materials m on m.materialid=gm.materialid
    ,)
    ,)'
    '
    group by  wldw.lswldw_dwmc,dw.lsbzdw_dwmc,org.organizationname,ms.materialsortname ,
    gs.issfiscalperiod ,nvl(gm.actoutquantity,)
    ) GZB
    group by GZB.customer,GZB.company,GZB.jt,GZB.pz;

对于这段SQL,自己查询需要等待14秒出来结果。

但是给各表的关联字段上添加索引之后:

    create index gs_index on goodsmovement(goodsmovementid)
    create index gm_index on goodsmovementitem(goodsmovementid)
    create index gs_index1 on goodsmovement(customerid)
    create index gs_index2 on goodsmovement(isscompanyid)
    create index gs_index3 on goodsmovement(movetypeid)
    create index gs_index4 on goodsmovement(issorgid)

    create index dwbh on lswldw (lswldw_wldwbh)

查询速度大大提高:只需要0.17秒,快了几百倍!

对于大量left join 的表查询,可以在关键的 连接节点字段上创建索引。的更多相关文章

  1. mybatis逆向工程,实现join多表查询,避免多表相同字段名的陷阱

    ​ mybatis逆向工程,实现join多表查询,避免多表相同字段名的陷阱 ​ 前言:使用 mybatis generator 生成表格对应的pojo.dao.mapper,以及对应的example的 ...

  2. Django ORM queryset object 解释(子查询和join连表查询的结果)

    #下面两种是基于QuerySet查询 也就是说SQL中用的jion连表的方式查询books = models.UserInfo.objects.all() print(type(books)) --- ...

  3. mysql中各种join连表查询总结

    通常我们需要连接多个表查询数据,以获取想要的结果. 一.连接可以分为三类: (1) 内连接:join,inner join (2) 外连接:left join,left outer join,righ ...

  4. LEFT JOIN 多表查询的应用

    表结构如下:只把主要字段列出 表一:付款记录表  Gather 字段:GatherID , AccountID, PayMents 金额, PayWay  付款方式 1 现金 2 刷卡 表2:销售记录 ...

  5. sql多表查询时怎么获取查到的字段

    首先,多表查询不能用hql(貌似hql就是不支持多表查询,如果可以,希望看到的朋友给个例子) List list = systemService.findListbySql("SELECT ...

  6. mysql常用基础操作语法(八)~~多表查询合并结果和内连接查询【命令行模式】

    1.使用union和union all合并两个查询结果:select 字段名 from tablename1 union select 字段名 from tablename2: 注意这个操作必须保证两 ...

  7. Mysql学习总结(24)——MySQL多表查询合并结果和内连接查询

    1.使用union和union all合并两个查询结果:select 字段名 from tablename1 union select 字段名 from tablename2: 注意这个操作必须保证两 ...

  8. oracle锁表查询,资源占用,连接会话,低效SQL等性能检查

    查询oracle用户名,机器名,锁表对象 select l.session_id sid, s.serial#, l.locked_mode, l.oracle_username, l.os_user ...

  9. 【mysql】mysql中单列索引、联合索引、Join联表查询建立索引 和 EXPLAIN的分析使用

    2.创建联合索引,从坐到右分别为:userid.openId.name   2. #### --------------  多表联合查询 update 2019/03/13  ------------ ...

随机推荐

  1. xmpp整理笔记:环境的快速配置(附安装包)

    现在虽然环信的xmpp框架很火,但是也有一些弊端.环信的框架部分代码不开源,而且收费模式不科学,用户量一直低于免费线则好,一旦超过,收费极高. xmpp感觉还是从xmppFramework框架学起比较 ...

  2. VSS提示"Could not find the Visual SourceSafe Internet Web Service connection information for the specified database

    转自:http://www.cnblogs.com/qqflying/archive/2007/12/18/1004051.html VSS连接错误提示: ====================== ...

  3. RecyclerView的下拉刷新和加载更多 动画

    下拉刷新和加载更多 1.https://github.com/jianghejie/XRecyclerView 2.http://blog.csdn.net/jabony/article/detail ...

  4. Xcode8 pod install 报错 “Generating Pods project Abort trap

    Xcode8 pod install 报错 "Generating Pods project Abort trap 今天在写一个新项目的时候,使用cocoapods在执行 $ pod ins ...

  5. UITableView详细注释

    style //普通 UITableViewStylePlain, //分组 UITableViewStyleGrouped //表格视图 UITableView * tableView = [[UI ...

  6. AutoLayout自动布局

      原文转自http://www.cnblogs.com/xjf125/p/4895978.html 目录: 一.什么是AutoLayout? 二.创建autoLayout的方法 三.VFL语言   ...

  7. tomcat常见错误及解决方案

    一,tomcat启动时错误 1:The JAVA_HOME environment variable is not defined This environment variable is neede ...

  8. LCS(Longest Common Subsequence 最长公共子序列)

    最长公共子序列 英文缩写为LCS(Longest Common Subsequence).其定义是,一个序列 S ,如果分别是两个或多个已知序列的子序列,且是所有符合此条件序列中最长的,则 S 称为已 ...

  9. windows server 远程连接设置

    1设置远程连接数修改 服务器安装了Windows Server 2008,现在要增加远程连接,开启服务器上的远程桌面连接,使用管理员账户远程登录.默认情况下Windows Server 2008允许一 ...

  10. 敏捷开发--scrum

    1.  请简述一下什么是敏捷开发(Agile Development),以及什么是持续集成.  敏捷开发是一种以人为核心.迭代.循序渐进的开发方法.在敏捷开发中,软件项目的构建被切分成多个子项目,各个 ...