Best practice when updating individual fields in a database record

I created a data access layer using .NET. Wherever I update the database record, I used sql along the lines

UPDATE Customer SET FirstName =: FirstName, LastName =: LastName, Address1 =: Address1, Address2 =: Address2, .... etc

This means that each field in the record is updated, although only one field can be changed. A colleague made a mistake about this, saying that we should update the field if it changed, referring to the bandwidth, because the problem is, for example, we had 160 fields, then we transfer data for 160 fields. I think I could save bandwidth between the web server and the database server if I checked to see if the value changed, and the generated sql was based solely on the values ​​that were actually changed.

Between the web server and the client, I now need to transfer both old and new values, so I potentially increase the throughput there (but then ASP.NET does this anyway, and I'm not sure if we can switch this bit so probably not a problem).

So what is best practice? Should I worry about updating all fields in a database record? How can I update only changed fields?

Edit added October 29: Does anyone know what NHibernate is doing? Perhaps this is an argument to invest time in learning how to do this.

+2
source share
7 answers

Premature optimization is the root of all evil.

Is there any reason for this, do you really have a bandwidth problem?

, ( ) , . , , , , .

- , , , "" . , .

+6

, , . , , .

, 1. A LastName "" "" . B 1 "" " " .

LastName "" ( , , B ).

, (, - ), , , .

+4

, - , 100 , . 8 . , , , , .

+2

. -, . , , ( ) , .

+1

, , ( , ), SQL , SQL SQL , ( SQL Injection, ). , , , , .

, , . SP - .

+1

PK ? ? ? ?

. , ( PK ).

+1

. ORM/DAL , 200 + .

, , , , .

, "" ORM, , . , , , SQL UPDATE, , .

N.B. SELECT , , "SELECT * FROM". , / , .

+1

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


All Articles