A database primary key generation method and device, electronic equipment and medium

By generating database primary keys using cached historical sequence numbers and difference timestamps, the problem of duplicate IDs in the snowflake algorithm is solved, improving generation efficiency and security.

CN114579540BActive Publication Date: 2026-02-06PINGAN YIQIANBAO E COMMERCE CO LTD
View PDF 3 Cites 0 Cited by

Patent Information

Application Number
CN202210206650.6
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2022-03-03
Publication Date
2026-02-06
Estimated Expiration
2042-03-03

AI Technical Summary

Technical Problem

Existing snowflake algorithms may cause duplicate IDs in database primary keys due to timestamps exceeding the number of digits, and may also cause duplicate IDs due to clock rollback.

Method used

By caching historical sequence numbers and historical difference timestamps, target sequence numbers and target timestamps are generated for use in generating database primary keys, reducing dependence on system time and improving generation efficiency.

Benefits of technology

It reduces the duplication rate of primary keys in the database, improves generation efficiency, and solves the problem of ID duplication in the snowflake algorithm.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN114579540B_ABST
    Figure CN114579540B_ABST
Patent Text Reader

Abstract

Embodiments of the present application disclose a database primary key generation method and device, electronic equipment and medium. The method comprises: in response to a database primary key generation request, obtaining a historical sequence number and a historical difference value timestamp corresponding to a historical database primary key generated last time in a local cache; determining a target sequence number according to the historical sequence number and determining a target timestamp according to the historical difference value timestamp; and generating the database primary key according to the target timestamp and the target sequence number. Through the technical scheme of the embodiments of the present application, it is not necessary to obtain system time every time the database primary key is generated, the generation efficiency of the database primary key is improved, and the problem of high repetition rate in the related database primary key generation mode is solved.
Need to check novelty before this filing date? Find Prior Art

Description

TECHNICAL FIELD

[0001] Embodiments of the present application relate to computer technology, and particularly relate to a database primary key generation method and device, electronic equipment and medium. BACKGROUND

[0002] Currently, the snowflake algorithm, a distributed database primary key generation scheme, is widely used in the industry. The algorithm obtains a numerical unique identifier (hereinafter referred to as ID) by reasonably allocating the meaning of bit representation on a limited 64-bit, greatly saving the storage space consumption of the database primary key.

[0003] However, the ID generated by the snowflake algorithm may be repeated with the previously generated ID due to the time exceeding the bit number of the timestamp in the snowflake algorithm. It may also be repeated with the previously generated ID due to the problem of clock rollback. Therefore, how to reduce the repetition rate of the ID is a problem to be solved at present. SUMMARY

[0004] Embodiments of the present application provide a database primary key generation method and device, electronic equipment and medium, which can solve the problem of database primary key repetition rate in related technologies.

[0005] In a first aspect, embodiments of the present application provide a database primary key generation method, comprising:

[0006] In response to a database primary key generation request, a historical sequence number corresponding to a historical database primary key generated last time in a local cache and a historical difference timestamp are obtained, wherein the historical difference timestamp is determined based on the difference between an initial timestamp and a reference timestamp;

[0007] A target sequence number is determined according to the historical sequence number, and a target timestamp is determined according to the historical difference timestamp;

[0008] The database primary key is generated according to the target timestamp and the target sequence number.

[0009] In a second aspect, embodiments of the present application also provide a database primary key generation device, the device comprising:

[0010] A response module is configured to, in response to a database primary key generation request, obtain a historical sequence number corresponding to a historical database primary key generated last time in a local cache and a historical difference timestamp, wherein the historical difference timestamp is determined based on the difference between an initial timestamp and a reference timestamp;

[0011] A determination module is configured to determine a target sequence number according to the historical sequence number, and determine a target timestamp according to the historical difference timestamp;

[0012] generating the database primary key according to the target time stamp and the target serial number.

[0013] In a third aspect, an electronic device is provided, and the electronic device comprises:

[0014] one or more processors;

[0015] a memory configured to store one or more programs,

[0016] When the one or more programs are executed by the one or more processors, the one or more processors implement the method for generating a database primary key according to any of the embodiments of the present application.

[0017] In a fourth aspect, a computer readable storage medium is provided, and the computer readable storage medium stores a computer program, and the computer program is executed by a processor to implement the method for generating a database primary key according to any of the embodiments of the present application.

