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

3

I'm doing some SQL queries in my Delphi program and I had this doubt, what is the cost to the database engine when a UPDATE does not find the record according to the parameters of WHERE ?

My question came from the need to make some IFs in the program to check beforehand if a certain number satisfies the UPDATE, I'll give an example:

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

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

Do not get me wrong, it's not too lazy to type, I have a certain neurosis in leaving the code as clean and clean as possible and depending on I would have to do a IF for each update.

What do colleagues have to say about this?

    
asked by anonymous 09.03.2018 / 22:18

1 answer

3

The cost of an UPDATE that does not affect any row is the same as a SELECT cost with the matching search.

Searching to see if records exist can make sense in certain cases, for example:

  • If you need to run more logic besides the update;
  • If you need to do a "before and after" data, you will have the amount of records to affect in the search result before;
  • As far as I remember the UPDATE returns the number of affected rows. If you upgrade from a library that encapsulates it, but does not bring that information, you'll need it in some other way.
09.03.2018 / 22:38