代码之家  ›  专栏  ›  技术社区  ›  Steven

有效替代外部连接

  •  0
  • Steven  · 技术社区  · 15 年前

    这个 RIGHT JOIN 在此查询上导致 TABLE ACCESS FULL lims.operator . 常规连接运行很快,但当然,示例 WHERE authorised_by IS NULL '不要出现。

    有没有比 右连接 在这种情况下?

      SELECT   full_name
      FROM       (SELECT   operator_id AS authorised_by, full_name
                    FROM lims.operator)
      RIGHT JOIN (SELECT   sample_id, authorised_by
                    FROM   lims.sample
                   WHERE   sample_template_id = 200)
      USING (authorised_by)
    

    注意:显示的所有列(全名除外)都已编入索引,并且是某些表的主键。

    3 回复  |  直到 15 年前
        1
  •  2
  •   DCookie    15 年前

    因为您正在执行外部联接,所以很容易地,执行完整表扫描比使用索引更有效。

    如果您确信应该使用索引,请使用提示强制执行:

    选择/*+索引(lims.operator operator_index_name)*/…

    然后看看会发生什么…

        2
  •  1
  •   BQ.    15 年前

    无需嵌套查询。试试这个:

    select s.full_name
    from lims.operator o, lims.sample s
    where o.operator_id = s.authorised_by(+)
    and s.sample_template_id = 200
    
        3
  •  1
  •   Stefan Steinegger    15 年前

    我很久没有为Oracle编写SQL,但是我会这样编写查询:

    SELECT lims.operator.full_name
    FROM       lims.operator
    RIGHT JOIN lims.sample
               on lims.operator.operator_id = lims.sample.authorized_by
               and sample_template_id = 200
    

    这仍然表现得那么糟糕吗?