My table DDL is:
CREATE TABLE accounts (
id serial NOT NULL,
active bool NULL DEFAULT true,
created_at timestamp NULL,
organization_id NULL,
CONSTRAINT accounts_pkey PRIMARY KEY (id)
);
CREATE INDEX index_accounts_on_active ON accounts USING btree (active);
CREATE INDEX index_accounts_on_created_at ON accounts USING btree (created_at);
CREATE INDEX index_accounts_on_organization_id ON accounts USING btree (organization_id);
My table has 200k records and my query is:
select count(*) from account where active = false and organization_id in (3,2,20,30,99,69) and created_at >= '2016-09-03 15:29:54.541924';
Explain analyze return this query plan:
Finalize Aggregate (cost=5791.37..5791.38 rows=1 width=8) (actual time=36.504..36.504 rows=1 loops=1)
-> Gather (cost=5791.26..5791.37 rows=1 width=8) (actual time=36.411..38.788 rows=2 loops=1)
Workers Planned: 1
Workers Launched: 1
-> Partial Aggregate (cost=4791.26..4791.27 rows=1 width=8) (actual time=31.313..31.313 rows=1 loops=2)
-> Parallel Seq Scan on accounts (cost=0.00..4625.94 rows=66126 width=0) (actual time=0.073..26.518 rows=56057 loops=2)
Filter: ((NOT active) AND (created_at >= '2016-09-03 15:29:54.541924'::timestamp without time zone) AND (organization_id = ANY ('{3,2,20,30,99,69}'::integer[])))
Rows Removed by Filter: 43943
Planning Time: 0.293 ms
Execution Time: 38.863 ms
Thanks for any suggestions.