LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   java: SQL duplicate key exception or query for key is faster (https://www.linuxquestions.org/questions/programming-9/java-sql-duplicate-key-exception-or-query-for-key-is-faster-611466/)

kpachopoulos 01-06-2008 02:03 AM

java: SQL duplicate key exception or query for key is faster
 
Hi,
i have to do some (hundreds of) INSERT operations in a database table. However, many of the data that are to be inserted, have already been inserted. Would it be faster to (1) wait for an SQL duplicate key exception or (2) to check the existence of the key by using a PreparedStatement query?
Is (2) faster than (1)?

Thanks

harry edwards 01-06-2008 04:32 AM

My felling is that performing a Prepared select would be quicker than catching a duplicate insert error - as long as the table is indexed and the select uses the index. In addition this method is cleaner. I believe it will be quicker because Java will not have to create and handle the error for each duplicate: there must be a significant overhead with instantiating an exception object.

I support you could always perform a test on a smaller table to prove which is quicker. It would be interesting to see the timings of such a test.


All times are GMT -5. The time now is 10:16 PM.