Primary key acquisition method and device

By automatically calculating and real-time update of the number of primary key updates, combined with the method of saving the unused primary keys, the problem of frequent manual adjustment and resource waste in the existing technology is solved, reducing the risk of online accidents.

CN113779053BActive Publication Date: 2025-02-21BEIJING WODONG TIANJUN INFORMATION TECH CO LTD +1
View PDF 2 Cites 0 Cited by

Patent Information

Application Number
CN202110114591.5
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2021-01-26
Publication Date
2025-02-21
Estimated Expiration
2041-01-26

AI Technical Summary

Technical Problem

The existing primary key acquisition method requires frequent manual adjustment of the number of updates, and the primary key resources are wasted when the system restarts or goes online, increasing the risk of online accidents.

Method used

By monitoring the margin and historical update data of the currently available primary key list, the number of updates for each batch of primary keys is automatically calculated and updated in real time, and the unused primary keys are saved to the database when restarted or launched.

Benefits of technology

It reduces the risk of online accidents, solves the problem of frequent adjustment of the number of primary key updates, and avoids wasting primary key resources.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN113779053B_ABST
    Figure CN113779053B_ABST
Patent Text Reader

Abstract

The present invention discloses a method and device for obtaining a primary key, and relates to the field of computer technology. A specific implementation of the method for obtaining the primary key includes: if the list balance in the current available primary key list reaches the update threshold, then determining the update number of the primary keys in this batch, the update number is determined based on the primary key update data of the historical batch; obtaining the primary key usage record of the current available primary key list; if the primary key usage record of the current available primary key list is not empty, then obtaining the update number of primary keys based on the primary key usage record of the current available primary key list, and updating the primary key usage record. This implementation can automatically calculate and update (adaptively) the number of primary keys updated in each batch in real time, can reduce the risk of online accidents, and can solve the problem that the existing acquisition method needs to manually and frequently modify the update number of primary keys according to the usage of the primary keys.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention relates to the field of computer technology, and in particular to a method and device for obtaining a primary key. Background Art

[0002] Generally, in the process of obtaining primary keys, it is necessary to manually set the number of primary keys to be updated in each batch in advance, that is, it can be manually adjusted according to the number of online business requests in daily operation and maintenance, and it needs to be manually adjusted again according to the estimated traffic when conducting promotion activities. The parameters for obtaining primary keys need to be adjusted frequently and the adjustment work is very dependent on experience. In addition, if the number of primary keys in the local memory is not used up, the system goes online or restarts, and the primary keys in the local memory will be discarded. When the system carries a lot of traffic, there will be a lot of online machines, and the number of primary keys stored in the memory of each machine is generally also very large, which will eventually waste a lot of primary keys, thereby accelerating the exhaustion of primary key resources. If the primary key is used for business purposes such as order numbers, for limited order number segments, it will also lead to a lot of waste and rapid exhaustion of order number resources. Summary of the invention

[0003] In view of this, an embodiment of the present invention provides a method and device for obtaining a primary key, which can solve the problem that the existing acquisition method requires manual and frequent modification of the update quantity of the primary key according to the usage of the primary key, as well as the problem of large-scale waste of primary key resources caused by restarting and going online, thereby reducing the risk of online accidents.

[0004] To achieve the above objective, according to one aspect of an embodiment of the present invention, a method for obtaining a primary key is provided.

[0005] The method for obtaining the primary key in the embodiment of the present invention includes:

[0006] If the list balance in the current available primary key list reaches the update threshold, the update quantity of the primary keys of this batch is determined, and the update quantity is determined according to the primary key update data of the historical batch;

[0007] Get the primary key usage record of the current available primary key list;

[0008] If the primary key usage record of the currently available primary key list is not empty, the updated number of primary keys is obtained according to the primary key usage record of the currently available primary key list, and the primary key usage record is updated.

[0009] Optionally, the primary key update data includes at least: the primary key update quantity of the historical batch and the primary key update time of the historical batch.

[0010] Optionally, determining the number of updates of primary keys in this batch includes:

[0011] Obtaining the first time when the primary key of a first historical batch is updated, where the first historical batch is the previous batch adjacent to the current batch;

[0012] If the first time is not empty, then calculating the second time taken for the first historical batch primary key to be consumed;

[0013] Obtaining a third time taken for the primary key of a second historical batch to be consumed, where the second historical batch is a previous batch adjacent to the first historical batch;

[0014] If the third time is not empty, obtaining the update quantity of the primary key of the first historical batch;

[0015] The update quantity of the primary keys of this batch is determined according to the second time, the third time and the update quantity of the primary keys of the first historical batch.

[0016] Optionally, determining the number of updates of the primary keys of the current batch according to the second time, the third time, and the number of updates of the primary keys of the first historical batch includes:

[0017] calculating a ratio of the second time to the third time;

[0018] If the update quantity of the first historical batch primary key is not empty, the product of the update quantity of the first historical batch primary key and the ratio is determined as the update quantity of the primary key of this batch.

[0019] Optionally, the primary key usage record includes at least: a primary key usage master record and multiple non-primary records, wherein the primary record is used to indicate the number of used primary keys, and the non-primary records are used to indicate the available primary key value interval.

