mercredi 19 août 2015

Insert unique entities to the sqlite DB

There will be up to 100k entities in sqlite DB with following structure:

  • ID (Numeric, PK)
  • KEY (Varchar, Unique/PK)
  • Other fields, mostly varchars

I have a list of about 100-1k entities. I want to add to the DB only those entities, which KEY is not present in the DB and show the list of added ones.

Example

As an relevant example you may consider something like this: library with books as entities. Each Book has global unique ISBN number (KEY) and unique id (ID) in the library catalog.
Some person brings to the library set of books. Library checks the books by ISBN in the catalog, takes 'new' books and shows to the person list of taken books.

Some thoughts how it can be achieved:

1) select all KEYs from the DB, put them into [hash]set, in loop verify that KEY from new entities does not exist in the set.
2) like #1 but instead of selecting all KEYs, select only KEYs that present both in DB and the list
3) in loop check existence of entity with additional select query
4) enable constraints in the DB, check existence by catching exceptions

All of them have their own disadvantages, I believe. Can you suggest something better?

For now I'm asking mostly about 'best practices', I believe any of the approach will work for my case without huge performance issues (no actual tests for now, I'm just in analysis phase), but how should it be done better?

Code will be in Java, I plan to use simple DAOs with JDBC, but if someone suggests Hibernate as an alternative approach, I will reconsider my thoughts.

Aucun commentaire:

Enregistrer un commentaire