[0018] The embodiments of the present application provide a method and device for generating a database primary key, an electronic device and a medium. The historical serial numbers and the historical difference time stamps used for generating the database primary key are cached. When the database primary key needs to be generated, the historical serial numbers and the historical difference time stamps are used to determine the target serial number and the target time stamp, and the database primary key is generated based on the determined target serial number and the target time stamp. The system time does not need to be obtained every time the database primary key is generated, the generation efficiency of the database primary key is improved, and the problem of high repetition rate in the related database primary key generation method is solved. BRIEF DESCRIPTION OF DRAWINGS

[0019] Figure 1 A flowchart of a method for generating a database primary key according to an embodiment of the present application is provided.

[0020] Figure 2 A flowchart of another method for generating a database primary key according to an embodiment of the present application is provided.

[0021] Figure 3 A flowchart of still another method for generating a database primary key according to an embodiment of the present application is provided.

[0022] Figure 4 A structural block diagram of a device for generating a database primary key according to an embodiment of the present application is provided.

[0023] Figure 5 A structural schematic diagram of an electronic device according to an embodiment of the present application is provided. DETAILED DESCRIPTION

[0024] The application will be described in further detail below with reference to the drawings and embodiments. It is to be understood that the specific embodiments described herein are merely illustrative of the application and are not to be used to limit the scope of the application. In addition, it should be noted that, for the sake of brevity, only the portions of the drawings that are needed to understand the application have been shown in the drawings.

[0025] Figure 1 A flowchart of a database primary key generation method provided by an embodiment of the application. The embodiment can be applied to a database primary key generation scenario. The method can be executed by a database primary key generation device. The device can be implemented by software and / or hardware and is generally configured in an electronic device. For example, the electronic device can be a server or the like. As shown in Figure 1 the method includes the following steps.

[0026] Step 110: In response to a database primary key generation request, a historical sequence number corresponding to a historical database primary key generated last time and a historical difference timestamp in a local cache are obtained.

[0027] The historical difference timestamp is determined based on a difference between an initial timestamp and a reference timestamp, and the historical difference timestamp is updated in a numerical increasing manner. The historical difference timestamp is a timestamp used when the database primary key is generated last time. The initial timestamp can be a local timestamp at a moment when an application program requesting to generate the database primary key is started, or can be a local timestamp in a case where a difference timestamp cached in a cache space is lost. The reference timestamp is a default timestamp, which can be set artificially.

[0028] The historical sequence number is a sequence number used when the database primary key is generated last time.

[0029] The database primary key refers to a combination of one column or multiple columns, the value of which can uniquely identify each row in a data table, and through which the entity integrity of the table can be forced. The database primary key is a numerical identifier. In the embodiment of the application, the database primary key is a binary string of 64 bits.

[0030] Specifically, the bits contained in the database primary key represent a sign bit, a timestamp, a sequence number and a machine code from high bits to low bits. Among them, 1 bit is used to represent the sign bit, 31 bits are used to represent the timestamp, 22 bits are used to represent the sequence number, and 10 bits are used to represent the machine code.

[0031] Table 1 is a composition table of a database primary key.

[0032]

[0033] It should be noted that, 31 bits are used to represent the timestamp constituting the database primary key, which can generate 231 The timestamp is a time stamp of a second. For each timestamp, the difference between the timestamp and the reference timestamp is stored instead of directly storing the timestamp, which can reduce the storage space occupation of single timestamp data, so that the external cache can cache more difference timestamps. In addition, the unit of the cached difference timestamp is accurate to the second, and when the server time has a millisecond-level gap, the difference timestamp will not be affected, which can avoid the generation of duplicate numbers when generating a database primary key.

[0034] The sequence number constituting the database primary key is represented by 22 bits, and 2 22 sequence numbers can be generated per second. That is, the upper limit value of the sequence number is 2 22 When the number value of the sequence number reaches the upper limit value of the sequence number, the sequence number returns to the initial sequence number, the historical difference timestamp is incremented by one unit of time, and the generation of the sequence number in the incremental manner is restarted. The initial sequence number can be a sequence number with all 22-bit values being zero.

