Visit Jeremy's Blog.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 01-06-2008, 02:03 AM   #1
Registered: Feb 2004
Location: Athens, Greece
Distribution: Gentoo,FreeBSD, Debian
Posts: 705

Rep: Reputation: 30
java: SQL duplicate key exception or query for key is faster

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)?

Old 01-06-2008, 04:32 AM   #2
harry edwards
Registered: Nov 2007
Location: Lincolnshire, UK
Distribution: CentOS, Fedora, and Suse
Posts: 365

Rep: Reputation: 48
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.

Last edited by harry edwards; 01-06-2008 at 04:35 AM.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Smart Key Signature ERRORS! How do I delete this bad key from my computer? Balarabay1 Linux - Software 4 09-27-2006 11:01 AM
Mysql Error: Duplicate entry '0' for key 1 w0lv3r1ne Linux - Server 1 08-17-2006 03:30 AM
SQL - getting the primary key autogenerated of an INSERT statement vharishankar Programming 3 12-05-2005 02:47 AM
duplicate gpg key issue Blackrose1 Linux - General 2 03-22-2004 09:33 AM
RSA public key encryption/private key decription koningshoed Linux - Security 1 08-08-2002 07:25 AM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:03 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration