Programmer through the trials - your business is variable it

Please do not tell me that with the ES or the other, in fact, many small and medium business of the company is the case, is based on mysql sqlserver carry out his or such business

Business scene

D sister did not know by way of illustration, we understand the situation yet. Cai Cai detail about here again. D sister program to record a log of orders for other services (such as statistics) use, here it is in a statistical business, OrderLog table design is as follows:

Column Name type of data description
OrderId nvarchar(100) Order number, primary key
UserId int Single-user id
Amount int Order Amount
Other field is omitted ...

In addition to a user information table UserInfo, designed as follows:

Column Name type of data description
UserId int User id, primary key
ProvinceId int Province of user id
cityId int City user id
CountyId int Id user districts

Complex orders related to the demolition of a single other operations, design tables may not be true, but does not affect Cai Cai thing to say

Variable business

Now if you want to count the total number of orders for a province, sql as follows:

select count(0) from OrderLog o inner join UserInfo u on o.UserId=u.UserId where ProvinceId=@ProvinceId
复制代码

Is there a problem, sql no problem, this time the provinces and districts A user information suddenly changed (perhaps in other areas to buy a house, a transfer of residence), that information UserInfo table has changed, and that using the above sql statistics user a previous order information provinces and districts will not be wrong? (Product dog say in what order under which the order belongs)

Positioning service

The above problems do you think is not very simple? Maybe just a little tinkering table is enough. However, this is not a business for the scene, but the design of all business scenarios Cai Cai say. Have you ever wondered why D sister design would be such a problem it?

A deep understanding of the business in order to avoid more similar mistake, must deeply understand the business point of constant and variable. Take D sister's statistics, the number of orders for your business is the area of ​​statistics, the definition of this business in the product design is invariant, that point in time that is the behavior would determine the nature of the business, the nature of the business with the other will not change and change. Specific to the current business is: X users in order to save the user does not change with changes in the area of ​​information, it plainly means that the user in order to generate X Province X will always belong to the province.

Speaking invariance nature of the business, there is a corresponding variability of business. If you worked on such business is similar to QQ space, it must have been in a similar visitor functions. When visitors to display records when the name of the visitors belong to the variability in the design of business in most cases. What does that mean? That a user to modify the name, it displays the user access to all records where the name will change at the same time.

Here, you look back at D sister business, where they involve a problem of system design, we all know, a good system design needs to change business point of abstraction extracted, D sister orders statistics point is that business changes user's provinces and counties will change the order of the amount of information, order number, etc. will not change. So you think it is not a sister of D data tables can be modified a little bit?

Improved data table

Improved User Information

According to the above described change point D sister cities that service area information of the user, the table abstract can be extracted from the user information, the primary key is no longer a user id

Column Name type of data description
Id int Primary Key Id, the primary key
UserId int User id
ProvinceId int Province of user id
cityId int City user id
CountyId int Id user districts

In this case customer orders log table becomes

Column Name type of data description
OrderId nvarchar(100) Order number, primary key
UserBId int Corresponding user table primary key id
Amount int Order Amount
Other field is omitted ...

Such design case, if the user's provinces and counties information are subject to change, the corresponding user information table there will be a number of provinces and districts user data

Here the user information table is not the object of the user master table, but derived from the service list according to the order

Improved service data table

According variability and invariance business, since the order area of ​​business statistics is defined as the constant nature of the business, and that the order of log table can be designed

Column Name type of data description
OrderId nvarchar(100) Order number, primary key
UserId int Single-user id
ProvinceId int Province of user id
cityId int City user id
CountyId int Id user districts
Amount int Order Amount
Other field is omitted ...

Written in the last

You read here, you may feel Cai Cai wrote this very sad fact, however, D sister's scene is a real problem encountered in the environment. In essence, it is the definition of non-denaturing and denaturing business operations and division, and architectural designs, database design actually need to change the business point of storage abstract problem, in fact, be said to be pulled out.

I hope you gain something - Cai Cai


Adding concern to see a more attractive version, harvest more exciting

Reproduced in: https: //juejin.im/post/5d039aece51d45108c59a530

Guess you like

Origin blog.csdn.net/weixin_34090562/article/details/93176243