Data storage method and device and electronic equipment
By adding data date time and data status fields to the database table, recording the data change process, and updating the status of the old recorded data when the data changes, the contradiction between the data change process and the use of storage space in the database is solved, and the data change process is realized. Efficient data storage and processing.
Patent Information
- Application Number
- CN202411714683.7
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-11-27
- Publication Date
- 2025-02-18
AI Technical Summary
While retaining the data change process in the database, how to reduce the use of data storage space and improve the efficiency of data transmission, loading and processing.
Add two fields to the database table to the date time and data status to record the date time and data generated by the data and whether the data is valid. When the data changes, keep the old record data and add new record data to compare the differences in the basic information fields. If there is a difference, set the unchanged field of the old record data to empty and update its status.
It realizes that while retaining the process of data change, it reduces redundant historical data, thereby reducing the use of data storage space and improving data processing efficiency.
Smart Images

Figure CN119474118A_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the technical field of data storage, and more particularly to a data storage method, device and electronic equipment. Background Art
[0002] The data in the database is constantly updated as the business develops. The data change process contains important information, which is crucial in the application of system management, analysis and mining, artificial intelligence and other scenarios. For example, the annual reports and precise recommendations commonly seen in various applications are the results of summarizing and analyzing the historical data generated by data changes. Therefore, various applications and systems currently pursue the complete and traceable data change process, but the complete preservation of the data change process also means that the required storage space continues to increase, which affects the efficiency of data transmission, data loading, data processing and other operations.
[0003] In summary, how to provide a data storage method that can reduce the occupancy of data storage space while retaining the complete data change process has become a technical problem that technical personnel in this field urgently need to solve. Summary of the invention
[0004] In view of this, the present invention discloses a data storage method, device and electronic device, so as to reduce the occupation of data storage space while retaining the complete data change process.
[0005] A data storage method, comprising:
[0006] When the data in the database table changes, the old record data in the database table is retained and new record data is added, wherein two fields, data date and time and data status, are added to the database table. The data date and time is used to record the date and time when the data is generated, and the data status is used to record whether the data is valid;
[0007] Compare whether there is a difference between the values of the basic information fields in the new record data and the old record data, wherein the basic information fields are fields other than the object unique id, the data date and time, and the data status;
[0008] If there is a difference, the values corresponding to the basic information fields of the old record data that have not changed relative to the new record data are set to empty, the data status of the old record data record is set to invalid data record, and the data status of the new record data record is set to valid data record, and the data date and time of the new record data record is set to the current date and time.
[0009] Optionally, if there is a difference, the values corresponding to the basic information fields of the old record data that have not changed relative to the new record data are all set to empty, the data status of the old record data record is set to invalid data record, and the data status of the new record data record is set to valid data record, and the data date and time of the new record data record is set to the current date and time, including:
[0010] Based on whether there is a difference between the values of the basic information fields in the new record data and the old record data, a set of fields with changed data values and a set of fields with unchanged data values are obtained;
[0011] Determine whether the data value change field set is empty;
[0012] If the data value changed field set is not empty, continue to determine whether the data value unchanged field set is empty;
[0013] If the data value unchanged field set is not empty, the values of the corresponding data value unchanged field set fields of the old record data record in the database table are all set to empty;
[0014] The data status of the old record data record in the database table is set to invalid data record, the data status of the new record data record is set to valid data record, and the data date and time of the new record data record is set to the current date and time.
[0015] Optionally, it also includes:
[0016] If the field set with unchanged data values is empty, determining that all fields in the new record data have changed;
[0017] The data status of the old record data record in the database table is set to invalid data record, the data status of the new record data record is set to valid data record, and the data date and time of the new record data record is set to the current date and time.
[0018] Optionally, it also includes:
[0019] When receiving a data adding instruction, a new record data is added to the database table, wherein the new record data includes: object unique id, basic information field, data date and time, and data status fields;
[0020] Set the data date and time recorded in the newly added record data to the current date and time;
[0021] Setting the data status recorded in the newly added record data to data record valid;
[0022] When the data date and time and data status recorded in the newly added record data are set, the newly added record data is inserted into the database table.
[0023] Optionally, it also includes:
[0024] When receiving a data deletion instruction, according to the first target object unique ID carried in the data deletion instruction, searching from the data table for target record data whose object unique ID is the first target object unique ID and whose data status is that the data record is valid;
[0025] The data status recorded in the target record data is set to data record invalid.
[0026] Optionally, it also includes:
[0027] When receiving the effective data query instruction, searching the database table for an object unique ID that is the same as the second target object unique ID according to the second target object unique ID carried in the effective data query instruction;
[0028] If the query result is empty, it is determined that the record data corresponding to the unique ID of the second target object does not exist;
[0029] If the query result is not empty, and the data status of the record data corresponding to the second target object unique id in the query result is that the data record is valid, it is determined that the record data corresponding to the second target object unique id exists and is in a valid state;
[0030] If the query result is not empty, and the data status of the record data corresponding to the second target object unique id in the query result is that the data record is invalid, it is determined that the record data corresponding to the second target object unique id has been deleted.
[0031] Optionally, it also includes:
[0032] When a historical data query instruction is received, all record data sets are obtained from the database table according to the third target object unique ID and the preset time period carried in the historical data query instruction;
[0033] Arrange each record data in the record data set in ascending order according to the data date and time to obtain a record data set to be processed, wherein the record data set to be processed has m rows of record data, each row of record data has n fields, and m and n are both positive integers;
[0034] Fill and restore the empty historical data in the to-be-processed record data set to obtain a target data set.
[0035] Optionally, the filling and recovery processing of the historical data that is blank in the record data set to be processed to obtain the target data set includes:
[0036] Starting from the first row of record data in the to-be-processed record data set, determining in sequence according to a preset order whether the value of each field in the first row of record data is empty;
[0037] For the empty target field, a filling value is determined from the corresponding same field that is not empty and filled until all the empty historical data in the m rows of record data in the to-be-processed record data set are filled to obtain the target data set.
[0038] A data storage device, comprising:
[0039] A new record data adding unit is used to retain the old record data in the database table and add new record data when the data in the database table changes, wherein two fields, data date and time and data status, are added to the database table, the data date and time is used to record the date and time when the data is generated, and the data status is used to record whether the data is valid;
[0040] A difference comparison unit, used to compare whether there is a difference between the values of the basic information fields in the new record data and the old record data, wherein the basic information fields are fields other than the three fields of the object unique id, the data date and time, and the data status;
[0041] A field adjustment unit is used to set the values corresponding to the basic information fields of the old record data that have not changed relative to the new record data to empty if there are differences, set the data status of the old record data record to invalid data record, set the data status of the new record data record to valid data record, and set the data date and time of the new record data record to the current date and time.
[0042] An electronic device, comprising: a memory and a processor;
[0043] The memory is used to store at least one instruction;
[0044] The processor is used to execute the at least one instruction to implement the data storage method described above.
[0045] From the above technical solution, it can be known that the present invention discloses a data storage method, device and electronic device, which adds two fields, data date and data status, in a database table. The data date and time are used to record the date and time of data generation, and the data status is used to record whether the data is valid. When the data in the database table changes, the old record data in the database table is retained and the new record data is added. Whether there is a difference between the values of the basic information fields in the new record data and the old record data, if there is a difference, the values corresponding to the basic information fields of the old record data that have not changed relative to the new record data are all set to null, the data status of the old record data record is set to invalid data record, and the data status of the new record data record is set to valid data record, and the data date and time of the new record data record is set to the current date and time. The present invention describes the data change process and data status of the new record data relative to the old record data by adding two fields, data generation date and time, and by setting the values corresponding to the basic information fields of the old record data that have not changed relative to the new record data to null, redundant historical data is reduced, thereby reducing the occupation of data storage space. BRIEF DESCRIPTION OF THE DRAWINGS
[0046] In order to more clearly illustrate the embodiments of the present invention or the technical solutions in the prior art, the drawings required for use in the embodiments or the description of the prior art will be briefly introduced below. Obviously, the drawings described below are only embodiments of the present invention. For ordinary technicians in this field, other drawings can be obtained based on the disclosed drawings without paying creative work.
[0047] Figure 1 A flow chart of a data storage method disclosed in an embodiment of the present invention;
[0048] Figure 2 A flowchart of a historical data query for a certain day disclosed in an embodiment of the present invention;
[0049] Figure 3 A schematic diagram of the structure of a data storage device disclosed in an embodiment of the present invention;
[0050] Figure 4 The present invention is a schematic diagram of the structure of an electronic device disclosed in an embodiment of the present invention. DETAILED DESCRIPTION
[0051] The following will be combined with the drawings in the embodiments of the present invention to clearly and completely describe the technical solutions in the embodiments of the present invention. Obviously, the described embodiments are only part of the embodiments of the present invention, not all of the embodiments. Based on the embodiments of the present invention, all other embodiments obtained by ordinary technicians in this field without creative work are within the scope of protection of the present invention.
[0052] The embodiments of the present invention disclose a data storage method, device and electronic device. By adding two fields, data generation date and time and data status, in a database table, the data change process and data status of new record data relative to old record data are described. By setting the values corresponding to the basic information fields of the old record data that have not changed relative to the new record data to empty, redundant historical data is reduced, thereby reducing the occupation of data storage space.
[0053] See also Figure 1 , a flow chart of a data storage method disclosed in an embodiment of the present invention, the method comprising:
[0054] Step S101: When data in a database table changes, old record data in the database table is retained, and new record data is added.
[0055] Compared with the traditional database table, the present application adds two fields, data date and time data_time and data status data_status, to the database table.
[0056] Among them, the data date and time is used to record the date and time when the data is generated, and the data status is used to record whether the data is valid.
[0057] Both the old record data and the new record data contain the contents of the two fields: data date and time and data status.
[0058] Step S102: Compare the values of the basic information fields in the new record data and the old record data to see whether there is any difference.
[0059] The basic information fields are fields other than the object unique ID, the data date and time, and the data status.
[0060] Assume that the old record data is R1 and the new record data is R2. Compare the values of the basic information fields except the object unique id, data_time, and data_status fields in R1 and R2 to see if there are any differences.
[0061] Step S103, if there is a difference, set the values corresponding to the basic information fields of the old record data that have not changed relative to the new record data to empty, set the data status of the old record data record to invalid data record, set the data status of the new record data record to valid data record, and set the data date and time of the new record data record to the current date and time.
[0062] In the present application, when there is a difference between the values of the basic information fields of R1 and R2, it is determined that R2 has changed relative to R1. At this time, the values corresponding to the basic information fields in R1 that have not changed relative to R2 are all set to empty to reduce redundant historical data and reduce the occupancy of data storage space; at the same time, the data status of the R2 record is set to the data record valid, the data status of the R1 record is set to the data record invalid, and the data date and time of the R2 record is set to the current date and time, so as to reduce the data storage space while retaining the complete data change process.
[0063] In practical applications, when the data status is set to data record valid, the value of the data status data_status can be set to 1; conversely, when the data status data_status is set to data record invalid, the value of the data status data_status can be set to 0. That is, the value of data_status recorded in R2 is set to 1, and the value of data_status recorded in R1 is set to 0.
[0064] For the data_status field, other identifiers other than 0 or 1 may be used to indicate whether the field is valid, such as y / n, + / -, etc.
[0065] In summary, the present invention discloses a data storage method, which adds two fields, data date and time and data status, in a database table. The data date and time is used to record the date and time of data generation, and the data status is used to record whether the data is valid. When the data in the database table changes, the old record data in the database table is retained and the new record data is added. The values of the basic information fields in the new record data and the old record data are compared to see whether there is a difference. If there is a difference, the values corresponding to the basic information fields of the old record data that have not changed relative to the new record data are all set to null, the data status of the old record data record is set to invalid data record, and the data status of the new record data record is set to valid data record, and the data date and time of the new record data record is set to the current date and time. The present invention describes the data change process and data status of the new record data relative to the old record data by adding two fields, data generation date and time, and data status in the database table. By setting the values corresponding to the basic information fields of the old record data that have not changed relative to the new record data to null, redundant historical data is reduced, thereby reducing the occupation of data storage space.
[0066] In one embodiment, step S103 may specifically include:
[0067] (1) Based on whether there is a difference between the values of the basic information fields in the new record data and the old record data, a set of fields whose data values have changed and a set of fields whose data values have not changed are obtained;
[0068] Specifically, assuming that the old record data is R1 and the new record data is R2, compare the values of the basic information fields in R1 and R2 except for the object unique id, data_time and data_status fields to see if there are any differences, and obtain the field set F where the data values have changed c , and the field set F whose data value has not changed nc .
[0069] (2) Determine whether the set of fields whose data values have changed is empty;
[0070] Judgment set F c Is it empty? If the set F c It is empty, indicating that the data in R1 and R2 are consistent and the data has not changed. No update is performed at this time and the program ends.
[0071] If the set F c If it is not empty, it indicates that the field value has changed. At this time, proceed to the next step.
[0072] (3) If the data value changed field set is not empty, continue to determine whether the data value unchanged field set is empty;
[0073] If the set F c Not empty, continue to judge set F nc Whether it is empty.
[0074] (4) If the data value unchanged field set is not empty, the values of the fields in the data value unchanged field set corresponding to the old record data record in the database table are all set to empty;
[0075] If the set F nc Not empty, the R1 record in the database table corresponds to the set F nc The values of the fields are set to empty to reduce redundant historical data and thus reduce the space occupied by data storage.
[0076] (5) The data status of the old record data record in the database table is set to invalid data record, the data status of the new record data record is set to valid data record, and the data date and time of the new record data record is set to the current date and time.
[0077] The data_status value of the R1 record in the database table is set to 0, the data_status value of the R2 record is set to 1, and the data_time value of the R2 record is set to T2, where T2 represents the current date and time.
[0078] In actual applications, once the data_status value and data_time value of the R2 record are set, R2 can be queried in the database table and the program ends.
[0079] In one embodiment, step S103 may further include:
[0080] If the field set with unchanged data values is empty, determining that all fields in the new record data have changed;
[0081] The data status of the old record data record in the database table is set to invalid data record, the data status of the new record data record is set to valid data record, and the data date and time of the new record data record is set to the current date and time.
[0082] Specifically, if the set F nc is empty, confirm that all fields in R2 have changed, at this time directly set the data_status value of R1 record in the database table to 0, set the data_status value of R2 record to 1, and set the data_time value of R2 record to T2, T2 represents the current date and time.
[0083] When the data of this application changes (i.e. is modified), the new record data and the old record data are compared based on the user's unique ID. For the fields that have changed, the field values of the old record data that have not changed are set to blank, which can reduce the space storage occupied by duplicate data (i.e. redundant historical data).
[0084] As shown in Table 1, the values of the name, gender, date of birth, registration date, place of registration, registration certificate, certificate number, and current city of residence fields in the old record data have not changed relative to the new record data. Therefore, there is no need to repeatedly store the same data in the old record data, only the changed field values need to be stored.
[0085] Table 1 Data modification example 1
[0086]
[0087] The present invention saves storage space by only recording the changed value information, and has a significant space saving effect for scenarios with frequent modifications and fewer changed fields. The present invention only clears the unchanged historical data to reduce storage space, and saves all field values of the latest effective record data, so it does not affect the direct query and use of the effective data.
[0088] At the same time, compared with the traditional data zipper algorithm, the present invention only uses one data date and time to record the start time and end time of the data. After all the data of the same object unique ID are queried, they are sorted in ascending order by data_time. The data date and time of a historical record in the result set is also the data end date and time of the previous record. For example, the data date and time recorded in the second row of Table 2 is also the end date and time of the first row of records.
[0089] Table 2 Data modification example 2
[0090]
[0091] It should be noted that the above embodiment involves a data modification scenario. For a data addition scenario, the following method may be used:
[0092] In one embodiment, the data storage method may further include:
[0093] (1) When receiving a data adding instruction, adding a new record data to the database table, wherein the new record data includes: object unique id, basic information field, data date and time, and data status fields;
[0094] (2) Setting the data date and time recorded in the newly added record data to the current date and time;
[0095] (3) Setting the data status of the newly added record data to the data record being valid;
[0096] (4) When the data date, time and data status recorded in the newly added record data are set, the newly added record data is inserted into the database table.
[0097] Specifically, when a data adding instruction is received, a new record data R3 is added to the database table, and the current date and time is T3.
[0098] Set the value of the data_time field of R3 to T3 and the value of the data_status field of R3 to 1. After the data_time field and the data_status field are set, insert R3 into the database table.
[0099] In one embodiment, the present application also discloses a data deletion scenario, which is as follows:
[0100] The data storage method may also include:
[0101] When receiving a data deletion instruction, according to the first target object unique ID carried in the data deletion instruction, searching from the data table for target record data whose object unique ID is the first target object unique ID and whose data status is that the data record is valid;
[0102] The data status recorded in the target record data is set to data record invalid.
[0103] Assuming that the unique ID of the first target object carried in the data deletion instruction is represented by x, deleting x in the database table means deleting the effective data of the object unique ID = x, specifically:
[0104] Find the record data R with object unique id=x and data_status=1 from the database table n ;
[0105] The data R will be recorded n The value of data_status is set to 0, which means that all record data of the object unique id = x has no valid record and the record data has been deleted.
[0106] Table 3 Data deletion example
[0107]
[0108] As can be seen from Table 3, according to the unique id of the object, the data status value of the target record data in the database table whose data status is valid for the data record can be set to 0. The data status of all record data with id=x is 0, indicating that there is no effective record for the record data and the record data has been deleted.
[0109] In one embodiment, the present application also discloses a data query scenario, which is as follows:
[0110] (I) For the effective data of the query object unique id=x, the data storage method may also include:
[0111] (1) when receiving a valid data query instruction, searching the database table for an object unique ID that is the same as the second target object unique ID carried in the valid data query instruction;
[0112] (2) If the query result is empty, it is determined that the record data corresponding to the unique ID of the second target object does not exist;
[0113] (3) If the query result is not empty, and the data status of the record data corresponding to the second target object unique ID in the query result is that the data record is valid, it is determined that the record data corresponding to the second target object unique ID exists and is in a valid state;
[0114] (4) If the query result is not empty, and the data status of the record data corresponding to the second target object unique ID in the query result is that the data record is invalid, it is determined that the record data corresponding to the second target object unique ID has been deleted.
[0115] Specifically, set the object unique id=x. If the query result is empty, it means that the record data does not exist; if the query result is not empty, and data_status=1 exists in the record data corresponding to the query result, it means that the data exists and is in effect; if the query result is not empty, and data_status=1 does not exist in the record data corresponding to the query result, it means that the data has been deleted.
[0116] (II) For historical data with query object unique id=x and date>=T, the data storage method may also include:
[0117] (1) when receiving a historical data query instruction, obtaining all record data sets from the database table according to the third target object unique ID and the preset time period carried in the historical data query instruction;
[0118] (2) Arranging each record data in the record data set in ascending order according to the data date and time to obtain a record data set to be processed, wherein the record data set to be processed has m rows of record data, each row of record data has n fields, and m and n are both positive integers;
[0119] (3) Fill and restore the historical data that are empty in the record data set to be processed to obtain the target data set.
[0120] The process of filling and restoring the empty historical data in the to-be-processed record data set to obtain the target data set may specifically include:
[0121] 1) Starting from the first row of record data in the to-be-processed record data set, determine in sequence in a preset order whether the value of each field in the first row of record data is empty;
[0122] 2) For the empty target field, a filling value is determined from the corresponding same field that is not empty and filled until all the empty historical data in the m rows of record data in the to-be-processed record data set are filled, thereby obtaining the target data set.
[0123] For details, see the process of querying historical data with unique object id=x and date>=T. Figure 2 The flowchart of historical data query for a certain day is as follows:
[0124] S1: Set data_time = T and id = x, and query all record datasets with id = x after T days.
[0125] S2: Sort all records in ascending order of data_time to obtain the result set R. The result set R has a total of m rows and n fields. Set R (x,y) , where x represents the x-th row of the record, and y is the y-th field of the x-th row of the record.
[0126] R (x,y ), for example, x = 1, y = 1 represents the first field of the first row of the record, x <= m, y <= n, and initially x = y = 0.
[0127] S3: Let x = x + 1.
[0128] S4: Let y = y + 1.
[0129] S5: Judge whether the value of R (x,y) is not empty and y < n. If so, go back to step S4; otherwise, proceed to the next step.
[0130] S6: Judge whether y = n. If so, proceed to the next step; if not, execute step S8.
[0131] S7: Judge whether x = m. If so, execute step S14; if not, execute step S3.
[0132] S8: Let x1 = x.
[0133] S9: Judge whether x1 < m. If so, proceed to the next step; if not, execute step S4.
[0134] S10: Let x1 = x1 + 1.
[0135] S11: Judge whether R (x1,y) is not empty. If so, proceed to the next step; if not, execute step S13.
[0136] S12: Assign the value of R (x1,y) to R (x,y) , and go back to S4.
[0137] S13: Judge whether x1 = m. If so, execute S4; if not, execute S10.
[0138] S14: Obtain the set R with null values filled.
[0139] S15: Filter and query the target data according to data_time.
[0140] S16: The program ends.
[0141] Based on the above discussion, it can be seen that when querying the effective data in this application, directly filter the corresponding object unique id and data_status=1 to query the corresponding effective data.
[0142] When querying historical data in this application, since the unchanged historical data is set to empty when the data is modified, it is necessary to process the query results and fill in and restore the empty historical data. The process is shown in Table 4-1, Table 4-2, Table 4-3 and Table 4-4.
[0143] Table 4-1 Query historical data after 2024 / 01 / 02 (original records in the database table)
[0144]
[0145] Table 4-2 Query historical data after 2024 / 01 / 02 (the first row of data is filled and restored)
[0146]
[0147] When filling and restoring the first row of data, if a field is empty, query downward for the first non-empty value and assign the value to the corresponding field in the first row. As shown in Table 4-2, the name to ID number are filled with the record values of the last row, and the current city of residence is filled with the value of the second row.
[0148] Table 4-3 Query historical data after 2024 / 01 / 02 (the second row of data is filled and restored)
[0149]
[0150] When filling and restoring the second row of data, the principle is the same as the first row. However, if the field value of the record in this row is not empty, it means that the field value has changed, and there is no need to query and fill it downward.
[0151] Table 4-4 Query historical data after 2024 / 01 / 02 (after filling recovery)
[0152]
[0153] After all rows are filled, the historical data is completely restored and can be used directly. At this time, the data not only retains the change process information completely, but also retains all historical information in full.
[0154] Data filling and recovery are not performed in the database. Instead, the data in the database is queried and filled and restored in the application, so the data in the database is not affected.
[0155] In summary, the present invention can completely retain historical data and data change status, will not cause historical data loss, and can completely retain the data change process through the two fields of data date time and data status, and can also retain historical information and status for deleted records. The present invention can minimize the storage space occupation, and by setting the unchanged historical data to empty, the storage space occupation can be reduced to the greatest extent, and duplicate data storage can be reduced, which is convenient for data transmission and processing. The present invention only needs to add two fields (data date time, data status) to complete the record change process and state preservation, and at the same time, the additional storage space occupation required for the values of the two fields is minimal, and the data date time is shared by the upper and lower records, that is, the date time of this record is not only the start time of this record, but also the end date time of the previous record, which further reduces the storage and space occupation of duplicate information. The present invention does not affect the normal effective data query. The most common scenario for the application system is the effective data query, because the present invention only sets the historical duplicate data to empty, and all field values of the effective data record are not modified, and can be directly queried, which will not affect the efficiency of normal business queries.
[0156] Corresponding to the above method embodiment, the present invention also discloses a data storage device.
[0157] See also Figure 3 , a schematic diagram of a data storage device disclosed in the present invention, the device may include:
[0158] The new record data adding unit 201 is used to retain the old record data in the database table and add new record data when the data in the database table changes.
[0159] Compared with the traditional database table, the present application adds two fields, data date and time data_time and data status data_status, to the database table.
[0160] Among them, the data date and time is used to record the date and time when the data is generated, and the data status is used to record whether the data is valid.
[0161] Both the old record data and the new record data contain the contents of the two fields: data date and time and data status.
[0162] The difference comparison unit 202 is used to compare whether there is a difference in the value of the basic information field in the new record data and the old record data, wherein the basic information field is the field other than the object unique id, the data date and time, and the data status.
[0163] The basic information fields are fields other than the object unique ID, the data date and time, and the data status.
[0164] Assume that the old record data is R1 and the new record data is R2. Compare the values of the basic information fields except the object unique id, data_time, and data_status fields in R1 and R2 to see if there are any differences.
[0165] The field adjustment unit 203 is used to set the values corresponding to the basic information fields of the old record data that have not changed relative to the new record data to empty if there are differences, set the data status of the old record data record to invalid data record, set the data status of the new record data record to valid data record, and set the data date and time of the new record data record to the current date and time.
[0166] In the present application, when there is a difference between the values of the basic information fields of R1 and R2, it is determined that R2 has changed relative to R1. At this time, the values corresponding to the basic information fields in R1 that have not changed relative to R2 are all set to empty to reduce redundant historical data and reduce the occupancy of data storage space; at the same time, the data status of the R2 record is set to the data record valid, the data status of the R1 record is set to the data record invalid, and the data date and time of the R2 record is set to the current date and time, so as to reduce the data storage space while retaining the complete data change process.
[0167] In practical applications, when the data status is set to data record valid, the value of the data status data_status can be set to 1; conversely, when the data status data_status is set to data record invalid, the value of the data status data_status can be set to 0. That is, the value of data_status recorded in R2 is set to 1, and the value of data_status recorded in R1 is set to 0.
[0168] For the data_status field, other identifiers other than 0 or 1 may be used to indicate whether the field is valid, such as y / n, + / -, etc.
[0169] In summary, the present invention discloses a data storage device, which adds two fields, data date and time and data status, to a database table. The data date and time is used to record the date and time of data generation, and the data status is used to record whether the data is valid. When the data in the database table changes, the old record data in the database table is retained and new record data is added. The values of the basic information fields in the new record data and the old record data are compared to see whether there is a difference. If there is a difference, the values corresponding to the basic information fields of the old record data that have not changed relative to the new record data are all set to null, the data status of the old record data record is set to invalid data record, and the data status of the new record data record is set to valid data record, and the data date and time of the new record data record is set to the current date and time. The present invention describes the data change process and data status of the new record data relative to the old record data by adding two fields, data generation date and time, to the database table, and reduces redundant historical data by setting the values corresponding to the basic information fields of the old record data that have not changed relative to the new record data to null, thereby reducing the occupation of data storage space.
[0170] In one embodiment, the field adjustment unit 203 is specifically configured to:
[0171] Based on whether there is a difference between the values of the basic information fields in the new record data and the old record data, a set of fields with changed data values and a set of fields with unchanged data values are obtained;
[0172] Determine whether the data value change field set is empty;
[0173] If the data value changed field set is not empty, continue to determine whether the data value unchanged field set is empty;
[0174] If the data value unchanged field set is not empty, the values of the corresponding data value unchanged field set fields of the old record data record in the database table are all set to empty;
[0175] The data status of the old record data record in the database table is set to invalid data record, the data status of the new record data record is set to valid data record, and the data date and time of the new record data record is set to the current date and time.
[0176] In one embodiment, the field adjustment unit 203 is further configured to:
[0177] If the field set with unchanged data values is empty, determining that all fields in the new record data have changed;
[0178] The data status of the old record data record in the database table is set to invalid data record, the data status of the new record data record is set to valid data record, and the data date and time of the new record data record is set to the current date and time.
[0179] In one embodiment, the data storage device may further include:
[0180] A new adding unit, used for adding a new record data in the database table when receiving a data adding instruction, wherein the new record data includes: an object unique id, a basic information field, a data date and time, and a data status field;
[0181] A date setting unit, used to set the data date and time recorded in the newly added record data to the current date and time;
[0182] A data status setting unit, used to set the data status of the record in the newly added record data to be valid;
[0183] The data inserting unit is used to insert the newly added record data into the database table after the data date and time and data status recorded in the newly added record data are set.
[0184] In one embodiment, the data storage device may further include:
[0185] a search unit configured to, when receiving a data deletion instruction, search from the data table for target record data whose object unique ID is the first target object unique ID and whose data status is that the data record is valid, according to the first target object unique ID carried in the data deletion instruction;
[0186] The state changing unit is used to set the data state recorded in the target record data to invalid data record.
[0187] In one embodiment, the data storage device may further include:
[0188] A first query unit is used for, when receiving a valid data query instruction, searching from the database table whether there is an object unique ID that is the same as the second target object unique ID according to the second target object unique ID carried in the valid data query instruction;
[0189] A first determining unit, configured to determine that the record data corresponding to the unique ID of the second target object does not exist if the query result is empty;
[0190] A second determining unit is configured to determine that the record data corresponding to the second target object unique ID exists and is in a valid state if the query result is not empty and the data status of the record data corresponding to the second target object unique ID in the query result is that the data record is valid;
[0191] The third determining unit is used to determine that the record data corresponding to the second target object unique id has been deleted if the query result is not empty and the data status of the record data corresponding to the second target object unique id in the query result is that the data record is invalid.
[0192] In one embodiment, the data storage device may further include:
[0193] A second query unit, configured to, when receiving a historical data query instruction, obtain all record data sets from the database table according to the third target object unique ID and a preset time period carried in the historical data query instruction;
[0194] An arranging unit, used for arranging each record data in the record data set in ascending order according to the data date and time, to obtain a record data set to be processed, wherein the record data set to be processed has m rows of record data, each row of record data has n fields, and m and n are both positive integers;
[0195] The filling and recovery unit is used to perform filling and recovery processing on the historical data that is empty in the record data set to be processed to obtain a target data set.
[0196] In one embodiment, the filling recovery unit may be specifically used to:
[0197] Starting from the first row of record data in the to-be-processed record data set, determining in sequence according to a preset order whether the value of each field in the first row of record data is empty;
[0198] For the empty target field, a filling value is determined from the corresponding same field that is not empty and filled until all the empty historical data in the m rows of record data in the to-be-processed record data set are filled to obtain the target data set.
[0199] It should be noted that, for the specific working principles of each component in the device embodiment, please refer to the corresponding part of the method embodiment, which will not be repeated here.
[0200] Corresponding to the above embodiment, Figure 4 As shown, the present invention also provides an electronic device, which may include: a processor 1 and a memory 2;
[0201] The processor 1 and the memory 2 communicate with each other via a communication bus 3;
[0202] Processor 1, configured to execute at least one instruction;
[0203] Memory 2, used to store at least one instruction;
[0204] The processor 1 may be a central processing unit (CPU), or an application specific integrated circuit (ASIC), or one or more integrated circuits configured to implement the embodiments of the present invention.
[0205] The memory 2 may include a high-speed RAM memory, and may also include a non-volatile memory (non-volatile memory), such as at least one disk memory.
[0206] Among them, the processor executes at least one instruction to implement the steps corresponding to the data storage method embodiment.
[0207] Finally, it should be noted that, in this article, relational terms such as first and second, etc. are only used to distinguish one entity or operation from another entity or operation, and do not necessarily require or imply any such actual relationship or order between these entities or operations. Moreover, the terms "include", "comprise" or any other variants thereof are intended to cover non-exclusive inclusion, so that a process, method, article or device including a series of elements includes not only those elements, but also other elements not explicitly listed, or also includes elements inherent to such process, method, article or device. In the absence of further restrictions, the elements defined by the sentence "comprise a ..." do not exclude the presence of other identical elements in the process, method, article or device including the elements.
[0208] The various embodiments in this specification are described in a progressive manner, and each embodiment focuses on the differences from other embodiments. The same or similar parts between the various embodiments can be referenced to each other.
[0209] The above description of the disclosed embodiments enables one skilled in the art to implement or use the present invention. Various modifications to these embodiments will be apparent to one skilled in the art, and the general principles defined herein may be implemented in other embodiments without departing from the spirit or scope of the present invention. Therefore, the present invention will not be limited to the embodiments shown herein, but rather to the widest scope consistent with the principles and novel features disclosed herein.
Claims
1. A data storage method, characterized in that: include: When the data in the database table changes, the old record data in the database table is retained and new record data is added, wherein two fields, data date and time and data status, are added to the database table. The data date and time is used to record the date and time when the data is generated, and the data status is used to record whether the data is valid; Compare whether there is a difference between the values of the basic information fields in the new record data and the old record data, wherein the basic information fields are fields other than the object unique id, the data date and time, and the data status; If there is a difference, the values corresponding to the basic information fields of the old record data that have not changed relative to the new record data are set to empty, the data status of the old record data record is set to invalid data record, and the data status of the new record data record is set to valid data record, and the data date and time of the new record data record is set to the current date and time.
2. The data storage method according to claim 1, characterized in that: If there is a difference, the values corresponding to the basic information fields of the old record data that have not changed relative to the new record data are all set to empty, the data status of the old record data record is set to invalid data record, and the data status of the new record data record is set to valid data record, and the data date and time of the new record data record is set to the current date and time, including: Based on whether there is a difference between the values of the basic information fields in the new record data and the old record data, a set of fields with changed data values and a set of fields with unchanged data values are obtained; Determine whether the data value change field set is empty; If the data value changed field set is not empty, continue to determine whether the data value unchanged field set is empty; If the data value unchanged field set is not empty, the values of the corresponding data value unchanged field set fields of the old record data record in the database table are all set to empty; The data status of the old record data record in the database table is set to invalid data record, the data status of the new record data record is set to valid data record, and the data date and time of the new record data record is set to the current date and time.
3. The data storage method according to claim 2, characterized in that: Also includes: If the field set with unchanged data values is empty, determining that all fields in the new record data have changed; The data status of the old record data record in the database table is set to invalid data record, the data status of the new record data record is set to valid data record, and the data date and time of the new record data record is set to the current date and time.
4. The data storage method according to claim 1 or 3, characterized in that: Also includes: When receiving a data adding instruction, a new record data is added to the database table, wherein the new record data includes: object unique id, basic information field, data date and time, and data status fields; Set the data date and time recorded in the newly added record data to the current date and time; Setting the data status recorded in the newly added record data to data record valid; When the data date and time and data status recorded in the newly added record data are set, the newly added record data is inserted into the database table.
5. The data storage method according to claim 1, characterized in that: Also includes: When receiving a data deletion instruction, according to the first target object unique ID carried in the data deletion instruction, searching from the data table for target record data whose object unique ID is the first target object unique ID and whose data status is that the data record is valid; The data status recorded in the target record data is set to data record invalid.
6. The data storage method according to claim 1, characterized in that: Also includes: When receiving the effective data query instruction, searching the database table for an object unique ID that is the same as the second target object unique ID according to the second target object unique ID carried in the effective data query instruction; If the query result is empty, it is determined that the record data corresponding to the unique ID of the second target object does not exist; If the query result is not empty, and the data status of the record data corresponding to the second target object unique id in the query result is that the data record is valid, it is determined that the record data corresponding to the second target object unique id exists and is in a valid state; If the query result is not empty, and the data status of the record data corresponding to the second target object unique id in the query result is that the data record is invalid, it is determined that the record data corresponding to the second target object unique id has been deleted.
7. The data storage method according to claim 1 or 6, characterized in that: Also includes: When a historical data query instruction is received, all record data sets are obtained from the database table according to the third target object unique ID and the preset time period carried in the historical data query instruction; Arrange each record data in the record data set in ascending order according to the data date and time to obtain a record data set to be processed, wherein the record data set to be processed has m rows of record data, each row of record data has n fields, and m and n are both positive integers; The historical data that is empty in the record data set to be processed is filled and restored to obtain a target data set.
8. The data storage method according to claim 7, characterized in that: The filling and recovery processing of the historical data which is empty in the record data set to be processed to obtain the target data set includes: Starting from the first row of record data in the to-be-processed record data set, determining in sequence according to a preset order whether the value of each field in the first row of record data is empty; For the empty target field, a filling value is determined from the corresponding same field that is not empty and filled until all the empty historical data in the m rows of record data in the to-be-processed record data set are filled to obtain the target data set.
9. A data storage device, characterized in that: include: A new record data adding unit is used to retain the old record data in the database table and add new record data when the data in the database table changes, wherein two fields, data date and time and data status, are added to the database table, the data date and time is used to record the date and time when the data is generated, and the data status is used to record whether the data is valid; A difference comparison unit, used to compare whether there is a difference between the values of the basic information fields in the new record data and the old record data, wherein the basic information fields are fields other than the three fields of the object unique id, the data date and time, and the data status; A field adjustment unit is used to set the values corresponding to the basic information fields of the old record data that have not changed relative to the new record data to empty if there are differences, set the data status of the old record data record to invalid data record, set the data status of the new record data record to valid data record, and set the data date and time of the new record data record to the current date and time.
10. An electronic device, characterized in that: The electronic device comprises: a memory and a processor; The memory is used to store at least one instruction; The processor is used to execute the at least one instruction to implement the data storage method according to any one of claims 1 to 8.