[0020] Optionally, the data storage structure of the primary key usage record includes at least the following information: storage table name, minimum primary key value used, maximum primary key value used, whether it is a primary record in the current available primary key list, data validity flag and update time.

[0021] Optionally, obtaining the updated number of primary keys and updating the primary key usage records includes:

[0022] Get a valid non-primary record in the current available primary key list in chronological order;

[0023] If the result of obtaining non-primary records is empty, obtain the primary record corresponding to the current available primary key list;

[0024] The update number of primary keys is obtained according to the current primary record, and the maximum value of the primary keys used in the current primary record is updated.

[0025] Optionally, updating the primary key usage record of the available primary key list further includes:

[0026] If the result of the obtained non-primary record is not empty, and the difference between the maximum value of the primary key and the minimum value of the primary key in the non-primary record is less than the update number of the primary key, then the update number of primary keys is obtained according to the primary key value of the current non-primary record, and the current non-primary record is updated to an invalid record; then the step of obtaining a valid non-primary record in the current available primary key list in chronological order is repeated until the non-primary record meets the requirements;

[0027] If the result of the obtained non-primary record is not empty, and the difference between the maximum value of the primary key and the minimum value of the primary key in the non-primary record is greater than or equal to the number of primary key updates, then the updated number of primary keys are obtained starting from the minimum value of the primary key; if there are any remaining primary keys in the current non-primary record, then the minimum value of the primary key of the current non-primary record is updated; if there are no remaining primary keys in the current non-primary record, then the current non-primary record is updated to an invalid record.

[0028] Optionally, updating the primary key usage record of the available primary key list includes:

[0029] When restarting or going online, a non-primary record is generated according to the relevant information of the remaining primary key to save the remaining primary key in the database.

[0030] To achieve the above objective, according to another aspect of an embodiment of the present invention, a primary key acquisition device is provided.

[0031] The primary key acquisition device of the embodiment of the present invention includes:

[0032] A determination module, configured to determine the update quantity of the primary keys of this batch if the list balance in the current available primary key list reaches the update threshold, wherein the update quantity is determined based on the primary key update data of the historical batch;

[0033] The first acquisition module is used to obtain the primary key usage record of the current available primary key list;

[0034] The second acquisition module is used to acquire the updated number of primary keys according to the primary key usage record of the current available primary key list if the primary key usage record of the current available primary key list is not empty, and update the primary key usage record.

[0035] To achieve the above objective, according to another aspect of an embodiment of the present invention, an electronic device is provided.

[0036] The electronic device according to the embodiment of the present invention comprises:

[0037] one or more processors;

[0038] a storage device for storing one or more programs,

[0039] When the one or more programs are executed by the one or more processors, the one or more processors implement the method as described above.

[0040] To achieve the above objective, according to another aspect of an embodiment of the present invention, a computer readable medium is provided.

[0041] The computer-readable medium of the embodiment of the present invention stores a computer program, and when the program is executed by a processor, the method described above is implemented.

[0042] One embodiment of the above invention has the following advantages or beneficial effects:

[0043] In an embodiment of the present invention, when obtaining the primary key, it can be obtained according to the update quantity, and the update quantity is determined according to the primary key update data of historical batches. The number of updated primary keys in each batch can be automatically calculated and updated (adaptively) in real time, which can reduce the risk of online accidents. It can solve the problem that the existing acquisition method requires manual and frequent modification of the update quantity of the primary key according to the usage of the primary key, and can also solve the problem of large waste of primary key resources caused by restarting and going online.

[0044] The further effects of the above-mentioned non-conventional optional manner will be described below in conjunction with the specific implementation manner. BRIEF DESCRIPTION OF THE DRAWINGS

[0045] The accompanying drawings are used to better understand the present invention and do not constitute an improper limitation of the present invention.

[0046] Figure 1 It is a flowchart of the existing method for obtaining the primary key;

[0047] Figure 2 This is a flowchart of the existing process of updating the list of available primary keys;

[0048] Figure 3 is a flowchart of a method for obtaining a primary key according to a first embodiment of the present invention;

[0049] Figure 4 is a flowchart of a method for obtaining a primary key according to a second embodiment of the present invention;

[0050] Figure 5 It is a schematic diagram of a process of updating a primary key usage record according to an embodiment of the present invention;

[0051] Figure 6 is a schematic diagram of a process of constructing a non-master record according to an embodiment of the present invention;

[0052] Figure 7 is a module schematic diagram of a device for obtaining a primary key according to an embodiment of the present invention;

[0053] Figure 8 is an exemplary system architecture diagram to which embodiments of the present invention may be applied;

[0054] Fig. 9 It is a schematic diagram of the structure of a computer system of a terminal device or a server suitable for implementing an embodiment of the present invention. DETAILED DESCRIPTION

[0055] The following is a description of exemplary embodiments of the present invention in conjunction with the accompanying drawings, including various details of the embodiments of the present invention to facilitate understanding, which should be considered as merely exemplary. Therefore, it should be recognized by those of ordinary skill in the art that various changes and modifications may be made to the embodiments described herein without departing from the scope and spirit of the present invention. Similarly, for clarity and conciseness, the description of well-known functions and structures is omitted in the following description.

[0056] With the increasing demand for high traffic and high concurrency, the current transaction system generally adopts the distributed database primary key acquisition method when obtaining the primary key. Figure 1 The process of the acquisition method generally includes the following steps S101 to S105.

