tags:

views:

47

answers:

1

I have a nice DataGridView showing what is basically some kind of log data to the user. The user can filter by district, region, settlement and street.
So far, the ComboBoxes are linked to each other, so at first only the district has values, and only after picking one, the region one is filled with the proper values, etc.

The new requirement is that all the list will only show values that actually exist in the log table. Meaning that if district 1 has 10 different regions, but only 2 of those appear in the data, I would show only those 2 regions. Sounds reasonable enough, especially for settlements and streets, where the ratio between total and used codes is much higher.
I thought of two major ways to do this.
First, I can filter with join:

select r.code, r.name
from Region r
join LogTable l on (r.code = l.regionCode)
where r.districtCode = :districtCode;

Or I can do this with a subselect:

select r.code, r.name
from Region r
where r.code in (select distinct l.regionCode
                 from LogTable l)
      and r.districtCode = :districtCode

A third option would be to start up with reading all distinct values of district, region, settlement and street on the logTaable, and then use the subselect option with a pre calculated "in" clause. That seems a bit less flexible, as new values entered after the initial reading will not be visible.

Is there any major difference between the ways? Or would both work just as well?

P.S. I don't think there are any indexes set on the LogTable Code columns.

A: 

If Oracle has a query analyzer you should run it on your three options. It will tell you which is fastest. It should also tell you if you need to add an index to LogTable.

Robert Harvey
yeah, the sub select was just a little bit faster. So I chose it.
Noam Gal