内连接与全并集

发布于 2025-01-07 14:15:35 字数 1277 浏览 1 评论 0原文

哪个版本的查询更快/最佳实践?(好奇心。)

更重要的是,它们是否等效?

执行这些查询在这个例子中完成同样的事情?

1)使用两个 OR 条件的 INNER JOIN:

SELECT
  DISTINCT (cat.id) as 'Cat ID:'
FROM
  cat
INNER JOIN cuteness_showdown ON
  (cat.id = cuteness_showdown.cat_1 OR cat.id = cuteness_showdown.cat_2);

2)分别查询每一列并 UNION ALL:

SELECT
  DISTINCT (table_1.id) as 'Cat ID:'
FROM
  (SELECT
    cuteness_showdown.cat_1 AS id
  FROM
    cuteness_showdown
  UNION ALL
  SELECT
    cuteness_showdown.cat_2 AS id
    FROM
    cuteness_showdown) AS table_1;

现在,如果我需要另一个列的列,哪个版本更快/最佳实践表?

1) 内部用两个OR条件JOIN(不变):

SELECT
  DISTINCT (cat.id) as 'Cat ID:',
  cat.name as 'Cat Name:'
FROM
  cat
INNER JOIN cuteness_showdown ON
  (cat.id = cuteness_showdown.cat_1 OR cat.id = cuteness_showdown.cat_2);

2)单独查询每一列并UNION ALL(需要INNER JOIN cat表):

SELECT
  DISTINCT (table_1.id) as 'Cat ID:'
  cat.name as 'Cat Name:'
FROM
  (SELECT
    cuteness_showdown.cat_1 AS id
  FROM
    cuteness_showdown
  UNION ALL
  SELECT
    cuteness_showdown.cat_2 AS id
  FROM
    cuteness_showdown) AS table_1
INNER JOIN cat on
  (table_1.id = cat.id);

Which version of the query is faster / best practice? (Curiousity thing.)

More importantly, are they equivalent?

Do these queries accomplish the same thing in this example?

1) INNER JOIN with two OR conditions:

SELECT
  DISTINCT (cat.id) as 'Cat ID:'
FROM
  cat
INNER JOIN cuteness_showdown ON
  (cat.id = cuteness_showdown.cat_1 OR cat.id = cuteness_showdown.cat_2);

2) Query each column separately and UNION ALL:

SELECT
  DISTINCT (table_1.id) as 'Cat ID:'
FROM
  (SELECT
    cuteness_showdown.cat_1 AS id
  FROM
    cuteness_showdown
  UNION ALL
  SELECT
    cuteness_showdown.cat_2 AS id
    FROM
    cuteness_showdown) AS table_1;

Now, which version is faster / best practice if I need a column from another table?

1) INNER JOIN with two OR conditions (no change):

SELECT
  DISTINCT (cat.id) as 'Cat ID:',
  cat.name as 'Cat Name:'
FROM
  cat
INNER JOIN cuteness_showdown ON
  (cat.id = cuteness_showdown.cat_1 OR cat.id = cuteness_showdown.cat_2);

2) Query each column separately and UNION ALL (needed to INNER JOIN cat table):

SELECT
  DISTINCT (table_1.id) as 'Cat ID:'
  cat.name as 'Cat Name:'
FROM
  (SELECT
    cuteness_showdown.cat_1 AS id
  FROM
    cuteness_showdown
  UNION ALL
  SELECT
    cuteness_showdown.cat_2 AS id
  FROM
    cuteness_showdown) AS table_1
INNER JOIN cat on
  (table_1.id = cat.id);

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

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

发布评论

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

评论(2

不知所踪 2025-01-14 14:15:35

要找出哪个更快,打开一个终端,编写一个脚本,每个脚本运行 1000 次,然后比较结果:)

至于它们是否等效,查询优化器通常会为多个 SQL 提供完全相同的执行计划做同样事情的查询,所以它们很可能是。我无法告诉您这些是否会得到这种处理,但您可以使用 EXPLAIN 自己查看执行计划并比较它们,假设您有一些数据。

如果执行计划确实相同,最佳实践是选择更具可读性的语句,以便其他维护代码的人可以轻松地做到这一点。或者,如果它们不相同,那么您必须决定一个难以阅读的语句是否值得额外的性能增益,这取决于您的项目中性能的重要程度。我认为,如果您有一个相对较小的数据库,不太可能扩展太多且响应时间低于 10 毫秒,那么性能不是问题,因此只需使其易于维护即可。

To find out which is faster, break out a terminal, write a script that runs each 1000 times and compare the results :)

As for whether they are equivalent, the query optimiser will very often come up with the exact same execution plan for several SQL queries that do the same thing, so they may well be. I can't tell you whether these ones will get that treatment, but you can use EXPLAIN to see the execution plans for yourself and compare them, assuming you have some data.

If the execution plans are indeed the same, best practice is about choosing the more readable statement so that anyone else who comes along to maintain the code can do so easily. Alternatively, if they are not the same, then you have to decide whether a harder-to read statement is worth the extra performance gain, which depends on how big a deal performance is in your project. I'd argue that if you have a relatively small DB which is unlikely to scale much and sub 10ms response times, then performance isn't an issue so just make it easy to maintain.

三生一梦 2025-01-14 14:15:35

还有另一种选择,

SELECT
  DISTINCT (cat.id) as 'Cat ID:'
FROM
  cat
INNER JOIN cuteness_showdown ON
  cat.id IN (cuteness_showdown.cat_1,cuteness_showdown.cat_2)

非常微小的差异,但有趣的是 IN() 似乎总是比 OR 更有效地针对键执行,将看看我是否可以以某种方式为某些固定数据模拟一些表。

There is another option,

SELECT
  DISTINCT (cat.id) as 'Cat ID:'
FROM
  cat
INNER JOIN cuteness_showdown ON
  cat.id IN (cuteness_showdown.cat_1,cuteness_showdown.cat_2)

Very minor difference, but anecdotally IN() has always seemed to execute more efficiently against keys than an OR, will see if I can mock up some tables for some firm data one way or the other.

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