I have this Query in PostgreSQL:
SELECT cte.*
FROM (
select ......
from A a
left join B b using (id)
left join C c on (c.cid=a.cid)
left join D d on (d.did=c.did)
left join E e on (e.eid=d.eid)
left JOIN ( F f
JOIN ( SELECT func() AS funcid) x ON f.fid = x.fid) ON a.id = f.id
left join G g on (g.gid=c.gid)
left join H h on (h.hid=c.hid)
where b.userid= first_param
order by .....
) as cte
where cte.issuedate=second_param
This query runs with 2 parameters: first_param
is INTEGER
and second_param
is DATE
- Both of them are related to fields of table B
This query generates data to my main work screen so its going to be used many times.
My dilemma is because of the structure of the query since first_param
and second_param
are in "diffrent levels" of the query I don't know if I should create an index on both columns together or index of each columns separately? Also, the second_param
is accessed from the Common Table Expressions and not directly from B
In other words... choose between:
CREATE INDEX name_a
ON B
USING btree
(userid,issuedate);
OR:
CREATE INDEX name_aa
ON B
USING btree
(userid);
CREATE INDEX name_ab
ON B
USING btree
(issuedate);
The guidelines of Indexes says that if we use 2 columns together many time then we should index it together but in this case I'm not so sure...
Can you advise?