Do I have to check if a key exists before inserting or handling an exception on insert?

Which operation is more expensive - issuing a request SELECTbefore INSERTor handling an exception during an operation INSERT?

For example, suppose I want to save some user query parameters, say param1(primary key and unique index) param2, param3in a database table with a request to display a notification if a duplicate key already exists. In addition, suppose this is a real-time scenario when the entire process is not expected to take more than 500 ms.

Why is it often recommended that you execute the insert statement for three parameters and then handle the insert exception to duplicate? I know that throwing exception has a cost - does the cost of a database query exceed the exception exception throw?

+4
source share
1 answer

If I insert 1,000,000 times and first check and then insert, that is, 1,000,000 checks and, say, 957 512 attachments for a total of 1 957 512 actions against the database. I still have to do something for the user's messaging for each of the 42,488 entries that cannot check.

If I throw and throw an exception, I have 1,000,000 actions against the database and 42,488 exceptions to handle.

, - onteh.

-1

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


All Articles