uniq(x) calculates the number of unique values. Use this function as a substitute for count(distinct x).
As you can see below, using uniq() rather than count(distinct x) results in some great performance upgrades. Try it for yourself👇
-- Took 40.65s
SELECT count(distinct tx_hash)
FROM blockchains.all_chains
WHERE signed_at > now() - interval '1 week'
-- Took 0.91s
SELECT uniq(tx_hash)
FROM blockchains.all_chains
WHERE signed_at > now() - interval '1 week'
View all other aggregate functions here