[0035] The generation request is a request sent by an application program requesting the generation of a database primary key to the server. After receiving the generation request of the database primary key, the server queries the local cache to obtain the historical sequence number and the historical difference timestamp. It should be noted that after generating the database primary key each time, the sequence number and the timestamp used to generate the database primary key are cached, which are used when the next database primary key generation request is received.

[0036] Exemplarily, the server obtains the generation request of the database primary key, and queries the historical sequence number and the historical difference timestamp used to generate the last historical database primary key in the local cache.

[0037] Step 120, determining a target sequence number according to the historical sequence number and a target timestamp according to the historical difference timestamp.

[0038] The target sequence number is the sequence number required to be used to generate the current database primary key. The target sequence number is generated based on the historical sequence number or the initial sequence number. For example, the target sequence number can be a sequence number obtained by incrementing the number value of the historical sequence number by a specific value. If there is no historical sequence number, the initial sequence number is used as the target sequence number.

[0039] The target timestamp is a difference timestamp required for generating the database primary key currently. The target timestamp is generated based on the historical difference timestamp. In one case, the target timestamp can be the historical difference timestamp. In another case, the target timestamp can be the difference timestamp obtained by increasing the historical difference timestamp by a unit timestamp. In a special case, if the historical difference timestamp does not exist in the local cache, the historical difference timestamp in the external cache is obtained. The target timestamp can be the sum of the historical difference timestamp and a preset timestamp cache interval.

[0040] Specifically, the reference serial number is generated in a numerical increasing manner based on the number value of the historical serial number. If the number value of the reference serial number is less than or equal to the upper limit value of the serial number, the target serial number is generated according to the reference serial number. If the number value of the reference serial number is greater than the upper limit value of the serial number, the target serial number is generated according to the initial serial number corresponding to the bit of the serial number. The upper limit value of the serial number is the maximum serial number value that can be represented by the bit of the serial number.

[0041] Specifically, if the number value of the reference serial number is less than or equal to the upper limit value of the serial number, the historical difference timestamp is taken as the target timestamp. If the number value of the reference serial number is greater than the upper limit value of the serial number, the target timestamp is generated according to the historical difference timestamp and a preset timestamp interval. In the embodiment of the application, the target timestamp is determined in a local increasing manner, which greatly reduces the number of times of obtaining the system timestamp and improves the performance of the server in generating the database primary key.

[0042] In the embodiment of the application, for each target timestamp, the factor triggering the timestamp increment is no longer time, but whether the bit representing the serial number has been exhausted. In the embodiment of the application, after the server obtains the generation request of the database primary key, the historical serial number used for generating the historical database primary key last time in the cache is obtained. The sum of the number value of the historical serial number and a set value is taken as the reference serial number. The set value can be set artificially.

[0043] When the number value of the reference serial number does not exceed the maximum serial number value that can be represented by the bit of the serial number, the server determines that the bit representing the serial number has not been exhausted, takes the reference serial number as the target serial number, and takes the historical difference timestamp as the target timestamp.

[0044] When the number value of the reference serial number is greater than the maximum serial number value that can be represented by the bit of the serial number, the server determines that the bit representing the serial number has been exhausted, takes the initial serial number corresponding to the bit of the serial number as the target serial number, and obtains the target timestamp by increasing the historical difference timestamp.

[0045] Exemplarily, upon detecting a generation request of a database primary key, it is inquired whether the number value of the last generated historical serial number exists in the local cache space. If yes, the number value in the local cache space is increased by one to obtain the number value of the reference serial number. If not, an initial number value is obtained as the number value of the reference serial number. The initial number value is the number value when all bits of the serial number represent zero. It is judged whether the number value of the reference serial number exceeds the upper limit value of the serial number. If yes, the initial number value is taken as the target serial number, and the historical difference value timestamp is increased by one time unit. If not, the reference serial number is taken as the target serial number, and the historical difference value timestamp is taken as the target timestamp.

[0046] In a special case, the initial serial number is taken as the target serial number when the historical serial number and the historical difference value timestamp corresponding to the last generated historical database primary key do not exist in the local cache. The historical difference value timestamp corresponding to the machine code in the external cache is obtained, and the sum of the historical difference value timestamp and the timestamp cache interval is taken as the target timestamp. Specifically, the historical difference value timestamp corresponding to the machine code is obtained according to the key-value pair of the machine code and the historical difference value timestamp in the external cache. The timestamp cache interval is accumulated on the basis of the historical difference value timestamp to obtain the target timestamp. The timestamp cache interval is a time interval of the target timestamp for generating the database primary key which is set in advance.