[0057] Step S101: Obtain an available primary key list from the local memory.

[0058] Step S102: Determine whether the list balance in the available primary key list reaches the update threshold. If yes, execute step S103. Otherwise, execute step S104.

[0059] In step S102, the update threshold refers to the ratio of the number of remaining available primary keys to the number of primary keys updated in each batch when the update operation is triggered. For example, if 100 primary keys are updated to the local memory each time, the update operation is started when there are less than 10 remaining, and the update threshold is 90%.

[0060] Step S103: Update the available primary key list, and then continue to execute step S101.

[0061] Step S104: Obtain an available primary key from the available primary key list.

[0062] Step S105: Delete the currently acquired primary key from the available primary key list.

[0063] See also Figure 2 The specific process of updating the available primary key list includes the following steps S1031 to S1035.

[0064] Step S1031: Obtain the primary key usage record of the current available primary key list.

[0065] Step S1032: Determine whether the primary key usage record is empty. If so, execute step S1033; otherwise, execute step S1034.

[0066] Step S1033: Create a primary key usage record for the current available primary key list.

[0067] In step S1033, the primary key usage record refers to a piece of data stored in the database. The storage structure of the usage record data is as follows:

[0068] Field Name Field Type Is it nullable? Remark table_name varchar(100) no Primary key, used to store the table name used_quantity bigint(20) no The number of primary keys used

[0069] Each table stores a usage record, where the table_name field is used to store the table name and used_quantity is used to store the number of primary keys used.

[0070] When the system needs to obtain a new batch of primary keys based on usage records, it first obtains used_quantity based on table_name, then adds a certain value to it, and finally returns the primary keys of the interval before and after the addition. If used_quantity cannot be obtained, it first inserts a table usage record, and then repeats the above process of obtaining the primary key.

[0071] For example, if 100 primary keys need to be obtained from the teacher table, and the corresponding used_quantity is found to be 3500 through query, then used_quantity is updated to 3600, and then a total of 100 primary keys with used_quantity between 3501 and 3600 are returned.

[0072] Step S1034: Obtain a specified number of primary keys and update the primary key usage records.

[0073] In step S1034, the specified quantity is generally configured manually. The specified quantity can be configured to be smaller when the daily usage rate is low, and the specified quantity can be configured to be larger when the promotion activity is highly concurrent. If the specified quantity is configured too large, it will take a long time to update the primary key, and the primary key values ​​in different machines will be quite different, which will pose a security risk. The default usage rule is that the later the primary key is generated, the larger the primary key. If the specified quantity is set too small during a big promotion, the machine will need to frequently request the database to update the primary key, which will ultimately affect QPS performance.

[0074] Step S1035: Update the acquired primary key list to the local memory.

[0075] In order to solve the problem that the existing acquisition method requires manual and frequent modification of the update quantity of the primary key according to the usage of the primary key, based on the above embodiments, an embodiment of the present invention provides a primary key acquisition method, which can automatically calculate and update (adaptively) the number of primary keys updated in each batch in real time, thereby reducing the risk of online accidents. Figure 3 FIG. 1 is a flow chart of a method for obtaining a primary key according to an embodiment of the present invention. Figure 3 As shown, the method for obtaining the primary key may include the following steps S301 to S303.

[0076] Step S301: If the list balance in the current available primary key list reaches the update threshold, the update quantity of the primary keys of this batch is determined, and the update quantity is determined according to the primary key update data of the historical batch.

[0077] In step S301, the update threshold refers to the ratio of the number of remaining available primary keys to the number of primary keys updated in each batch when the update operation is triggered. The update number refers to the target number of primary keys obtained from the database, and the update number is related to the primary key update data of a preset number of historical batches adjacent to the current batch, and the specific value of the preset number can be determined according to actual needs. For example: the preset number is two, that is, the update number of the primary key can be obtained based on the primary key update data of two historical batches adjacent to the current batch. The primary key update data refers to the historical parameter data of each primary key acquisition process, and the primary key update data includes at least: the number of primary key updates of the historical batch and the primary key update time of the historical batch, and the time taken for the primary keys of the historical batch to be consumed is determined based on the primary key update time of the historical batch.

[0078] Furthermore, when obtaining the number of updates of the primary key based on the primary key update data of two historical batches adjacent to the current batch, the specific process of obtaining the number of updates of the primary key is roughly as follows: first, obtain the first time of the primary key update of the first historical batch, the first historical batch is the previous batch adjacent to the current batch; if the first time is not empty, calculate the second time taken for the primary key of the first historical batch to be consumed; then obtain the third time taken for the primary key of the second historical batch to be consumed, the second historical batch is the previous batch adjacent to the first historical batch; if the third time is not empty, obtain the number of updates of the primary key of the first historical batch; finally, determine the number of updates of the primary key of the current batch based on the second time, the third time and the number of updates of the primary key of the first historical batch. For example, the ratio of the second time and the third time can be calculated; if the number of updates of the primary key of the first historical batch is not empty, the product of the number of updates of the primary key of the first historical batch and the ratio is determined as the number of updates of the primary key of the current batch.

[0079] Step S302: Obtain the primary key usage record of the current available primary key list.

