What is the cost of an update when it does not find the record?

Asked

Viewed 54 times

3

I’m doing some Sqls in my Delphi program and I came up with this question, what is the cost to the bank engine when a UPDATE does not find the record according to the parameters of WHERE?

My question arose by the need to make some IFs in the program to check before if a certain number satisfies the UPDATE, I will give an example:

  if (param_pedido > 0){
     update tabela set campo = valor where pedido = param_pedido 
     update ...
     update ...
     update ...
     update ...
  }

If we evaluate the IF doesn’t make much sense unless the UPDATE is very costly for the database when it does not find the record in question.

Don’t get me wrong, it’s not lazy to type, I have a certain neurosis in letting the code as dry and clean as possible and depending I would have to do a IF for each update.

What colleagues have to say about this?

1 answer

3

The cost of a UPDATE that does not affect any line is the same cost of a SELECT with the corresponding search.

The search to find out if the records exist may make sense in certain cases, for example:

  • If you need to perform more logic than update;
  • If you need to do a "before and after" of the data, you will have the amount of records to affect in the result of the before search;
  • As far as I can remember UPDATE returns the number of affected rows. If you upgrade through a library that encapsulates you but doesn’t bring that information, you will need it otherwise.
  • Thanks for the reply Renan, my question is very simple even, I do not need to make use of the data after the update is only an update to remove a request number of a renegotiation, I know that there is a cost to the UPDATE as you put, the issue is to leave this cost to the bank or to the application, as it is a simple select and these results will not transit on the network because it is an UPDATE, I believe that the IF in this case is expendable, but I may be wrong so I raised the issue.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.