[0047] In step S130, the database primary key is generated according to the target timestamp and the target serial number.

[0048] Exemplarily, the database primary key is generated by combining the sign bit, the target timestamp, the target serial number and the machine code. The sign bit is used to represent the sign of the database primary key. Generally, the value of the sign bit is 0 by default, which is used to represent that the database primary key is a positive integer. The machine code is used to identify the server for generating the database primary key. In one case, the machine code is a machine identification, in which case one machine code represents one server. In another case, the machine code includes a cluster identification (or a machine room identification) and a machine identification. In this case, one machine code represents one server in xx cluster (or xx machine room).

[0049] Specifically, before the serial number is exhausted, the target timestamp is the same as the historical difference timestamp adopted by the last time the database primary key is generated, and after the serial number is exhausted, the target timestamp is determined by incrementing the historical difference timestamp. Before the serial number is exhausted, the target serial number is determined by incrementing the historical serial number. When the serial number is exhausted, the target serial number is determined by the initial serial number. According to the above-mentioned order from high bit to low bit, the bits of the symbol bit, the target timestamp, the target serial number and the machine code are combined to obtain the database primary key. The database primary key is generated by using the above-mentioned combination order, which solves the problem that malicious users maliciously crawl data according to the generation rule of the serial number due to the generation mode of the incrementing serial number, and guarantees the security of the interface or data.

[0050] The technical scheme of the embodiment generates the database primary key based on the determined target serial number and target timestamp, realizes that the system time does not need to be acquired each time the database primary key is generated, improves the generation efficiency of the database primary key, and solves the problem of high repetition rate in the related database primary key generation mode.

[0051] On the basis of the above technical scheme, the steps of the method are further described. Figure 2 The flowchart of another database primary key generation method provided by the embodiment is shown as Figure 2 The method comprises the following steps.

[0052] In step 201, when the generation request of the database primary key is acquired for the first time, the current local timestamp is taken as the initial timestamp, the difference timestamp between the initial timestamp and the reference timestamp is calculated, and the target timestamp is generated according to the difference timestamp.

[0053] In one case, the server acquires the current system time as the local timestamp. The unit of the local timestamp is millisecond, and after the difference timestamp between the local timestamp and the reference timestamp is calculated, the unit conversion of the difference timestamp is performed, the unit of the difference timestamp is accurate to second, and the target timestamp is obtained. In this way, when the server time appears a slight gap of millisecond, the difference timestamp will not be affected, and the repeated number in the generation of the database primary key can be avoided.

[0054] In step 202, the database primary key is generated according to the initial serial number and the target timestamp.

[0055] In step 203, the target serial number and the target timestamp are stored in the local cache as the historical serial number and the historical difference timestamp.

[0056] Step 204, determine the time interval between the current time and the time when the last time stamp is stored to the external cache, if the time interval meets the preset time stamp cache interval, obtain the machine code and the historical difference time stamp in the local cache.

[0057] Wherein, the current time is the system time of the current server.

[0058] Step 205, store the machine code and the historical difference time stamp in the form of key-value pair to the external cache with the machine code as the key value.

[0059] It should be noted that, in order to prevent the problem of database primary key duplication caused by server downtime or restart, according to the preset time stamp cache interval, the machine code and the target time stamp currently used to generate the database primary key are stored in the form of key-value pair to the external cache with the machine code as the key value. Wherein, the external cache is provided by an external storage device.

[0060] Considering that the time interval of server recovery may be relatively short, which may fall within the cache interval time, resulting in the stored difference time stamp plus the cache interval time exceeding the next expected time stamp, therefore, the preset time stamp cache interval needs to be set to a suitable time according to the application scenario.

[0061] Step 206, when the request for generating the database primary key is obtained again, it is judged whether the historical sequence number and the historical difference time stamp corresponding to the last generated historical database primary key exist in the local cache, if yes, step 207 is executed, otherwise step 209 is executed.

[0062] In one case, after generating the database primary key according to the target time stamp and the target sequence number each time, the target time stamp and the target sequence number are cached in the local cache. Therefore, the historical sequence number and the historical difference time stamp corresponding to the last generated historical database primary key exist in the local cache.

