In my database schema I have an entity that is identified. The identifier can be reused and thus there is a one-to-many relation with the entity. Example: A person can have a nickname. Nicknames are not unique and can be shared amongst many people. So the schema might look like:
PERSON
id
name
nickname_id
NICKNAME
id
name
The issue is that when inserting a new person, I have to first query NICKNAME
to see if the nickname exists. If it doesn't then I have to create a row in NICKNAME
. When inserting many persons, this can be slow as each person insertion results in a query to NICKNAME
.
I could optimize large insertions by first querying Nickname for all the nicknames. JPA query language:
SELECT n FROM NICKNAME n WHERE name in ('Krusty', 'Doppy', 'Flash', etc)
And then create the new nicknames as necessary, followed by setting nickname_id on the persons.
This complicates the software a bit as it has to temporarily store nicknames in memory. Furthermore, some databases have a limit on the parameters of the IN
clause (SQL Server is 2100 or so) so I have perform multiple queries.
I'm curious how this issue is dealt with by others. More specifically, when a database is normalized and an entity has a relationship with another, inserting a new entity basically results in having to check the other entity. For large inserts this can be slow unless the operation is lifted into the code domain. Is there someway to auto insert the related table rows?
FYI I'm using Hibernate's implementation of JPA