CN111708831A - Global primary key generation method and device - Google Patents

Global primary key generation method and device Download PDF

Info

Publication number
CN111708831A
CN111708831A CN202010398983.4A CN202010398983A CN111708831A CN 111708831 A CN111708831 A CN 111708831A CN 202010398983 A CN202010398983 A CN 202010398983A CN 111708831 A CN111708831 A CN 111708831A
Authority
CN
China
Prior art keywords
data
global
server
digit
primary key
Prior art date
Legal status (The legal status is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the status listed.)
Pending
Application number
CN202010398983.4A
Other languages
Chinese (zh)
Inventor
韩立伟
Current Assignee (The listed assignees may be inaccurate. Google has not performed a legal analysis and makes no representation or warranty as to the accuracy of the list.)
Beijing QIYI Century Science and Technology Co Ltd
Original Assignee
Beijing QIYI Century Science and Technology Co Ltd
Priority date (The priority date is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the date listed.)
Filing date
Publication date
Application filed by Beijing QIYI Century Science and Technology Co Ltd filed Critical Beijing QIYI Century Science and Technology Co Ltd
Priority to CN202010398983.4A priority Critical patent/CN111708831A/en
Publication of CN111708831A publication Critical patent/CN111708831A/en
Pending legal-status Critical Current

Links

Images

Classifications

    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F16/00Information retrieval; Database structures therefor; File system structures therefor
    • G06F16/20Information retrieval; Database structures therefor; File system structures therefor of structured data, e.g. relational data
    • G06F16/27Replication, distribution or synchronisation of data between databases or within a distributed database system; Distributed database system architectures therefor
    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F16/00Information retrieval; Database structures therefor; File system structures therefor
    • G06F16/20Information retrieval; Database structures therefor; File system structures therefor of structured data, e.g. relational data
    • G06F16/22Indexing; Data structures therefor; Storage structures
    • G06F16/2282Tablespace storage structures; Management thereof
    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F16/00Information retrieval; Database structures therefor; File system structures therefor
    • G06F16/20Information retrieval; Database structures therefor; File system structures therefor of structured data, e.g. relational data
    • G06F16/23Updating
    • G06F16/2308Concurrency control
    • G06F16/2315Optimistic concurrency control
    • G06F16/2322Optimistic concurrency control using timestamps

Abstract

The invention provides a global primary key generation method, a global primary key generation device, electronic equipment and a computer readable storage medium, and relates to the field of data processing. A server applied in a distributed database; the method comprises the following steps: receiving a data insertion instruction, and recording the receiving time of the data insertion instruction; the data insertion instructions include: service identity data; determining a timestamp based on the reception time; randomly generating a serial number; and generating a global main key to be inserted corresponding to the data insertion instruction according to the identity of the server, the timestamp, the service identity data and the sequence number. The embodiment of the invention is unique even under the condition of large concurrency. Meanwhile, in the process of generating the global primary key, multi-step operations such as generation and copying are not needed, the influence of other factors is avoided, the generation mode is simple, and the generation efficiency is high.

Description