[0080] The data storage structure of the primary key usage record includes at least the following information: storage table name, the minimum primary key value used, the maximum primary key value used, whether it is the primary record of the current available primary key list, the data validity flag, and the update time. The data storage structure of the primary key usage record is shown in the following table:

[0081]

[0082]

[0083] It should be noted that the primary key usage record includes at least: one primary key usage master record and multiple non-primary records, wherein the master record is used to indicate the number of primary keys used, and the non-primary record is used to indicate the available primary key value range. It is understandable that each table has one primary key usage master record and multiple non-primary records, the primary record is definitely valid, and some non-primary records are valid and some are invalid. The used_quantity_begin of the primary record is definitely = 0, used_quantity_end has the same meaning as used_quantity, and the non-primary record records the available primary key value range through used_quantity_begin and used_quantity_end. The combined unique index is table_name+isMaster+isValid+used_quantity_begin.

[0084] Step S303: if the primary key usage record of the current available primary key list is not empty, then the update number of primary keys is obtained according to the primary key usage record of the current available primary key list, and the primary key usage record is updated.

[0085] In step S303, before obtaining the updated number of primary keys, a valid non-primary record of the currently available primary key list may be obtained in chronological order, and then the obtaining process may be divided into the following situations according to whether the obtained non-primary record is empty:

[0086] Scenario 1: The result of obtaining non-master records is empty.

[0087] If the result of the obtained non-primary record is empty, the primary record corresponding to the current available primary key list is obtained; the update number of primary keys is obtained according to the current primary record, and the maximum value of the used primary keys of the current primary record is updated.

[0088] Scenario 2: The result of obtaining non-primary records is not empty, and the difference between the maximum value and the minimum value of the primary key in the non-primary record is less than the number of updates to the primary key.

[0089] If the result of the non-primary record obtained is not empty, and the difference between the maximum value of the primary key and the minimum value of the primary key in the non-primary record is less than the number of primary key updates, then the updated number of primary keys are obtained according to the primary key value of the current non-primary record, and the current non-primary record is updated to an invalid record; then repeat the step of obtaining a valid non-primary record in the current available primary key list in chronological order until the non-primary record meets the requirements.

[0090] Scenario 3: The result of obtaining non-primary records is not empty, and the difference between the maximum value and the minimum value of the primary key in the non-primary record is greater than or equal to the number of updates to the primary key.

[0091] If the result of the obtained non-primary record is not empty, and the difference between the maximum value of the primary key and the minimum value of the primary key in the non-primary record is greater than or equal to the number of primary key updates, then the updated number of primary keys are obtained starting from the minimum value of the primary key; if there are any remaining primary keys in the current non-primary record, then the minimum value of the primary key of the current non-primary record is updated; if there are no remaining primary keys in the current non-primary record, then the current non-primary record is updated to an invalid record.

[0092] It should be emphasized that in order to solve the problem of massive waste of primary key resources caused by restart and online, when restarting or going online, non-primary records are generated according to the relevant information of the remaining primary keys to save the remaining primary keys in the database.

[0093] After step S303, the update quantity of the primary keys of this batch, the update time of the primary keys of this batch, and the time taken for the primary keys of the first historical batch to be consumed may be saved in the memory.

[0094] In an embodiment of the present invention, when obtaining the primary key, it can be obtained based on the update quantity, and the update quantity is determined based on the primary key update data of historical batches. The number of primary keys updated in each batch can be automatically calculated and updated (adaptively) in real time, which can reduce the risk of online accidents and solve the problem that the existing acquisition method requires manual and frequent modification of the update quantity of the primary key according to the usage of the primary key.

[0095] In order to solve the problem that the existing acquisition method requires manual and frequent modification of the update quantity of the primary key according to the usage of the primary key, based on the above embodiments, an embodiment of the present invention provides a primary key acquisition method, which can automatically calculate and update (adaptively) the number of primary keys updated in each batch in real time, thereby reducing the risk of online accidents. Figure 4 FIG. 1 is a flow chart of a method for obtaining a primary key according to an embodiment of the present invention. Figure 4 As shown, the method for obtaining the primary key may include the following steps S401 to S413.

[0096] Step S401: obtaining the first time of updating the primary key of a first historical batch, where the first historical batch is the previous batch adjacent to the current batch.

[0097] Step S402: Determine whether the first time is empty. If the first time is empty, execute steps S403 to S404; if the first time is not empty, execute step S405.

[0098] Step S403: If the first time is empty, the update time of the primary key of this batch is saved in the memory.

[0099] Step S404: Return to the default value, and then end the process; the default value can be preset, for example: the default value is 100.

[0100] Step S405: If the first time is not empty, calculate the second time taken for the first historical batch primary key to be consumed.

[0101] Step S406: obtaining a third time taken for the primary key of a second historical batch to be consumed, where the second historical batch is a previous batch adjacent to the first historical batch.

[0102] Step S407: Determine whether the third time is empty; if the third time is empty, execute steps S408 to S410. If the third time is not empty, execute step S411.

[0103] Step S408: If the third time is empty, the primary key update time of this batch is saved in the memory.

[0104] Step S409: Save the time taken for the first historical batch of primary keys to be consumed into the memory.

[0105] Step S410: Return to the default value and then end the process.