[0063] In a special case, after the server downtime or restart, the historical sequence number and the historical difference time stamp corresponding to the last generated historical database primary key in the local cache are erased. In this case, the historical sequence number and the historical difference time stamp corresponding to the last generated historical database primary key in the local cache are lost, therefore, the historical sequence number and the historical difference time stamp corresponding to the last generated historical database primary key do not exist in the local cache.

[0064] Step 207, obtain the historical sequence number and the historical difference time stamp corresponding to the last generated historical database primary key in the local cache.

[0065] Exemplarily, a historical serial number used for generating a last database primary key in the local cache and a historical difference timestamp are acquired.

[0066] Step 208, determining a target serial number according to the historical serial number and determining a target timestamp according to the historical difference timestamp, and performing step 211.

[0067] Exemplarily, the target serial number is generated by incrementing the historical serial number before the serial number corresponding to the current historical difference timestamp is exhausted. The current historical difference timestamp is taken as the target timestamp before the serial number corresponding to the current historical difference timestamp is exhausted. If the serial number corresponding to the current historical difference timestamp is exhausted, the target serial number is determined based on the initial serial number, and the target timestamp is calculated by incrementing the historical difference timestamp. The embodiment of the application calculates the target timestamp by the historical difference timestamp, and does not acquire the system time every time the database primary key is generated, thereby improving the generation efficiency of the database primary key.

[0068] Specifically, when the application is started, the server acquires the current local timestamp as the initial timestamp, calculates the difference timestamp between the initial timestamp and the reference timestamp, and the unit of the difference timestamp is second. The server determines the target serial number by incrementing the serial number locally in response to the generation request of the database primary key sent by the application, until the serial number of 22 bits corresponding to the current difference timestamp is exhausted, and then the current difference timestamp is incremented by one unit timestamp, the serial number is restored to the initial serial number, and the generation of the serial number is restarted. In this way, the relationship between the serial number generation and the timestamp is weakened, and the problem that the serial number is wasted due to the strong correlation between the serial number and the timestamp and the change of the timestamp before the serial number is exhausted is avoided, the use rate of the serial number is improved, and the number of available serial numbers is increased. In addition, the system time does not need to be read every time the database primary key is generated, the performance of the server for generating the database primary key is improved, and the problem of database primary key repetition caused by clock rollback is also avoided to some extent.

[0069] Step 209, determining the target serial number according to the initial serial number, wherein the initial serial number is the serial number when all bits of the serial number are zero.

[0070] Step 210, acquiring the historical difference timestamp corresponding to the machine code in the external cache, and taking the sum of the historical difference timestamp and the timestamp cache interval as the target timestamp.

[0071] In the event of a server crash or restart, the previously cached historical difference timestamp can be retrieved from external storage based on the machine code. Using the sum of the previously cached historical difference timestamp and the preset timestamp cache interval as the target timestamp ensures minimal wasted data.

[0072] In a special case, if historical timestamp differences in external storage are lost due to damage to an external cache device, the server will use the current system time as the local timestamp. The local timestamp is determined based on the server's clock. After converting the local timestamp to seconds, the difference between the local timestamp and the base timestamp is used as the target timestamp difference.

[0073] Step 211: Generate the database primary key based on the target timestamp and the target sequence number.

[0074] In this embodiment of the invention, by reducing the dependence of database primary key generation on time and converting the unit of timestamp to seconds, the probability of duplicate database primary keys is reduced, effectively improving the problem of duplicate database primary keys.

[0075] Based on the above technical solutions, a specific method for generating database primary keys is provided. Figure 3 A flowchart illustrating another method for generating a database primary key provided by an embodiment of the present invention is shown below. Figure 3 As shown, the method includes:

[0076] Step 310: Obtain the database primary key generation request.

[0077] Step 320: Determine whether the historical sequence number corresponding to the historical database primary key generated last time exists in the local cache. If it does, proceed to step 330; otherwise, proceed to step 340.

[0078] Step 330: Calculate the result of adding 1 to the value of the historical sequence number, and use it as the target sequence number. Then proceed to step 350.

[0079] Step 340: Obtain the initial sequence number representing the bits of the sequence number, and use it as the target sequence number.

[0080] In this embodiment of the invention, the initial sequence number is the sequence number when all the bits representing the sequence number are 0.

