सबसे पहले, distinct . की कोई आवश्यकता नहीं है . क्वेरी को इस प्रकार लिखा जा सकता है:
select *
from example@sqldat.com
where column1 in (
select column2
from example@sqldat.com
where column3 > 0
)
order by column1
दूसरा, इसे लिखने के (कम से कम) दो और तरीके हैं। या तो JOIN . के साथ :
select t1.*
from example@sqldat.com t1
join example@sqldat.com t2
where t2.column2 = t1.column1
and t2.column3 > 0
group by
t1.id, t1.column1, ...
order by t1.column1
या (मेरी प्राथमिकता) EXISTS . के साथ :
select t1.*
from example@sqldat.com t1
where exists
( select *
from example@sqldat.com
where t2.column2 = t1.column1
and t2.column3 > 0
)
order by column1
किसी भी मामले में, आपको उन सभी के लिए निष्पादन योजनाओं की जांच करनी चाहिए।
यदि आपके पास table1.column1 . पर एक अनुक्रमणिका है, तो मैं प्रदर्शन के सर्वोत्तम होने की अपेक्षा करता हूं और table2 . के लिए , या तो column2 . पर एक इंडेक्स या (column3, column2) . पर एक कंपोजिट इंडेक्स