I have a very massive PostgreSQL database table. One of its columns stores a boolean parameter. What I want is a very simple query:
SELECT COUNT(*) FROM myTable WHERE myTable.isSthTrue;
The problem I have is that it is very slow as it needs to check every row if it satisfies the criteria. Adding an index to this parameter speeds up the calculation roughly two times, but doesn't really improve the complexity in general.
Some people on the internet suggest adding triggers that update the count which is stored in a separate table. But that feels like too much effort for an easy problem like this.
explain (analyze, verbose)for the query with and without the index? And what is the current performance? And how fast do you need it to be? Also which version are you using?isSthTrue WHERE isSthTrue is true?seq scanwithout index andIndex only scanwith index, as expected. Performance is in hundreds of milis. So I don't want it to take dozens of seconds in real system to do such a simple query. I am using 9.3.sth version.