I want to make a view for which i will be requiring a query having self join in it. I am worried about its performance and behavior as it is going to be fetch some handsome amount of data. Details of table and query are as under (original table has more columns and i will be requiring results from multiple columns; this is just to present you with an example):-
Person Table
PK NAME SPOUSE_REF_ID IS_MARRIED
1 John Null Y
2 Alice 1 Y
3 Varvatos Null Y
4 Barbara 5 Y
5 Stanely Null Y
6 Ross Null N
7 Henry Null N
I need to query data of those who are married and have spouse_ref_id with them. Thus the query i made is:-
select a.NAME spouse_name, a.SERVICE_NO, a.id,a.employment_category_id, pf.name name, pf.service_no,pf.id,
pf.employment_category_id
from person a
inner join person pf on
a.id = pf.spouse_ref_id
where a.IS_MARRIED ='Y'
Please advise me regarding its optimization and if you think that restructuring query will fetch better results or self join is not advisable in queries etc etc..
I would appreciate a reply having an optimized view.