本文介绍了查找MAX(db_timestamp)查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在postgres中有多个索引的BIG表。它具有db_timestamp,ID,用户名的索引。

I have BIG table with multiple indexes in postgres. It has indexes on db_timestamp, id, username.

我想找到特定用户名的最大时间戳。
问题是简单的查询,例如

I want to find the MAX timestamp for particular username.The problem is the simple query like

SELECT MAX(db_timestamp) FROM Foo WHERE username = 'foo'

由于表大小巨大,所以花费了很多时间(我们正在谈论450GB表,索引大小超过30 GB) 。

takes so much time because of the huge table size( we are talking 450GB table with over 30 GB index sizes).

他们有什么方法可以优化此查询或向Postgres告知要使用的查询计划吗?

Is their any way to optimize this query or tell postgres about what query plan to use?

推荐答案

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

Use create an index on username and db_timestamp with correct sort order:

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

检查EXPLAIN看看事情是否按预期进行。

Check EXPLAIN to see if things work as they should.

这篇关于查找MAX(db_timestamp)查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-02 23:23