postgresql - 查找 MAX(db timestamp) 查询

标签 postgresql

我在 postgres 中有一个带有多个索引的大表。它有关于 db_timestamp、id、用户名的索引。

我想找到特定用户名的最大时间戳。 问题是像

这样的简单查询
SELECT MAX(db_timestamp) FROM Foo WHERE username = 'foo'

因为表很大(我们说的是 450GB 的表,索引大小超过 30GB),所以要花很多时间。

他们有什么方法可以优化此查询或告诉 postgres 使用什么查询计划吗?

最佳答案

使用正确的排序顺序在用户名和 db_timestamp 上创建索引:

CREATE INDEX idx_foo ON foo (username ASC, db_timestamp DESC);

检查 EXPLAIN 以查看是否一切正常。

关于postgresql - 查找 MAX(db timestamp) 查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2970060/

相关文章:

javascript - 未处理的拒绝 SequelizeDatabaseError : column "foo_bar" of relation "bar" already exists

sql - 从子查询更新表执行非常慢

python - 在 django 中对列表的每个元素调用方法的快速策略是什么

postgresql - Heroku 上的 Sylius,获取 "phpcr_workspaces"不存在

postgresql - 在 postgres 中有很多序列有什么问题吗?

php - postgresql 帮助 php 循环

postgresql - 在对象数组上执行异步 pg-node 查询

python - StreamingHttpResponse : return database connection to pool/close it

ruby-on-rails - 有没有办法将 Rails 默认时间戳更改为 Y-m-d H :i:s (instead of Y-m-d H:i:s. u) 或者让 laravel 忽略 Y-m-d H :i:s. u 的小数部分?

postgresql - 在 Postgres 中删除一个被另一个引用的表