Global primary key generation method and device
Technical Field
The present invention relates to the field of data processing, and in particular, to a method and an apparatus for generating a global primary key, an electronic device, and a computer-readable storage medium.
Background
In the distributed database, the global primary key corresponding to each data record stored in each sub-table needs to be unique in the whole distributed database for query, statistics, sorting and the like.
At present, in a distributed database, the generation modes of the global primary key corresponding to each data record in each sub-table mainly include the following: firstly, a plurality of global main keys are self-increased in one table, and then the global main keys are copied into the sublist; second, the current time is directly used as a global primary key, and the like.
However, in the first aspect, when the amount of concurrency is large, the efficiency is low, and in the second aspect, when the amount of concurrency is large, the global primary keys corresponding to the respective data records overlap.
Disclosure of Invention
Embodiments of the present invention provide a global primary key generation method, an apparatus, an electronic device, and a computer-readable storage medium, so as to solve the problems of low generation efficiency and duplication of global primary keys corresponding to data records in each sub-table in a distributed database under the condition of a large concurrency amount. The specific technical scheme is as follows:
in a first aspect of the present invention, a global primary key generation method is provided, which is applied to a server in a distributed database; the method comprises the following steps:
receiving a data insertion instruction, and recording the receiving time of the data insertion instruction; the data insertion instructions include: service identity data;
determining a timestamp based on the reception time;
randomly generating a serial number;
and generating a global main key to be inserted corresponding to the data insertion instruction according to the identity of the server, the timestamp, the service identity data and the sequence number.
Optionally, the generating a global primary key to be inserted corresponding to the data insertion instruction according to the identity of the server, the timestamp, the service identity data, and the sequence number includes:
and combining the timestamp, the identity identifier of the server, the service identity data and the sequence number in sequence to obtain a global main key to be inserted corresponding to the data insertion instruction.
Optionally, the sequentially combining the timestamp, the identity of the server, the service identity data, and the sequence number to obtain the global primary key to be inserted corresponding to the data insertion instruction further includes:
and determining the total number of bits of the global main key to be inserted corresponding to the data insertion instruction according to the service life of the server, the identity identification of the server in the distributed database, the identity data of the service to be recorded in the distributed database and the concurrency of the same service identity data by the server in the same timestamp.
Optionally, the determining, based on the service life of the server, the identity of the server in the distributed database, the identity data of the service to be recorded in the distributed database, and the concurrency of the same service identity data by the server in the same timestamp, a total number of bits of the global primary key to be inserted corresponding to the data insertion instruction includes:
determining a first digit number occupied by the timestamp in the global primary key to be inserted based on the service life of the server;
determining the identity of the server and the second number of digits occupied in the global primary key to be inserted based on the binary representation of the identity of the server in the distributed database;
determining a third digit occupied by the service identity data in the global primary key to be inserted based on a binary representation of the identity data of the service to be recorded in the distributed database;
determining the fourth digit of the serial number in the global main key to be inserted based on the concurrency of the server in the same timestamp and the same service identity data; and the sum of the first digit, the second digit, the third digit and the fourth digit is equal to the total digit of the global primary key to be inserted.
Optionally, the distributed database includes a plurality of data tables, each data table corresponds to different service identity data, and the method further includes:
inserting the data to be inserted corresponding to the data insertion instruction and the global main key to be inserted corresponding to the data insertion instruction into a data table corresponding to the service identity data;
receiving a data query instruction; the data query instruction comprises: a global primary key to be queried;
searching target data corresponding to the global main key to be inquired in a data table corresponding to the service identity data in the global main key to be inquired; the global main key to be inserted of the target data is matched with the global main key to be inquired;
and returning the target data.
In a second aspect of the present invention, there is provided a global primary key generation apparatus, applied to a server in a distributed database; the device comprises:
the first receiving module is used for receiving a data insertion instruction and recording the receiving time of the data insertion instruction; the data insertion instructions include: service identity data;
a timestamp determination module to determine a timestamp based on the receive time;
the serial number generation module is used for randomly generating a serial number;
and the global primary key generation module is used for generating a global primary key to be inserted corresponding to the data insertion instruction according to the identity of the server, the timestamp, the service identity data and the serial number.
Optionally, the global primary key generating module includes:
and the global primary key generating unit is used for combining the timestamp, the identity identifier of the server, the service identity data and the serial number in sequence to obtain the to-be-inserted global primary key corresponding to the data insertion instruction.
Optionally, the apparatus further includes:
and the total digit determining module is used for determining the total digit of the global main key to be inserted corresponding to the data insertion instruction according to the service life of the server, the identity identification of the server in the distributed database, the identity data of the service to be recorded in the distributed database and the concurrency of the same service identity data by the server in the same timestamp.
Optionally, the total number of bits determining module includes:
the first digit determining unit is used for determining a first digit occupied by the timestamp in the global main key to be inserted based on the service life of the server;
a second digit determining unit, configured to determine, based on a binary representation of an identity of a server in the distributed database, a second digit occupied by the identity of the server in the global primary key to be inserted;
a third digit determining unit, configured to determine, based on a binary representation of identity data of a service to be recorded in the distributed database, a third digit occupied by the service identity data in the global primary key to be inserted;
a fourth digit determining unit, configured to determine, based on the concurrency of the same service identity data in the same timestamp by the server, a fourth digit occupied by the serial number in the global primary key to be inserted; and the sum of the first digit, the second digit, the third digit and the fourth digit is equal to the total digit of the global primary key to be inserted.
Optionally, the distributed database includes a plurality of data tables, each data table corresponds to different service identity data, and the apparatus further includes:
the inserting module is used for inserting the data to be inserted corresponding to the data inserting instruction and the global main key to be inserted corresponding to the data inserting instruction into the data table corresponding to the service identity data;
the second receiving module is used for receiving a data query instruction; the data query instruction comprises: a global primary key to be queried;
the searching module is used for searching target data corresponding to the global main key to be inquired in a data table corresponding to the service identity data in the global main key to be inquired; the global main key to be inserted of the target data is matched with the global main key to be inquired;
and the return module is used for returning the target data.
In yet another aspect of the present invention, there is also provided a computer-readable storage medium having stored therein instructions, which when run on a computer, cause the computer to execute any one of the above-described global primary key generation methods.
In yet another aspect of the present invention, there is also provided a computer program product containing instructions which, when run on a computer, cause the computer to perform any of the above-described global primary key generation methods.
The embodiment of the invention provides a global primary key generation method and a device, which are applied to a server in a distributed database, wherein the method comprises the following steps: receiving a data insertion instruction, and recording the receiving time of the data insertion instruction; the data insertion instructions include: service identity data; determining a timestamp based on the reception time; randomly generating a serial number; and generating a global main key to be inserted corresponding to the data insertion instruction according to the identity of the server, the timestamp, the service identity data and the sequence number. The embodiment of the invention can solve the problems of low generation efficiency and repetition of the global primary key corresponding to each data record in each sub-table in the distributed database under the condition of large concurrency.
In the embodiment of the invention, the identity identification of the server can distinguish that a certain piece of data is inserted by the server subsequently, the timestamp can distinguish the receiving time of the piece of data, the service identity data can distinguish the service type to which the piece of data belongs, the randomly generated serial number can distinguish different data received by the same server aiming at the same service type at the same time, and the server still has uniqueness according to the global main key formed by the four parts even under the condition of larger concurrency. Meanwhile, in the process of generating the global primary key, multi-step operations such as generation and copying are not needed, the influence of other factors is avoided, the generation mode is simple, and the generation efficiency is high.
Drawings
In order to more clearly illustrate the embodiments of the present invention or the technical solutions in the prior art, the drawings used in the description of the embodiments or the prior art will be briefly described below.
FIG. 1 is a flowchart illustrating steps of a method for generating a global primary key according to an embodiment of the present invention;
FIG. 2 is a flowchart illustrating steps of another method for generating global primary keys according to an embodiment of the present invention;
FIG. 3 is a flowchart illustrating a process of determining a total number of bits to be inserted into a global primary key according to an embodiment of the present invention;
FIG. 4 is a diagram of a global primary key in an embodiment of the present invention;
FIG. 5 is a block diagram of a data query device according to an embodiment of the present invention;
FIG. 6 is another data query device in an embodiment of the invention;
fig. 7 is a schematic structural diagram of an electronic device in an embodiment of the present invention.
Detailed Description
The technical solutions in the embodiments of the present invention will be described below with reference to the drawings in the embodiments of the present invention.
Referring to fig. 1, fig. 1 is a flowchart illustrating steps of a global primary key generation method according to an embodiment of the present invention, where the method may be applied to a server in a distributed database, and an identity of the server in the distributed database is not particularly limited. The server is used for receiving the data insertion instruction and generating a global main key to be inserted.
The method mainly comprises the following steps:
step 101: receiving a data insertion instruction, and recording the receiving time of the data insertion instruction; the data insertion instructions include: service identity data.
In the embodiment of the present invention, a server of the distributed database may receive a data insertion instruction, where the data insertion instruction may include service identity data. The service identity data is used for representing the service to which the data to be inserted corresponding to the data insertion instruction belongs. The service categories may include: drama, movie, documentary, etc. For example, if the service identity data included in the data insertion instruction is a tv series, it represents that the data to be inserted corresponding to the data insertion instruction belongs to a tv series service.
The server can record the receiving time of the data insertion instruction while receiving the data insertion instruction, wherein the receiving time can be accurate to millisecond, microsecond, nanosecond or the like, and further possible repetition of the generated global main key to be inserted can be avoided to a certain extent.
For example, if the server 1 receives the data insertion instruction a in 2019, 3, 15, the receiving time of the recorded data insertion instruction a is: 2019-03-15.
Step 102: based on the reception time, a timestamp is determined.
In the embodiment of the present invention, a reference time may be set in advance, and a difference between the reception time and the reference time may be determined as a timestamp. The reference time here may be the time at which the distributed database is put into use, etc. In the embodiment of the present invention, this is not particularly limited. The time stamp can distinguish the time of receipt of the piece of data.
For example, for the above example, if the time when the distributed database starts to be put into use is: 3, 13 and 9 days in 2019, and 21 points in time. Then, the timestamp may be: a difference between day 15 on month 3 2019 and day 9 on month 13 on month 3 2019, which may be in milliseconds.
Step 103: the sequence number is randomly generated.
In the embodiment of the present invention, the server may generate the serial number by using a random number generation algorithm. The randomly generated sequence number may distinguish between different data received by the same server for the same service at the same time. The generation method of the serial number is not particularly limited.
Step 104: and generating a global main key to be inserted corresponding to the data insertion instruction according to the identity of the server, the timestamp, the service identity data and the sequence number.
The distributed database may include at least one server, and the identity of the server included in the distributed database is not particularly limited. Each server has an identity that is used to distinguish between different servers. The identity of the server is set in the global primary key to be inserted, so that the server can distinguish that certain data is subsequently inserted by the server. The specific setting of the identity of the server is not particularly limited.
The server may perform permutation and combination on the identity identifier of the server, the timestamp, the service identity data, and the sequence number, and obtain the global primary key to be inserted corresponding to the data insertion instruction. Or, content may be added on the basis of the permutation and combination to obtain the to-be-inserted global primary key and the like corresponding to the data insertion instruction.
The identity of the server can distinguish that a certain piece of data is inserted by the server subsequently, the timestamp can distinguish the receiving time of the piece of data, the service identity data can distinguish the service type to which the piece of data belongs, the randomly generated serial number can distinguish different data received by the same server aiming at the same service type at the same time, and the server still has uniqueness according to the global main key formed by the four parts even under the condition of larger concurrency. Meanwhile, in the process of generating the global primary key, multi-step operations such as generation and copying are not needed, the influence of other factors is avoided, the generation mode is simple, and the generation efficiency is high.
Referring to fig. 2, fig. 2 is a flowchart of steps of another global primary key generation method in an embodiment of the present invention, which may be applied to a server in a distributed database, and the foregoing description is referred to for the server in the distributed database. The method can comprise the following steps:
step 201: receiving a data insertion instruction, and recording the receiving time of the data insertion instruction; the data insertion instructions include: service identity data.
Step 202: based on the reception time, a timestamp is determined.
Step 203: the sequence number is randomly generated.
In the embodiment of the present invention, the steps 201 to 203 may refer to the steps 101 to 103, respectively, and are not described herein again to avoid repetition.
Step 204: and combining the timestamp, the identity identifier of the server, the service identity data and the sequence number in sequence to obtain a global main key to be inserted corresponding to the data insertion instruction.
In the embodiment of the present invention, the timestamp, the identity identifier of the server, the service identity data, and the serial number may be combined in sequence to obtain the global primary key to be inserted corresponding to the data insertion instruction. The global main key to be inserted can be obtained only by combining the four parts in sequence, and the generation mode of the global main key to be inserted is simple and high in generation efficiency.
The specific combination order of the above four parts is not particularly limited. For example, in the global primary key to be inserted, the timestamp, the identity of the server, the service identity data, and the sequence number may be sequentially combined to obtain the global primary key to be inserted corresponding to the data insertion instruction. Or, in the global primary key to be inserted, the identity identifier of the server, the service identity data, the sequence number, and the timestamp may be sequentially combined to obtain the global primary key to be inserted corresponding to the data insertion instruction.
Optionally, before the step 204, the method may further include the steps of: and determining the total number of bits of the global main key to be inserted corresponding to the data insertion instruction according to the service life of the server, the identity identification of the server in the distributed database, the identity data of the service to be recorded in the distributed database and the concurrency of the same service identity data by the server in the same timestamp.
Specifically, the global main key to be inserted is obtained by sequentially combining the four parts, and the four parts are respectively in direct relation with the service life of the server, the identity of the server in the distributed database, the identity data of the service to be recorded in the distributed database and the concurrency of the identity data of the same service in the same timestamp, so that the total number of bits of the global main key to be inserted is determined according to the four factors, and the problem that the global main key to be inserted is long or repeated in the using process can be avoided.
Optionally, referring to fig. 3, fig. 3 is a flowchart illustrating a determination of a total number of bits to be inserted into a global primary key according to an embodiment of the present invention. The determining, based on the service life of the server, the identity of the server in the distributed database, the identity data of the service to be recorded in the distributed database, and the concurrency of the same service identity data by the server in the same timestamp, a total number of bits of the global primary key to be inserted corresponding to the data insertion instruction may include the following steps:
step S1: and determining the first digit of the timestamp in the global primary key to be inserted based on the service life of the server.
Specifically, the timestamp may be a binary representation of a difference between the receiving time and the reference time, where a first digit of the timestamp in the global primary key to be inserted is: the receiving time corresponding to the service life of the server subtracts the bit number occupied by the binary representation of the difference of the reference time. The larger the difference between the receiving time and the reference time is, the more the first digit of the timestamp which is possibly needed in the global main key to be inserted is, and the first digit of the timestamp in the global main key to be inserted is determined based on the service life of the server, so that the timestamp cannot overflow in the service life of the server.
For example, if the service life of the server is 17 years, the reference time is 3 months, 13 days, 9 hours and 21 minutes in 2019. Then, the receiving time corresponding to the service life of the server is: 3, 13 and 9 days of 2036, 21 minutes. The receiving time corresponding to the service life of the server is as follows: the difference of 21 minutes 9/13/3/2036 minus the aforementioned reference time 2019 is converted into milliseconds. And representing the milliseconds after the difference conversion by using a binary system, wherein the milliseconds after the difference conversion are 39-bit binary systems, and the first digit of the timestamp in the global main key to be inserted is 39 bits.
Step S2: and determining the second number of digits occupied by the identity of the server in the global primary key to be inserted based on the binary representation of the identity of the server in the distributed database.
Specifically, the second number of the server identity occupied in the global primary key to be inserted needs to distinguish the identity of each server. The identity of each server may be represented in binary, and the number of bits in binary representation of the identity of the server with the largest number of bits in binary representation of the identity in each server may be determined as the second number of bits.
For example, if the distributed databases collectively include: server a, server B, server C3 servers. The identity of the server A occupies 8 bits by binary representation, the identity of the server B occupies 7 bits by binary representation, and the identity of the server C occupies 6 bits by binary representation. The number of digits occupied by the id of the server a in binary is the largest among 3 servers, namely, server a, server B and server C, the second number occupied by the id of the server in the global primary key to be inserted may be determined to be 8.
For another example, if the ids of the servers are natural numbers from 1 and the ids of the servers are different, the number of the servers is the maximum id in the ids of the servers. The number of bits in the binary representation of the number of servers can be determined as the identity of the server, and the second number of bits occupied in the global primary key to be inserted is determined. If the number of servers in the distributed database is 10, the 10 is converted into a binary expression as follows: 1010. if 4 bits are occupied, the second number occupied by the server id in the global primary key to be inserted may be determined to be 4.
It should be noted that, in order to avoid the problem of overflow of the id of the server, which may occur due to the increase of the number of servers, a larger second digit may be set in advance. For example, if the ids of the servers are natural numbers from 1, the ids of the servers are different. If it is expected that the maximum number of servers in the distributed database may be 200, 200 is converted to a binary representation as: 11001000. if 8 bits are occupied, the second number occupied by the server id in the global primary key to be inserted may be determined to be 8.
Step S3: and determining the third digit occupied by the service identity data in the global primary key to be inserted based on the binary representation of the identity data of the service to be recorded in the distributed database.
Specifically, the third digit of the service identity data in the global primary key to be inserted needs to distinguish the identity of each service. The identity data of each service may be represented in binary, and the number of bits in binary representation of the identity data of the service having the largest number of bits in binary representation of the identity data among the services may be determined as the third number of bits.
For example, if the distributed databases collectively include: 10 services. The identity data of each service is represented by binary, and the number of bits of binary representation in 10 identity data of 10 services is at most 10 bits, so that the third bit number occupied by the service identity data in the global primary key to be inserted can be determined as 10.
It should be noted that, also in order to solve the problem of overflow of the identity data of the service, which may occur due to the increasing number of services, a larger third bit number may be set in advance.
For example, if the identity data of the service is a natural number starting from 1, the identity data of each service is different. If the expected maximum number of services in the distributed database is 300, 300 is converted into a binary representation as: 100101100. and if 9 bits are occupied totally, the third bit occupied by the service identity data in the global primary key to be inserted can be determined as 9.
Step S4: determining the fourth digit of the serial number in the global main key to be inserted based on the concurrency of the server in the same timestamp and the same service identity data; and the sum of the first digit, the second digit, the third digit and the fourth digit is less than or equal to the total digit of the global primary key to be inserted.
Specifically, the same server is in the same timestamp, the concurrency amount of the same service identity data is represented by binary, the binary digit occupied by the maximum concurrency amount can be determined as the fourth digit occupied by the serial number in the global main key to be inserted, and then the serial number is used for distinguishing the concurrency amounts of the same service identity data of the same server in the same timestamp.
For example, the unit of the timestamp is milliseconds, if the maximum concurrency amount for the same service identity data is 63 for the same server within the same timestamp, the 63 is represented by binary: 111111, which occupies 6 bits. Then the fourth digit of the sequence number that is to be inserted into the global primary key may be 6.
Also, in order to avoid an increasing amount of concurrency, a larger fourth digit may be set in advance.
In the global main key to be inserted, since the global main key to be inserted is obtained by sequentially combining the timestamp, the identity of the server, the service identity data and the serial number, the sum of the first digit, the second digit, the third digit and the fourth digit is equal to the total digit of the global main key to be inserted.
It should be noted that, optionally, in addition to the timestamp, the identity identifier of the server, the service identity data, and the sequence number, the global primary key to be inserted may further include a sign bit, and the like, and therefore, the sum of the first digit, the second digit, the third digit, and the fourth digit may be less than or equal to the total digit of the global primary key to be inserted.
For example, referring to fig. 4, fig. 4 is a global primary key according to an embodiment of the present invention. The total number of bits of the global primary key is 64 bits. As noted in fig. 4, the first bit 0 of the 64 bits may be a sign bit, with 0 representing a positive number and 1 representing a negative number for long type data. Typically the global primary key is a positive number, the sign bit may be 0. The 2 nd to 40 th bits of the 64 bits may be time stamps. The 41 st to 48 th bits of the 64 bits may be the identity of the server. The 49 th to 58 th bits of the 64 bits may be service identity data. The 59 th to 64 th bits of the 64 bits may be a sequence number. In the global main key shown in fig. 4, the timestamp, the identity identifier of the server, the service identity data, and the serial number are sequentially ordered and combined, and then the sign bit is added at the forefront.
One code of the global primary key to be inserted corresponding to the data insertion instruction may be obtained as follows:
Figure BDA0002488742160000111
Figure BDA0002488742160000121
Figure BDA0002488742160000131
Figure BDA0002488742160000141
Figure BDA0002488742160000151
Figure BDA0002488742160000161
step 205: the distributed database comprises a plurality of data tables, each data table corresponds to different service identity data, the data to be inserted corresponding to the data insertion instruction and the global main key to be inserted corresponding to the data insertion instruction are inserted into the data table corresponding to the service identity data.
Specifically, the distributed database may include a plurality of data tables, and each data table corresponds to different service identity data. That is, one data table records only data corresponding to one service identity data. The data to be inserted corresponding to the data insertion instruction and the global primary key to be inserted corresponding to the data insertion instruction may be inserted into the data table corresponding to the service identity data.
For example, the data table 1 in the distributed database only records the service identity data as data corresponding to the television series. If the service identity data included in the data insertion instruction is a tv series, the data to be inserted corresponding to the data insertion instruction and the global primary key to be inserted corresponding to the data insertion instruction may be inserted into the data table 1.
Step 206: receiving a data query instruction; the data query instruction comprises: and (5) waiting to query the global primary key.
The server may receive a data query instruction, the data query instruction including: and (5) waiting to query the global primary key.
Step 207: searching target data corresponding to the global main key to be inquired in a data table corresponding to the service identity data in the global main key to be inquired; and the global primary key to be inserted of the target data is matched with the global primary key to be inquired.
Only in the data table corresponding to the service identity data in the global main key to be queried, the target data matching the global main key to be inserted and the global main key to be queried is searched, instead of searching in each data table, which is equivalent to reducing the search range and having high search efficiency.
For example, for the above example, if the service identity data in the global primary key to be queried is a tv play, the target data to be inserted into the global primary key and matched with the global primary key to be queried may be searched in the data table 1. And other data tables of which the service identity data is not the television play do not need to be inquired, so that the search range is small and the search efficiency is high.
Step 208: and returning the target data.
And the server returns the target data.
The identity of the server can distinguish that a certain piece of data is inserted by the server subsequently, the timestamp can distinguish the receiving time of the piece of data, the service identity data can distinguish the service type to which the piece of data belongs, the randomly generated serial number can distinguish different data received by the same server aiming at the same service type at the same time, and the server still has uniqueness according to the global main key formed by the four parts even under the condition of larger concurrency. Meanwhile, in the process of generating the global primary key, multi-step operations such as generation and copying are not needed, the influence of other factors is avoided, the generation mode is simple, and the generation efficiency is high.
It should be noted that, for simplicity of description, the method embodiments are described as a series of acts or combination of acts, but those skilled in the art will recognize that the embodiments are not limited by the order of acts described, as some steps may occur in other orders or concurrently depending on the embodiments. Further, those skilled in the art will appreciate that the embodiments described in the specification are presently preferred and that no particular act is required to implement the embodiments of the application.
Fig. 5 is a data query apparatus 500 applied to a server in a distributed database according to an embodiment of the present invention; the device comprises:
a first receiving module 501, configured to receive a data insertion instruction, and record a receiving time of the data insertion instruction; the data insertion instructions include: service identity data;
a timestamp determination module 502 for determining a timestamp based on the reception time;
a serial number generation module 503, configured to randomly generate a serial number;
a global primary key generation module 504, configured to generate a global primary key to be inserted corresponding to the data insertion instruction according to the identity of the server, the timestamp, the service identity data, and the sequence number.
Optionally, on the basis of fig. 5, referring to fig. 6, the global primary key generating module 504 includes:
and the global primary key generation unit 5041 is configured to combine the timestamp, the identity of the server, the service identity data, and the sequence number in order to obtain the to-be-inserted global primary key corresponding to the data insertion instruction.
Optionally, the apparatus further includes:
and the total digit determining module is used for determining the total digit of the global main key to be inserted corresponding to the data insertion instruction according to the service life of the server, the identity identification of the server in the distributed database, the identity data of the service to be recorded in the distributed database and the concurrency of the same service identity data by the server in the same timestamp.
Optionally, the total number of bits determining module includes:
the first digit determining unit is used for determining a first digit occupied by the timestamp in the global main key to be inserted based on the service life of the server;
a second digit determining unit, configured to determine, based on a binary representation of an identity of a server in the distributed database, a second digit occupied by the identity of the server in the global primary key to be inserted;
a third digit determining unit, configured to determine, based on a binary representation of identity data of a service to be recorded in the distributed database, a third digit occupied by the service identity data in the global primary key to be inserted;
a fourth digit determining unit, configured to determine, based on the concurrency of the same service identity data in the same timestamp by the server, a fourth digit occupied by the serial number in the global primary key to be inserted; and the sum of the first digit, the second digit, the third digit and the fourth digit is equal to the total digit of the global primary key to be inserted.
Optionally, referring to fig. 6, the distributed database includes a plurality of data tables, each data table corresponds to different service identification data, and the apparatus further includes:
an inserting module 505, configured to insert the data to be inserted corresponding to the data inserting instruction, and the global primary key to be inserted corresponding to the data inserting instruction into the data table corresponding to the service identity data;
a second receiving module 506, configured to receive a data query instruction; the data query instruction comprises: a global primary key to be queried;
a searching module 507, configured to search, in a data table corresponding to the service identity data in the global primary key to be queried, target data corresponding to the global primary key to be queried; the global main key to be inserted of the target data is matched with the global main key to be inquired;
a returning module 508, configured to return the target data.
In summary, in the embodiment of the present invention, the id of the server may distinguish that a certain piece of data is inserted by the server subsequently, the timestamp may distinguish the receiving time of the piece of data, the service id data may distinguish the service type to which the piece of data belongs, the randomly generated sequence number may distinguish different pieces of data received by the same server at the same time for the same service type, and the server is still unique according to the global primary key formed by the four parts even when the concurrency is large. Meanwhile, in the process of generating the global primary key, multi-step operations such as generation and copying are not needed, the influence of other factors is avoided, the generation mode is simple, and the generation efficiency is high.
For the device embodiment, since it is basically similar to the method embodiment, the description is simple, and for the relevant points, refer to the partial description of the method embodiment.
The embodiments in the present specification are described in a progressive manner, each embodiment focuses on differences from other embodiments, and the same and similar parts among the embodiments are referred to each other.
The embodiment of the present invention further provides an electronic device, as shown in fig. 7, which includes a processor 71, a communication interface 72, a memory 73 and a communication bus 74, where the processor 71, the communication interface 72, and the memory 73 complete mutual communication through the communication bus 74,
a memory 73 for storing a computer program;
the processor 71, when executing the program stored in the memory 73, implements the following steps:
receiving a data insertion instruction, and recording the receiving time of the data insertion instruction; the data insertion instructions include: service identity data;
determining a timestamp based on the reception time;
randomly generating a serial number;
and generating a global main key to be inserted corresponding to the data insertion instruction according to the identity of the server, the timestamp, the service identity data and the sequence number.
The communication bus mentioned in the above terminal may be a Peripheral Component Interconnect (PCI) bus, an Extended Industry Standard Architecture (EISA) bus, or the like. The communication bus may be divided into an address bus, a data bus, a control bus, etc. For ease of illustration, only one thick line is shown, but this does not mean that there is only one bus or one type of bus.
The communication interface is used for communication between the terminal and other equipment.
The Memory may include a Random Access Memory (RAM) or a non-volatile Memory (non-volatile Memory), such as at least one disk Memory. Optionally, the memory may also be at least one memory device located remotely from the processor.
The Processor may be a general-purpose Processor, and includes a Central Processing Unit (CPU), a Network Processor (NP), and the like; the device can also be a Digital Signal Processor (DSP), an Application Specific Integrated Circuit (ASIC), a Field Programmable Gate Array (FPGA) or other Programmable logic device, a discrete Gate or transistor logic device, or a discrete hardware component.
In yet another embodiment of the present invention, a computer-readable storage medium is further provided, which stores instructions that, when executed on a computer, cause the computer to execute the global primary key generation method described in any one of the above embodiments.
In yet another embodiment, the present invention further provides a computer program product containing instructions which, when run on a computer, cause the computer to perform the global primary key generation method described in any of the above embodiments.
In the above embodiments, the implementation may be wholly or partially realized by software, hardware, firmware, or any combination thereof. When implemented in software, may be implemented in whole or in part in the form of a computer program product. The computer program product includes one or more computer instructions. When loaded and executed on a computer, cause the processes or functions described in accordance with the embodiments of the invention to occur, in whole or in part. The computer may be a general purpose computer, a special purpose computer, a network of computers, or other programmable device. The computer instructions may be stored in a computer readable storage medium or transmitted from one computer readable storage medium to another, for example, from one website site, computer, server, or data center to another website site, computer, server, or data center via wired (e.g., coaxial cable, fiber optic, Digital Subscriber Line (DSL)) or wireless (e.g., infrared, wireless, microwave, etc.). The computer-readable storage medium can be any available medium that can be accessed by a computer or a data storage device, such as a server, a data center, etc., that incorporates one or more of the available media. The usable medium may be a magnetic medium (e.g., floppy Disk, hard Disk, magnetic tape), an optical medium (e.g., DVD), or a semiconductor medium (e.g., Solid State Disk (SSD)), among others.
It is noted that, herein, relational terms such as first and second, and the like may be used solely to distinguish one entity or action from another entity or action without necessarily requiring or implying any actual such relationship or order between such entities or actions. Also, the terms "comprises," "comprising," or any other variation thereof, are intended to cover a non-exclusive inclusion, such that a process, method, article, or apparatus that comprises a list of elements does not include only those elements but may include other elements not expressly listed or inherent to such process, method, article, or apparatus. Without further limitation, an element defined by the phrase "comprising an … …" does not exclude the presence of other identical elements in a process, method, article, or apparatus that comprises the element.
All the embodiments in the present specification are described in a related manner, and the same and similar parts among the embodiments may be referred to each other, and each embodiment focuses on the differences from the other embodiments. In particular, for the system embodiment, since it is substantially similar to the method embodiment, the description is simple, and for the relevant points, reference may be made to the partial description of the method embodiment.
The above description is only for the preferred embodiment of the present invention, and is not intended to limit the scope of the present invention. Any modification, equivalent replacement, or improvement made within the spirit and principle of the present invention shall fall within the protection scope of the present invention.