[0081] Step 350: Determine whether the value of the target sequence number exceeds the upper limit of the sequence number. If yes, proceed to step 360; otherwise, proceed to step 370.

[0082] Step 360, the target serial number is restored to the initial serial number, and the operation result of the history difference timestamp plus 1 is calculated as the target timestamp, and step 370 is executed.

[0083] Step 370, the database primary key is constructed according to the target symbol bit, the target timestamp, the target serial number and the machine code.

[0084] In the embodiment of the application, the number of serial numbers available for generating the database primary key is increased by using the serial number corresponding to each difference timestamp, and nearly 100,000 serial numbers per second can be added, so that the repetition rate of the database primary key is reduced.

[0085] Figure 4 A structural block diagram of a database primary key generation device provided by the embodiment of the application is provided. The device is used to execute the database primary key generation method of any embodiment of the application, and solves the problem of the repetition rate of the database primary key by executing the method. The device can be realized by software and / or hardware, and is usually configured in an electronic device. For example, the electronic device can be a server.

[0086] As shown in Figure 4 The database primary key generation device 400 includes a response module 410, a determination module 420 and a generation module 430.

[0087] The response module 410 is used to acquire the history serial number and the history difference timestamp corresponding to the last generated history database primary key in the local cache in response to the generation request of the database primary key, wherein the history difference timestamp is determined based on the difference between the initial timestamp and the reference timestamp.

[0088] The determination module 420 is used to determine the target serial number according to the history serial number, and determine the target timestamp according to the history difference timestamp.

[0089] The generation module 430 is used to generate the database primary key according to the target timestamp and the target serial number.

[0090] The embodiment of the application provides a database primary key generation device, which buffers the history serial number and the history difference timestamp used for generating the database primary key, determines the target serial number and the target timestamp by using the history serial number and the history difference timestamp when the database primary key needs to be generated, generates the database primary key based on the determined target serial number and target timestamp, realizes that the system time does not need to be acquired every time the database primary key is generated, improves the generation efficiency of the database primary key, and solves the problem of high repetition rate in the related database primary key generation mode.

[0091] Optionally, the device further includes:

[0092] The cache module is configured to store the target serial number and the target timestamp into a local cache as the historical serial number and the historical difference timestamp after generating the database primary key according to the target timestamp and the target serial number; determine a time interval between a current time and a time of storing a last timestamp into an external cache, and if the time interval meets a preset timestamp cache interval, acquire a machine code and the historical difference timestamp in the local cache; and store the machine code and the historical difference timestamp in a form of a key-value pair into the external cache with the machine code as a key value.

[0093] Optionally, the determining module 420 is specifically configured to:

[0094] determine a target serial number according to an initial serial number when the historical serial number and the historical difference timestamp corresponding to a last generated historical database primary key do not exist in the local cache, where the initial serial number is a serial number with all bits of the serial number being zero;

[0095] acquire the historical difference timestamp corresponding to the machine code in the external cache, and take a sum of the historical difference timestamp and the timestamp cache interval as a target timestamp.

[0096] Optionally, the determining module 420 is specifically configured to:

[0097] generate a reference serial number in a numerical value increment manner based on a number value of the historical serial number;

[0098] if the number value of the reference serial number is less than or equal to an upper limit value of a serial number, generate a target serial number according to the reference serial number, where the upper limit value of the serial number is a maximum serial number value that can be represented by bits of the serial number;

[0099] if the number value of the reference serial number is greater than the upper limit value of the serial number, generate a target serial number according to an initial serial number corresponding to the bits of the serial number.

[0100] Optionally, the determining module 420 is specifically configured to:

[0101] if the number value of the reference serial number is less than or equal to an upper limit value of a serial number, take the historical difference timestamp as a target timestamp;

[0102] if the number value of the reference serial number is greater than the upper limit value of the serial number, generate a target timestamp according to the historical difference timestamp and a preset timestamp interval.

[0103] Optionally, the database primary key comprises bits from high to low, which represent a sign bit, a timestamp, a serial number and a machine code, wherein the sign bit is represented by 1 bit, the timestamp is represented by 31 bits, the serial number is represented by 22 bits, and the machine code is represented by 10 bits.

[0104] Optionally, the generating module 430 is specifically configured to:

