Method, device, electronic device and storage medium for querying data in database
Through the mapping relationship between the full index record index strategy and the data storage location, the target storage location is directly determined, which solves the problem of high resource consumption in deep paging queries and improves query performance.
Patent Information
- Application Number
- CN201910992618.3
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2019-10-18
- Publication Date
- 2024-09-20
- Estimated Expiration
- 2039-10-18
AI Technical Summary
In the existing technology, in the deep paging query scenario, the number of scans of the query cursor and the retention time of the data snapshot are long, resulting in high resource consumption and reduced query performance.
Through the mapping relationship between the full index record index strategy and the data storage location, the target storage location is directly determined to query the target data, reducing distributed database query time and resource waste.
It effectively reduces the time and resource consumption of deep paging queries and improves query performance. It is especially suitable for scenarios with large amounts of data and low update frequency.
Smart Images

Figure CN111797091B
Abstract
Description
Technical Field
[0001] The present invention relates to the field of computer technology, and in particular to a method, device, electronic device and storage medium for querying data in a database. Background Art
[0002] The mechanism for executing queries in distributed databases is usually as follows: the master node first receives the query request, which usually includes the starting position (from) of the query data and the size (size) of the data, and then distributes the query request to each node. Each node retrieves size pieces of local data based on the query request and returns it to the master node. The master node then determines the size pieces of data queried by the query request from the data returned by each node. For distributed databases, when the starting position of the query request is small, it does not consume too many resources. However, when deep paging queries are performed (the starting position is very large, such as hundreds of thousands or millions), each node needs to query from the starting position, and the number of records returned to the master node is (from+size). The data received by the master node reaches the total number of nodes × (from+size), and sometimes it is necessary to perform overall sorting. In the end, the result returned by the master node is only size records, and the other large amounts of data in the front will be discarded. This will cause great consumption and waste of server resources and reduce query performance.
[0003] At present, cursors are usually used to alleviate the problem of resource waste in deep page queries in distributed databases. After receiving a query request, the master node can temporarily generate a data snapshot and a cursor pointing to the beginning of the data snapshot based on the query request. The cursor contains the offset of the query result, so there is no need to traverse from the beginning when querying data. The query can be advanced by continuously scanning the cursor, thereby saving resource consumption of deep page queries and greatly improving the performance of deep page queries.
[0004] In the process of implementing the present invention, the inventors found that there are at least the following problems in the prior art:
[0005] Because the number of data that can be scanned by the cursor each time a query is made is fixed, for deep paged query scenarios, multiple scans are sometimes required to meet the needs of data query. Therefore, the number and time of data queries will be longer, and the temporarily generated data snapshots will need to be retained for a relatively long time. This will also cause great consumption and waste of resources and reduce query performance. Summary of the invention
[0006] In view of this, an embodiment of the present invention provides a method, device, system and storage medium for database query in a database, which can reduce the time of distributed database query, especially deep page query, and reduce the waste of resources and improve service performance.
[0007] To achieve the above objective, according to one aspect of an embodiment of the present invention, a method for querying data in a database is provided.
[0008] A method for database query in a database in an embodiment of the present invention is used for a first node, including: receiving a query request sent by a client, the query request including a query strategy; determining whether a target index strategy identical to the query strategy is queried; if so, determining a target storage location corresponding to the target index strategy from a full index based on the target index strategy; wherein the full index is used to record a mapping relationship between an index strategy and a data storage location; and sending the target data stored in the target storage location to the client.
[0009] In one embodiment, it also includes: if not, sending the query request to the second node; receiving feedback information from the second node; judging whether the second node stores the target index strategy based on the feedback information; if not, querying the target data by a preset method.
[0010] In yet another embodiment, the further step further includes: determining an indexing strategy for the full index to be generated;
[0011] Obtain a data storage location corresponding to the index strategy of the full index to be generated; and generate a full index based on the index strategy of the full index to be generated and the data storage location corresponding to the index strategy of the full index to be generated.
[0012] In yet another embodiment, the indexing strategy for the full index to be generated is determined based on the indexing strategy sent by the client, or the indexing strategy for the full index to be generated is determined by the first node.
[0013] In another embodiment, the indexing strategy includes indexing conditions; or, the indexing strategy includes indexing conditions and sorting methods; the data storage location includes a shard identifier of the storage data shard and a record identifier of the location in the data storage shard.
[0014] To achieve the above objective, according to another aspect of an embodiment of the present invention, a method for querying data in a database is provided.
[0015] Another method for database query in a database of an embodiment of the present invention is used for a second node, including: receiving a query request sent by a first node, the query request including a query strategy; determining whether a target index strategy identical to the query strategy is queried; if so, determining a target storage location corresponding to the target index strategy from the full index based on the target index strategy; wherein the full index is used to record the mapping relationship between the index strategy and the data storage location; and sending the target data stored in the target storage location to the client or the first node.
[0016] In one embodiment, it also includes: if yes, sending first feedback information to the first node, the first feedback information indicating that the target index strategy is stored; if no, sending second feedback information to the first node, the second feedback information indicating that the target index strategy is not stored.
[0017] In another embodiment, it also includes: receiving the index strategy of the full index to be generated sent by the first node; obtaining the data storage location corresponding to the index strategy of the full index to be generated; and generating the full index based on the index strategy of the full index to be generated and the data storage location corresponding to the index strategy of the full index to be generated.
[0018] In another embodiment, the indexing strategy includes indexing conditions; or, the indexing strategy includes indexing conditions and sorting methods; the data storage location includes a shard identifier of the storage data shard and a record identifier of the location in the data storage shard.
[0019] To achieve the above objective, according to another aspect of the present invention, a device for querying a database in a database is provided.
[0020] A database query device of the present invention is arranged at a first node, and includes: a receiving unit, used to receive a query request sent by a client, the query request including a query strategy; a judging unit, used to judge whether a target index strategy identical to the query strategy is queried; a determining unit, used to, if so, determine a target storage location corresponding to the target index strategy from a full index based on the target index strategy; wherein the full index is used to record a mapping relationship between an index strategy and a data storage location; and a sending unit, used to send the target data stored in the target storage location to the client.
[0021] In one embodiment, the sending unit is also used to send the query request to the second node if not; the receiving unit is also used to receive feedback information from the second node; the receiving unit is also used to determine whether the second node stores the target index strategy based on the feedback information; the device also includes: a query unit, which is also used to query the target data by a preset method if not.
[0022] In another embodiment, the determination unit is also used to determine the index strategy of the full index to be generated; the device also includes: an acquisition unit, used to obtain the data storage location corresponding to the index strategy of the full index to be generated; a generation unit, used to generate the full index based on the index strategy of the full index to be generated and the data storage location corresponding to the index strategy of the full index to be generated.
[0023] In yet another embodiment, the indexing strategy for the full index to be generated is determined based on the indexing strategy sent by the client, or the indexing strategy for the full index to be generated is determined by the first node.
[0024] In another embodiment, the indexing strategy includes indexing conditions; or, the indexing strategy includes indexing conditions and sorting methods; the data storage location includes a shard identifier of the storage data shard and a record identifier of the location in the data storage shard.
[0025] To achieve the above objective, according to another aspect of the present invention, a device for querying a database in a database is provided.
[0026] Another database query device of the present invention is arranged at a second node, and includes: a receiving unit, used to receive a query request sent by a first node, the query request including a query strategy; a judging unit, used to judge whether a target index strategy identical to the query strategy is queried; a determining unit, used to, if so, determine a target storage location corresponding to the target index strategy from the full index based on the target index strategy; wherein the full index is used to record the mapping relationship between the index strategy and the data storage location; a sending unit, used to send the target data stored in the target storage location to the client or the first node.
[0027] In one embodiment, the sending unit is also used to send first feedback information to the first node if yes, the first feedback information indicating that the target index strategy is stored; if no, send second feedback information to the first node, the second feedback information indicating that the target index strategy is not stored.
[0028] In another embodiment, the receiving unit is also used to receive the index strategy of the full index to be generated sent by the first node; the device also includes: an acquisition unit, used to obtain the data storage location corresponding to the index strategy of the full index to be generated; a generation unit, used to generate a full index based on the index strategy of the full index to be generated and the data storage location corresponding to the index strategy of the full index to be generated.
[0029] In another embodiment, the indexing strategy includes indexing conditions; or, the indexing strategy includes indexing conditions and sorting methods; the data storage location includes a shard identifier of the storage data shard and a record identifier of the location in the data storage shard.
[0030] To achieve the above objective, according to another aspect of an embodiment of the present invention, an electronic device is provided.
[0031] An electronic device according to an embodiment of the present invention comprises: one or more processors; a storage device for storing one or more programs, wherein when the one or more programs are executed by the one or more processors, the one or more processors implement the method for querying data in a database provided by an embodiment of the present invention.
[0032] To achieve the above objective, according to another aspect of an embodiment of the present invention, a computer readable medium is provided.
[0033] A computer-readable medium according to an embodiment of the present invention stores a computer program, which, when executed by a processor, implements a method for querying data in a database provided by an embodiment of the present invention.
[0034] One embodiment of the above invention has the following advantages or beneficial effects: In the embodiment of the present invention, the full index records the mapping relationship between the index strategy and the data storage location, that is, for the target index strategy, the full index has recorded the target storage location of the data corresponding to the target index strategy. Therefore, after receiving the query request, if the target index strategy that is the same as the query strategy is queried, the target storage location can be directly determined based on the full index, and then the target data can be queried. This can reduce the time of distributed database queries, especially deep paging queries, reduce resource waste, and improve query performance.
[0035] The further effects of the above-mentioned non-conventional optional manner will be described below in conjunction with the specific implementation manner. BRIEF DESCRIPTION OF THE DRAWINGS
[0036] The accompanying drawings are used to better understand the present invention and do not constitute an improper limitation of the present invention.
[0037] Figure 1 is a schematic diagram of a system architecture of a method for querying data in a database according to an embodiment of the present invention;
[0038] Figure 2 is a schematic diagram of an indexing strategy in a full index according to an embodiment of the present invention;
[0039] Figure 3 is a schematic diagram of a main process of a method for querying data in a database according to an embodiment of the present invention;
[0040] Figure 4 is a schematic diagram of another main process of a method for querying data in a database according to an embodiment of the present invention;
[0041] Figure 5 is a schematic diagram of another main process of a method for querying data in a database according to an embodiment of the present invention;
[0042] Figure 6 is a schematic diagram of a main unit of a device for querying data in a database according to an embodiment of the present invention;
[0043] Figure 7 is a schematic diagram of another main unit of the device for querying data in a database according to an embodiment of the present invention;
[0044] Figure 8 is another exemplary system architecture diagram to which the embodiments of the present invention can be applied;
[0045] Fig. 9 It is a schematic diagram of the structure of a computer system suitable for implementing the embodiment of the present invention. DETAILED DESCRIPTION
[0046] The following is a description of exemplary embodiments of the present invention in conjunction with the accompanying drawings, including various details of the embodiments of the present invention to facilitate understanding, which should be considered as merely exemplary. Therefore, it should be recognized by those of ordinary skill in the art that various changes and modifications may be made to the embodiments described herein without departing from the scope and spirit of the present invention. Similarly, for clarity and conciseness, the description of well-known functions and structures is omitted in the following description.
[0047] It should be pointed out that the embodiments of the present invention and the features in the embodiments may be combined with each other if there is no conflict.
[0048] The method for querying data in a database provided by an embodiment of the present invention can be used for data query in a distributed database, especially for the scenario of deep paging query in a distributed database. Distributed databases usually use smaller computer systems (nodes), and each computer can be placed in a separate location. Each computer may have a complete copy or a partial copy of a DBMS (Database Management System) and has its own local database. Multiple computers located in different locations are interconnected through a network to form a complete, global, logically centralized, and physically distributed large database.
[0049] The distributed database includes a first node and at least one second node, and the second node communicates with a device outside the distributed database through the first node. Figure 1 It is a system architecture for distributed databases. Figure 1 As shown. Figure 1 In the system architecture shown, the server is a distributed database including multiple data nodes. The embodiment of the present invention is described by taking four nodes (node 1, node 2, node 3 and node 4) as an example, where node 1 is a master node that can directly communicate with the server external device, and nodes 2, 3 and 4 are slave nodes. Figure 1 In the system architecture shown, the master node may be the first node, and node 2, node 3, and node 4 may be the second node.
[0050] In the embodiment of the present invention, each node can pre-store a full index, and each full index includes an index strategy and an index record corresponding to the index strategy. The index record records the data storage location that satisfies the corresponding index strategy data, that is, the full index is used to record the mapping relationship between the index strategy and the data storage location, that is, the data storage location that satisfies the index strategy data can be queried from the full index through the index strategy. The index strategy can include one or more index conditions, or one or more index conditions and one or more sorting methods. Figure 2 A schematic diagram of an indexing strategy is shown in Figure 2 In the index strategy package, there are three index conditions and two sorting methods. The index record is used to record the data storage location that meets the corresponding index strategy data, and can include the shard identifier of the data storage shard and the record identifier of the location in the data storage shard. In a distributed database, data is usually stored in shard storage. Each data in the shard has a corresponding identifier, that is, a record identifier, so the corresponding unique data can be found by the shard identifier of the data storage shard and the record identifier of the location in the data storage shard.
[0051] When the client needs to query data from the server, the client can send a query request to the server. The query request includes the client's requirements for querying data, that is, the query request includes a query strategy. After the server receives the query request sent by the client, it can determine whether the target index strategy that can be queried is the same as the query strategy in the query request. If so, it means that the server has stored the same target index strategy as the query strategy, that is, it has stored the full index including the target index strategy. Then, according to the corresponding relationship stored in the full index, the index record with the target index strategy can be determined, that is, the data storage location that satisfies the target index strategy is determined, that is, the target storage location. The query strategy is the same as the target index strategy, so the target storage location for storing data corresponding to the query strategy can be directly determined based on the index records in the full index, and then the target data can be queried through the target storage location and sent.
[0052] Thus, in the embodiment of the present invention, by storing the target index strategy that is the same as the query strategy in the query request, the target data can be directly queried according to the target storage location corresponding to the target index strategy in the full index, thereby reducing the time of data query in the deep page query scenario, reducing the resource consumption of data query, and improving the performance of the server. Especially for scenarios with large data volume, low data update frequency, and fewer data query conditions, the stored full index can occupy fewer storage resources, thereby achieving the purpose of reducing the resource consumption of data query and improving the performance of the server by occupying fewer storage resources. For example, when the distributed database mainly stores historical order data tables, the amount of data is large, but because it is historical order data, the data will not change, and the query conditions may only have relatively few conditions such as department and category, which are also relatively fixed in actual applications. In this scenario, the purpose of reducing the resource consumption of data query and improving the performance of the server can be achieved by the embodiment of the present invention.
[0053] It should be noted that, in the embodiments of the present invention, it can also be used for data query in a distributed response database cluster. The database cluster is a distributed response, which can reduce the load and memory pressure of a single database server while supporting larger data storage and access.
[0054] The embodiment of the present invention provides a method for querying data in a database, which is executed by a first node and can be specifically Figure 1 Node 1 of the server in the system architecture shown, such as Figure 3 As shown, the method includes the following steps.
[0055] S201: receiving a query request sent by a client.
[0056] The query request includes a query strategy.
[0057] When querying data, the client sends a query request, and the query request includes a query strategy so that the server can determine which data the client wants to query based on the query strategy.
[0058] The embodiment of the present invention may be executed by a master node (node 1) in a distributed database, and the query request is sent by a client directly or indirectly (eg, through a device such as a gateway).
[0059] S202: Determine whether a target index strategy that is the same as the query strategy is found.
[0060] Among them, the first node can store the full index in advance, so after receiving the query request, it can be determined whether the full index matching it is stored based on the query strategy. The full index includes the correspondence between the index strategy and the index record, so if the target index strategy that is the same as the query strategy is queried, the full index matching the query strategy can be queried, and then the index record corresponding to the target index strategy can be determined.
[0061] In one implementation of the embodiment of the present invention, when querying data, some clients not only require simple data query, but also need to sort the queried data in a certain way, and require the data returned by the server to be sorted data, so the query strategy can include query conditions, or query conditions and sorting methods. At this time, the first node needs to query the target index strategy with the same query conditions and sorting methods in the full index.
[0062] It should be noted that if the query strategy only includes query conditions, then the data requested by the client is not limited whether it is sorted or in which sorting method it is sorted. Therefore, the first node can determine the index strategy with the same index conditions as the query conditions as the target index strategy without considering whether the target index strategy includes a sorting method.
[0063] S203: If yes, determine a target storage location corresponding to the target index strategy from the full index based on the target index strategy.
[0064] Among them, the full index is used to record the mapping relationship between the index strategy and the data storage location. When the target index strategy that is the same as the query strategy is queried, it means that the full index matching the query strategy is stored in the first node, and then the index record corresponding to the target index strategy can be determined based on the record of the full index, that is, the data storage location that satisfies the target index strategy data, that is, the data storage location that satisfies the query strategy data, that is, the target storage location.
[0065] S204: Send the target data stored in the target storage location to the client.
[0066] After the target storage location is determined in step S203, the data stored in the target storage location, ie, the target data, can be acquired based on the target storage location, and then the target data can be sent. In this step, the first node can send the target data directly to the client.
[0067] It should be noted that the storage location of the full index may not be limited. For example, the server may determine the storage location of the full index based on the usage of storage resources in each node, or the full indexes may be stored in a dispersed manner, that is, a portion is stored in each node, etc. Therefore, the full index matching the query strategy may not be stored in the first node. At this time, after the judgment in step S202, if it is determined that the first node has not queried the target index strategy that is the same as the query strategy, it is necessary to send the query request to each second node, so as to query the full index matching the query strategy from the second node, and then determine the target data.
[0068] Therefore, one implementation of the present invention further includes: if not, sending a query request to the second node; receiving feedback information from the second node; judging whether the second node stores the target index strategy based on the feedback information; if not, querying the target data by a preset method.
[0069] Among them, after the judgment of step S202, if the judgment result is no, a query request is sent to the second node, so that the second node can judge whether it has queried the target index strategy that is the same as the query strategy according to the query request. After the second node obtains the judgment result, it will send feedback information to the first node. Based on the feedback information sent by the second node, the first node can determine whether the second node has stored the target index strategy. If the second node stores the target index strategy, it means that the second node can determine the target data, and the first node does not need to perform any more operations; if the second node does not store the target index strategy, the first node needs to execute the preset method again to query the target data.
[0070] It should be noted that when the server includes multiple second nodes, the first node can send a query request to all the second nodes, and then receive feedback information sent by all the second nodes, and then determine whether there is a second node that stores the target index strategy based on the received feedback information. If a second node stores the target index strategy, the second node can determine the target data; if no second node stores the target index strategy, the first node needs to execute the preset method to query the target data.
[0071] The preset method can be any method that can realize the query of target data, such as querying target data by inverted index. When querying target data by inverted index in the implementation of the present invention, the distributed database can establish an inverted index for each searchable field, and each item in the index table includes an attribute value and the address of each record with the attribute value. Since the attribute value is not determined by the record, but the position of the record is determined by the attribute value, it is called an inverted index.
[0072] In another implementation of the embodiment of the present invention, the method of generating a full index in the first node can be: determining the index strategy of the full index to be generated; obtaining the data storage location corresponding to the index strategy of the full index to be generated; and generating the full index based on the index strategy of the full index to be generated and the data storage location corresponding to the index strategy of the full index to be generated.
[0073] Among them, generating a full index requires first determining the index strategy of the full index to be generated. The index strategy can be determined according to the client's instructions, such as receiving the index strategy sent by the client. The index strategy can also be determined by the first node, for example, by counting the sending frequency of each query strategy in the query request sent by the client. When the sending frequency of the query strategy reaches the frequency preset value, it can be determined that this query strategy is determined as the index strategy to generate a full index, or the first node can count the number of times each query strategy in the query request sent by the client is sent within a time period. When the number of times reaches a certain threshold, it can be determined that this query strategy is determined as the index strategy to generate a full index, wherein the frequency preset value, threshold, and time period values can all be set as needed. After determining the index strategy for the full index to be generated, the data storage location corresponding to the index strategy can be obtained by a preset method or the like, that is, the index record corresponding to the index strategy can be obtained, and then the full index can be generated based on the index strategy for the full index to be generated and the data storage location corresponding to the index strategy for the full index to be generated. In the embodiment of the present invention, multiple customized parameters and interfaces may be opened, for example, a method for determining an index strategy for a full index to be generated, etc. This enables more targeted optimization for some special needs and further improves query performance.
[0074] It should be noted that, in the embodiment of the present invention, each node in the distributed database can asynchronously create and update the full index according to the situation. For example, each node can automatically update the stored full index when it detects that the data in the database has changed and is in an idle state.
[0075] In the embodiment of the present invention, the full index records the mapping relationship between the index strategy and the data storage location, that is, for the target index strategy, the full index has recorded the target storage location of the data corresponding to the target index strategy. Therefore, after receiving the query request, if the target index strategy that is the same as the query strategy is queried, the target storage location can be directly determined based on the full index, and then the target data can be queried. This can reduce the time of distributed database queries, especially deep paging queries, reduce resource waste, and improve query performance.
[0076] The embodiment of the present invention provides another method for querying data in a database, which is executed by the second node and can be specifically Figure 1Node 2, Node 3 or Node 4 of the server in the system architecture shown, such as Figure 4 As shown, the method includes the following steps.
[0077] S301: Receive a query request sent by a first node.
[0078] The query request includes a query strategy.
[0079] When querying data, the client sends a query request, and the query request includes a query strategy so that the server can determine which data the client wants to query based on the query strategy. The first node of the server receives the query request and determines whether the target index strategy that is the same as the query strategy is queried. Since the storage location of the full index is not limited in the implementation of the present invention, if the first node does not query the target index strategy that is the same as the query strategy, it is necessary to send the query request to each second node, so as to query the full index that matches the query strategy from the second node, and then determine the target data.
[0080] S302: Determine whether a target index strategy that is the same as the query strategy is found.
[0081] Among them, the second node can store the full index in advance, so after receiving the query request, it can determine whether the full index matching it is stored based on the query strategy. The full index includes the correspondence between the index strategy and the index record, so if the target index strategy that is the same as the query strategy is queried, the full index matching the query strategy can be queried, and then the index record corresponding to the target index strategy can be determined.
[0082] In one implementation of the embodiment of the present invention, when querying data, some clients not only require simple data query, but also need to sort the queried data in a certain way, and require the data returned by the server to be sorted data, so the query strategy can include query conditions, or query conditions and sorting methods. At this time, the second node needs to query the target index strategy with the same query conditions and sorting methods in the full index.
[0083] It should be noted that if the query strategy only includes query conditions, then the data requested by the client is not limited whether it is sorted and in which sorting method it is sorted. Therefore, the second node can determine the index strategy with the same index conditions as the query conditions as the target index strategy without considering whether the target index strategy includes a sorting method.
[0084] S303: If yes, determine a target storage location corresponding to the target index strategy from the full index based on the target index strategy.
[0085] Among them, the full index is used to record the mapping relationship between the index strategy and the data storage location. When the target index strategy that is the same as the query strategy is queried, it means that the full index matching the query strategy is stored in the second node, and then the index record corresponding to the target index strategy can be determined based on the record of the full index, that is, the data storage location that satisfies the target index strategy data, that is, the data storage location that satisfies the query strategy data, that is, the target storage location.
[0086] S304: Send the target data stored in the target storage location to the client or the first node.
[0087] After the target storage location is determined in step S303, the data stored in the target storage location, ie, the target data, can be acquired based on the target storage location, and then the target data can be sent.
[0088] It should be noted that in this step, the second node can send the determined target data to the first node, so that the first node can send the target data to the client. In addition, the second node can also convert to the function of the first node after determining the target data, that is, it can directly communicate with the device outside the server, and then directly send the target data to the client, thereby reducing the time and resource consumption of the data query process.
[0089] In another implementation of an embodiment of the present invention, the method implemented by the present invention also includes: if yes, sending first feedback information to the first node, the first feedback information indicating that the target index strategy is stored; if not, sending second feedback information to the first node, the second feedback information indicating that the target index strategy is not stored.
[0090] Through the judgment of step S302, if the second node queries the target index strategy that is the same as the query strategy, then while executing step S303, it is necessary to send the first feedback information to the first node, and the first feedback information indicates that the target index strategy is stored, that is, notifying the first node that the target index strategy is stored in the second node, and the target data can be determined. Through the judgment of step S302, if the second node does not query the target index strategy that is the same as the query strategy, it is necessary to send the second feedback information to the first node, and the second feedback information indicates that the target index strategy is not stored, that is, notifying the first node that the target index strategy is not stored in the second node, and the target data cannot be determined, so that the first node can continue to query the target data in other ways.
[0091] It should be noted that if the second node needs to send target data to the client in step S304, then when the second node sends the first feedback information to the first node, it also needs to notify the first node that the second node needs to temporarily convert to the function of the first node so that it can send the target data to the client.
[0092] It should be noted that, in the embodiment of the present invention, each node can generate a full index. In order to save storage resources, a full index can be generated and stored on only one node. After the node generates a full index, it can notify all nodes to prevent repeated generation. The method for determining the node for generating the full index can be determined by the first node according to an allocation algorithm (random, polling, the most free resources, etc.).
[0093] In another implementation of the embodiment of the present invention, the method for generating a full index in the second node can be: receiving the index strategy of the full index to be generated sent by the first node; obtaining the data storage location corresponding to the index strategy of the full index to be generated; and generating the full index based on the index strategy of the full index to be generated and the data storage location corresponding to the index strategy of the full index to be generated.
[0094] Among them, generating a full index requires first determining the index strategy of the full index to be generated. After the first node determines the index strategy and determines that it is generated by the second node, the index strategy can be sent directly to the second node. The first node can determine the index strategy of the full index to be generated according to the client's instructions, such as receiving the index strategy sent by the client, and can also determine the index strategy of the full index to be generated by itself, for example, by counting the frequency of each query strategy in the query request sent by the client. When the sending frequency of the query strategy reaches a preset value, it can be determined that this query strategy is determined as the index strategy to generate the full index. After the second node receives the index strategy of the full index to be generated, it can obtain the data storage location corresponding to the index strategy through a preset method, that is, obtain the index record corresponding to the index strategy, and then generate the full index based on the index strategy of the full index to be generated and the data storage location corresponding to the index strategy of the full index to be generated.
[0095] In the embodiment of the present invention, the full index records the mapping relationship between the index strategy and the data storage location, that is, for the target index strategy, the full index has recorded the target storage location of the data corresponding to the target index strategy. Therefore, after receiving the query request, if the target index strategy that is the same as the query strategy is queried, the target storage location can be directly determined based on the full index, and then the target data can be queried. This can reduce the time of distributed database queries, especially deep paging queries, reduce resource waste, and improve query performance.
[0096] The embodiment of the present invention provides another method for querying data in a database, combining Figure 1 The system architecture shown in Figure 1 Based on the system architecture shown in the figure, the first node is node 1, the second node is node 2, node 3 and node 4, and the data processing flow of node 1 and node 2 is Figure 2 and Figure 3 The method of querying data in the database in the embodiment shown is specifically described. Figure 5 As shown, the method includes the following steps.
[0097] S401: Node 1 receives a query request sent by a client.
[0098] Node 1 is the master node in the distributed database, so the query request sent by the client is received by node 1. The query request includes a query strategy. The embodiment of the present invention takes the query strategy including query condition 1 and sorting by field A as an example to illustrate that the client wants to query data that meets query condition 1 and requires the queried data to be sorted by field A.
[0099] It should be noted that the query request sent by the client is usually a request through an SQL statement or a request using a dedicated JSON query structure for elasticsearch. In a distributed database, the master node receives the query request. In a distributed database, a diversion strategy such as polling can be used to specify which node is the master node. After the master node (node 1) receives the query request, it can first determine whether the query request is legal. If the query request is legal, subsequent processing is performed. The way to determine whether the query request is legal can be: determine whether the format of the SQL request is standardized, or whether the JSON structure conforms to the specifications supported by the database, or determine whether the table or field queried in the query request exists, etc.
[0100] S402: Node 1 determines whether a target index strategy that is the same as the query strategy is found. If so, step S403 is executed; if not, step S404 is executed.
[0101] Among them, after receiving the query request, node 1 can query the target index strategy that is the same as the query strategy based on the query strategy. In the embodiment of the present invention, the query strategy includes query condition 1 and sorting by field A, and the target index strategy also needs to be query condition 1 and sorting by field A. If the full index including the target index strategy is stored in node 1, node 1 can query the target index strategy; if the full index including the target index strategy is not stored in node 1, node 1 cannot query the target index strategy.
[0102] S403: If yes, determine a target storage location corresponding to the target index strategy from the full index based on the target index strategy, and send the target data stored in the target storage location to the client.
[0103] If yes, it means that the full index including the target index strategy is stored in node 1, so the index record corresponding to the target index strategy can be determined from the full index based on the target index strategy, that is, the target storage location. Then node 1 can query the target data stored in the target storage location and send the target data to the client.
[0104] It should be noted that since the query strategy includes the sorting method, that is, sorting by field A, the data returned to the client needs to be data sorted by field A. The target index strategy includes sorting by field A, and the order of the target storage locations in the index record is the order of the corresponding data after sorting by field A. Therefore, after node 1 queries the stored data according to the target storage location, it needs to sort the queried data according to the order of the values in the target storage location and send the sorted data to the client.
[0105] S404: If not, node 1 sends a query request to node 2.
[0106] If not, it means that the full index including the target index strategy is not stored in node 1, and a query request needs to be sent to other second nodes to determine whether there is a second node that stores the full index including the target index strategy. The present invention is implemented using node 2 in the second node as an example for explanation.
[0107] S405: After receiving the query request, node 2 determines whether a target index strategy that is the same as the query strategy is found. If not, step S406 is executed; if yes, step S407 is executed.
[0108] After receiving the query request sent by node 1, node 2 can query the target index strategy that is the same as the query strategy based on the query strategy. If the full index including the target index strategy is stored in node 2, node 2 can query the target index strategy; if the full index including the target index strategy is not stored in node 2, node 2 cannot query the target index strategy.
[0109] S406: If not, node 2 sends feedback information to node 1.
[0110] If not, it means that node 2 does not store the full index including the target index strategy, and needs to send feedback information to node 1 to notify node 1 that it does not store the full index including the target index strategy.
[0111] S407: If yes, node 2 determines a target storage location corresponding to the target index strategy from the full index based on the target index strategy.
[0112] If yes, it means that the full index including the target index strategy is stored in node 2, so the index record corresponding to the target index strategy can be determined from the full index based on the target index strategy, that is, the target storage location. Then node 2 can query the target data stored in the target storage location.
[0113] S408: Node 2 sends feedback information to node 1.
[0114] Among them, after node 2 determines that the full index including the target index strategy is stored, it needs to send feedback information to node 1 to notify node 1 that it has stored the full index including the target index strategy and can query the target data.
[0115] In the embodiment of the present invention, the example of node 2 sending target data to the client after determining that the full index including the target index strategy is stored is used for explanation. Since node 2 is a slave node and needs to be converted to a master node before sending target data to the client, the feedback information sent to node 1 in this step also needs to notify node 1 that it needs to be temporarily converted to a master node in order to send data to the client.
[0116] S409: Node 2 sends the target data stored in the target storage location to the client.
[0117] In the embodiment of the present invention, the example of node 2 sending target data to the client after determining that the full index including the target index policy is stored is used for explanation. After node 2 determines that the full index including the target index policy is stored, it can obtain the target data according to the target storage location, and then send the target data to the client after being converted to the master node.
[0118] S410: After receiving the feedback information from each node, node 1 determines whether there is a node storing the target index strategy. If so, the process ends; if not, step S411 is executed.
[0119] Among them, node 1 does not store the full index including the target index policy. After sending a query request to other second nodes, it will receive feedback information returned by each second node. The feedback information returned by each second node indicates whether it stores the full index including the target index policy. Therefore, after node 1 receives the feedback information from each node, it can determine whether there is a node that stores the target index policy based on the feedback information. If it is determined that there is a node that stores the target index policy, the node can obtain the target data and send it, that is, the query process has been completed and the process can be ended. If it is determined that there is no node that stores the target index policy, it means that no node can obtain the target data based on the full index. At this time, node 1 needs to query the data required by the client through other methods, so step S411 is executed.
[0120] S411: If not, node 1 queries the target data through a preset method.
[0121] The preset method may be any achievable method, such as querying the target data by means of an inverted index.
[0122] It should be noted that, in the embodiment of the present invention, the data processing flow and principle of nodes 3 and 4 are the same as the data processing flow and principle of node 2, and will not be described in detail here.
[0123] In the embodiment of the present invention, the full index records the mapping relationship between the index strategy and the data storage location, that is, for the target index strategy, the full index has recorded the target storage location of the data corresponding to the target index strategy. Therefore, after receiving the query request, if the target index strategy that is the same as the query strategy is queried, the target storage location can be directly determined based on the full index, and then the target data can be queried. This can reduce the time of distributed database queries, especially deep paging queries, reduce resource waste, and improve query performance.
[0124] In order to solve the problems existing in the prior art, an embodiment of the present invention provides a device 500 for querying data in a database, which is arranged at a first node, such as Figure 6 As shown, the device 500 includes:
[0125] The receiving unit 501 is used to receive a query request sent by a client, wherein the query request includes a query strategy;
[0126] A determination unit 502 is used to determine whether a target index strategy that is the same as the query strategy is found;
[0127] A determination unit 503 is configured to, if yes, determine a target storage location corresponding to the target index strategy from the full index based on the target index strategy; wherein the full index is used to record a mapping relationship between the index strategy and the data storage location;
[0128] The sending unit 504 is configured to send the target data stored in the target storage location to the client.
[0129] It should be understood that the manner of implementing the embodiments of the present invention is different from the Figure 3 The method of the illustrated embodiment is the same and will not be repeated here.
[0130] In an implementation of the embodiment of the present invention, the sending unit 504 is further configured to send the query request to the second node if no;
[0131] The receiving unit 501 is further configured to receive feedback information from the second node;
[0132] The determining unit 502 is further configured to determine whether the second node stores the target indexing strategy based on the feedback information;
[0133] The device 500 further includes:
[0134] The query unit is further configured to query the target data by a preset method if no.
[0135] In another implementation of the embodiment of the present invention, the determining unit is further used to determine an indexing strategy for the full index to be generated;
[0136] The device 500 further includes:
[0137] An acquisition unit, used to acquire a data storage location corresponding to the index strategy for the full index to be generated;
[0138] A generating unit is used to generate a full index based on the index strategy of the full index to be generated and the data storage location corresponding to the index strategy of the full index to be generated.
[0139] In another implementation of the embodiment of the present invention, the indexing strategy of the full index to be generated is determined based on the indexing strategy sent by the client, or the indexing strategy of the full index to be generated is determined by the first node.
[0140] In another implementation of the embodiment of the present invention, the index strategy includes an index condition; or, the index strategy includes an index condition and a sorting method;
[0141] The data storage location includes a slice identifier of the storage data slice and a record identifier of the location in the data storage slice.
[0142] It should be understood that the manner of implementing the embodiments of the present invention is different from the Figure 3 The method of the illustrated embodiment is the same and will not be repeated here.
[0143] In the embodiment of the present invention, the full index records the mapping relationship between the index strategy and the data storage location, that is, for the target index strategy, the full index has recorded the target storage location of the data corresponding to the target index strategy. Therefore, after receiving the query request, if the target index strategy that is the same as the query strategy is queried, the target storage location can be directly determined based on the full index, and then the target data can be queried. This can reduce the time of distributed database queries, especially deep paging queries, reduce resource waste, and improve query performance.
[0144] In order to solve the problems existing in the prior art, the embodiment of the present invention provides another device 600 for querying data in a database, which is arranged at a second node, such as Figure 7As shown, the device 600 includes:
[0145] The receiving unit 601 is configured to receive a query request sent by a first node, where the query request includes a query strategy;
[0146] A determination unit 602 is used to determine whether a target index strategy that is the same as the query strategy is found;
[0147] A determination unit 603 is configured to determine, if yes, a target storage location corresponding to the target index strategy from the full index based on the target index strategy; wherein the full index is used to record a mapping relationship between the index strategy and the data storage location;
[0148] The sending unit 604 is configured to send the target data stored in the target storage location to the client or the first node.
[0149] It should be understood that the manner of implementing the embodiments of the present invention is different from the Figure 4 The method of the illustrated embodiment is the same and will not be repeated here.
[0150] In one implementation of an embodiment of the present invention, the sending unit 604 is also used to send first feedback information to the first node, if yes, and the first feedback information indicates that the target index strategy is stored; if not, send second feedback information to the first node, and the second feedback information indicates that the target index strategy is not stored.
[0151] In another implementation of the embodiment of the present invention, the receiving unit 601 is further configured to receive an indexing strategy for the full index to be generated sent by the first node;
[0152] The device 600 further includes:
[0153] An acquisition unit, used to acquire a data storage location corresponding to the index strategy for the full index to be generated;
[0154] A generating unit is used to generate a full index based on the index strategy of the full index to be generated and the data storage location corresponding to the index strategy of the full index to be generated.
[0155] In one implementation of an embodiment of the present invention, the index strategy includes index conditions; or, the index strategy includes index conditions and sorting methods; the data storage location includes a shard identifier of the storage data shard and a record identifier of the location in the data storage shard.
[0156] It should be understood that the manner of implementing the embodiments of the present invention is different from the Figure 4 The method of the illustrated embodiment is the same and will not be repeated here.
[0157] In the embodiment of the present invention, the full index records the mapping relationship between the index strategy and the data storage location, that is, for the target index strategy, the full index has recorded the target storage location of the data corresponding to the target index strategy. Therefore, after receiving the query request, if the target index strategy that is the same as the query strategy is queried, the target storage location can be directly determined based on the full index, and then the target data can be queried. This can reduce the time of distributed database queries, especially deep paging queries, reduce resource waste, and improve query performance.
[0158] According to an embodiment of the present invention, the present invention also provides an electronic device and a readable storage medium.
[0159] The electronic device of the present invention comprises: at least one processor; and a memory communicatively connected to the at least one processor; wherein the memory stores instructions executable by the one processor, and the instructions are executed by the at least one processor so that the at least one processor executes the method for data query in a database provided in an embodiment of the present invention.
[0160] Figure 8 An exemplary system architecture 700 is shown to which the method for querying data in a database or the apparatus for querying data in a database according to an embodiment of the present invention can be applied.
[0161] like Figure 8 As shown, system architecture 700 may include terminal devices 701, 702, 703, network 704 and server 705. Network 704 is used to provide a medium for communication links between terminal devices 701, 702, 703 and server 705. Network 704 may include various connection types, such as wired, wireless communication links or optical fiber cables, etc.
[0162] Users can use terminal devices 701, 702, and 703 to interact with server 705 through network 704 to receive or send messages, etc. Various communication client applications can be installed on terminal devices 701, 702, and 703, such as shopping applications, web browser applications, search applications, instant messaging tools, email clients, social platform software, etc. (only examples).
[0163] The terminal devices 701 , 702 , and 703 may be various electronic devices having a display screen and supporting web browsing, including but not limited to smart phones, tablet computers, laptop computers, and desktop computers.
[0164] The server 705 may be a server that provides various services, such as a backend management server (only an example) that provides support for shopping websites browsed by users using the terminal devices 701, 702, and 703. The backend management server may analyze and process the received data such as product information query requests, and feed back the processing results (such as product information - only an example) to the terminal device.
[0165] It should be noted that the method for querying data in a database provided in the embodiment of the present invention is generally executed by the server 705 , and accordingly, the device for querying data in a database is generally set in the server 705 .
[0166] It should be understood that Figure 8 The number of terminal devices, networks and servers in the embodiment is only for illustration. Any number of terminal devices, networks and servers may be provided according to implementation requirements.
[0167] Reference below Fig. 9 , which shows a schematic diagram of the structure of a computer system 800 suitable for implementing an embodiment of the present invention. Fig. 9 The computer system shown is only an example and should not bring any limitation to the functions and scope of use of the embodiments of the present invention.
[0168] like Fig. 9 As shown, the computer system 800 includes a central processing unit (CPU) 801, which can perform various appropriate actions and processes according to a program stored in a read-only memory (ROM) 802 or a program loaded from a storage part 808 into a random access memory (RAM) 803. In the RAM 803, various programs and data required for the operation of the system 800 are also stored. The CPU 801, the ROM 802, and the RAM 803 are connected to each other via a bus 804. An input / output (I / O) interface 805 is also connected to the bus 804.
[0169] The following components are connected to the I / O interface 805: an input section 806 including a keyboard, a mouse, etc.; an output section 807 including a cathode ray tube (CRT), a liquid crystal display (LCD), etc., and a speaker, etc.; a storage section 808 including a hard disk, etc.; and a communication section 809 including a network interface card such as a LAN card, a modem, etc. The communication section 809 performs communication processing via a network such as the Internet. A drive 810 is also connected to the I / O interface 805 as needed. A removable medium 811, such as a magnetic disk, an optical disk, a magneto-optical disk, a semiconductor memory, etc., is installed on the drive 810 as needed, so that a computer program read therefrom is installed into the storage section 808 as needed.
[0170] In particular, according to the embodiments disclosed in the present invention, the process described above with reference to the flowchart can be implemented as a computer software program. For example, the embodiments disclosed in the present invention include a computer program product, which includes a computer program carried on a computer-readable medium, and the computer program includes a program code for executing the method shown in the flowchart. In such an embodiment, the computer program can be downloaded and installed from the network through the communication part 809, and / or installed from the removable medium 811. When the computer program is executed by the central processing unit (CPU) 801, the above-mentioned functions defined in the system of the present invention are executed.
[0171] It should be noted that the computer-readable medium shown in the present invention may be a computer-readable signal medium or a computer-readable storage medium or any combination of the above two. The computer-readable storage medium may be, for example, but not limited to, an electrical, magnetic, optical, electromagnetic, infrared, or semiconductor system, device or device, or any combination of the above. More specific examples of computer-readable storage media may include, but are not limited to: an electrical connection with one or more wires, a portable computer disk, a hard disk, a random access memory (RAM), a read-only memory (ROM), an erasable programmable read-only memory (EPROM or flash memory), an optical fiber, a portable compact disk read-only memory (CD-ROM), an optical storage device, a magnetic storage device, or any suitable combination of the above. In the present invention, a computer-readable storage medium may be any tangible medium containing or storing a program that can be used by or in combination with an instruction execution system, device or device. In the present invention, a computer-readable signal medium may include a data signal propagated in a baseband or as part of a carrier wave, which carries a computer-readable program code. This propagated data signal may take a variety of forms, including but not limited to electromagnetic signals, optical signals, or any suitable combination of the above. Computer-readable signal media may also be any computer-readable medium other than computer-readable storage media, which may send, propagate or transmit a program for use by or in conjunction with an instruction execution system, apparatus or device. The program code contained on the computer-readable medium may be transmitted using any appropriate medium, including but not limited to: wireless, wire, optical cable, RF, etc., or any suitable combination of the above.
[0172] The flow chart and block diagram in the accompanying drawings illustrate the possible architecture, function and operation of the system, method and computer program product according to various embodiments of the present invention. In this regard, each box in the flow chart or block diagram can represent a unit, a program segment, or a part of a code, and the above-mentioned unit, program segment, or a part of a code contains one or more executable instructions for realizing the specified logical function. It should also be noted that in some alternative implementations, the functions marked in the box can also occur in a different order from the order marked in the accompanying drawings. For example, two boxes represented in succession can actually be executed substantially in parallel, and they can sometimes be executed in the opposite order, depending on the functions involved. It should also be noted that each box in the block diagram or flow chart, and the combination of the boxes in the block diagram or flow chart can be implemented with a dedicated hardware-based system that performs a specified function or operation, or can be implemented with a combination of dedicated hardware and computer instructions.
[0173] The units involved in the embodiments of the present invention may be implemented by software or hardware. The units described may also be set in a processor, for example, it may be described as: a processor includes a receiving unit, a query unit, a determination and acquisition unit, and a sending unit. The names of these units do not constitute a limitation on the units themselves in some cases, for example, the receiving unit may also be described as a "unit of the function of the receiving unit".
[0174] As another aspect, the present invention further provides a computer-readable medium, which may be included in the device described in the above embodiment; or may exist independently without being assembled into the device. The above computer-readable medium carries one or more programs, and when the above one or more programs are executed by a device, the device executes the method for querying data in a database provided by the present invention.
[0175] The above specific implementations do not constitute a limitation on the protection scope of the present invention. It should be understood by those skilled in the art that various modifications, combinations, sub-combinations and substitutions may occur depending on design requirements and other factors. Any modification, equivalent substitution and improvement made within the spirit and principle of the present invention shall be included in the protection scope of the present invention.
Claims
1. A method for querying data in a database. It is characterized in that For the first node, including: Receiving a query request sent by a client, wherein the query request includes a query strategy; Determine whether a target index strategy that is the same as the query strategy is found; If so, determining a target storage location corresponding to the target index strategy from the full index based on the target index strategy; wherein the full index is used to record a mapping relationship between the index strategy and the data storage location; Sending the target data stored in the target storage location to the client; Also includes: If not, sending the query request to the second node; receiving feedback information from the second node; Determine whether the second node stores the target index strategy based on the feedback information; If not, query the target data by a preset method; Also includes: Determine the indexing strategy for the full index to be generated; Obtaining a data storage location corresponding to the index strategy for the full index to be generated; A full index is generated based on the index strategy of the full index to be generated and the data storage location corresponding to the index strategy of the full index to be generated.
2. The method according to claim 1, It is characterized in that The indexing strategy of the full index to be generated is determined based on the indexing strategy sent by the client, or the indexing strategy of the full index to be generated is determined by the first node.
3. The method according to claim 1 or 2, It is characterized in that The indexing strategy includes indexing conditions; or, the indexing strategy includes indexing conditions and sorting methods; The data storage location includes a slice identifier of the storage data slice and a record identifier of the location in the data storage slice.
4. A method for querying data in a database. It is characterized in that For the second node, including: receiving a query request sent by a first node, wherein the query request includes a query strategy; Determine whether a target index strategy that is the same as the query strategy is found; If so, determining a target storage location corresponding to the target index strategy from the full index based on the target index strategy; wherein the full index is used to record a mapping relationship between the index strategy and the data storage location; Sending the target data stored in the target storage location to the client or the first node; Also includes: If yes, further sending first feedback information to the first node, wherein the first feedback information indicates that the target index strategy is stored; If not, sending second feedback information to the first node, wherein the second feedback information indicates that the target index strategy is not stored; Also includes: Receiving the indexing strategy for the full index to be generated sent by the first node; Obtaining a data storage location corresponding to the index strategy for the full index to be generated; A full index is generated based on the index strategy of the full index to be generated and the data storage location corresponding to the index strategy of the full index to be generated.
5. A device for querying data in a database, It is characterized in that Set up at the first node, including: A receiving unit, configured to receive a query request sent by a client, wherein the query request includes a query strategy; A judging unit, used to judge whether a target index strategy identical to the query strategy is found; a determination unit, configured to, if yes, determine a target storage location corresponding to the target index strategy from the full index based on the target index strategy; wherein the full index is used to record a mapping relationship between the index strategy and the data storage location; A sending unit, configured to send the target data stored in the target storage location to the client; The sending unit is further used to send the query request to the second node if no; the receiving unit is further used to receive feedback information from the second node; the receiving unit is further used to determine whether the second node stores the target index strategy based on the feedback information; the device also includes: a query unit, further used to query the target data by a preset method if no; The determination unit is also used to determine the index strategy of the full index to be generated; the device also includes: an acquisition unit, used to obtain the data storage location corresponding to the index strategy of the full index to be generated; a generation unit, used to generate the full index based on the index strategy of the full index to be generated and the data storage location corresponding to the index strategy of the full index to be generated.
6. A device for querying data in a database, It is characterized in that Set up on the second node, including: A receiving unit, configured to receive a query request sent by the first node, wherein the query request includes a query strategy; A judging unit, used to judge whether a target index strategy identical to the query strategy is found; a determination unit, configured to, if yes, determine a target storage location corresponding to the target index strategy from the full index based on the target index strategy; wherein the full index is used to record a mapping relationship between the index strategy and the data storage location; A sending unit, configured to send the target data stored in the target storage location to the client or the first node; The sending unit is further configured to send first feedback information to the first node if yes, the first feedback information indicating that the target index strategy is stored; and if no, send second feedback information to the first node, the second feedback information indicating that the target index strategy is not stored; The receiving unit is also used to receive the index strategy of the full index to be generated sent by the first node; the device also includes: an acquisition unit, used to obtain the data storage location corresponding to the index strategy of the full index to be generated; a generation unit, used to generate the full index based on the index strategy of the full index to be generated and the data storage location corresponding to the index strategy of the full index to be generated.
7. An electronic device, It is characterized in that include: one or more processors; a storage device for storing one or more programs, When the one or more programs are executed by the one or more processors, the one or more processors implement the method according to any one of claims 1 to 4.
8. A computer readable medium having a computer program stored thereon, It is characterized in that When the program is executed by a processor, the method according to any one of claims 1 to 4 is implemented.
