可以共享CTE名称的SQL脚本并行运行吗?

huangapple go评论64阅读模式
英文:

Can SQL scripts with shared CTE names be run in parallel?

问题

你有两个 SQL 脚本,它们都包含公共表达式(CTE)。这两个脚本中的CTE都使用了相同的名称。例如:

脚本1:

with c1 as (
  select * from t1
),
c2 as (
  select * from t2
)

脚本2:

with c1 as (
  select * from t3
)

我的问题是,考虑到这两个脚本都有一个名为 c1 的CTE,我可以并行运行这两个脚本吗?

谢谢 可以共享CTE名称的SQL脚本并行运行吗?

我尝试过并行运行这些脚本,但出现了奇怪的错误,所以我想确保不是两个脚本中共享的CTE名称导致了问题。

英文:

I have two sql scripts, and they both have CTEs. The CTEs have the same name in the two scripts. For example:

Script1:
with c1 as (
  select * from t1
),
c2 as (
  select * from t2
)

Script2:
with c1 as (
  select * from t3
)

My question is, given the fact that both scripts have a CTE named c1, can I run the scripts in parallel?

Thank you 可以共享CTE名称的SQL脚本并行运行吗?

I have tried running the scripts in parallel, but I'm getting weird errors, so I wanna make sure it's not the share CTE names across the two scripts that's causing the problem.

答案1

得分: 1

是的,您可以并行运行这些脚本。CTE将独立执行,因此不存在数据损坏或竞争条件的风险。但您应考虑为CTE使用不同的名称,以提高代码的可读性。

Script1:
使用c1_t1作为名称,选择t1中的所有内容。
使用c2_t2作为名称,选择t2中的所有内容。

Script2:
使用c1_t3作为名称,选择t3中的所有内容。

英文:

Yes, you can run the scripts in parallel. The CTEs will be executed independently, so there is no risk of data corruption or race conditions. Though you should consider using different names for the CTEs to make your code more readable.

Script1:
with c1_t1 as (
  select * from t1
),
c2_t2 as (
  select * from t2
) 

Script2:
with c1_t3 as (
  select * from t3
) 

huangapple
  • 本文由 发表于 2023年6月5日 08:13:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/76402914.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定