连接多个大表

发布于 2025-01-02 01:09:49 字数 523 浏览 2 评论 0原文

我有三个表

Table T1: Columns {a,b,c}, combination {a,b} is unique in Table T1
Table T2: Columns {a,c,d}, combination {a,c} is unique in Table T2
Table T3: Columns {a,c,e}, combination {a,c} is unique in Table T3

,其中 {a,b,c} 是字符串,{d,e} 是数字

我希望我的输出是

Table T4: Columns {a,b} where e/d < x.

表 T1,T2,T3 都非常大(数百万行),所以我实现这个的方式是通过对表 T1 T2 进行左联接并将结果保存到临时表,然后对表 T1 T3 进行左联接并保存结果,然后对结果进行左联接,最后将这些临时表合并为 1 个最终表桌子。

我的问题是,有没有更好/更有效的方法来做到这一点而不破坏数据库?

I have three tables

Table T1: Columns {a,b,c}, combination {a,b} is unique in Table T1
Table T2: Columns {a,c,d}, combination {a,c} is unique in Table T2
Table T3: Columns {a,c,e}, combination {a,c} is unique in Table T3

where {a,b,c} are strings and {d,e} are numbers

I want my output to be

Table T4: Columns {a,b} where e/d < x.

Tables T1,T2,T3 are all very large (millions of rows), so the way I implemented this is by doing an left join on table T1 T2 and saving the results to a temp table, then doing an left join on tables T1 T3 and saving the results, then doing an left join of the results and finally combining these temp tables into 1 final table.

My question is, is there a better/more efficient way to do this without blowing up the database?

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

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

发布评论

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

评论(1

当梦初醒 2025-01-09 01:09:49

您是否尝试过直接这样做?

select t1.a, t1.b
into t4
from t1
join t2 on t1.a = t2.a and t1.c = t2.c
join t3 on t1.a = t3.a and t1.c = t3.c
where t3.e / t2.d < x

在临时表中存储数百万条记录对于性能或存储都没有好处......

Have you tried just doing this the straight-forward way?

select t1.a, t1.b
into t4
from t1
join t2 on t1.a = t2.a and t1.c = t2.c
join t3 on t1.a = t3.a and t1.c = t3.c
where t3.e / t2.d < x

Storing millions of records in temp tables isn't good for performance or storage...

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