[0106] Step S411: If the third time is not empty, calculate the ratio m of the second time and the third time, and then execute step S412.

[0107] Step S412: Obtain the update quantity of the primary key of the first historical batch.

[0108] Step S413: Determine whether the update quantity of the first historical batch primary key is empty. If the update quantity of the first historical batch primary key is empty, execute steps S414 to S417. If the update quantity of the first historical batch primary key is not empty, execute step S418.

[0109] Step S414: If the update quantity of the primary key of the first historical batch is empty, the update time of the primary key of this batch is saved in the memory.

[0110] Step S415: Save the time taken for the first historical batch of primary keys to be consumed into the memory.

[0111] Step S416: Save the update quantity of the primary keys of this batch to the memory.

[0112] Step S417: Return to the default value and end the process.

[0113] Step S418: If the update quantity of the primary key of the first historical batch is not empty, the product of the update quantity of the primary key of the first historical batch and the ratio m is determined as the update quantity of the primary key obtained from the database in this batch.

[0114] Step S419: Save the update time of the primary key of this batch to the memory.

[0115] Step S420: Save the time taken for the first historical batch of primary keys to be consumed into the memory.

[0116] Step S421: Save the update quantity of the primary key of this batch to the memory.

[0117] Step S422: Return the update quantity of the primary key of this batch.

[0118] In an embodiment of the present invention, when determining the number of updates, it can be determined based on the primary key update data of a preset number of historical batches adjacent to the current batch. The number of primary keys updated in each batch can be automatically calculated and updated (adaptively) in real time, which can reduce the risk of online accidents and solve the problem that the existing acquisition method requires manual and frequent modification of the number of primary key updates based on the usage of the primary key.

[0119] In order to solve the problem of massive waste of primary key resources caused by restarting and going online, based on the above embodiments, an embodiment of the present invention provides a method for obtaining a primary key. The method implements a processing flow through a new data storage structure of primary key usage records, so that the primary keys that are not used during each restart can be stored in a database (DB), and returned and continued to be used the next time the primary key is obtained, which can solve the problem of massive waste of primary key resources caused by restarting and going online. Figure 5 FIG. 1 is a flow chart of a method for obtaining a primary key according to an embodiment of the present invention. Figure 5 As shown, the method for obtaining the primary key may include the following steps S501 to S513.

[0120] Among them, the data storage structure of the primary key usage record is shown in the following table:

[0121] Field Name Field Type Is it nullable? Remark table_name varchar(100) no Storage table name used_quantity_begin bigint(20) no Minimum primary key value used used_quantity_end bigint(20) no Maximum value of the primary key used isMaster tinyint(4) no Is this the master record of the current table? isValid tinyint(4) no Data valid flag updateTime datetime no Update time

[0122] It is understandable that each table has a primary key using a master record and multiple non-master records. The master record is definitely valid, and some non-master records are valid and some are invalid. The used_quantity_begin of the master record is definitely = 0, and used_quantity_end has the same meaning as used_quantity. The non-master record records the available primary key value range through used_quantity_begin and used_quantity_end. The combined unique index is table_name+isMaster+isValid+used_quantity_begin.

[0123] Step S501: Obtain a valid non-primary record in the current available primary key list in chronological order.

[0124] Step S502: Determine whether the result of the obtained non-primary record is empty; if so, execute steps S503 to S505; otherwise, execute step S506.

[0125] Step S503: If the result of obtaining the non-master record is empty, obtain the master record corresponding to the specified data table.

[0126] Step S504: record the primary key of the updated quantity in the current primary record into the returned result, so as to obtain the primary key of the updated quantity according to the current primary record.

[0127] Step S505: Update the maximum value of the primary key used in the current primary record.

[0128] Step S506: If the result of the non-primary record is not empty, determine whether the difference between the maximum value of the primary key and the minimum value of the primary key in the non-primary record is greater than or equal to the update quantity of the primary key. If so, execute steps S507 to S510. Otherwise, execute steps S511 to S512.

[0129] Step S507: If the result of the non-primary record obtained is not empty, and the difference between the maximum value of the primary key and the minimum value of the primary key in the non-primary record is greater than or equal to the number of updates of the primary key, then the primary key records of the updated number are recorded in the returned result starting from the minimum value of the primary key, so as to obtain the updated number of primary keys starting from the minimum value of the primary key, and then execute steps S508 to S510.

[0130] Step S508: Determine whether there are any remaining primary keys in the current non-primary record; if so, execute step S510; otherwise, execute step S511.

[0131] Step S509: If there are any remaining primary keys in the current non-primary record, update the minimum value of the primary key of the current non-primary record and then return the result.

[0132] Step S510: If the current non-primary record has no remaining primary key, the current non-primary record is updated to an invalid record, and then the result is returned.

[0133] Step S511: If the difference between the maximum value and the minimum value of the primary key in the non-primary record is less than the number of primary key updates, the primary key of the update number is obtained according to the primary key value of the current non-primary record.

[0134] Step S512: Update the current non-master record to an invalid record. Then repeat step S501 until the non-master record meets the requirements.

[0135] The embodiment of the present invention designs a data storage structure for primary key usage records, so that the unused primary keys during each restart can be stored in a database (DB), and returned and continued to be used the next time the primary keys are obtained, which can solve the problem of large-scale waste of primary key resources caused by restarting and going online.