Claims (10)

1. A global primary key generation method is characterized by being applied to a server in a distributed database; the method comprises the following steps:
receiving a data insertion instruction, and recording the receiving time of the data insertion instruction; the data insertion instructions include: service identity data;
determining a timestamp based on the reception time;
randomly generating a serial number;
and generating a global main key to be inserted corresponding to the data insertion instruction according to the identity of the server, the timestamp, the service identity data and the sequence number.
2. The method according to claim 1, wherein the generating a global primary key to be inserted corresponding to the data insertion instruction according to the identity of the server, the timestamp, the service identity data, and the sequence number includes:
and combining the timestamp, the identity identifier of the server, the service identity data and the sequence number in sequence to obtain a global main key to be inserted corresponding to the data insertion instruction.
3. The method according to claim 2, wherein the step of combining the timestamp, the identity of the server, the service identity data, and the sequence number in order to obtain a global primary key to be inserted corresponding to the data insertion instruction further comprises:
determining a first digit number occupied by the timestamp in the global primary key to be inserted based on the service life of the server;
determining the identity of the server and the second number of digits occupied in the global primary key to be inserted based on the binary representation of the identity of the server in the distributed database;
determining a third digit occupied by the service identity data in the global primary key to be inserted based on a binary representation of the identity data of the service to be recorded in the distributed database;
determining the fourth digit of the serial number in the global main key to be inserted based on the concurrency of the server in the same timestamp and the same service identity data; and the sum of the first digit, the second digit, the third digit and the fourth digit is equal to the total digit of the global primary key to be inserted.
4. The method of claim 1, wherein the distributed database comprises a plurality of data tables, each data table corresponding to different service identity data, the method further comprising:
inserting the data to be inserted corresponding to the data insertion instruction and the global main key to be inserted corresponding to the data insertion instruction into a data table corresponding to the service identity data;
receiving a data query instruction; the data query instruction comprises: a global primary key to be queried;
searching target data corresponding to the global main key to be inquired in a data table corresponding to the service identity data in the global main key to be inquired; the global main key to be inserted of the target data is matched with the global main key to be inquired;
and returning the target data.
5. The global primary key generation device is applied to a server in a distributed database; the device comprises:
the first receiving module is used for receiving a data insertion instruction and recording the receiving time of the data insertion instruction; the data insertion instructions include: service identity data;
a timestamp determination module to determine a timestamp based on the receive time;
the serial number generation module is used for randomly generating a serial number;
and the global primary key generation module is used for generating a global primary key to be inserted corresponding to the data insertion instruction according to the identity of the server, the timestamp, the service identity data and the serial number.
6. The apparatus of claim 5, wherein the global primary key generation module comprises:
and the global primary key generating unit is used for combining the timestamp, the identity identifier of the server, the service identity data and the serial number in sequence to obtain the to-be-inserted global primary key corresponding to the data insertion instruction.
7. The apparatus of claim 6, further comprising: a total digit determination module; wherein the total number of bits determining module comprises:
the first digit determining unit is used for determining a first digit occupied by the timestamp in the global main key to be inserted based on the service life of the server;
a second digit determining unit, configured to determine, based on a binary representation of an identity of a server in the distributed database, a second digit occupied by the identity of the server in the global primary key to be inserted;
a third digit determining unit, configured to determine, based on a binary representation of identity data of a service to be recorded in the distributed database, a third digit occupied by the service identity data in the global primary key to be inserted;
a fourth digit determining unit, configured to determine, based on the concurrency of the same service identity data in the same timestamp by the server, a fourth digit occupied by the serial number in the global primary key to be inserted; and the sum of the first digit, the second digit, the third digit and the fourth digit is equal to the total digit of the global primary key to be inserted.
8. The apparatus of claim 5, wherein the distributed database comprises a plurality of data tables, each data table corresponding to different service identity data, the apparatus further comprising:
the inserting module is used for inserting the data to be inserted corresponding to the data inserting instruction and the global main key to be inserted corresponding to the data inserting instruction into the data table corresponding to the service identity data;
the second receiving module is used for receiving a data query instruction; the data query instruction comprises: a global primary key to be queried;
the searching module is used for searching target data corresponding to the global main key to be inquired in a data table corresponding to the service identity data in the global main key to be inquired; the global main key to be inserted of the target data is matched with the global main key to be inquired;
and the return module is used for returning the target data.
9. An electronic device is characterized by comprising a processor, a communication interface, a memory and a communication bus, wherein the processor and the communication interface are used for realizing mutual communication by the memory through the communication bus;
a memory for storing a computer program;
a processor for implementing the method steps of any of claims 1 to 4 when executing a program stored in the memory.
10. A computer-readable storage medium, on which a computer program is stored which, when being executed by a processor, carries out the method steps of any one of claims 1 to 4.
CN202010398983.4A 2020-05-12 2020-05-12 Global primary key generation method and device Pending CN111708831A (en)

