I hope this is the appropriate place to ask this... I am a C# developer, and have inherited some interesting code. In once place, profiling points out a huge bottleneck and I have discovered this gem (pseudocode):

tableList = select name from table1, table2 where table1.ID=table2.ID
// tableList comes back with 62,000 rows

loop over tableList, chunks of 100 rows
foreach 100 rows
barcodeList = select distinct barcode from table3, table4 where table.NO=table4.NO and
t3.ID in (100 rows of name from tableList)
foreach barcode in barcodeList
if barcode not in finalList add to finalList

Hopefully this makes sense. The final result (finalList) only has about 50 records, but the process to get that list takes a looooong time. Is there a way to merge the two queries that will get me the same results and allow me to remove all this looping crud?

Thanks!