Last week I heard an interesting discussions between a tester and a developer.
The tester was upset that he discovered the following flow on an update action of an item from database:
For each update, the server deletes the item from database and recreates the item. Each deleted item is marked only as deleted in database but is not physical removed, because we need a tracking mechanism. This is a very bad practice. We are talking about a small web-application that recreates each item when the user changes some fields on it.
Imagine the following scenario: The web application is an e-commerce solution and has around 10.000 items listed. Each week, we receive from each producer updates related to each product that we import from Excel files. Because of this in only 3 months, our database will have around 120.000 items, even if only 10.000 are active.
The developer said to the testers that was the most convenient way for him. But this solutions is farthest the best one.
So what we can do to solve these problems? First of all we should look what we should do when an item is updated. If we need some tracking capabilities, that we should create a separate table/tables that track the changes.
If you are a tester, you should never accept a response like “It was the most convenient”. The “tracking” should never be made in the same table. Also if this was the most easier way to update an item from the database that say no again.
The tester was upset that he discovered the following flow on an update action of an item from database:
- Client: request and get an item from server
- Client: change some data from the item
- Client: send an update command to the server
- Server: get the update command request
- Server: delete from database the given item
- Server: recreate the item as a new item with the updated data
For each update, the server deletes the item from database and recreates the item. Each deleted item is marked only as deleted in database but is not physical removed, because we need a tracking mechanism. This is a very bad practice. We are talking about a small web-application that recreates each item when the user changes some fields on it.
Imagine the following scenario: The web application is an e-commerce solution and has around 10.000 items listed. Each week, we receive from each producer updates related to each product that we import from Excel files. Because of this in only 3 months, our database will have around 120.000 items, even if only 10.000 are active.
The developer said to the testers that was the most convenient way for him. But this solutions is farthest the best one.
So what we can do to solve these problems? First of all we should look what we should do when an item is updated. If we need some tracking capabilities, that we should create a separate table/tables that track the changes.
If you are a tester, you should never accept a response like “It was the most convenient”. The “tracking” should never be made in the same table. Also if this was the most easier way to update an item from the database that say no again.
I keep hearing this a lot and I don't agree. It really depends on how the DB is handling it. Using, for example, a table partition upon the table in question, splitting it on two or more HDDs will allow 100 million+ records and still perform well.
ReplyDeleteBut why whould you like an update action to contain a delete and an add action - for general cases.
DeleteThere can be some custom cases when you have more versions of a product, but in this case you will not marker a product as deleted. You will add a new version of a product.
Depends very much on what you want to do when you want to update an item. But if you don’t have any tracking or versioning on the given item I don’t see way you delete the existing item and add a new one. Only because for the developer is more easily to implement in this way I don’t think is it enough.
Indeed, if we are not talking about some 'temporal database' or other case when reverting to some previous point in time is a common requirement, it doesn't make sense to replace all updates with a 'mark as deleted'+insert operations, even if from a performance point of view the performance won't be affected so much.
Delete