[0136] exist Figure 5 Based on the illustrated embodiment, in order to solve the problem of massive waste of primary key resources caused by restart and online, non-primary records can be generated according to relevant information of the remaining primary keys during restart or online to save the remaining primary keys into the database.

[0137] See also Figure 6 , constructing a non-primary record may specifically include the following steps: first, set the minimum value of the remaining primary key to used_quantity_begin; then set the maximum value of the remaining primary key to used_quantity_end; then set isMaster = 2-non-primary record. Then set the data table name, and finally save the remaining primary key to the database. It should be noted that the embodiment of the present invention does not specifically limit the setting order of each parameter.

[0138] Figure 7 is a schematic diagram of a module of a device for obtaining a primary key according to an embodiment of the present invention, see Figure 7 The primary key acquisition device 700 may specifically include the following modules:

[0139] A determination module 701 is used to determine the update quantity of the primary keys of this batch if the list balance in the current available primary key list reaches the update threshold, wherein the update quantity is determined according to the primary key update data of the historical batch;

[0140] The first acquisition module 702 is used to acquire the primary key usage record of the current available primary key list;

[0141] The second acquisition module 703 is used to acquire the update number of primary keys according to the primary key usage records of the current available primary key list if the primary key usage records of the current available primary key list are not empty, and update the primary key usage records.

[0142] Optionally, the primary key update data includes at least: the primary key update quantity of the historical batch and the primary key update time of the historical batch.

[0143] Optionally, the determining module 701 is further configured to:

[0144] Obtaining the first time when the primary key of a first historical batch is updated, where the first historical batch is the previous batch adjacent to the current batch;

[0145] If the first time is not empty, then calculating the second time taken for the first historical batch primary key to be consumed;

[0146] Obtaining a third time taken for the primary key of a second historical batch to be consumed, where the second historical batch is a previous batch adjacent to the first historical batch;

[0147] If the third time is not empty, obtaining the update quantity of the primary key of the first historical batch;

[0148] The update quantity of the primary keys of this batch is determined according to the second time, the third time and the update quantity of the primary keys of the first historical batch.

[0149] Optionally, the determining module 701 is further configured to:

[0150] calculating a ratio of the second time to the third time;

[0151] If the update quantity of the first historical batch primary key is not empty, the product of the update quantity of the first historical batch primary key and the ratio is determined as the update quantity of the primary key of this batch.

[0152] Optionally, the primary key usage record includes at least: a primary key usage master record and multiple non-primary records, wherein the primary record is used to indicate the number of used primary keys, and the non-primary records are used to indicate the available primary key value interval.

[0153] Optionally, the data storage structure of the primary key usage record includes at least the following information: storage table name, minimum primary key value used, maximum primary key value used, whether it is a primary record in the current available primary key list, data validity flag and update time.

[0154] Optionally, the second acquisition module 703 is further used to:

[0155] Get a valid non-primary record in the current available primary key list in chronological order;

[0156] If the result of obtaining non-primary records is empty, obtain the primary record corresponding to the current available primary key list;

[0157] The update number of primary keys is obtained according to the current primary record, and the maximum value of the primary keys used in the current primary record is updated.

[0158] Optionally, the second acquisition module 703 is further used to:

[0159] If the result of the obtained non-primary record is not empty, and the difference between the maximum value of the primary key and the minimum value of the primary key in the non-primary record is less than the update number of the primary key, then the update number of primary keys is obtained according to the primary key value of the current non-primary record, and the current non-primary record is updated to an invalid record; then the step of obtaining a valid non-primary record in the current available primary key list in chronological order is repeated until the non-primary record meets the requirements;

[0160] If the result of the obtained non-primary record is not empty, and the difference between the maximum value of the primary key and the minimum value of the primary key in the non-primary record is greater than or equal to the number of primary key updates, then the updated number of primary keys are obtained starting from the minimum value of the primary key; if there are any remaining primary keys in the current non-primary record, then the minimum value of the primary key of the current non-primary record is updated; if there are no remaining primary keys in the current non-primary record, then the current non-primary record is updated to an invalid record.

[0161] Optionally, the primary key acquisition device further includes:

[0162] The generation module is used to generate non-primary records according to the relevant information of the remaining primary keys when restarting or going online, so as to save the remaining primary keys into the database.

[0163] This implementation can automatically calculate and update (adaptively) the number of primary keys updated in each batch in real time, which can reduce the risk of online accidents and solve the problem that the existing acquisition method requires manual and frequent modification of the number of primary key updates based on the usage of the primary keys.

[0164] Figure 8 An exemplary system architecture 800 is shown to which the method or apparatus for obtaining a primary key according to an embodiment of the present invention may be applied.

[0165] like Figure 8 As shown, system architecture 800 may include terminal devices 801, 802, 803, a network 804, and a server 805. Network 804 is used to provide a medium for communication links between terminal devices 801, 802, 803 and server 805. Network 804 may include various connection types, such as wired, wireless communication links, or optical fiber cables, etc.

[0166] Users can use terminal devices 801, 802, 803 to interact with server 805 through network 804 to receive or send messages, etc. Various communication client applications can be installed on terminal devices 801, 802, 803. Terminal devices 801, 802, 803 include but are not limited to smart phones, tablet computers, laptop computers, desktop computers, etc.

