【问题标题】:PostgreSQL — Select column1 where MIN(column2)PostgreSQL — 选择 column1 where MIN(column2)
【发布时间】:2021-06-06 08:33:53
【问题描述】:

注意:出于性能原因,我想避免 DISTINCT ON。

注意 2:我错了。感谢@Gordon Linoff,使用正确的索引查询非常有效!

具有以下结构:

| id | image_url     | sort | t1_id |
|----|---------------|------|-------|
| 1  | https://.../1 | 10   | 1     |
| 2  | https://.../2 | 20   | 1     |
| 3  | https://.../3 | 30   | 1     |
| 4  | https://.../4 | 30   | 2     |
| 5  | https://.../5 | 20   | 2     |
| 6  | https://.../6 | 10   | 2     |

我想通过t1_id获取最低sort行的image_url,类似如下:

SELECT * FROM t2 WHERE MIN(sort) GROUP BY (t1_id);

得到以下结果:

| id | image_url     | sort | t1_id |
|----|---------------|------|-------|
| 1  | https://.../1 | 10   | 1     |
| 6  | https://.../6 | 10   | 2     |

提前致谢!

【问题讨论】:

  • 我很确定 DISTINCT ON 不会比任何其他技术慢(例如使用 ROW_NUMBER 或 sbquery)。因此,您的问题更为普遍,您可能只想为您的 DBMS 提供适当的索引。

标签: sql postgresql postgresql-13


【解决方案1】:

Postgres 有一个方便的扩展名为distinct on

select distinct on (t1_id) t2.*
from t2
order by t1_id, sort asc;

这通常是解决此类问题的最快方法。特别是,这可以利用(t1_id, sort [desc]) 上的索引。

但是,您可以尝试其他方法,例如:

select t2.*
from t2
where t2.sort = (select min(tt2.sort)
                 from t2 tt2
                 where tt2.t1_id = t2.t1_id
                );

这将使用相同的索引。如果这样更快,请发表评论并附上相关性能。

【讨论】:

  • 正如我在第一行所指出的,我的查询中的毫秒从 200 毫秒变为 700 毫秒,并带有不同的开启。
  • @Zeswen。 . .查询必须做更多的工作,因此需要更多时间也就不足为奇了。 distinct on 通常是最快的方法——尤其是使用正确的索引。
  • 我没有注意到您在(t1_id, sort [asc]) 中提到的索引。它确实在性能方面发挥了作用,非常感谢!通过索引中的asc 学到了一些新东西:)
猜你喜欢
  • 2015-05-05
  • 2015-01-10
  • 1970-01-01
  • 2018-02-20
  • 2021-01-30
  • 1970-01-01
  • 1970-01-01
  • 2016-01-13
  • 1970-01-01
相关资源
最近更新 更多