There is an application scenario where the user has two consecutive operations A and B, which must be performed after operation A is completed. If operation A is not completed, Operation B is triggered, it is displayed that the user needs to perform operation A first, that is, to query whether operation A has been executed in operation B. The problem raised here is to record user participation records and provide query methods for users and operations. When different data volumes exist, our storage solutions are very different. As data grows, our solutions continue to evolve.
1. The data volume is small, and user operations are fixed:
Storage: MySQL
Solution: We use UID as the key, one row per user, and each user including the user as the column storage, such as UID = 100, fixed storage as operation A and operation B, the table structure is roughly as follows:
Table_operation
Uid operation_a operation_ B
100 1 1
If you want to query whether A user participates in A or B, you can directly use SQL: SELECT * FROM table_operation WHERE uid = 100 AND action_a = 1 to achieve the goal.
Problem: user operations are fixed, and expansion is difficult. To increase user operation behaviors, you need to add fields or table storage. The method for adding fields is below a certain data magnitude (such as 1 million) it is feasible. If there is no relation between the behaviors, the performance of adding a table storage solution will be good.
2. The data volume is small and user operations are not fixed:
Compared with scenario 1, in the current scenario, except the uid variable, user operation variables are added, that is, we need to pay attention to the user and user operation variables.
Storage: MySQL
Solution 1: add an operation table, generate an operation id, and store uid and oid in the user operation behavior table. When a user executes a new operation, a record is inserted in the operation behavior table. The table structure is roughly as follows:
Table_operation_info
Oid name
1 operation_a
2 operation_ B
Table_operation
Uid oid
1 1
1 2
To query whether user 1 has performed operation A, use SQL: SELECT * FROM table_operation WHERE oid = 1 AND oid = 1.
Problem: when a user has a large number of operations, the user operation speed increases rapidly, and the data volume increases gradually. A single MySQL table may fail to load. The solution is described in subsequent scenarios.
3. Large data volumes and fixed user behavior
Storage: MySQL
Solution: Compared with scenario 1, the current scenario is different in that the data volume is larger than scenario 1, and the data volume is too large to meet the single-Table load of MySQL. This solution solves this problem. When a single table is too large, the most cost-effective method is to use table sharding. In our current scenario, the variable is uid, as long as the table is sharded horizontally Based on uid.
4. Large data volumes and unfixed user behavior
Storage: MySQL
Solution 1: This solution applies to situations where user operation behaviors can be classified, that is, two table sharding operations are added on the basis of scenario 1, Table sharding by Operation Behavior and table sharding by user. In the current solution, we need to deal with two variables: Operation Behavior and user. The two sub-tables correspond to the two variables respectively. The sub-tables are operated according to business rules, and the data volume is reduced horizontally by user ID.
Solution 2: This solution is completely horizontal table sharding. Based on scenario 2, it is split horizontally by user.
5. Large data volume
Storage: MySQL
Solution 1: database/table sharding. At this time, a database cannot meet the requirements. The rules are implemented based on the previous scenario and different databases can be placed on different machines according to actual needs.
Solution 2: store data by bit based on database/table sharding. Since an operation has been performed in a status, that is, the status 0, 1, or not, we can store data by a single bit, the 64-bit flag can store 64 operation behavior tags.
Other Storage
Key-value Database
Our requirements do not actually require too many relational database functions. A simple k-v database can implement our functions and improve the performance. After all, we have done little, yes.
Whether it is memory-based or non-memory (you can choose based on actual needs, or hotspot data in the memory, silence data in non-memory ), suppose we have enough storage space.
Solution 1:
Using uid + oid as the key, the value can be stored in the State, or you can only store whether or not to participate (0 and 1), but there will be too many keys, especially when the data volume is too large, number of UIDs * Number of oid, which may not be similar to the magnitude you have.
Solution 2:
Generally, the data volume of user operations is less than the user's level, and the data of user operations is controllable. To reduce the number of keys, we can use oid + User partition index id as the key. Here, the so-called User partition index refers to dividing users into one partition by a certain number, all users are recorded in this range. For example, if 10000 is used as an interval, the user with uid between 1 and 9999 is allocated to the range 0, here, you can store 1 and 0 whether the user has performed this operation. The value corresponding to a key is initialized to store 10000 0. If a user with uid = 100 performs an operation, set 100th zeros to 1.
Solution 3:
On the basis of solution 2, 10000 zeros are converted to 10000 01 bits. Assume that one single bit stores 50 bits, only 200 BITs are required in total.
Solution 4:
When the number of users is too large, most users may not participate in an operation. Then, based on solution 3, we add a simple sparse matrix compression to add an index for each storage space, it is stored only when the storage value is not 0.
Solution 5:
I did not expect it. I look forward to your sharing.
Summary
• As the data volume increases, the general idea is to split the table. When a table is not fixed, when a database is not engaged in database sharding, when a machine is not engaged in timed addition to the machine.
• For different storage media options, you need to consider the costs and needs. All options are balanced.
• Saves space and stores data by bit.
• Do not optimize it too early.