[0167] It should be noted that the primary key acquisition method provided in the embodiment of the present invention is generally executed by the server 805 , and accordingly, the primary key acquisition device is generally disposed in the server 805 .

[0168] It should be understood that Figure 8 The number of terminal devices, networks and servers in the embodiment is only for illustration. Any number of terminal devices, networks and servers may be provided according to implementation requirements.

[0169] Reference below Fig. 9 , which shows a schematic diagram of the structure of a computer system 900 of a terminal device suitable for implementing an embodiment of the present invention. Fig. 9 The terminal device shown is only an example and should not bring any limitation to the functions and scope of use of the embodiments of the present invention.

[0170] like Fig. 9 As shown, the computer system 900 includes a central processing unit (CPU) 901, which can perform various appropriate actions and processes according to a program stored in a read-only memory (ROM) 902 or a program loaded from a storage part 908 into a random access memory (RAM) 903. In the RAM 903, various programs and data required for the operation of the system 900 are also stored. The CPU 901, the ROM 902, and the RAM 903 are connected to each other via a bus 904. An input / output (I / O) interface 905 is also connected to the bus 904.

[0171] The following components are connected to the I / O interface 905: an input section 906 including a keyboard, a mouse, etc.; an output section 907 including a cathode ray tube (CRT), a liquid crystal display (LCD), etc., and a speaker, etc.; a storage section 908 including a hard disk, etc.; and a communication section 909 including a network interface card such as a LAN card, a modem, etc. The communication section 909 performs communication processing via a network such as the Internet. A drive 910 is also connected to the I / O interface 905 as needed. A removable medium 911, such as a magnetic disk, an optical disk, a magneto-optical disk, a semiconductor memory, etc., is installed on the drive 910 as needed, so that a computer program read therefrom is installed into the storage section 908 as needed.

[0172] In particular, according to the embodiments disclosed in the present invention, the process described above with reference to the flowchart can be implemented as a computer software program. For example, the embodiments disclosed in the present invention include a computer program product, which includes a computer program carried on a computer-readable medium, and the computer program includes a program code for executing the method shown in the flowchart. In such an embodiment, the computer program can be downloaded and installed from the network through the communication part 909, and / or installed from the removable medium 911. When the computer program is executed by the central processing unit (CPU) 901, the above-mentioned functions defined in the system of the present invention are executed.

[0173] It should be noted that the computer-readable medium shown in the present invention may be a computer-readable signal medium or a computer-readable storage medium or any combination of the above two. The computer-readable storage medium may be, for example, but not limited to, an electrical, magnetic, optical, electromagnetic, infrared, or semiconductor system, device or device, or any combination of the above. More specific examples of computer-readable storage media may include, but are not limited to: an electrical connection with one or more wires, a portable computer disk, a hard disk, a random access memory (RAM), a read-only memory (ROM), an erasable programmable read-only memory (EPROM or flash memory), an optical fiber, a portable compact disk read-only memory (CD-ROM), an optical storage device, a magnetic storage device, or any suitable combination of the above. In the present invention, a computer-readable storage medium may be any tangible medium containing or storing a program that can be used by or in combination with an instruction execution system, device or device. In the present invention, a computer-readable signal medium may include a data signal propagated in a baseband or as part of a carrier wave, which carries a computer-readable program code. This propagated data signal may take a variety of forms, including but not limited to electromagnetic signals, optical signals, or any suitable combination of the above. Computer-readable signal media may also be any computer-readable medium other than computer-readable storage media, which may send, propagate or transmit a program for use by or in conjunction with an instruction execution system, apparatus or device. The program code contained on the computer-readable medium may be transmitted using any appropriate medium, including but not limited to: wireless, wire, optical cable, RF, etc., or any suitable combination of the above.

[0174] The flow chart and block diagram in the accompanying drawings illustrate the possible architecture, functions and operations of the system, method and computer program product according to various embodiments of the present invention. In this regard, each box in the flow chart or block diagram can represent a module, a program segment, or a part of a code, and the above-mentioned module, program segment, or a part of the code contains one or more executable instructions for realizing the specified logical function. It should also be noted that in some alternative implementations, the functions marked in the box can also occur in a different order from the order marked in the accompanying drawings. For example, two boxes represented in succession can actually be executed substantially in parallel, and they can sometimes be executed in the opposite order, depending on the functions involved. It should also be noted that each box in the block diagram or flow chart, and the combination of the boxes in the block diagram or flow chart can be implemented with a dedicated hardware-based system that performs a specified function or operation, or can be implemented with a combination of dedicated hardware and computer instructions.

[0175] As another aspect, the present invention also provides a computer-readable medium, which may be included in the device described in the above embodiment; or it may exist independently without being assembled into the device. The above computer-readable medium carries one or more programs, and when the above one or more programs are executed by a device, the device includes: if the list balance in the current available primary key list reaches the update threshold, then determine the update number of the primary key of this batch, and the update number is determined based on the primary key update data of the historical batch; obtain the primary key usage record of the current available primary key list; if the primary key usage record of the current available primary key list is not empty, then obtain the update number of primary keys based on the primary key usage record of the current available primary key list, and update the primary key usage record.

