连接三个表时的多个键/索引/约束

发布于 2024-09-04 18:24:12 字数 715 浏览 2 评论 0原文

当我试图区分这些术语的歧义时,我变得越来越困惑。我有一个查询花费的时间比必要的时间长,只是因为我无法在表上获取用于其他连接的密钥。

我在 t1 中只有一列是“唯一”的,还有其他列是 73.8% 唯一的,我不知道如何将它们合并为键、约束、索引或我正在寻找的任何内容。

SELECT t1.*, 
    (SELECT
         t3.comments
     FROM dashboard_data.unit_comments As t3
     WHERE t1.sr=t3.sr) comments,
    (SELECT
         t4.priority
     FROM dashboard_data.units_to_ship As t4
     Where t1.rma=t4.rma) priority
FROM report_tables.idle_report As t1

所以,基本上,我想让 SR 和 RMA 成为它们自己的密钥,以便我可以在该查询中使用所有表的密钥,但在花了一天的时间搜索网络并阅读不同来源对这些术语的解释之后,我仍然无法弄清楚弄清楚我需要做什么来优化这个查询。

我很抱歉,因为我知道这个问题被问了很多,但即使在阅读了其他一些问题和答案之后,我似乎也无法将它们应用于我的具体案例。我什至可以使用钥匙吗?如果您需要更多信息,请告诉我。另外,我打算粘贴“解释选择”,但我不知道如何轻松格式化制表符分隔(excel)副本。我在完整的格式化参考中没有看到它。

I'm getting more and more confused as I try to distinguish from the ambiguities of these terms. I have a query that is taking longer than necessary simply because I cannot get the key on on table to work for the other joins.

I have only one column that is "Unique" in t1, there are others which are 73.8% unique and I cannot figure out how to incorporate them as Keys, constraints, indexes or whatever it is I'm looking for.

SELECT t1.*, 
    (SELECT
         t3.comments
     FROM dashboard_data.unit_comments As t3
     WHERE t1.sr=t3.sr) comments,
    (SELECT
         t4.priority
     FROM dashboard_data.units_to_ship As t4
     Where t1.rma=t4.rma) priority
FROM report_tables.idle_report As t1

So, basically, I want to make SR and RMA their own keys so that I can utilize keys for all tables in this query but after spending a day searching the web and reading different sources' interpretations of these terms, I still can't figure out what it is I need to do to optimize this query.

I apologize because I know this is asked a lot but even after reading some of the other questions and answers I can't seem to apply them to my specific case. Can I even use keys for this? Please let me know if you need more info. Also, I was going to paste in the EXPLAIN SELECT but I can't figure out how to easily format a tab delimited (excel) copy. I didn't see it in the full reference for formatting.

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

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

发布评论

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

评论(1

陈独秀 2024-09-11 18:24:13

好吧,所以我想我可能终于找到了答案。主键是唯一的,并且是唯一的,我可以有一个主键,但仍然有单独的索引。所以我将尝试使用 ALTER TABLE 为 RMA 和 SR 创建索引。

仍然不确定限制是什么,根据我收集的信息,我猜它只是键或索引的同义词。

这些是我的发现:

  • 索引或 PK 可以是单列或多列
  • 索引或 PK 可以是前缀
  • 只能有一个 PK
  • 可以有许多索引
  • PK 必须是唯一的
  • 索引可以是唯一的或非唯一的

我在上面的陈述中是否正确,我错过了什么(至少是基本信息)吗?

Okay, so I think I may have finally found the answer. Primary Keys are Unique and Only Unique, I can have a primary key and still have separate indeces. So I will try using ALTER TABLE to create indexes for RMA and SR.

Still not sure what constraints are, from what I've gathered, I guess it's just a synonym for key or index.

These are my findings:

  • Indeces OR PKs can be single or multiple columns
  • Indeces OR PKs can be prefixes
  • There can be ONLY ONE PK
  • There can be MANY indeces
  • PK MUST be unique
  • Indeces can be unique OR non-unique

Am I correct in my above statements, did I miss any (at least for the basic info)?

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