Priority Applications (1)

Application Number Priority Date Filing Date Title
CN202010398983.4A CN111708831A (en) 2020-05-12 2020-05-12 Global primary key generation method and device

Applications Claiming Priority (1)

Application Number Priority Date Filing Date Title
CN202010398983.4A CN111708831A (en) 2020-05-12 2020-05-12 Global primary key generation method and device

Publications (1)

Publication Number Publication Date
CN111708831A true CN111708831A (en) 2020-09-25

Family

ID=72537307

Family Applications (1)

Application Number Title Priority Date Filing Date
CN202010398983.4A Pending CN111708831A (en) 2020-05-12 2020-05-12 Global primary key generation method and device

Country Status (1)

Country Link
CN (1) CN111708831A (en)

Cited By (1)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN112905703A (en) * 2021-03-12 2021-06-04 北京格瑞拉科技有限公司 User information management method and system

Citations (4)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
US20100023797A1 (en) * 2008-07-25 2010-01-28 Rajeev Atluri Sequencing technique to account for a clock error in a backup system
CN102880705A (en) * 2012-09-28 2013-01-16 用友软件股份有限公司 Database primary key generating device and database primary key generating method
CN106572165A (en) * 2016-10-26 2017-04-19 宜人恒业科技发展(北京)有限公司 Distributed global unique ID application method
CN110928912A (en) * 2018-09-20 2020-03-27 北京京东尚科信息技术有限公司 Method and device for generating unique identifier