[0176] According to the technical solution of the embodiment of the present invention, the number of primary keys updated in each batch can be automatically calculated and updated (adaptively) in real time, which can reduce the risk of online accidents. It can solve the problem that the existing acquisition method requires manual and frequent modification of the number of primary key updates according to the usage of the primary keys. It can also solve the problem of large-scale waste of primary key resources caused by restarting and going online.

[0177] The above specific implementations do not constitute a limitation on the protection scope of the present invention. It should be understood by those skilled in the art that various modifications, combinations, sub-combinations and substitutions may occur depending on design requirements and other factors. Any modification, equivalent substitution and improvement made within the spirit and principle of the present invention shall be included in the protection scope of the present invention.

Claims

1. A method for obtaining a primary key, characterized in that: include: If the list balance in the current available primary key list reaches the update threshold, the update quantity of the primary keys of this batch is determined, and the update quantity is determined according to the primary key update data of the historical batch; Get the primary key usage record of the current available primary key list; If the primary key usage record of the currently available primary key list is not empty, the updated number of primary keys is obtained according to the primary key usage record of the currently available primary key list, and the primary key usage record is updated.

2. The method according to claim 1, characterized in that The primary key update data at least includes: the primary key update quantity of the historical batch and the primary key update time of the historical batch.

3. The method according to claim 2, characterized in that Determine the number of updates to the primary key in this batch including: Obtaining the first time when the primary key of a first historical batch is updated, where the first historical batch is the previous batch adjacent to the current batch; If the first time is not empty, then calculating the second time taken for the first historical batch primary key to be consumed; Obtaining a third time taken for the primary key of a second historical batch to be consumed, where the second historical batch is a previous batch adjacent to the first historical batch; If the third time is not empty, obtaining the update quantity of the primary key of the first historical batch; The update quantity of the primary keys of this batch is determined according to the second time, the third time and the update quantity of the primary keys of the first historical batch.

4. The method according to claim 3, characterized in that Determining the update quantity of the primary key of the current batch according to the second time, the third time, and the update quantity of the primary key of the first historical batch includes: calculating a ratio of the second time to the third time; If the update quantity of the first historical batch primary key is not empty, the product of the update quantity of the first historical batch primary key and the ratio is determined as the update quantity of the primary key of this batch.

5. The method according to claim 1, characterized in that The primary key usage record includes at least: a primary key usage primary record and multiple non-primary records, wherein the primary record is used to indicate the number of primary keys used, and the non-primary records are used to indicate the available primary key value interval.

6. The method according to claim 5, characterized in that The data storage structure of the primary key usage record includes at least the following information: storage table name, the minimum primary key value used, the maximum primary key value used, whether it is the primary record of the current available primary key list, data validity flag and update time.

7. The method according to claim 5, characterized in that Obtaining the updated number of primary keys and updating the primary key usage records includes: Get a valid non-primary record in the current available primary key list in chronological order; If the result of obtaining non-primary records is empty, obtain the primary record corresponding to the current available primary key list; The update number of primary keys is obtained according to the current primary record, and the maximum value of the primary keys used in the current primary record is updated.

8. The method according to claim 7, characterized in that Updating the primary key usage record of the available primary key list also includes: If the result of the obtained non-primary record is not empty, and the difference between the maximum value of the primary key and the minimum value of the primary key in the non-primary record is less than the update number of the primary key, then the update number of primary keys is obtained according to the primary key value of the current non-primary record, and the current non-primary record is updated to an invalid record; then the step of obtaining a valid non-primary record in the current available primary key list in chronological order is repeated until the non-primary record meets the requirements; If the result of the obtained non-primary record is not empty, and the difference between the maximum value of the primary key and the minimum value of the primary key in the non-primary record is greater than or equal to the number of primary key updates, then the updated number of primary keys are obtained starting from the minimum value of the primary key; if there are any remaining primary keys in the current non-primary record, then the minimum value of the primary key of the current non-primary record is updated; if there are no remaining primary keys in the current non-primary record, then the current non-primary record is updated to an invalid record.

9. The method according to claim 5, characterized in that Updating the primary key usage record of the available primary key list includes: When restarting or going online, a non-primary record is generated according to the relevant information of the remaining primary key to save the remaining primary key in the database.

10. A primary key acquisition device, characterized in that: include: A determination module, configured to determine the update quantity of the primary keys of this batch if the list balance in the current available primary key list reaches the update threshold, wherein the update quantity is determined based on the primary key update data of the historical batch; The first acquisition module is used to obtain the primary key usage record of the current available primary key list; The second acquisition module is used to acquire the updated number of primary keys according to the primary key usage record of the current available primary key list if the primary key usage record of the current available primary key list is not empty, and update the primary key usage record.

11. An electronic device, characterized in that: include: one or more processors; a storage device for storing one or more programs, When the one or more programs are executed by the one or more processors, the one or more processors implement the method according to any one of claims 1 to 9.

12. A computer readable medium having a computer program stored thereon, characterized in that: When the program is executed by a processor, the method according to any one of claims 1 to 9 is implemented.

Citation Information

Patent Citations

  • Method and system for processing data, and related device

    CN104239486A

  • Data processing method and device for slowly changing dimension table and electronic device

    CN109376148A