Data storage method and device, storage medium and computer equipment
Patent Information
- Application Number
- CN202210751642.X
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2022-06-28
- Publication Date
- 2026-08-21
- Estimated Expiration
- 2042-06-28
AI Technical Summary
然而,由于不同业务之间的数据规模往往会存在较大差距,使用按月分表的数据表来存储数据时容易导致单表存储不均,进而造成单表承载压力过大或单表存储资源浪费的情况
[0029]本申请提供的一种数据存储方法,可以响应于存储请求确定关键字,并根据关键字判断在第一数据库中是否查找到对应的目标主键标号,该目标主键标号用于表征待存储数据在数据表中的唯一标识,若在第一数据库中查找到对应的目标主键标号,则根据目标主键标号将待存储数据进行数据存储。如此,在响应存储请求确定关键字后,根据该关键字查询出存储数据的目标主键标号,并根据该目标主键标号确定对应的分表,进而将存储数存储至该分表中。从而使得待存储数据可以存放至目标主键标号所在的分表中,避免将数据直接存储至月份对应的数据表中,从而减轻单表存储的承载压力,有效地提升数据库中单表之间存储数据的均衡性。
Smart Images

Figure CN115357577B_ABST
Abstract
Description
Technical Field
[0001] This application relates to the field of database technology, and more specifically, to a data storage method, apparatus, storage medium, and computer equipment. Background Technology
[0002] A database is a repository for organizing, storing, and managing data according to a data structure. Specifically, it is a large, organized, shareable, and centrally managed collection of data stored permanently on a computer. With societal development, the amount of data has increased dramatically. Now, people are using computer and database technologies to scientifically preserve these massive amounts of data so that they can better utilize these data resources.
[0003] Currently, when using databases for data storage, databases are typically created based on business needs and partitioned by month. However, since the data size often varies significantly between different business operations, using monthly partitioned tables to store data can easily lead to uneven storage within a single table, resulting in excessive pressure on a single table or wasted storage resources. Summary of the Invention
[0004] This application provides a data storage method, apparatus, storage medium, and computer device. The aim is to improve the data balance across individual tables in a database.
[0005] On one hand, embodiments of this application provide a data storage method, which includes: determining a keyword in response to a storage request, the keyword including at least a data table name; determining whether a corresponding target primary key identifier is found in a first database based on the keyword, the target primary key identifier being used to characterize the unique identifier of the data to be stored in the data table; if a corresponding target primary key identifier is found in the first database, then storing the data to be stored based on the target primary key identifier.
[0006] Optionally, the data to be stored is stored according to the target primary key number, including: determining the storage time at the current point in time; determining the target data table in the second database according to the storage time and the target primary key number; and storing the data to be stored in the target data table.
[0007] Optionally, storing the data to be stored in the target data table includes: determining the storage location of the data to be stored in the target data table based on the target primary key number; and inserting the data to be stored into the storage location based on the storage location.
[0008] Optionally, the storage time includes the monthly storage time. Determining the storage time at the current point in time includes: determining the monthly storage time at the current point in time; determining the target data table in the second database based on the storage time and the target primary key number includes: matching the monthly storage time with the storage time of each data table in the second database to obtain at least one monthly matching data table whose storage time matches the monthly storage time; and determining the target data table from the at least one monthly matching data table based on the target primary key number.
[0009] Optionally, determining the target data table from at least one monthly matching data table based on the target primary key label includes: obtaining the label range of each monthly matching data table; matching the target primary key label with the label range of each monthly matching data table, and using the monthly matching data table whose label range includes the target primary key label as the target data table.
[0010] Optionally, after determining whether the corresponding target primary key label is found in the first database based on the keyword, the method further includes: if the target primary key label is not found in the first database, then performing a number segment generation operation; determining whether the number segment generation operation is successful; and if the number segment generation operation is successful, storing the generated primary key label in the first database.
[0011] Optionally, after determining whether the corresponding target primary key number is found in the first database based on the keyword, the method further includes: if the number segment generation operation fails, then re-determine the keyword; return to the step of determining whether the corresponding target primary key number is found in the first database based on the keyword.
[0012] Optionally, the number segment generation operation is performed, including: obtaining a primary key number segment table, which includes at least the maximum primary key label, the number segment step size, and the version number; and generating a preset number of primary key labels based on the maximum primary key label, the number segment length, and the version number.
[0013] Optionally, after generating a preset number of primary key labels based on the maximum primary key label, the number segment length, and the version number, the method further includes updating the maximum primary key label and the version number.
[0014] Optionally, before determining the keyword in response to the storage request, the method further includes: obtaining the usage rate of primary key labels in the first database; determining whether the usage rate is greater than a preset threshold, the preset threshold being used to characterize the minimum number of primary key labels stored in the first database; and if the usage rate is greater than the preset threshold, performing a number segment generation operation.
[0015] On the other hand, embodiments of this application also provide a data storage device, which includes: a determining module, configured to determine a keyword in response to a storage request, the keyword including at least a data table name; a searching module, configured to determine whether a corresponding target primary key identifier is found in a first database based on the keyword, the target primary key identifier being used to characterize the unique identifier of the data to be stored in the data table; and a storage module, configured to store the data to be stored based on the target primary key identifier if a corresponding target primary key identifier is found in the first database.
[0016] Optionally, the storage module may include a time determination unit, a data table determination unit, and a data storage unit. The time determination unit is used to determine the storage time at the current time point; the data table determination unit is used to determine the target data table in the second database based on the storage time and the target primary key number; and the data storage unit is used to store the data to be stored into the target data table.
[0017] Optionally, the data storage unit may be specifically used to: determine the storage location of the data to be stored in the target data table according to the target primary key number; and insert the data to be stored into the storage location according to the storage location.
[0018] Optionally, the storage time includes monthly storage time, and the time determination unit can be specifically used to determine the monthly storage time at the current time point;
[0019] The data table determination unit may include a time matching subunit and a data table determination subunit. The time matching subunit is used to match the storage time of the month with the storage time of each data table in the second database to obtain at least one matching data table whose storage time matches the storage time of the month. The data table determination subunit is used to determine the target data table from the at least one matching data table based on the target primary key identifier.
[0020] Optionally, the data table determination subunit may be specifically used to: obtain the label range of each month's matching data table; match the target primary key label with the label range of each month's matching data table, and take the month's matching data table whose label range includes the target primary key label as the target data table.
[0021] Optionally, the data storage device may further include: a first execution module, configured to perform a number segment generation operation if the target primary key identifier is not obtained in the first database; a first judgment module, configured to determine whether the number segment generation operation was successfully executed; and a second storage module, configured to store the generated primary key identifier in the first database if the number segment generation operation was successfully executed.
[0022] Optionally, the data storage device may further include: a second determining module, used to re-determine the keyword if the number segment generation operation fails; and a second searching module, used to return to the execution and determine whether the corresponding target primary key number is found in the first database based on the keyword.
[0023] Optionally, the execution module may be specifically used to: obtain a primary key number segment table, wherein the primary key number segment table includes at least a maximum primary key number, a number segment step size, and a version number; and generate a preset number of primary key numbers based on the maximum primary key number, the number segment length, and the version number.
[0024] Optionally, the execution module can also be specifically used to update the maximum primary key number and the version number.
[0025] Optionally, the data storage device may further include: an acquisition module for acquiring the usage rate of primary key labels in a first database; a second judgment module for judging whether the usage rate is greater than a preset threshold, the preset threshold being used to characterize the minimum number of primary key labels stored in the first database; and a second execution module for performing a number segment generation operation if the usage rate is greater than the preset threshold.
[0026] On the other hand, embodiments of this application also provide a computer device, which includes a processor and a memory. The memory stores computer program instructions, and when the computer program instructions are called by the processor, the above-described data storage method is executed.
[0027] On the other hand, embodiments of this application also provide a computer-readable storage medium storing program code, wherein the above-described data storage method is executed when the program code is run by a processor.
[0028] On the other hand, embodiments of this application also provide a computer program product or computer program, which includes computer instructions stored in a storage medium. A processor of a computer device reads the computer instructions from the storage medium and executes the computer instructions, causing the computer to perform the steps in the data storage method described above.
[0029] This application provides a data storage method that, in response to a storage request, determines a keyword and checks whether a corresponding target primary key identifier is found in a first database. This target primary key identifier uniquely identifies the data to be stored within the data table. If a corresponding target primary key identifier is found in the first database, the data is stored based on this identifier. Thus, after determining the keyword in response to the storage request, the target primary key identifier is retrieved based on that keyword, and the corresponding partition is determined based on this identifier. The data is then stored in this partition. This allows the data to be stored in the partition containing the target primary key identifier, avoiding direct storage in the month's corresponding data table. This reduces the storage load on a single table and effectively improves the data balance across tables in the database. Attached Figure Description
[0030] To more clearly illustrate the technical solutions in the embodiments of this application, the accompanying drawings used in the description of the embodiments will be briefly introduced below. Obviously, the accompanying drawings described below are only some embodiments of this application. For those skilled in the art, other drawings can be obtained based on these drawings without creative effort.
[0031] Figure 1 A schematic flowchart of a data storage method provided in an embodiment of this application is shown.
[0032] Figure 2 The illustration shows an application scenario diagram of a data storage method provided in an embodiment of this application.
[0033] Figure 3 A flowchart illustrating another data storage method provided in an embodiment of this application is shown.
[0034] Figure 4 This illustration shows a schematic diagram of primary key label storage provided in an embodiment of this application.
[0035] Figure 5 A flowchart of a data storage task provided in an embodiment of this application is shown.
[0036] Figure 6 A block diagram of a data storage device provided in an embodiment of this application is shown.
[0037] Figure 7 This is a block diagram of a computer device provided in an embodiment of this application.
[0038] Figure 8 This is a block diagram of a computer-readable storage medium provided in an embodiment of this application. Detailed Implementation
[0039] The embodiments of this application are described in detail below. Examples of the embodiments are shown in the accompanying drawings, wherein the same or similar reference numerals denote the same or similar elements or elements having the same or similar functions throughout. The embodiments described below with reference to the accompanying drawings are exemplary and are only used to explain this application, and should not be construed as limiting this application.
[0040] To enable those skilled in the art to better understand the solutions of this application, the technical solutions of the embodiments of this application will be clearly and completely described below with reference to the accompanying drawings. Obviously, the described embodiments are only a part of the embodiments of this application, and not all of them. All other embodiments obtained by those skilled in the art based on the embodiments of this application without creative effort are within the scope of protection of this application.
[0041] Currently, when using a database management system, users create different databases according to specific business types. For example, a company mainly operates three businesses: A, B, and C. The company creates separate databases for these three business types to store business data, such as database A, database B, and database C. Each business database is divided into monthly units, creating data tables for each month. For example, database A contains data tables for each of the twelve months. The company will then store the business data of business A for each month into the corresponding month's data table.
[0042] However, since the amount of data generated by each business varies from month to month, and the data scale of different businesses can differ significantly, simply partitioning tables by month may exceed the storage capacity of a single table. For example, if the storage limit of a data table is five million records, and business A stores six million records in May, then the data table... The system cannot store the actual amount of data. Furthermore, if business A only stores 700,000 records in December, then the data table... This will result in a large number of storage items remaining, leading to a waste of storage resources.
[0043] To address the aforementioned problems, the inventors, after extensive research, proposed a data storage method according to embodiments of this application. This method, in response to a storage request, determines a keyword and checks whether a corresponding target primary key identifier is found in a first database based on the keyword. This target primary key identifier is used to uniquely identify the data to be stored in the data table. If a corresponding target primary key identifier is found in the first database, the data to be stored is stored according to the target primary key identifier. Thus, after determining the keyword in response to the storage request, the target primary key identifier for the stored data is retrieved based on the keyword, and the corresponding partition table is determined based on the target primary key identifier. The data to be stored is then stored in this partition table, thereby avoiding direct storage of data in the monthly data table and improving the balance of load on a single table.
[0044] The following will use a computer device as an example to illustrate the specific process of this embodiment. Of course, it is understood that the computer device used in this embodiment can be a server or a terminal, etc. The server can be an independent physical server, a server cluster or distributed system composed of multiple physical servers, or a cloud server that provides basic cloud computing services such as cloud services, cloud databases, cloud computing, cloud functions, cloud storage, network services, cloud communication, middleware services, domain name services, security services, CDN, blockchain, and big data and artificial intelligence platforms. The terminal can be a smartphone, tablet computer, laptop computer, desktop computer, smart speaker, etc., but is not limited to these.
[0045] Please see Figure 1 , Figure 1 This application illustrates a data storage method provided in an embodiment, which may include the following steps:
[0046] Step S110: Determine the keyword in response to the storage request.
[0047] Considering that data storage by month may lead to an uneven load on a single table, this embodiment of the application adopts a table partitioning strategy based on a combination of storage time and primary key number range to partition the data table, and then stores the data to be stored in the data table corresponding to the primary key number range.
[0048] Storage requests refer to instructions for storing data. Storage instructions are generated when data to be stored is retrieved. For example, when a user stores business data in a database system, the database system generates a storage request the moment the user clicks "upload data." The key is a range of data, which must include at least the business prefix, table name, the maximum currently requested primary key ID (max_id), the length of each requested range (step), and version number. The range of data included in the key can be set according to actual needs and is not limited here.
[0049] As one implementation method, when a storage request is received, a determination can be made in response to the storage request. Specifically, keywords can be assembled based on different number range data, such as business prefix, table_name, max_id, step, and version number, which can be denoted as key = "business prefix: table_name: max_id: step: version". In the embodiments of this application, the specific business type can be determined by the keywords, and the storage time (e.g., month) can be distinguished, thereby facilitating the subsequent retrieval of the primary key number.
[0050] Step S120: Determine whether the corresponding target primary key label is found in the first database based on the keyword.
[0051] To achieve a more balanced load among the data tables storing data, this application adopts a table partitioning strategy based on a combination of time and primary key number. This ensures that each data table storing data has a predetermined range of primary key numbers. Therefore, during data storage, the primary key number of each data table can no longer use the database's auto-incrementing primary key number. Instead, it needs to be obtained from a database that stores the primary key number, i.e., the first database.
[0052] The first database refers to a cached database, such as a Remote Dictionary Server (Redis) database. This first database maintains a number segment table to store primary key identifiers. The cached database has good throughput. Redis uses a single thread to handle requests, avoiding the overhead of thread switching and lock contention, effectively improving data storage efficiency. The target primary key identifier refers to the primary key used when storing the data, also known as a unique identity document (ID).
[0053] As one implementation method, the generated primary key labels can be stored in a first database. Further, based on the keyword, a query is performed in the first database to determine if a target primary key label corresponding to the keyword exists. For example, if the range of primary key labels in the number segment table stored in Redis is [0000, 1999], and the target primary key label ID = 1998 is retrieved as needed, the number segment table in Redis is queried to determine if the target primary key label ID can be found.
[0054] Step S130: If the corresponding target primary key number is found in the first database, the data to be stored is stored according to the target primary key number.
[0055] In this context, data storage refers to storing the data to be stored in a non-cached database, also known as a second database, based on the target primary key identifier. For example, this could be a relational database (MySQL) hosted on a hard drive or cloud server. By storing the primary key identifier in a cached database (the first database) and then storing the data in a non-cached database (the second database), load balancing can be ensured for different business databases when storing data, resulting in more stable database storage performance.
[0056] In some embodiments, the step of storing the data to be stored according to the target primary key identifier if a corresponding target primary key identifier is found in the first database may include:
[0057] (1) Determine the storage time at the current point in time.
[0058] The storage time refers to the current point in time when the data is stored. This storage time can have different types, including monthly storage time (n months) or daily storage time (n months m days). It should be noted that the type of storage time can be set according to the actual data volume and data type of the business data. For example, when the daily data volume of the business data is large, the storage time can be set to monthly storage time; when the data types of the business data (product type, quotation) vary significantly from day to day, it can be set to daily storage time.
[0059] As one implementation method, if the corresponding target primary key identifier is found in the first database, the storage time of the current point in time is determined. For example, if the current time is June 1, 2022, the storage time of the month at the current point in time can be determined to be June 2022.
[0060] (2) Determine the target data table in the second database based on the storage time and the target primary key number.
[0061] The target data table refers to the table used to store the data to be stored. This table contains table information, including the table name and the range of primary key identifiers. When storing data, the target data table can be located in the second database based on the storage time and the target primary key identifier.
[0062] As one implementation, the step of determining the target data table in the second database based on storage time and target primary key identifier may include:
[0063] (2.1) Match the monthly storage time with the storage time of each data table in the second database to obtain at least one monthly matching data table whose storage time matches the monthly storage time.
[0064] (2.2) Determine the target data table from the matching data table of at least one month based on the target primary key number.
[0065] Taking the storage time as the month as an example, considering that the second database stores multiple data tables for each month, we can first find the matching data table for the month based on the month storage time, and then determine the data table containing the target primary key number from the matching data table. Here, the matching data table refers to the data table in the second database whose month matches the month storage time.
[0066] For example, the range of labels for each month's matching data table is obtained, and the target primary key label is matched with the range of labels for each month's matching data table. Then, the month's matching data table whose label range includes the target primary key label is taken as the target data table.
[0067] For example, if the month storage time is June, all data tables containing the month June will be retrieved from the second database, resulting in three data tables for June. in, The label range is [0000, 999]. The range of the numbers is [1000, 1999]. The label range is [2000, 2999], and the target primary key label ID = 1878 ∈ [1000, 1999], so As the target data table.
[0068] (3) Store the data to be stored in the target data table.
[0069] As one implementation method, after determining the target data table, the storage location of the data to be stored in the target data table can be determined based on the target primary key identifier. Furthermore, the data to be stored is inserted into the storage location according to the storage location. For example, if the target primary key identifier ID = 1878... If the storage location is item 879 / row, then the data to be stored will be stored in [location]. Line 879.
[0070] Because the database cannot use auto-incrementing IDs, a primary key identifier range (ID range) needs to be generated each time to store data. Therefore, the primary key identifier range may be exhausted. Consequently, when determining whether the corresponding target primary key identifier is found in the first database, it is possible that the target primary key identifier cannot be found.
[0071] In other embodiments, if the target primary key identifier is not obtained from the first database, a number segment generation operation is performed, and it is determined whether the number segment generation operation was successful. If the number segment generation operation was successful, the generated primary key identifier is stored in the first database. The generation operation is used to generate a new primary key identifier.
[0072] Specifically, if the corresponding target primary key number cannot be found in the first database, a number segment generation operation can be performed. If the number segment generation operation is successful, the generated primary key number is stored in the first database. If the number segment generation operation is unsuccessful, a new key can be determined from the second database, and the target primary key number can be retrieved again.
[0073] In this embodiment, a keyword can be determined in response to a storage request, and a corresponding target primary key identifier can be found in the first database based on the keyword. This target primary key identifier is used to uniquely identify the data to be stored in the data table. If the corresponding target primary key identifier is found in the first database, the data to be stored is stored according to the target primary key identifier. Thus, after determining the keyword in response to the storage request, the target primary key identifier of the stored data is retrieved based on the keyword, and the corresponding partition table is determined based on the target primary key identifier. The stored data is then stored in the partition table corresponding to the target primary key identifier, avoiding direct storage in the monthly data table, thereby improving the balance of the load on a single table.
[0074] Based on the methods described in the above embodiments, the following examples will provide further detailed explanations.
[0075] The following explanation will take the integration of data storage devices into computer equipment as an example, and will address... Figure 3 The process shown combines Figure 2 The application scenarios shown are described in detail. This computer device can be a server or a terminal device, etc. Please refer to [link / reference]. Figure 3 , Figure 3 This application illustrates another data storage method provided by an embodiment of the present application. In a specific embodiment, this data storage method can be applied to, for example... Figure 2 In the data storage scenario shown.
[0076] The data storage service provider provides a service server, which includes a cloud server 310. The cloud server 310 includes a database 311 for storing primary key identifiers (a first database, such as a Redis database) and a database 312 for storing business data (a second database, such as a MySQL database). The database storing primary key identifiers is installed in the cache of the cloud server 310. The database storing business data is used to store business data uploaded by users from the client. The client is the data storage software 330 opened on the user's mobile phone 320 when using the data storage service.
[0077] It is important to note that Figure 2 This is merely a schematic diagram illustrating one application provided in this application embodiment. The application scenarios described in this embodiment are for the purpose of more clearly illustrating the technical solutions of this embodiment and do not constitute a limitation on the technical solutions provided in this embodiment. For example, Figure 2 The number of clients can be multiple, enabling a single server to provide data storage services to multiple clients simultaneously. The devices installed on the clients can be tablets, laptops, Augmented Reality (AR) / Virtual Reality (VR) devices, virtual digital humans, etc. As will be apparent to those skilled in the art, with the emergence of new application scenarios, the technical solutions provided in this invention are also applicable to similar technical problems. The data storage method specifically includes the following steps:
[0078] Step S210: The computer device obtains the usage rate of the primary key labels in the first database.
[0079] Considering that a new primary key label can be generated after a primary key label has been used up, which involves a certain waiting time, to improve data storage efficiency, new primary key labels can be generated before all primary key labels have been used up. Here, utilization rate refers to the utilization rate of primary key labels in the number segment table of the first database; for example, the ratio of the number of primary key labels already used to the number of primary key labels generated at once.
[0080] As one implementation method, the computer device can periodically obtain the number of used primary key labels in the number segment table of the first database, and then calculate the utilization rate of the primary key labels. For example, the number segment table can store the number of primary key labels generated at one time as α = 1000, and the cloud server 310 can obtain the number of IDs used in the number segment table as β = 600 every 0.35 seconds, and then calculate the current utilization rate of the primary key labels as θ = β / α = 60%.
[0081] Step S220: The computer device determines whether the usage rate is greater than a preset threshold.
[0082] The preset threshold is a pre-set condition used to determine whether to generate a new primary key ID. The preset threshold can be determined based on device performance and actual experiments, and is not limited here. When the usage rate of IDs in the cache exceeds the preset threshold, the next primary key ID segment will be automatically generated to improve storage efficiency.
[0083] Step S230: If the usage rate is greater than the preset threshold, the computer device performs a number segment generation operation.
[0084] The segment generation operation involves generating new primary key labels. Specifically, a computer device can apply for an ID segment by executing code (SQL statements) related to the segment generation operation, which generates primary key labels in ascending order. For example, the cloud server 310 can execute SQL statements to generate new primary key labels and store them in the cached primary key segment data table.
[0085] Step S240: The computer device determines the keyword in response to the storage request.
[0086] For example, when a computer device receives a storage request, it can respond to the storage request by determining the following: Specifically, keywords can be assembled based on different number segment data service prefixes, table_name, max_id, step, and version number, which can be denoted as key = "service prefix: table_name: max_id: step: version". The specific service type can be identified through these keywords, and the storage time (e.g., month) can be distinguished, thus facilitating the subsequent retrieval of the primary key identifier.
[0087] Step S250: The computer device determines whether the corresponding target primary key label is found in the first database based on the keyword.
[0088] For example, the primary key identifier generated by the computer device can be stored in Redis. Further, the system queries Redis to see if a target primary key identifier ID corresponding to the keyword exists. Specifically, the range of primary key identifiers in the number segment table stored in Redis is [0000, 1999]. The computer device can query the number segment table in Redis based on the target primary key identifier ID = 1998, thereby determining whether the target primary key identifier can be found.
[0089] Step S260: If the target primary key number is not obtained from the first database, the computer device performs a number segment generation operation.
[0090] Because the primary key ID used by the cloud server 310 in this embodiment to store business data is not an auto-incrementing ID from the MySQL database, but rather an ID within a range (ID range) obtained from the Redis database in the cache, there is a possibility that the corresponding target primary key ID cannot be found when determining whether it has been found in the first database. Therefore, if the target primary key ID is not found in the first database, the computer device can perform a range generation operation.
[0091] As one implementation, if the target primary key identifier is not obtained from the first database, the step of the computer device performing the number segment generation operation may include:
[0092] (1) Computer equipment obtains the primary key number segment table.
[0093] (2) The computer device generates a preset number of primary key labels based on the maximum primary key label, the number segment length and the version number.
[0094] Table 1
[0095] id int PRI NO Primary Key ID max_id bigint NO Maximum primary key number step int NO Number segment step size table_name varchar(128) NO Monthly table name, such as: demo_202107 version int NO Version number update_ip varchar(64) NO Update node IP update_time datetime NO Update time
[0096] The primary key number segment table is used to store the generated primary key numbers. The primary key number segment table includes at least the maximum primary key number, the number segment step size, and the version number. For example, the table information for the primary key number segment table is shown in Table 1.
[0097] For example, a computer device can generate a preset number of primary key labels based on the maximum primary key label, the number segment length, and the version number. For instance, a cloud server 310 can execute the following SQL statement to generate a new preset number of primary key labels:
[0098] Update id_generator set max_id = max_id + step, / / Update the maximum primary key index
[0099] version=version+1 where version=#{version}and table_name="demo_202107"
[0100] / / Update version number
[0101] When a new primary key identifier is generated on cloud server 310, it can be stored in Redis and maintained using a List structure. The number of new primary key identifiers is determined by the segment step size. For example, if the segment step size is 1000, the maximum primary key identifier after the k-th generation is max_id. k =1999, then the maximum primary key ID generated in the (k+1)th generation is max_id.k+1 =step+max_id k =2999. The generated primary key identifier range is [2000, 2999]. Updating the version number ensures data safety during multi-node contention and guarantees data correctness during concurrency.
[0102] Step S270: The computer device determines whether the number segment generation operation was successfully executed.
[0103] After the number segment generation operation is completed, it can be determined whether the operation was successful. For example, cloud server 310 can determine whether a new primary key identifier was successfully generated.
[0104] Step S280a: If the number segment generation operation is successful, the computer device will store the generated primary key number in the first database.
[0105] As one implementation method, if the computer device determines that the number segment generation operation was successfully executed, the generated primary key identifier can be stored in the primary key number segment table of the first database. Please refer to [link to relevant documentation]. Figure 4 , Figure 4 The diagram illustrates the storage of primary key labels. The cloud server 310 maintains the newly generated primary key label [2000, 2999] in Redis using a List structure.
[0106] Specifically, based on the key, primary key number 2000 is stored in value1, primary key number 2001 is stored in value2, and so on, according to this linked list structure, primary key numbers [2000, 2999] are stored in the corresponding values.
[0107] Step S280b: If the number segment generation operation fails, the computer device re-determines the keyword and returns to determine whether the corresponding target primary key number is found in the first database based on the keyword.
[0108] As one implementation method, if the computer device determines that the number segment generation operation has failed, it can re-determine the keyword and return to the execution to determine whether the corresponding target primary key label is found in the first database based on the keyword, so as to request to execute the number segment generation operation again and regenerate the primary key label.
[0109] Step S290: If the corresponding target primary key number is found in the first database, the computer device stores the data to be stored according to the target primary key number.
[0110] Data storage refers to storing the data to be stored in a non-cached database, i.e., a secondary database, such as MySQL, based on the target primary key identifier. This approach, using Redis to store the primary key identifier and MySQL to store the data, ensures balanced database load across different business operations, resulting in more stable storage performance.
[0111] In some embodiments, the step of storing the data to be stored by the computer device according to the target primary key identifier if a corresponding target primary key identifier is found in the first database may include:
[0112] (1) The computer device determines the storage time at the current point in time.
[0113] The storage time can be either a monthly storage time or a daily storage time. For example, taking the monthly storage time as an example, when the cloud server 310 responds to a storage request, it can obtain the current time point: 20220601, and determine that the monthly storage time of the current time point is 202206.
[0114] (2) The computer equipment determines the target data table in the second database based on the storage time and the target primary key number.
[0115] The target data table refers to the data table used to store the data to be stored. This data table contains data table information, including the table name and the range of primary key IDs. In this embodiment, the target data table is constructed based on time and the range of primary key IDs, as shown in Table 2 below:
[0116] Table 2
[0117] demo_202107_1 1-5000000 demo_202107_2 5000001-100000000 … …
[0118] The stored data is maintained in the target data table. The target data table's information includes the table name and primary key index range. The table name includes the storage month and table number. The storage month indicates which month's business data the target data table stores, and the table number indicates which table within that month it is. For example, the table name "demo_202107_2" indicates that the target data table is the second table in July.
[0119] The instruction manual requires that each target data table stores data within a fixed ID range. The number of data tables per month is determined by the amount of business data stored that month and the maximum data limit of each table. The table number in the table name can be calculated based on the ID and the maximum data limit of the table. For example, if ID = 5000009 and the maximum data limit of the table is 5000000, then the table number is ID / maximum data limit of the table = (5000009 / 5000000) + 1 = 1.
[0120] As one implementation, the step of the computer device determining the target data table in the second database based on the storage time and the target primary key identifier may include:
[0121] (2.1) The computer device matches the monthly storage time with the storage time of each data table in the second database to obtain at least one monthly matching data table whose storage time matches the monthly storage time.
[0122] (2.2) The computer device determines the target data table from at least one month's matching data table based on the target primary key number.
[0123] For example, a computer device obtains a range of labels for a data table that can be matched each month, then matches the target primary key label with the range of labels for the data table that matches each month, and uses the data table that matches the month whose label range includes the target primary key label as the target data table.
[0124] For example, when cloud server 310 retrieves the month storage time 202206, it can query all data tables in MySQL for June as the month matching data table, including: Furthermore, based on the obtained target primary key ID = 1878, the system determines which data table, or target data table, will store the business data. (Query found) Therefore As the target data table.
[0125] (3) The computer equipment stores the data to be stored in the target data table.
[0126] As one implementation method, a computer device can determine the storage location of the data to be stored in a target data table based on the target primary key identifier, and insert the data to be stored into the storage location according to the storage location. For example, cloud server 310 determines... When targeting a data table, if the target primary key ID = 1878, If the storage location is line 879, then the data to be stored will be stored in [location]. Line 879.
[0127] For example, please refer to Figure 5 , Figure 5 A flowchart of a data storage task is shown. In a specific embodiment, this data storage task can be applied to, for example... Figure 2 In the data storage scenario shown, specifically, the user installs data storage software 330 on mobile phone 320 to store business data, and cloud server 310 creates data tables in MySQL.
[0128] When a user uses data storage software 330 to store acquired business data via mobile phone 320, data storage software 330 sends a storage request to cloud server 310. After receiving the storage request, cloud server 310 responds by determining the key and then checks whether the corresponding target primary key ID is found in Redis. If found, it retrieves the target primary key ID from Redis, determines the month to match the data table based on the current time, and then determines the target data table based on the target primary key ID, storing the business data in the target data table.
[0129] Optionally, if the corresponding target primary key ID is not found in Redis, a number segment generation operation is performed. Specifically, the cloud server 310 can request a primary key ID using an update statement and determine whether the number segment generation operation was successful. If the number segment generation operation is successful, the generated primary key ID is stored in Redis. If the number segment generation operation fails, the key can be redefined, and the target primary key ID can be retrieved again.
[0130] In this embodiment, the usage rate of primary key labels in the first database can be obtained, and it can be determined whether the usage rate is greater than a preset threshold. If the usage rate is greater than the preset threshold, a number segment generation operation is performed, and a new primary key label is generated before the primary key labels are used up, thereby improving the efficiency of data storage.
[0131] Furthermore, in response to the storage request, a keyword is determined, which includes at least the table name. Based on the keyword, it is determined whether a corresponding target primary key identifier is found in the first database. If the target primary key identifier is not found in the first database, the computer device performs a number segment generation operation and determines whether the number segment generation operation is successful. If the number segment generation operation is successful, the computer device stores the generated primary key identifier in the first database. If the number segment generation operation is unsuccessful, the computer device re-determines the keyword and returns to the process of determining whether a corresponding target primary key identifier is found in the first database based on the keyword. If a corresponding target primary key identifier is found in the first database, the computer device stores the data to be stored based on the target primary key identifier.
[0132] In this way, after determining the keyword in response to the storage request, the target primary key number of the stored data is retrieved based on the keyword, and the corresponding partition is determined based on the target primary key number. Then, the stored data is stored in the partition corresponding to the target primary key number, so that the stored data is stored in the partition corresponding to the target primary key number, avoiding direct storage in the monthly data table, thereby improving the balance of the load on a single table.
[0133] Please see Figure 6The diagram illustrates a structural block diagram of a data storage device 400 provided in an embodiment of this application. The data storage device 400 includes: a determining module 410, configured to determine a keyword in response to a storage request, the keyword including at least a data table name; a searching module 420, configured to determine whether a corresponding target primary key identifier is found in a first database based on the keyword, the target primary key identifier being used to characterize the unique identifier of the data to be stored in the data table; and a storage module 430, configured to store the data to be stored according to the target primary key identifier if a corresponding target primary key identifier is found in the first database.
[0134] In some embodiments, the storage module 430 may include a time determination unit, a data table determination unit, and a data storage unit. The time determination unit is used to determine the storage time at the current time point; the data table determination unit is used to determine the target data table in the second database based on the storage time and the target primary key number; and the data storage unit is used to store the data to be stored into the target data table.
[0135] In some embodiments, the data storage unit may be specifically used to: determine the storage location of the data to be stored in the target data table according to the target primary key number; and insert the data to be stored into the storage location according to the storage location.
[0136] In some embodiments, the storage time includes monthly storage time, and the time determination unit can be specifically used to determine the monthly storage time at the current time point;
[0137] The data table determination unit may include a time matching subunit and a data table determination subunit. The time matching subunit is used to match the storage time of the month with the storage time of each data table in the second database to obtain at least one matching data table whose storage time matches the storage time of the month. The data table determination subunit is used to determine the target data table from the at least one matching data table based on the target primary key identifier.
[0138] In some embodiments, the data table determination subunit may be specifically used to: obtain the label range of each month matching data table; match the target primary key label with the label range of each month matching data table, and take the month matching data table whose label range includes the target primary key label as the target data table.
[0139] In some embodiments, the data storage device 400 may further include: a first execution module, configured to perform a number segment generation operation if a target primary key identifier is not obtained from the first database; a first judgment module, configured to determine whether the number segment generation operation was successfully executed; and a second storage module, configured to store the generated primary key identifier in the first database if the number segment generation operation was successfully executed.
[0140] In some embodiments, the data storage device 400 may further include: a second determining module, configured to re-determine the keyword if the number segment generation operation fails; and a second searching module, configured to return to the execution of determining whether the corresponding target primary key number is found in the first database based on the keyword.
[0141] In some embodiments, the execution module 430 may be specifically used to: obtain a primary key number segment table, the primary key number segment table including at least a maximum primary key number, a number segment step size, and a version number; and generate a preset number of primary key numbers based on the maximum primary key number, the number segment length, and the version number.
[0142] In some embodiments, the execution module 430 may also be specifically used to update the maximum primary key number and the version number.
[0143] In some embodiments, the data storage device 400 may further include: a usage rate acquisition module, configured to acquire the usage rate of primary key labels in a first database; a second judgment module, configured to determine whether the usage rate is greater than a preset threshold, the preset threshold being used to characterize the minimum number of primary key labels stored in the first database; and a second execution module, configured to execute a number segment generation operation if the usage rate is greater than the preset threshold.
[0144] Those skilled in the art will clearly understand that, for the sake of convenience and brevity, the specific working process of the above-described device and module can be referred to the corresponding process in the foregoing method embodiments, and will not be repeated here.
[0145] In the several embodiments provided in this application, the coupling between modules can be electrical, mechanical, or other forms of coupling.
[0146] Furthermore, the functional modules in the various embodiments of this application can be integrated into one processing module, or each module can exist physically separately, or two or more modules can be integrated into one module. The integrated modules described above can be implemented in hardware or as software functional modules.
[0147] The solution provided in this application can determine a keyword in response to a storage request, and then determine whether a corresponding target primary key identifier is found in the first database based on the keyword. This target primary key identifier is used to uniquely identify the data to be stored in the data table. If a corresponding target primary key identifier is found in the first database, the data to be stored is stored according to the target primary key identifier. Thus, after determining the keyword in response to the storage request, the target primary key identifier of the stored data is retrieved based on the keyword, and the corresponding partition is determined based on the target primary key identifier. The stored data is then stored in the partition corresponding to the target primary key identifier, avoiding direct storage in the monthly data table, thereby improving the balance of load on a single table.
[0148] like Figure 7 As shown in the figure, this application embodiment also provides a computer device 500, which includes a processor 510, a memory 520, a power supply 530, and an input unit 540. The memory 520 stores computer program instructions. When the computer program instructions are called by the processor 510, they can execute various method steps provided in the above embodiments. Those skilled in the art will understand that the structure of the computer device shown in the figures does not constitute a limitation on the computer device, and may include more or fewer components than shown, or combine certain components, or have different component arrangements. Wherein:
[0149] The processor 510 may include one or more processing cores. The processor 510 connects to various parts of the entire battery management system using various interfaces and lines. It executes instructions, programs, code sets, or instruction sets stored in the memory 520, calls data stored in the memory 520, performs various functions and processes data within the battery management system, and performs various functions and processes data within the computer device, thereby providing overall control of the computer device. Optionally, the processor 510 may be implemented using at least one hardware form of Digital Signal Processing (DSP), Field-Programmable Gate Array (FPGA), or Programmable Logic Array (PLA). The processor 510 may integrate one or more of the following: a Central Processing Unit (CPU), a Graphics Processing Unit (GPU), and a modem. The CPU primarily handles the operating system, user interface, and applications; the GPU is responsible for rendering and drawing the displayed content; and the modem handles wireless communication. It is understood that the modem may also be implemented separately as a communication chip, without being integrated into the processor 510.
[0150] The memory 520 may include random access memory (RAM) or read-only memory (ROM). The memory 520 may be used to store instructions, programs, code, code sets, or instruction sets. The memory 520 may include a program storage area and a data storage area. The program storage area may store instructions for implementing an operating system, instructions for implementing at least one function (such as touch functionality, sound playback functionality, image playback functionality, etc.), instructions for implementing the various method embodiments described below, etc. The data storage area may also store data created during the use of the computer device (such as phonebook and audio / video data). Accordingly, the memory 520 may also include a memory controller to provide the processor 510 with access to the memory 520.
[0151] The power supply 530 can be logically connected to the processor 510 through a power management system, thereby enabling functions such as charging, discharging, and power consumption management through the power management system. The power supply 530 may also include one or more DC or AC power supplies, recharging systems, power fault detection circuits, power converters or inverters, power status indicators, and other arbitrary components.
[0152] The input unit 540 can be used to receive input digital or character information, and to generate keyboard, mouse, joystick, optical or trackball signal inputs related to user settings and function control.
[0153] Although not shown, the computer device 500 may also include a display unit, etc., which will not be described in detail here. Specifically, in this embodiment, the processor 510 in the computer device loads the executable files corresponding to the processes of one or more applications into the memory 520 according to the following instructions, and the processor 510 runs the applications stored in the memory 520, thereby implementing the various method steps provided in the foregoing embodiments.
[0154] like Figure 8 As shown, this application embodiment also provides a computer-readable storage medium 600, which stores computer program instructions 610, which can be called by a processor to execute the methods described in the above embodiments.
[0155] Computer-readable storage media can be electronic storage devices such as flash memory, EEPROM (Electrically Erasable Programmable Read-Only Memory), EPROM, hard disk, or ROM. Optionally, computer-readable storage media includes non-volatile computer-readable storage medium. Computer-readable storage medium 600 has storage space for program code that performs any of the method steps described above. This program code can be read from or written to one or more computer program products. The program code can be compressed, for example, in a suitable form.
[0156] According to one aspect of this application, a computer program product or computer program is provided, comprising computer instructions stored in a computer-readable storage medium. A processor of a computer device reads the computer instructions from the computer-readable storage medium and executes the computer instructions, causing the computer device to perform the methods provided in the various optional implementations of the above embodiments.
[0157] The above are merely preferred embodiments of this application and are not intended to limit this application in any way. Although this application has disclosed preferred embodiments as above, it is not intended to limit this application. Any person skilled in the art can make some modifications or alterations to the above-disclosed technical content to create equivalent embodiments without departing from the scope of the technical solution of this application. Any simple modifications, equivalent changes and alterations made to the above embodiments based on the technical essence of this application without departing from the scope of the technical solution of this application shall still fall within the scope of the technical solution of this application.
Claims
1. A data storage method, characterized in that, The method includes: In response to a storage request, a keyword is determined, the keyword including at least the table name; Based on the keyword, it is determined whether the corresponding target primary key number is found in the first database. The target primary key number is used to represent the unique identifier of the data to be stored in the data table. The target primary key number is located within the range of primary key numbers corresponding to the data table name. If a corresponding target primary key number is found in the first database, the data to be stored is stored according to the target primary key number, so that the data to be stored is stored in the target data table corresponding to the target primary key number, and the target data table carries the data table name and the range of the primary key number; The step of storing the data to be stored according to the target primary key number includes: determining the storage time at the current time point; determining the target data table in the second database according to the storage time and the target primary key number; and storing the data to be stored into the target data table.
2. The method according to claim 1, characterized in that, The step of storing the data to be stored into the target data table includes: The storage location of the data to be stored in the target data table is determined based on the target primary key number; The data to be stored is inserted into the storage location according to the storage location.
3. The method according to claim 1, characterized in that, The storage time includes monthly storage time, and determining the storage time at the current point in time includes: Determine the storage time of the current month; The step of determining the target data table in the second database based on the storage time and the target primary key number includes: The storage time of the month is matched with the storage time of each data table in the second database to obtain at least one matching data table whose storage time matches the storage time of the month. The target data table is determined from the matching data table of at least one month based on the target primary key number.
4. The method according to claim 3, characterized in that, The step of determining the target data table from the at least one monthly matching data table based on the target primary key index includes: Obtain the range of numbers from the matching data table for each month; The target primary key number is matched with the number range of each month's matching data table, and the month matching data table whose number range includes the target primary key number is taken as the target data table.
5. The method according to claim 1, characterized in that, After determining whether the corresponding target primary key identifier is found in the first database based on the keyword, the method further includes: If the target primary key identifier is not obtained from the first database, then the identifier generation operation is performed; Determine whether the number segment generation operation was successfully executed; If the number segment generation operation is successful, the generated primary key number will be stored in the first database.
6. The method according to claim 5, characterized in that, The method further includes: If the number segment generation operation fails, the keywords will be re-determined; Return to the previous step and determine whether the corresponding target primary key number is found in the first database based on the keyword.
7. The method according to claim 5, characterized in that, The execution segment generation operation includes: Obtain the primary key number segment table, which includes at least the maximum primary key number, the number segment length, and the version number; A preset number of primary key labels are generated based on the maximum primary key label, the number segment length, and the version number.
8. The method according to claim 7, characterized in that, After generating a preset number of primary key labels based on the maximum primary key label, the number segment length, and the version number, the method further includes: Update the maximum primary key number and the version number.
9. The method according to claim 1, characterized in that, Prior to determining the keyword in response to the storage request, the method further includes: Get the usage rate of primary key labels in the first database; Determine whether the usage rate is greater than a preset threshold, where the preset threshold is used to characterize the minimum number of primary key labels stored in the first database; If the usage rate is greater than the preset threshold, then a number segment generation operation is performed.
10. A data storage device, characterized in that, The device includes: A determination module is used to determine a keyword in response to a storage request, wherein the keyword includes at least the data table name; The search module is used to determine whether the corresponding target primary key number is found in the first database based on the keyword. The target primary key number is used to represent the unique identifier of the data to be stored in the data table. The target primary key number is located within the range of primary key numbers corresponding to the data table name. The storage module is used to store the data to be stored according to the target primary key number if a corresponding target primary key number is found in the first database, so as to store the data to be stored in the target data table corresponding to the target primary key number, wherein the target data table carries the data table name and the range of the primary key number; The storage module includes: a time determination unit for determining the storage time at the current time point; a data table determination unit for determining the target data table in the second database based on the storage time and the target primary key number; and a data storage unit for storing the data to be stored into the target data table.
11. A computer-readable storage medium, characterized in that, The computer-readable storage medium stores program code that can be invoked by a processor to execute the method as described in any one of claims 1 to 9.
12. A computer device, characterized in that, include: Memory; One or more processors are coupled to the memory; One or more applications, wherein the one or more applications are stored in the memory and configured to be executed by the one or more processors, the one or more applications being configured to perform the method as described in any one of claims 1 to 9.
13. A computer program product, characterized in that, The computer program product includes computer instructions stored in a storage medium, a processor of a computer device reads the computer instructions from the storage medium, and the processor executes the computer instructions to cause the computer to perform the method as described in any one of claims 1 to 9.
Citation Information
Patent Citations
DATA STORAGE METHOD, device AND COMPUTER device
CN109408509A
Data query method and device, terminal and computer readable storage medium
CN110727702A