I have rather simple query with big total runtime. Can you advise me how can I optimize it?
Here is explain analyze: http://explain.depesz.com/s/9xC5
query:
select wpis_id from spoleczniak_oznaczone
where etykieta_id in(
select tag_id
from spoleczniak_subskrypcje
where postac_id = 376476
);
spoleczniak_oznaczone:
Column | Type | Modifiers
-------------+---------+--------------------------------------------------------------------
id | integer | not null default nextval('spoleczniak_oznaczone_id_seq'::regclass)
etykieta_id | integer | not null
wpis_id | integer | not null
Indexes:
"spoleczniak_oznaczone_pkey" PRIMARY KEY, btree (id)
"spoleczniak_oznaczone_etykieta_id" btree (etykieta_id)
"spoleczniak_oznaczone_wpis_id" btree (wpis_id)
Foreign-key constraints:
"spoleczniak_oznaczone_etykieta_id_fkey" FOREIGN KEY (etykieta_id) REFERENCES spoleczniak_etykiety(id) DEFERRABLE INITIALLY DEFERRED
"spoleczniak_oznaczone_wpis_id_fkey" FOREIGN KEY (wpis_id) REFERENCES spoleczniak_tablica(id) DEFERRABLE INITIALLY DEFERRED
spoleczniak_subskrypcje:
Column | Type | Modifiers
-----------+---------+----------------------------------------------------------------------
id | integer | not null default nextval('spoleczniak_subskrypcje_id_seq'::regclass)
postac_id | integer | not null
tag_id | integer | not null
Indexes:
"spoleczniak_subskrypcje_pkey" PRIMARY KEY, btree (id)
"spoleczniak_subskrypcje_postac_id" btree (postac_id)
"spoleczniak_subskrypcje_postac_tag" btree (postac_id, tag_id)
"spoleczniak_subskrypcje_tag_id" btree (tag_id)
Foreign-key constraints:
"spoleczniak_subskrypcje_postac_id_fkey" FOREIGN KEY (postac_id) REFERENCES postac_postacie(id) DEFERRABLE INITIALLY DEFERRED
"spoleczniak_subskrypcje_tag_id_fkey" FOREIGN KEY (tag_id) REFERENCES spoleczniak_etykiety(id) DEFERRABLE INITIALLY DEFERRED
spoleczniak_subskrypcje_postac_tag. Also your row estimates are a bit off. So maybe it is a problem with statisticsvacuum analyze;on both tables.