Patent Citations (4)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
US20100023797A1 (en) * 2008-07-25 2010-01-28 Rajeev Atluri Sequencing technique to account for a clock error in a backup system
CN102880705A (en) * 2012-09-28 2013-01-16 用友软件股份有限公司 Database primary key generating device and database primary key generating method
CN106572165A (en) * 2016-10-26 2017-04-19 宜人恒业科技发展(北京)有限公司 Distributed global unique ID application method
CN110928912A (en) * 2018-09-20 2020-03-27 北京京东尚科信息技术有限公司 Method and device for generating unique identifier

Cited By (1)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN112905703A (en) * 2021-03-12 2021-06-04 北京格瑞拉科技有限公司 User information management method and system

Similar Documents

Publication Publication Date Title
CN108932236B (en) File management method and device
CN107818115B (en) Method and device for processing data table
CN111352902A (en) Log processing method and device, terminal equipment and storage medium
CN108647357B (en) Data query method and device
CN111061758B (en) Data storage method, device and storage medium
US20230205755A1 (en) Methods and systems for improved search for data loss prevention
CN110727663A (en) Data cleaning method, device, equipment and medium
US20110246479A1 (en) Creating Indexes for Databases
CN110659282A (en) Data route construction method and device, computer equipment and storage medium
CN107169003B (en) Data association method and device
CN115757406A (en) Data storage method and device, electronic equipment and storage medium
CN111708831A (en) Global primary key generation method and device
CN112182021B (en) User data query method, device and system
CN110909266B (en) Deep paging method and device and server
CN110727895B (en) Sensitive word sending method and device, electronic equipment and storage medium
CN111897837B (en) Data query method, device, equipment and medium
CN109992695B (en) Video information query method and device
CN114564501A (en) Database data storage and query methods, devices, equipment and medium
CN110046180B (en) Method and device for locating similar examples and electronic equipment
CN114153830B (en) Data verification method and device, computer storage medium and electronic equipment
CN117076465B (en) Data association query method and related equipment
CN115794817A (en) Main key generation method and electronic equipment
CN110209666B (en) data storage method and terminal equipment
CN115809248A (en) Data query method and device and storage medium
CN116155860A (en) Identity identification generation method and device and electronic equipment

Legal Events

Date Code Title Description
PB01 Publication
PB01 Publication
SE01 Entry into force of request for substantive examination
SE01 Entry into force of request for substantive examination