外连接的有效替代方案

发布于 2024-07-18 02:25:32 字数 565 浏览 11 评论 0原文

此查询上的 RIGHT JOIN 会导致 lims.operator 上出现 TABLE ACCESS FULL。 常规联接运行速度很快,但当然,示例“WHEREauthorized_by IS NULL”不会显示。

在这种情况下,是否有比 RIGHT JOIN 更有效的替代方案?

  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)

注意:显示的所有列(full_name 除外)均已编入索引,并且是某个表的主键。

The RIGHT JOIN on this query causes a TABLE ACCESS FULL on lims.operator. A regular join runs quickly, but of course, the samples 'WHERE authorised_by IS NULL' do not show up.

Is there a more efficient alternative to a RIGHT JOIN in this case?

  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)

NOTE: All columns shown (except full_name) are indexed and the primary key of some table.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

若有似无的小暗淡 2024-07-25 02:25:32

由于您正在执行外连接,因此很可能执行全表扫描实际上比使用索引更有效。

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

SELECT /*+ INDEX (lims.operatoroperator_index_name)*/ ...

然后看看会发生什么...

Since you're doing an outer join, it could easily be that it actually is more efficient to do a full table scan rather than use the index.

If you are convinced the index should be used, force it with a hint:

SELECT /*+ INDEX (lims.operator operator_index_name)*/ ...

then see what happens...

嗳卜坏 2024-07-25 02:25:32

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

select s.full_name
from lims.operator o, lims.sample s
where o.operator_id = s.authorised_by(+)
and s.sample_template_id = 200

No need to nest queries. Try this:

select s.full_name
from lims.operator o, lims.sample s
where o.operator_id = s.authorised_by(+)
and s.sample_template_id = 200
趴在窗边数星星i 2024-07-25 02:25:32

我已经有一段时间没有为 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

这仍然执行得那么糟糕吗?

I didn't write sql for oracle since a while, but i would write the query like this:

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

Does this still perform that bad?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文