0

Is there a way to optimize such a query in PostgreSQL 9.5, 9.6?

SELECT x FROM (
    SELECT x,count(x) cnt FROM very_big_table WHERE (conditions) GROUP BY x
) sub
WHERE cnt > 10

Indexes to all WHERE conditions and x is obvios. Any other suggestions?

1

2 Answers 2

2

Try using the HAVING() clause which is used exactly for this purposes (filters on aggregated columns) :

SELECT t.x,count(t.x) as cnt
FROM YourTable t
WHERE < >
GROUP BY t.x
HAVING count(t.x) > 10
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for your suggestion, but I do not see any performance change.
0

Well, there is no way to speed up this query but to work on WHERE expression

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.