I have a complex query containing a join, a union, a sub-query, some collection expressions and some predicates.
I am wondering what is the best way to go about creating an index for this query.
- Is it possible to create an index for such a complex query?
- Should I try to simplify the query into multiple queries and create the indexes on the individual queries?
- Should I create multiple indexes rather than 1 complex index?
The following is a sample of the query
select
user
, array_agg(group
.id) as groups from writegroup
join writeuser
on keysgroup
.userIds
wheregroup
.userIds is not missing
andgroup
.docType = “group”
anduser
.docType = “user”
anduser
.extension.TestUser is not missing
and (LOWER(user
.extension.TestUser.personalDetail.firstName) like ‘%andrew%’
or LOWER(user
.extension.TestUser.personalDetail.middleName) like ‘%andrew%’
or LOWER(user
.extension.TestUser.personalDetail.lastName) like ‘%andrew%’
or LOWER(user
.extension.TestUser.personalDetail.nickName ) like ‘%andrew%’)
group byuser
union
select as groups ,user
from writeuser
whereuser
.docType = “user”
anduser
.extension.TestUser is not missing
and every item in (select u from write g unnest g.userIds u where g.userIds is not missing) satisfiesuser
.id != item.u end
and (LOWER(user
.extension.TestUser.personalDetail.firstName) like ‘%andrew%’
or LOWER(user
.extension.TestUser.personalDetail.middleName) like ‘%andrew%’
or LOWER(user
.extension.TestUser.personalDetail.lastName) like ‘%andrew%’
or LOWER(user
.extension.TestUser.personalDetail.nickName ) like ‘%andrew%’)
If there are any questions about this topic, please let me know.
Any guidence or direction to relevant documentation would be greatly appreciated.
Regards,
Damian