Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does a unique column constraint affect the performance of INSERT operations in SQL Server

I am wondering if having a UNIQUE column constraint will slow down the performance of insert operations on a table.

like image 307
Cerezo Avatar asked Mar 04 '23 11:03

Cerezo


2 Answers

Yes, indexes will slow it down marginally (perhaps not even noticeably).

HOWEVER, do not forego proper database design to because you want it to be fast as possible. Indexes will slow down an insert a tiny amount; if this amount is unacceptable, your design is almost certainly wrong in the first place and you are attacking the issue from the wrong angle.

When in doubt, test. If you need to be able to insert 100,000 rows a minute. Test that scenario.

like image 146
UnhandledExcepSean Avatar answered Mar 17 '23 02:03

UnhandledExcepSean


I would say, yes. The difference between inserting into HEAP with and without such constraint will be visible since general rule applies: more indexes - slower inserts. Also unique index has to be checked if a row can be inserted or such value (or combination) already exists, so double work.

The slowdown will be more visible on bulk inserts of large amounts of rows. And vice versa on single spotted inserts the impact going to be smaller.

The other thing that unique constraints and indexes help query optimizer to build better SELECT plans...

like image 21
Alexander Volok Avatar answered Mar 17 '23 00:03

Alexander Volok