How to count unique values?

Solution 1:

SELECT COUNT(DISTINCT ip_address) FROM bandits

Solution 2:

As mentioned here: https://stackoverflow.com/questions/11250253/postgresql-countdistinct-very-slow, it could be much faster to use a bit longer version instead:

SELECT count(*) FROM (SELECT DISTINCT ip_address FROM bandits) AS bandits_distinct