# \d credit Table "public.credit" Column | Type | Modifiers -----------+-----------------------------+----------------------------------------------------- id | integer | not null default nextval('credit_id_seq'::regclass) dated | timestamp without time zone | debit_id | integer | credit_id | integer | total | integer | note | character varying(255) | advert_id | integer | Indexes: "credit_pkey" PRIMARY KEY, btree (id) "credit_creditindex" btree (credit_id) "credit_datedindex" btree (dated) "credit_debitindex" btree (debit_id) Foreign-key constraints: "advert_id_exists" FOREIGN KEY (advert_id) REFERENCES advert(id) "credit_id_exists" FOREIGN KEY (credit_id) REFERENCES tg_user(id) "debit_id_exists" FOREIGN KEY (debit_id) REFERENCES tg_user(id) # explain SELECT credit.id, credit.dated, credit.debit_id, credit.credit_id, credit.total, credit.note, credit.advert_id FROM credit WHERE (((credit.credit_id) = (5624)) OR ((credit.debit_id) = (5624))) ORDER BY dated DESC LIMIT 30; QUERY PLAN -------------------------------------------------------------------------------------------------------- Limit (cost=0.00..1085.11 rows=30 width=50) -> Index Scan Backward using credit_datedindex on credit (cost=0.00..197996.92 rows=5474 width=50) Filter: ((credit_id = 5624) OR (debit_id = 5624)) # explain select credit.id, credit.dated, credit.debit_id, credit.credit_id, credit.total, credit.note, credit.advert_id FROM credit WHERE (((credit.credit_id) = (5624)) OR ((credit.debit_id) = (5624))) LIMIT 30; QUERY PLAN ------------------------------------------------------------------------------------------------- Limit (cost=36.17..128.40 rows=30 width=50) -> Bitmap Heap Scan on credit (cost=36.17..16868.45 rows=5475 width=50) Recheck Cond: ((credit_id = 5624) OR (debit_id = 5624)) -> BitmapOr (cost=36.17..36.17 rows=5476 width=0) -> Bitmap Index Scan on credit_creditindex (cost=0.00..21.02 rows=3149 width=0) Index Cond: (credit_id = 5624) -> Bitmap Index Scan on credit_debitindex (cost=0.00..15.14 rows=2327 width=0) Index Cond: (debit_id = 5624) (8 rows)