I need to save batch of objects.
Slow performance (150 000 objects are saved in 15 minutes) is due to checks.
Check for each object – the aggregate of some fields should be unique.
So the questions are:
- will it help if I create additional indexed column with hash-value of those fields and check that column before detailed check?
- is there any other solution?
Assuming you are currently checking row-by-row, and that table locking during the update is not a problem, I guess you’re trying to do a special case of UPSERT here (what happens if there IS already a matching row? Do you abort the process, or skip the row, or something else?)
In SQL Server 2008, a
MERGEstatement is probably what you’re looking for.In SQL Server 2005 and earlier, you can use a
LEFT JOINto do something similar (where a key column in the joined table is null)UPDATE(d twice): based on your feedback, let’s assume that you are using SQL Server 2005 and want to add new rows based on a compound (multi-column) key, while avoiding (and possibly alerting on) existing/duplicate rows.
You have a table “Table1” that contains some data:
You have a temp table (or table variable, or loading/staging table, or something) that contains the data you want to load:
You do an inner join to get any “already existing” rows (and then raise an error, or do whatever you want with that list of problem entries):
And a left join to insert the records that don’t have an already-matching record in the final table:
Some notes about this approach:
BULK INSERT,bcp,SQLXML, SSIS/Import-Export wizard, etc for this)