SQL performance - is it better to throw and raise an exception or does validation exist?

I consider optimization in a particularly heavy part of my code. The challenge is to insert statistics into the table. These data fall into a significant part of other programs. Otherwise, I would consider using SQL Bulk attachments, etc.

So my question is ...

Is it possible to try to insert some data, knowing that it can (not too often) throw a SqlException for a repeating row?

Is an exception performance hit worse than checking each row before inserting?

+3
source share
6 answers

-, - , . , , , , .

-, , , , . . , ​​ . (), DB .

+6

, . , , . , , , , SQL- .

, , . , .

- . , . , , .

+6

, IF

IF (SELECT COUNT (*) FROM X WHERE Y = Z) = 0 INSERT INTO (X) ('XX')....

ELSE...

+2

, , . , - ..

INSERT , ..

IF NOT EXISTS, INSERT, , , .

, . Identity/Auto-number, ( 100%), , ..

, , , ..

+2

, . , IP-.

, , .

0

.

, , , , , .

, , , .

, .

, , .

0

Source: https://habr.com/ru/post/1703263/


All Articles