加入时间太长

发布于 2024-10-19 02:28:04 字数 332 浏览 5 评论 0原文

我有一个查询,在 Oracle 中运行大约需要 5 分钟:

select t1.A,t2.B,t2.C,t2.D,t2.E 
from TABLE1 t1 join TABLE2 t2 
on t2.X = t1.Y 
where t1.F = <integer> 
and t2.G = <integer> 
and t1.H = <integer> 

t1 有 170 万行,t2 有 3100 万行。我有 t2.X 和 t1.F 的索引。尝试在 t1.Y、t2.G 和 t1.H 上添加索引,但没有帮助。有什么想法可以提高此连接的性能吗?

I have this query that takes about 5 minutes to run in Oracle:

select t1.A,t2.B,t2.C,t2.D,t2.E 
from TABLE1 t1 join TABLE2 t2 
on t2.X = t1.Y 
where t1.F = <integer> 
and t2.G = <integer> 
and t1.H = <integer> 

t1 has 1.7 million rows and t2 has 31 million. I have indices on t2.X and t1.F. Tried adding indices on t1.Y, t2.G and t1.H but they didn't help. Any ideas how I can improve performance of this join?

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

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

发布评论

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

评论(1

生死何惧 2024-10-26 02:28:04

查看涉及的所有字段,每个表上的覆盖索引将涉及可能的列

t1: Y, F, H, A
t2: X, G, C, D, E

任何条件的选择性如何:t2.X,t2.G或t1.Y,t1.F,t1.H?

如果没有一个单独的列具有足够的选择性(理想情况下为 0.5% 或更少),您可能需要创建一个或多个涉及多个列的覆盖索引,例如

t2 (G, X)
t1 (H, F, Y)

注意: 列的顺序在索引中非常重要 - 始终将最具选择性的(将列数据划分为最不同的集合的索引)放在第一位。

以存储为代价,您可以通过提供所有索引来使索引覆盖查询索引本身中的必要列。这意味着查询根本不需要返回表数据。

create index ix_t2 on t2 (G,X) INCLUDE (C,D,E)
create index ix_t1 on t1 (H,F,Y) INCLUDE (A)

编辑

看起来我在其中插入了 SQL Server DDL。在 Oracle 中,您必须将索引扩展为 t2 (G,X, C,D,E) - 但这会增加索引使用的选择性要求,因为索引键变得非常长。

Looking at all the fields involved, a covering index on each table would involve may columns

t1: Y, F, H, A
t2: X, G, C, D, E

How selective are ANY of the criteria: t2.X, t2.G or t1.Y, t1.F, t1.H ?

If none of the individual columns are selective enough (ideally 0.5% or less), you may want to create a or several covering index that involve multiple columns, for example

t2 (G, X)
t1 (H, F, Y)

Note: The order of columns in an index is very important - always put the most selective (the one that divides the column data into the most distinct sets) first.

At the expense of storage, you can make the index COVER the query by providing all the necessary columns in the index itself. This means the query does not need to go back to table data at all.

create index ix_t2 on t2 (G,X) INCLUDE (C,D,E)
create index ix_t1 on t1 (H,F,Y) INCLUDE (A)

EDIT

Looks like I slipped a SQL Server DDL in there. In Oracle, you would have to expand the index to become t2 (G,X, C,D,E) - but that increases selectivity requirements for index usage since the index key becomes very long.

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