posted 24 years ago
Well, I know little about optimizing SQL, and nothing at all about postgresql in particular. But offhand: keep the number of different connections to the minimum you can get away with. Often you can just create a single connection once, and then keep re-using the same connection for different queries. The only reasons I've ever had for multiple connections are if you need to be daling with more than one query or resultset at a time, or more than one database. Dunno if you're creating multiple connections, but if you are it can eat up a lot of time.
Also, try using PreparedStatements rather than Statements if you're running the same type of query many times with different inputs. You save on the time the DB spends interpreting your SQL and figuring out how to optimize it. Dunno how much effect this has, but I'd think it would be noticeable at least.
Should you query for the whole table at once? Umm, I guess it depends how much of the table you need. (And is anyone else trying to access the table while you've got it locked, assuming you have some sort of locking?) I usually try to select all the rows I know I will need into one big ResultSet, and loop through it to process each row as necessary. If there are other statements to execute as well, that's when you need another connection, as your first connection is still holding the results if your earlier query. I've always assumed (but never really tested) that this is faster than selecting each row individually. But if the table is particularly huge then memory restrictions could prevent this, or other users needing to access the table may restrict you to one row at a time. And I'm sure there are other considerations I haven't encountered.
If you're already minimizing the number of connections and using PreparedStatements, you may want to post some code to show what exactly you're doing. I personally will be out of town shortly for a week or so, but I'm sure others will have suggestions. Good luck.
[This message has been edited by Jim Yingst (edited April 08, 2000).]
"I'm not back." - Bill Harding, Twister