FROM 子句中表的排列/顺序对提高性能有什么影响吗?

发布于 2024-10-10 09:54:08 字数 130 浏览 1 评论 0原文

一般来说,FROM 子句中表的排列或顺序对提高查询性能有什么影响吗?我所说的排列是指最小的桌子和最大的桌子。

任何不同的经验/想法/意见/因素也将受到赞赏。

就我而言,我们使用 PostgreSQL v8.2.3。

In general, does arrangement or order of tables in the FROM clause make any difference in improving the performance of the query? By arrangement I mean smallest table and largest table.

Any different experience/ideas/opinions/factors are also appreciated.

In my case, we're using PostgreSQL v8.2.3.

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

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

发布评论

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

评论(4

半葬歌 2024-10-17 09:54:08

对于内部连接,它不应该有任何区别 - 优化器将生成以尽可能多的不同顺序执行连接的计划(最多from子句中的geqo_threshold表)。

外连接不是对称的,因此语句中的顺序很重要(尽管实际的执行顺序仍然由服务器决定)。

For inner joins, it should not make any difference- the optimiser will generate plans doing the joins in as many different orders as is possible (up to geqo_threshold tables in the from clause).

Outer joins are not symmetric, so there the ordering in the statement is significant (although the actual execution order is still decided by the server).

苯莒 2024-10-17 09:54:08

您是否使用类似“SELECT FROM table1,table2”之类的内容?
这些表是“隐式”交叉连接的,所以我认为这并不重要。

我要做的第一件事是使用 EXPLAIN 测试两个可能的查询,看看是否有任何区别?

Are you using something like "SELECT FROM table1,table2" ?
The tables are "implicitly" cross-joined, so I don't think it matters.

The first thing I would do is test two possible queries using EXPLAIN, and see if there is any difference?

偷得浮生 2024-10-17 09:54:08

(通用 SQL 建议,而不是 Postgre 特定的)

不应该有什么区别 - 优化器应该根据可用的信息构建一个计划,并且 SQL 通常被设计为优化器可以自由地重新排序连接、条件检查等,前提是结果相同。

特定查询可能会导致特定优化器(对于特定数据库、服务器状态、产品版本等)出现问题,但这种情况应该很少遇到。

(Generic SQL advice, rather than Postgre specific)

It shouldn't make a difference - the optimizer should be building a plan based on the information it has available to it, and SQL has generally been designed so that the optimizer is free to reorder joins, condition checks, etc, provided that the result is the same.

It may be that a particular query causes problems for a particular optimizer (for a certain DB, server state, version of product, etc), but it should be rare to encounter such a situation.

无远思近则忧 2024-10-17 09:54:08

如果你写成 FROM foo, bar, baz 那么不,这没有什么区别。如果您编写 FROM foo JOIN bar ... JOIN baz ... (内部联接),那么如果您少于 join_collapse_limit 元素;否则,连接顺序是固定的,您可以使用它来手动优化此类内容(但这很少需要)。对于外连接,连接顺序在编写时是固定的,因为这会影响结果。

If you write FROM foo, bar, baz then no, it doesn't make a difference. If you write FROM foo JOIN bar ... JOIN baz ... (inner joins), then it doesn't make a difference if you have less than join_collapse_limit elements; otherwise the join order is fixed, which you can use to manually optimize such things (but that is rarely needed). For outer joins, the join order is fixed as it is written, because that affects the results.

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