[0105] combine the sign bit, the target timestamp, the target serial number and the machine code to generate the database primary key.

[0106] The database primary key generation apparatus provided by the embodiment of the present application can execute the database primary key generation method provided by any of the embodiments of the present application, and has the corresponding function modules and beneficial effects of executing the method.

[0107] Figure 5 A structural schematic diagram of an electronic device provided by the embodiment of the present application is shown in FIG. 5. Figure 5 As shown in FIG. 5, the electronic device 500 includes a processor 510 and a memory 520; the number of processors 510 in the electronic device can be one or more, Figure 5 and the processor 510 in the electronic device is taken as an example; the processor 510 and the memory 520 in the electronic device can be connected through a bus or other means, Figure 5 and the connection through the bus is taken as an example.

[0108] The memory 520, as a kind of computer readable storage medium, can be used to store software programs, computer executable programs and modules, such as program instructions / modules (for example, the response module 410, the determination module 420 and the generation module 430 in the database primary key generation apparatus) corresponding to the database primary key generation method in the embodiment of the present application. The processor 510 executes various function applications and data processing of the electronic device by running the software programs, instructions and modules stored in the memory 520, that is, the database primary key generation method described above is realized.

[0109] The memory 520 can mainly include a program storage area and a data storage area, wherein the program storage area can store an operating system and at least one application program required by a function; the data storage area can store data created according to the use of the terminal, etc. In addition, the memory 520 can include a high-speed random access memory, and can also include a non-volatile memory, such as at least one magnetic disk storage device, a flash memory device, or other non-volatile solid-state memory device. In some examples, the memory 520 can further include a memory remotely arranged with respect to the processor 510, which can be connected to the electronic device through a network. Examples of the above-mentioned network include but are not limited to the Internet, an intranet, a local area network, a mobile communication network and a combination thereof.

[0110] The embodiment of the present application also provides a storage medium containing computer executable instructions, which are used for executing a database primary key generation method when executed by a computer processor, and the method comprises the following steps of:

[0111] In response to a database primary key generation request, a historical sequence number corresponding to a historical database primary key generated last time in a local cache and a historical difference timestamp are acquired, wherein the historical difference timestamp is determined based on a difference between an initial timestamp and a reference timestamp;

[0112] A target sequence number is determined according to the historical sequence number, and a target timestamp is determined according to the historical difference timestamp;

[0113] The database primary key is generated according to the target timestamp and the target sequence number.

[0114] Of course, the computer executable instructions of the storage medium provided by the embodiment of the present application are not limited to the method operations described above, and can also perform related operations in the database primary key generation method provided by any embodiment of the present application.

[0115] Through the above description of the embodiments, those skilled in the art can clearly understand that the present application can be realized by means of software and necessary general hardware, and of course can also be realized by hardware, but in many cases the former is a better embodiment. Based on such understanding, the technical solutions of the present application can be embodied in the form of a software product, and the computer software product can be stored in a computer readable storage medium, such as a floppy disk, a read-only memory (ROM), a random access memory (RAM), a FLASH, a hard disk or an optical disk, etc., and includes a number of instructions to make an electronic device (which can be a personal computer, a server, or a network device, etc.) execute the method described in each embodiment of the present application.

[0116] It is worth noting that in the above embodiment of the database primary key generation device, each unit and module included is only divided according to functional logic, but is not limited to the above division, as long as the corresponding functions can be realized; in addition, the specific names of each functional unit are only for easy mutual differentiation, and do not limit the protection scope of the present application.

[0117] Note that the above merely describes preferred embodiments of the present application and the principles of the technology applied. Those skilled in the art will understand that the present application is not limited to the specific embodiments described herein, and that various obvious changes, modifications and updates can be made without departing from the scope of the present application. Therefore, although the present application has been described in detail through the above embodiments, the present application is not limited to the above embodiments, and can include more other equivalent embodiments without departing from the concept of the present application, and the scope of the present application is determined by the scope of the claims.

Claims

