|
|
Re: Swing Client Database Connection
|
Posted: Dec 6, 2005 1:16 AM
|
|
That much is clear.
You join all recordsets from every table before applying the "where" part. Some database servers optimize a statement like this in the compiler , but most database servers don't.
I assume you have the following structure.
table1.column1 table1.column2
table2.column2 table2.column3
table 3.column3 table3.column4
I don't know exactly the SQL dialect which you are using, but since they all are vers close, you shouldn't have a problem to apply my code to your db.
1. Since you join 3 tables you should start with a sub-query in which we only get the filter table3
select distinct column2 from table2 join (select distinct column3 from table3 where column4 like ? ) using (column3)
could be that you must give a name to the sub-query, that would be something like
select distinct column2 from table2 join (select distinct column3 from table3 where column4 like ? ) as temp_table using (column3)
2. If you don't neet anyting else from table 2 you can use the "in" syntax, you don't even have to join.
select column1, column2 from table1 where column2 in (select distinct column2 from table2 join (select distinct column3 from table3 where column4 like ? ) using (column3))
If not, use the standard inner join
select column1, column2 from table1 join (select distinct column2 from table2 join (select distinct column3 from table3 where column4 like ? ) using (column3)) using (column2)
Finally: The prepared statement is a nice thing to use, but in this case rather useless, since compiling the query does not take nearly as long as executing. It's up to you if you want to read it or not.
Which DB Server are you using, by the way?
|
|