Use all delete all add instead of compare update

Recently, I encountered a demand. It is necessary to refresh the report statistics order data at intervals of every day, and then the data is calculated according to the day. Since the statistics are the daily order data of each merchant, there will be many items in a day. Merchant order data is counted into the report, so the storage logic should be designed like this. When the statistical data is entered into the report, first check whether the merchant’s data has been stored in the database. If not, perform data initialization. If the merchant’s data has been initialized that day Once it's in the warehouse, you can only update it.

If you follow the usual practice, you should first calculate the order statistics collection of all merchants, and then traverse this collection, search for whether the merchant has data in the database on the same day, if not, do the initialization data operation for this merchant, that is, new Increase operation, if there is, modify operation according to the searched primary key id. Just thinking about it here, I feel that a lot of things have been done in a loop, check the database in the loop, insert or modify data in the loop, and check the database in the loop. As long as it can be avoided, try to avoid it.

So after thinking about it, regardless of whether the merchant’s data has been initialized or not, there will only be one order statistics data for this merchant on the day of the day. The modification operation is just for value coverage, so you can use all delete and all add. In this way of thinking, delete all means delete in batches, and add all in batches, so there is no need to open any cycle to make judgments.

PS: Here is just to share an idea. The specific situation is still more reliable to code according to the business. Of course, this idea is best if it can be helpful.

Guess you like

Origin blog.csdn.net/weixin_38106322/article/details/111052406