postgresql COUNT(DISTINCT …) very slow

I have a very simple SQL query: SELECT COUNT(DISTINCT x) FROM table; My table has about 1.5 million rows. This query is running pretty slowly; it takes about 7.5s, compared to SELECT COUNT(x) FROM table; which takes about 435ms. Is there any way to change my query to improve performance? I’ve tried grouping and doing … Read more

What’s faster, SELECT DISTINCT or GROUP BY in MySQL?

If I have a table CREATE TABLE users ( id int(10) unsigned NOT NULL auto_increment, name varchar(255) NOT NULL, profession varchar(255) NOT NULL, employer varchar(255) NOT NULL, PRIMARY KEY (id) ) and I want to get all unique values of profession field, what would be faster (or recommended): SELECT DISTINCT u.profession FROM users u or … Read more