1. A method of generating a database primary key, characterized by, The method comprises the following steps: in response to a generation request of a database primary key, obtaining a historical serial number corresponding to a last generated historical database primary key in a local cache and a historical difference timestamp, wherein the historical difference timestamp is determined based on a difference between an initial timestamp and a reference timestamp, the initial timestamp is a local timestamp at a starting time of an application program requesting generation of the database primary key, or a local timestamp in a case where a cached difference timestamp in a cache space is lost; determining a target serial number according to the historical serial number and determining a target timestamp according to the historical difference timestamp; generating the database primary key according to the target timestamp and the target serial number; wherein the determining of the target serial number according to the historical serial number and the determining of the target timestamp according to the historical difference timestamp comprise: generating a reference serial number in a numerical incremental manner based on a number value of the historical serial number; if the number value of the reference serial number is less than or equal to a serial number upper limit value, taking the reference serial number as the target serial number and taking the historical difference timestamp as the target timestamp, wherein the serial number upper limit value is a maximum serial number value that can be represented by a bit representing a serial number; if the number value of the reference serial number is greater than the serial number upper limit value, generating an initial serial number corresponding to the bit representing a serial number as the target serial number and incrementing the historical difference timestamp to obtain the target timestamp.

2. The method of claim 1, wherein, after the generating of the database primary key according to the target timestamp and the target serial number, further comprising: storing the target serial number and the target timestamp to the local cache as the historical serial number and the historical difference timestamp; determining a time interval between a current time and a time at which a last stored timestamp is stored to an external cache, and if the time interval satisfies a preset timestamp cache interval, obtaining a machine code and the historical difference timestamp in the local cache; storing the machine code and the historical difference timestamp in a form of a key-value pair to the external cache by taking the machine code as a key value.

3. The method of claim 2, wherein, when the historical serial number and the historical difference timestamp corresponding to a last generated historical database primary key do not exist in the local cache, the determining of the target serial number according to the historical serial number and the determining of the target timestamp according to the historical difference timestamp comprise: determining a target serial number according to an initial serial number, wherein the initial serial number is a serial number when all bits representing a serial number are zero; obtaining the historical difference timestamp corresponding to the machine code in the external cache and taking a sum of the historical difference timestamp and the timestamp cache interval as a target timestamp.

4. The method of claim 1, wherein, The database primary key comprises bits representing a sign bit, a timestamp, a serial number and a machine code from high bits to low bits, wherein 1 bit is used to represent the sign bit, 31 bits are used to represent the timestamp, 22 bits are used to represent the serial number, and 10 bits are used to represent the machine code.

5. The method of claim 4, wherein, the generating of the database primary key according to the target timestamp and the target serial number comprises: combining the symbol bit, the target timestamp, the target serial number and the machine code to generate the database primary key.

6. An apparatus for generating a database primary key, characterized by comprising: The method comprises the following steps: In response to a request for generating a database primary key, a response module acquires a historical serial number and a historical difference timestamp corresponding to a historical database primary key generated last time in a local cache, wherein the historical difference timestamp is determined based on a difference between an initial timestamp and a reference timestamp, the initial timestamp being a local timestamp at a starting moment of an application program requesting to generate a database primary key, or a local timestamp in a case where a cached difference timestamp is lost; A determination module determines a target serial number according to the historical serial number and a target timestamp according to the historical difference timestamp; A generation module generates the database primary key according to the target timestamp and the target serial number; The determination of the target serial number according to the historical serial number and the determination of the target timestamp according to the historical difference timestamp comprise the following steps: A reference serial number is generated in a numerical incremental manner based on a number value of the historical serial number; If the number value of the reference serial number is less than or equal to an upper limit value of a serial number, the reference serial number is taken as the target serial number and the historical difference timestamp is taken as the target timestamp, wherein the upper limit value of the serial number is a maximum serial number value that can be represented by a bit representing a serial number; If the number value of the reference serial number is greater than the upper limit value of the serial number, an initial serial number corresponding to the bit representing a serial number is taken as the target serial number and a target timestamp is obtained by incrementing the historical difference timestamp.

7. An electronic device, comprising: The electronic device comprises: one or more processors; a memory 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 for generating a database primary key according to any one of claims 1-5.

8. A computer-readable storage medium having stored thereon a computer program, characterized in that, The computer program is executed by the processor to implement the method for generating a database primary key according to any one of claims 1-5.

Citation Information

Patent Citations

  • Method and apparatus for generating global unique ID

    CN109309736A

  • ID generator and ID generation method of multi-domain machine room

    CN111611247A

  • Method for adjusting time stamp offset and packet- transmitting device using the same

    JP2001168883A