Primo, non c'è bisogno di distinct . La query può essere scritta come:
select *
from example@sqldat.com
where column1 in (
select column2
from example@sqldat.com
where column3 > 0
)
order by column1
In secondo luogo, ci sono (almeno) altri due modi per scriverlo. O con 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
o (mia preferenza) con 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
In ogni caso, dovresti controllare tutti i piani di esecuzione.
Mi aspetto che le prestazioni siano migliori se hai un indice su table1.column1 e per table2 , o un indice su column2 o un indice composito su (column3, column2)