Data query method and device, electronic equipment and storage medium
By splitting and converting SQL query statements and generating target graph queries, the problem of cumbersome writing of conversion rules in the existing technology is solved, and efficient SQL-to-graph query conversion is achieved.
Patent Information
- Application Number
- CN202311069954.3
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2023-08-23
- Publication Date
- 2025-02-25
AI Technical Summary
In the prior art, when converting SQL queries into graph queries, conversion rules need to be written manually, which is a lot of work and is difficult to deal with complex queries.
By splitting the target SQL query statement, multiple target subqueries are obtained and each target subquery is converted into the corresponding target graph query, thereby achieving effective conversion of complex SQL queries.
It reduces the workload of manually writing conversion rules, improves the efficiency of converting SQL queries into graph queries, and can effectively deal with complex queries.
Smart Images

Figure CN119513123A_ABST
Abstract
Description
Technical Field
[0001] The present application relates to the field of database technology, and in particular to a data query method and device, an electronic device, and a storage medium. Background Art
[0002] A relational database is a traditional data storage method that can store a variety of entities in the real world. In addition, SQL queries (i.e., relational database queries) can query some information required by users from relational databases, so relational database queries are indispensable in the computer field.
[0003] SQL is a programming language used to manage relational databases, which has a wide range of applications. However, for large-scale data and complex queries, the query efficiency of SQL is limited. To solve this problem, some emerging database technologies, such as graph databases, have emerged. Graph databases can store data in the form of graphs, allowing queries to be performed using graph query languages. Among them, neo4j is a popular open source graph database that supports the Cypher query language.
[0004] In the existing technical solutions, there are some existing methods for converting SQL queries into graph queries (such as neo4j). These existing methods usually use ANTLR tools to convert SQL query statements into abstract syntax trees (ASTs), and then convert ASTs into the form of nodes and edges of neo4j. However, these existing methods usually require manual writing of conversion rules, which is labor-intensive and difficult to handle complex query situations.
[0005] Therefore, in the related art, when converting SQL queries into graph queries, there is a large workload of manually writing conversion rules, and it is difficult to deal with complex queries. Summary of the invention
[0006] The present application provides a data query method and device, an electronic device and a storage medium to at least solve the problems existing in the related art that when converting SQL queries into graph queries, there is a large workload of manually writing conversion rules and it is difficult to cope with complex queries.
[0007] According to one aspect of an embodiment of the present application, a data query method is provided, including:
[0008] Splitting a target SQL query statement to obtain a plurality of target sub-queries, wherein the target SQL query statement is a statement for performing data query in a relational database;
[0009] According to all conditions in each target subquery, a target graph query corresponding to each target subquery is converted, wherein the target graph query is a graph structure for indicating a relationship between any two conditions among all conditions in the corresponding target subquery;
[0010] By querying each target graph query in the relational database, a subquery result corresponding to each target graph query is obtained;
[0011] Based on all sub-query results, a target query result corresponding to the target SQL query statement is obtained.
[0012] Optionally, as in the aforementioned data query method, the target SQL query statement is split to obtain multiple target sub-queries, including:
[0013] Determine the target grammar rule corresponding to the target SQL query statement;
[0014] generating an analyzer corresponding to the target grammar rule;
[0015] The target SQL query statement is converted into an abstract syntax tree by the analyzer;
[0016] A plurality of subtrees are obtained by splitting the abstract syntax tree, and each target subquery represented by each subtree is obtained.
[0017] Optionally, as in the aforementioned data query method, the step of splitting the abstract syntax tree to obtain multiple subtrees includes:
[0018] Determine a query granularity for performing a data query, wherein the query granularity is used to indicate the number of conditions on which each query is based;
[0019] Determine a splitting strategy for splitting the abstract syntax tree according to the query granularity;
[0020] According to the splitting strategy, the abstract syntax tree is split to obtain the multiple subtrees that meet the query granularity.
[0021] Optionally, as in the aforementioned data query method, converting all conditions in each target subquery to obtain a target graph query corresponding to each target subquery includes:
[0022] In the case where the designated subquery corresponds to the main query statement, generating a first designated node corresponding to the designated subquery, wherein the designated subquery is any one of the multiple target subqueries;
[0023] generating a second designated node corresponding to each designated condition of all designated conditions of the designated subquery;
[0024] According to the query type corresponding to each specified condition, generate an edge corresponding to each specified condition;
[0025] Each second designated node is connected to the first designated node through the edge corresponding to each second designated node to obtain the target graph query corresponding to the designated subquery.
[0026] Optionally, as in the aforementioned data query method, querying in the relational database through each target graph query to obtain a subquery result corresponding to each target graph query includes:
[0027] Through the node value of each node in the target graph query, a query is performed in the relational database to obtain the subquery result corresponding to the target graph query, wherein the node value of each node is the value of the condition corresponding to each node;
[0028] Map the subquery result to the root node of the target graph query.
[0029] Optionally, as in the aforementioned data query method, obtaining a target query result corresponding to the target SQL query statement based on all sub-query results includes:
[0030] Merging all sub-query results to obtain a merged data set;
[0031] Deduplication is performed on the merged data set to obtain the target query result.
[0032] Optionally, as in the aforementioned data query method, the target SQL query statement is split to obtain multiple target sub-queries, including:
[0033] Splitting the target SQL query statement to obtain multiple candidate sub-queries;
[0034] The multiple candidate sub-queries are deduplicated to obtain the multiple target sub-queries.
[0035] According to another aspect of the embodiment of the present application, a data query device is also provided, including:
[0036] A splitting module, used to split the target SQL query statement to obtain multiple target sub-queries, wherein the target SQL query statement is a statement used to perform data query in a relational database;
[0037] A conversion module, used for converting, according to all conditions in each target subquery, a target graph query corresponding to each target subquery, wherein the target graph query is a graph structure for indicating a relationship between any two conditions among all conditions in the corresponding target subquery;
[0038] A query module, used to query the relational database through each target graph query to obtain a subquery result corresponding to each target graph query;
[0039] The result module is used to obtain the target query result corresponding to the target SQL query statement based on all sub-query results.
[0040] According to another aspect of the embodiments of the present application, there is also provided an electronic device, including a processor, a communication interface, a memory and a communication bus, wherein the processor, the communication interface and the memory communicate with each other via the communication bus; wherein the memory is used to store a computer program; and the processor is used to execute the method steps in any of the above embodiments by running the computer program stored in the memory.
[0041] According to another aspect of the embodiments of the present application, a computer-readable storage medium is provided, in which a computer program is stored, wherein the computer program is configured to execute the method steps in any of the above embodiments when executed.
[0042] In an embodiment of the present application, a target SQL query statement is split to obtain multiple target sub-queries, and then converted to obtain a target graph query corresponding to each target sub-query, so that a complex target SQL query statement can be split into multiple simple target sub-queries, and a target graph query corresponding to each target sub-query can be obtained, thereby effectively improving the conversion efficiency of the target SQL query statement into a graph query, so that complex queries can be handled; and, through the method of this embodiment, all target sub-queries are automatically identified, and each target sub-query is converted into a corresponding target graph query, so that the workload of manually writing conversion rules can be reduced; thereby, the problem in the related art that the workload of manually writing conversion rules when converting SQL queries into graph queries is large and it is difficult to handle complex queries can be overcome. BRIEF DESCRIPTION OF THE DRAWINGS
[0043] The accompanying drawings, which are incorporated in and constitute a part of this specification, illustrate embodiments consistent with the present application and, together with the description, serve to explain the principles of the present application.
[0044] In order to more clearly illustrate the embodiments of the present application or the technical solutions in the prior art, the drawings required for use in the embodiments or the description of the prior art will be briefly introduced below. Obviously, for ordinary technicians in this field, other drawings can be obtained based on these drawings without paying any creative labor.
[0045] Figure 1 is a schematic diagram of a hardware environment of an optional data query method according to an embodiment of the present application;
[0046] Figure 2 is a flowchart of an optional data query method according to an embodiment of the present application;
[0047] Figure 3 is a flowchart of another optional data query method according to an embodiment of the present application;
[0048] Figure 4 is a flowchart of another optional data query method according to an embodiment of the present application;
[0049] Figure 5 is a structural block diagram of an optional data query device according to an embodiment of the present application;
[0050] Figure 6 It is a structural block diagram of an optional electronic device according to an embodiment of the present application. DETAILED DESCRIPTION
[0051] In order to enable those skilled in the art to better understand the solution of the present application, the technical solution in the embodiments of the present application will be clearly and completely described below in conjunction with the drawings in the embodiments of the present application. Obviously, the described embodiments are only part of the embodiments of the present application, not all of the embodiments. Based on the embodiments in the present application, all other embodiments obtained by ordinary technicians in this field without creative work should fall within the scope of protection of the present application.
[0052] It should be noted that the terms "first", "second", etc. in the specification and claims of the present application and the above-mentioned drawings are used to distinguish similar objects, and are not necessarily used to describe a specific order or sequence. It should be understood that the data used in this way can be interchangeable where appropriate, so that the embodiments of the present application described herein can be implemented in an order other than those illustrated or described herein. In addition, the terms "including" and "having" and any of their variations are intended to cover non-exclusive inclusions, for example, a process, method, system, product or device comprising a series of steps or units is not necessarily limited to those steps or units clearly listed, but may include other steps or units that are not clearly listed or inherent to these processes, methods, products or devices.
[0053] First, some nouns or terms that appear in the description of the embodiments of the present application are subject to the following interpretation:
[0054] 1. SQL (Structured Query Language, relational database): It is a database language with multiple functions such as data manipulation and data definition;
[0055] 2. ANTLR: ANTLR is a tool for grammatical analysis;
[0056] 3. AST: Abstract Syntax Tree;
[0057] 4. Neo4j: Neo4j is a high-performance NOSQL graph database that stores structured data on the network instead of in tables.
[0058] 5. Cypher: Cypher is a declarative graph database query language that is rich in expressiveness and can efficiently query and update graph data.
[0059] According to one aspect of an embodiment of the present application, a data query method is provided. Optionally, in this embodiment, the above data query method can be applied to Figure 1 In the hardware environment composed of terminal 1402 and server 1404 shown in FIG. Figure 1 As shown, server 1404 is connected to terminal 1402 via a network, and can be used to provide services (such as game services, application services, etc.) for the terminal or a client installed on the terminal. A database can be set up on the server or independently of the server to provide data storage services for server 1404.
[0060] The above network may include but is not limited to at least one of the following: wired network, wireless network. The above wired network may include but is not limited to at least one of the following: wide area network, metropolitan area network, local area network, and the above wireless network may include but is not limited to at least one of the following: WIFI (Wireless Fidelity), Bluetooth. The terminal may not be limited to a PC, a mobile phone, a tablet computer, etc.
[0061] The data query method of the embodiment of the present application can be executed by a server, a terminal, or a server and a terminal together. The terminal can also execute the data query method of the embodiment of the present application by a client installed thereon.
[0062] Taking the data query method in this embodiment executed by the server as an example, Figure 2 An optional data query method provided in an embodiment of the present application includes the following steps:
[0063] Step S101 , splitting a target SQL query statement to obtain a plurality of target sub-queries, wherein the target SQL query statement is a statement used to perform data query in a relational database.
[0064] The data query method in this embodiment can be applied to scenarios where data query needs to be performed in a relational database through SQL query statements.
[0065] The target SQL query statement input by the user for performing data query in the relational database can be obtained.
[0066] The target SQL query statement may be a SQL query statement for performing a single-table query or a SQL query statement for performing a multi-table query.
[0067] For example, the SELECT main query statement is: SELECT column1, column2 FROM table1 WHERE column3 = 'value'; wherein, for each clause in the SELECT statement: SELECT is used to indicate the column or expression to be returned, FROM is used to indicate the table from which the data is retrieved, WHERE is used for row-level filtering, and in addition, the following clauses may also be included: GROUD BY (for grouping description), HAVING (for group-level filtering), and ORDER BY (for output sorting order).
[0068] After obtaining the target SQL query statement, the target SQL query statement may be split to obtain multiple target sub-queries.
[0069] The target subquery can be a main query statement or a subquery in the main query statement.
[0070] As an optional embodiment, as in the aforementioned data query method, step S101 splits the target SQL query statement to obtain multiple target sub-queries, including the following steps:
[0071] Step S201, determining a target grammar rule corresponding to a target SQL query statement.
[0072] Optionally, a large number of SQL query statements may be analyzed to determine target grammar rules that may be used as target SQL query statements.
[0073] After determining the target grammar rule, install the ANTLR tool and define the target grammar rule through ANTLR. For example, you can define a query rule, which consists of SELECT, column name (columnList), FROM and table name (tableList). Both column name and table name are identifiers consisting of one or more characters (letters).
[0074] Step S202: Generate a parser corresponding to the target grammatical rule.
[0075] Specifically, after obtaining the target grammar rule defined by ANTLR, the code corresponding to the target grammar rule can be saved, and can be saved as a "SQL.g4" file. Then, the ANTLR tool is used to generate an analyzer corresponding to the target grammar rule, and the analyzer can include a lexical analyzer LEXER and a syntax analyzer PARSER.
[0076] Furthermore, the analyzer may be adapted to analyze any SQL query statement to determine each main query statement and / or each sub-query statement included in the SQL statement to be analyzed.
[0077] Step S203: convert the target SQL query statement into an abstract syntax tree through the analyzer.
[0078] Specifically, after the analyzer is determined, the target SQL query statement can be analyzed by the analyzer.
[0079] For example, a LEXER instance may be created and the PARSER rule may be executed to parse the target SQL query statement to obtain an abstract syntax tree (ie, AST).
[0080] Exemplarily, a specific example of converting a target SQL query statement into an abstract syntax tree is as follows:
[0081] Taking the SQL query statement "SELECT alias FROM user WHERE username = 'admin' GROUP BY username" as an example, the following is the abstract syntax tree (AST) of the SQL query statement "SELECT alias FROM user WHERE username = 'admin' GROUP BY username":
[0082]
[0083]
[0084] The description of this AST hierarchy is as follows:
[0085] (1) The top layer is a QUERY node, which represents a query statement.
[0086] (2) The child node of the QUERY node is a SELECT node, which represents the SELECT statement.
[0087] (3) The child node of the SELECT node is a SELECT_EXPR node, which represents the expression to be queried.
[0088] (4) The child node of the SELECT_EXPR node is an ALIAS node, which represents the alias of the query expression.
[0089] (5) The child node of the ALIAS node is a COLUMN_NAME node, which represents the column name of the alias.
[0090] (6) Another child node of the SELECT_EXPR node is a TABLE_REF node, which represents the table referenced by the query expression.
[0091] (7) The child node of the TABLE_REF node is a TABLE_NAME node, which represents the name of the table.
[0092] (8) The third child node of the SELECT node is a WHERE_CLAUSE node, which represents the WHERE clause of the query.
[0093] (9) The child node of the WHERE_CLAUSE node is a CONDITION node, which represents the condition in the WHERE clause.
[0094] (10) The comparison operator of the Condition node is a COMPARISON_OP node, and its value is an equal sign (=).
[0095] (11) The left child node of the Condition node is an Expression node, which represents the column to be compared.
[0096] (12)The column name of the Expression node is USERNAME.
[0097] (13) The right child node of the Condition node is an Expression node, which represents the value to be compared.
[0098] (14) The value of the Expression node is a string whose value is "admin".
[0099] (15) The last child node of the SELECT node is a GROUP_BY_CLAUSE node, which represents the GROUP BY clause of the query.
[0100] (16) The child node of the GROUP_BY_CLAUSE node is an Expression node, which represents the column to be grouped.
[0101] (17)The column name of the Expression node is USERNAME.
[0102] Step S204: multiple sub-trees are obtained according to the splitting of the abstract syntax tree, and each target sub-query represented by each sub-tree is obtained.
[0103] After obtaining the abstract syntax tree, you can get the root node of the abstract syntax data, and then get the number of child nodes of the root node; then traverse all child nodes (perform the following steps for each child node: get the child node, print the string representation of the child node, and continue to the next child node).
[0104] After all child nodes are determined, multiple subtrees can be obtained in the abstract syntax tree according to the preset splitting logic, and then each target subquery represented by each subtree can be obtained.
[0105] Step S102, based on all conditions in each target subquery, convert to obtain a target graph query corresponding to each target subquery, wherein the target graph query is a graph structure used to indicate the relationship between any two conditions in all conditions in the corresponding target subquery.
[0106] Specifically, after all target sub-queries are determined, all conditions included in each target sub-query may be determined.
[0107] In this embodiment, the condition may be a statement type in the target subquery, a subquery statement type of the subquery statement, and a subquery statement value. The subquery statement type may be, for example, a FROM subquery statement (the corresponding value may be, for example, table1, table2, etc.), a SELECT subquery statement (the corresponding value may be, for example, column1, column2, etc.).
[0108] For any target subquery, after obtaining all conditions of the target subquery, the target subquery can be transformed based on all conditions of the target subquery. The target graph query corresponding to the target subquery can be obtained by establishing a graph structure for indicating the relationship between any two conditions among all conditions in the corresponding target subquery.
[0109] Step S103, querying in the relational database through each target graph query to obtain sub-query results corresponding to each target graph query.
[0110] After obtaining the target graph query corresponding to each target subquery, each target graph query can be used to query in the relational database. For example, the node corresponding to each condition in the target graph query can be used to query in the relational database, and then the query subset corresponding to each condition can be obtained, and then the subquery result corresponding to each target graph query can be obtained based on all query subsets corresponding to the target graph query.
[0111] Step S104: obtaining a target query result corresponding to the target SQL query statement based on all sub-query results.
[0112] After all sub-query results are obtained, all sub-query results may be processed (eg, merged, etc.) to obtain a target query result corresponding to the target SQL query statement.
[0113] As an optional embodiment, as in the aforementioned data query method, step S104 obtains the target query result corresponding to the target SQL query statement based on all sub-query results, including the following steps:
[0114] A merged data set is obtained based on the merging of all sub-query results; and duplicates are removed from the merged data set to obtain the target query result.
[0115] That is, after all sub-query results are obtained, all sub-query results may be merged to obtain a merged data set including all sub-query results; after obtaining the merged data set, further processing (eg, deduplication) is performed to obtain the target query result.
[0116] For example, the UNION statement in Cypher can be used to merge all subquery results into a merged data set. Then, the merged data set is deduplicated by the UNION statement to retain unique rows (i.e., a complete data in a relational database is generally located in the same row), thereby obtaining the target query result.
[0117] The method in this embodiment splits the target SQL query statement to obtain multiple target sub-queries, and converts them to obtain a target graph query corresponding to each target sub-query, so that a complex target SQL query statement can be split into multiple simple target sub-queries, and a target graph query corresponding to each target sub-query can be obtained, thereby effectively improving the conversion efficiency of converting the target SQL query statement into a graph query, so that complex queries can be handled; and, through the method of this embodiment, all target sub-queries are obtained by automatic identification, and each target sub-query is converted into a corresponding target graph query, thereby reducing the workload of manually writing conversion rules; and thus, it can overcome the problem in the related art that when converting SQL queries into graph queries, the workload of manually writing conversion rules is large, and it is difficult to handle complex queries.
[0118] As an optional embodiment, as in the aforementioned data query method, the step S204 of splitting the abstract syntax tree to obtain multiple subtrees includes the following steps:
[0119] Step S301: determining a query granularity for performing a data query, wherein the query granularity is used to indicate the number of conditions on which each query is based.
[0120] Optionally, a query granularity for performing data query through a target SQL query statement may be determined in advance.
[0121] The query granularity may be used to indicate the number of conditions on which each query is based. For example, when the query granularity is each sub-query statement, the query result corresponding to each sub-query statement may be obtained. When the query granularity is the main query statement, the query granularity is the query result corresponding to all conditions (i.e., all sub-query statements) included in the main query statement.
[0122] Step S302: determining a splitting strategy for splitting the abstract syntax tree according to the query granularity.
[0123] After determining the query granularity, a splitting strategy for splitting the abstract syntax tree can be determined. For example, when the query granularity is to query according to the main query statement, the splitting strategy can be to split and obtain a subtree corresponding to each main query statement. When the query granularity is to query according to each sub-query statement, the splitting strategy can be to split and obtain a subtree corresponding to each sub-query statement.
[0124] Step S303: split the abstract syntax tree according to the splitting strategy to obtain multiple subtrees that meet the query granularity.
[0125] After the splitting strategy is determined, the abstract syntax tree can be split according to the splitting strategy, and then multiple subtrees that meet the query granularity can be obtained.
[0126] Through the method in this embodiment, different subtrees can be obtained based on different query granularities, thereby meeting query requirements of different granularities.
[0127] like Figure 3 As shown, as an optional embodiment, as the aforementioned data query method, step S102 converts all conditions in each target subquery to obtain a target graph query corresponding to each target subquery, including the following steps:
[0128] Step S401 : when the designated subquery corresponds to the main query statement, generate a first designated node corresponding to the designated subquery, wherein the designated subquery is any one of the plurality of target subqueries.
[0129] When the designated subquery corresponds to the main query statement, for example, when the designated subquery corresponds to the SELECT main query statement, a first designated node can be generated to indicate the corresponding SELECT main query statement. For example, one representation method of the first designated node is: (SELECT).
[0130] Step S402: Generate a second designated node corresponding to each designated condition of all designated conditions of the designated subquery.
[0131] After the specified subquery is determined, all specified conditions in the specified subquery may be determined, for example: SELECT name, age, FROM employees, WHERE department = 'HR'.
[0132] And generate a second specified node corresponding to each specified condition. Based on the above example, (Column:name) and (Column:age) can be generated according to SELECT name,age, (Table:employees) can be generated according to FROM employees, and (Condition:department='HR') can be generated according to WHERE department='HR'.
[0133] Step S403: Generate an edge corresponding to each specified condition according to the query type corresponding to each specified condition.
[0134] After determining each specified condition, the query type corresponding to each specified condition can also be identified. For example, the query type corresponding to SELECT name, age is SELECTS, the query type corresponding to FROM employees is FROM, and the corresponding edge generated can be "--[FROM]-->", and the query type corresponding to WHERE department = 'HR' is WHERE, and the corresponding edge generated can be "--[WHERE]-->".
[0135] Step S404: Connect each second specified node to the first specified node through the edge corresponding to each second specified node to obtain a target graph query corresponding to the specified subquery.
[0136] After obtaining the second specified node corresponding to each specified condition and the edge corresponding to each specified condition, each second specified node can be connected to the first specified node through the corresponding edge, so as to obtain the target graph query corresponding to the specified subquery.
[0137] Based on the above examples, we can get the following target graph query of Neo4j graph structure:
[0138] (SELECT)--[SELECTS]-->(Column:name);
[0139] (SELECT)--[SELECTS]-->(Column:age);
[0140] (SELECT)--[FROM]-->(Table:employees);
[0141] (SELECT)--[WHERE]-->(Condition:department='HR');
[0142] In this example, (SELECT) represents the entire SELECT main query statement, and the relationship between it and each sub-query statement is represented by an arrow. Each sub-query statement is also represented as a node and has a corresponding label.
[0143] (Column:name) and (Column:age) represent the column names in the SELECT clause. The relationship between them and (SELECT) is indicated by the edge labeled [SELECTS].
[0144] (Table:employees) indicates the table name in the FROM clause. The relationship between it and (SELECT) is indicated by the edge labeled [FROM].
[0145] (Condition:department='HR') represents the condition in the WHERE clause. The relationship between it and (SELECT) is represented by the edge labeled [WHERE].
[0146] This graph structure can be stored and queried using the Neo4j graph database to represent the relationships between elements in SQL queries.
[0147] Through the method of this embodiment, an implementation method is provided that can convert a target subquery in the form of a subtree into a corresponding target graph query, which has the advantages of high conversion efficiency and accuracy.
[0148] like Figure 4 As shown, as an optional embodiment, as the aforementioned data query method, step S103 queries in a relational database through each target graph query to obtain a subquery result corresponding to each target graph query, including the following steps:
[0149] Step S501, querying in a relational database through the node value of each node in the target graph query to obtain a subquery result corresponding to the target graph query, wherein the node value of each node is the value of the condition corresponding to each node.
[0150] Step S502, mapping the sub-query result to the root node of the target graph query.
[0151] For each target graph query, the node value of each node in the target graph query can be used to query in the relational database, and then the subquery result corresponding to the target graph query can be queried. And the subquery result can be mapped to the root node of the target graph query, that is, when the specified subquery corresponds to the main query statement, for example, when the specified subquery corresponds to the SELECT main query statement, the first specified node corresponding to the SELECT main query statement (that is, the root node of the target graph query corresponding to the specified subquery) can be a node (SELECT), and then the subquery result can be mapped to the node (SELECT) (for example, the mapping can be achieved by establishing a corresponding relationship between the subquery result and the root node, or by using the subquery result as the value of the root node).
[0152] For example, assume that a target SQL query statement has the following query statement:
[0153] MATCH(e:Employee);
[0154] WHERE e.department = 'HR';
[0155] RETURN e.name AS name,e.age AS age;
[0156] UNION;
[0157] MATCH(e:Employee);
[0158] WHERE e.department='Marketing';
[0159] RETURN e.name AS name,e.age AS age;
[0160] In the above example, nodes with a specific department attribute value are selected from the node named Employee, and their name and age attributes are returned. Each subquery is a different MATCH and WHERE clause.
[0161] Then, the results of these subqueries are combined into one result set using the UNION statement. In addition, the UNION statement is used to perform a deduplication operation on the results to retain unique rows.
[0162] This example shows how to use the UNION statement of the Cypher query language to combine the results of multiple subqueries into a single result set. You can adjust and expand this example as needed based on your actual situation.
[0163] In addition, in the case where there are two target subqueries:
[0164] (1) Execute the first target subquery:
[0165] (1.1) Match the first root node that matches the first target subquery condition;
[0166] (1.2) Return the subquery result to the first root node;
[0167] (2) Execute the second target subquery:
[0168] (2.1) Match the second root node that matches the second target subquery condition;
[0169] (2.2) Return the subquery result to the second root node;
[0170] (3) Merger results:
[0171] (3.1) Use the UNION statement to combine the results of the first subquery and the second subquery.
[0172] (4) End the process.
[0173] First, the first subquery is executed. In this step, the nodes that match the first subquery condition are matched and the required attributes (that is, the subquery results, that is, the values of the columns required for the query) are returned.
[0174] Next, the second subquery is executed. In this step, the nodes that match the second subquery condition are matched and the required attributes are returned.
[0175] Then, the results of the first subquery and the second subquery are combined using the UNION statement.
[0176] Finally, the process ends.
[0177] By following this process, you can use the UNION statement of the Cypher query language to merge the query results of multiple subqueries into one result set. According to actual needs, you can adjust and expand this process according to the number and conditions of the target subqueries.
[0178] As an optional embodiment, as in the aforementioned data query method, step S101 splits the target SQL query statement to obtain multiple target sub-queries, including:
[0179] The target SQL query statement is split to obtain multiple candidate sub-queries; and the multiple candidate sub-queries are deduplicated to obtain multiple target sub-queries.
[0180] That is, after splitting the target SQL query statement, multiple candidate subqueries are first deduplicated to obtain multiple target subqueries after deduplication, that is, any two target subqueries among the multiple target subqueries are different from each other.
[0181] It should be noted that, for the aforementioned method embodiments, for the sake of simplicity, they are all expressed as a series of action combinations, but those skilled in the art should be aware that the present application is not limited by the described order of actions, because according to the present application, certain steps can be performed in other orders or simultaneously. Secondly, those skilled in the art should also be aware that the embodiments described in the specification are all preferred embodiments, and the actions and modules involved are not necessarily required by the present application.
[0182] Through the description of the above implementation methods, those skilled in the art can clearly understand that the method according to the above embodiment can be implemented by means of software plus a necessary general hardware platform, and of course it can also be implemented by hardware, but in many cases the former is a better implementation method. Based on such an understanding, the technical solution of the present application, or the part that contributes to the prior art, can be embodied in the form of a software product, which is stored in a storage medium (such as ROM (Read-Only Memory, read-only memory) / RAM (Random Access Memory, random access memory), a disk, or an optical disk), and includes a number of instructions for a terminal device (which can be a mobile phone, a computer, a server, or a network device, etc.) to execute the methods described in each embodiment of the present application.
[0183] According to another aspect of the embodiments of the present application, a data query device for implementing the above data query method is also provided. Figure 5 is a structural block diagram of an optional data query device according to an embodiment of the present application, such as Figure 5 As shown, the device may include:
[0184] A splitting module 1 is used to split the target SQL query statement to obtain multiple target sub-queries, wherein the target SQL query statement is a statement used to query data in a relational database;
[0185] A conversion module 2, used for converting, according to all conditions in each target subquery, a target graph query corresponding to each target subquery, wherein the target graph query is a graph structure used to indicate the relationship between any two conditions in all conditions in the corresponding target subquery;
[0186] A query module 3, used to query in a relational database through each target graph query to obtain a subquery result corresponding to each target graph query;
[0187] The result module 4 is used to obtain the target query result corresponding to the target SQL query statement based on all sub-query results.
[0188] It should be noted that the splitting module 1 in this embodiment can be used to execute the above step S101, the conversion module 2 in this embodiment can be used to execute the above step S102, the query module 3 in this embodiment can be used to execute the above step S103, and the result module 4 in this embodiment can be used to execute the above step S104.
[0189] The device in this embodiment, in addition to the above modules, may also include a module for executing any method in any of the above data query method embodiments.
[0190] It should be noted that the examples and application scenarios implemented by the above modules and corresponding steps are the same, but are not limited to the contents disclosed in the above embodiments. It should be noted that the above modules as part of the device can be run in Figure 1 In the hardware environment shown, it can be implemented by software or by hardware, wherein the hardware environment includes a network environment.
[0191] According to another aspect of the embodiments of the present application, an electronic device for implementing the above data query method is also provided. The electronic device may be a server, a terminal, or a combination thereof.
[0192] According to another embodiment of the present application, there is also provided an electronic device, including: Figure 6 As shown, the electronic device may include: a processor 1501 , a communication interface 1502 , a memory 1503 and a communication bus 1504 , wherein the processor 1501 , the communication interface 1502 , and the memory 1503 communicate with each other via the communication bus 1504 .
[0193] Memory 1503, used for storing computer programs;
[0194] The processor 1501 is used to implement the following steps when executing the program stored in the memory 1503:
[0195] Step S101 , splitting a target SQL query statement to obtain a plurality of target sub-queries, wherein the target SQL query statement is a statement used to perform data query in a relational database.
[0196] Step S102, based on all conditions in each target subquery, convert to obtain a target graph query corresponding to each target subquery, wherein the target graph query is a graph structure used to indicate the relationship between any two conditions in all conditions in the corresponding target subquery.
[0197] Step S103, querying in the relational database through each target graph query to obtain sub-query results corresponding to each target graph query.
[0198] Step S104: obtaining a target query result corresponding to the target SQL query statement based on all sub-query results.
[0199] Optionally, in this embodiment, the above-mentioned communication bus can be a PCI (Peripheral Component Interconnect) bus, or an EISA (Extended Industry Standard Architecture) bus, etc. The communication bus can be divided into an address bus, a data bus, a control bus, etc. For ease of representation, only one thick line is used in the figure, but it does not mean that there is only one bus or one type of bus. The communication interface is used for communication between the above-mentioned electronic device and other devices.
[0200] The memory may include a random access memory (RAM) or a non-volatile memory (NVM), such as at least one disk memory. Optionally, the memory may also be at least one storage device located away from the aforementioned processor.
[0201] As an example, the memory 1503 may include but is not limited to the splitting module 1, the conversion module 2, the query module 3 and the result module 4 in the data query device. In addition, it may also include but is not limited to other module units in the data query device, which will not be repeated in this example.
[0202] The above-mentioned processor can be a general-purpose processor, which can include but not be limited to: CPU (Central Processing Unit), NP (Network Processor), etc.; it can also be DSP (Digital Signal Processor), ASIC (Application Specific Integrated Circuit), FPGA (Field-Programmable Gate Array) or other programmable logic devices, discrete gate or transistor logic devices, discrete hardware components.
[0203] An embodiment of the present application further provides a computer-readable storage medium, the storage medium including a stored program, wherein the method steps of the above method embodiment are executed when the program is run.
[0204] Optionally, in this embodiment, the storage medium may include, but is not limited to, various media that can store program codes, such as a USB flash drive, a ROM, a RAM, a mobile hard disk, a magnetic disk, or an optical disk.
[0205] The serial numbers of the above-mentioned embodiments of the present application are for description only and do not represent the advantages or disadvantages of the embodiments.
[0206] If the integrated units in the above embodiments are implemented in the form of software functional units and sold or used as independent products, they can be stored in the above computer-readable storage medium. Based on this understanding, the technical solution of the present application, or the part that contributes to the prior art, or all or part of the technical solution can be embodied in the form of a software product, which is stored in a storage medium and includes several instructions for enabling one or more computer devices (which may be personal computers, servers, or network devices, etc.) to execute all or part of the steps of the methods described in the various embodiments of the present application.
[0207] In the above embodiments of the present application, the description of each embodiment has its own emphasis. For parts that are not described in detail in a certain embodiment, please refer to the relevant description of other embodiments.
[0208] In the several embodiments provided in the present application, it should be understood that the disclosed client can be implemented in other ways. Among them, the device embodiments described above are only schematic. For example, the division of the units is only a logical function division. There may be other division methods in actual implementation. For example, multiple units or components can be combined or integrated into another system, or some features can be ignored or not executed. Another point is that the mutual coupling or direct coupling or communication connection shown or discussed can be through some interfaces, indirect coupling or communication connection of units or modules, which can be electrical or other forms.
[0209] The units described as separate components may or may not be physically separated, and the components shown as units may or may not be physical units, that is, they may be located in one place or distributed on multiple network units. Some or all of the units may be selected according to actual needs to achieve the purpose of the solution provided in this embodiment.
[0210] In addition, each functional unit in each embodiment of the present application may be integrated into one processing unit, or each unit may exist physically separately, or two or more units may be integrated into one unit. The above-mentioned integrated unit may be implemented in the form of hardware or in the form of software functional units.
[0211] The above is only a preferred embodiment of the present application. It should be pointed out that for ordinary technicians in this technical field, several improvements and modifications can be made without departing from the principles of the present application. These improvements and modifications should also be regarded as the scope of protection of the present application.
Claims
1. A data query method, characterized in that: include: Splitting a target SQL query statement to obtain a plurality of target sub-queries, wherein the target SQL query statement is a statement for performing data query in a relational database; According to all conditions in each target subquery, a target graph query corresponding to each target subquery is converted, wherein the target graph query is a graph structure for indicating a relationship between any two conditions among all conditions in the corresponding target subquery; By querying each target graph query in the relational database, a subquery result corresponding to each target graph query is obtained; Based on all sub-query results, a target query result corresponding to the target SQL query statement is obtained.
2. The data query method according to claim 1, characterized in that: The target SQL query statement is split to obtain multiple target sub-queries, including: Determine the target grammar rule corresponding to the target SQL query statement; generating an analyzer corresponding to the target grammar rule; The target SQL query statement is converted into an abstract syntax tree by the analyzer; A plurality of subtrees are obtained by splitting the abstract syntax tree, and each target subquery represented by each subtree is obtained.
3. The data query method according to claim 2, characterized in that: The step of splitting the abstract syntax tree to obtain multiple subtrees includes: Determine a query granularity for performing a data query, wherein the query granularity is used to indicate the number of conditions on which each query is based; Determine a splitting strategy for splitting the abstract syntax tree according to the query granularity; According to the splitting strategy, the abstract syntax tree is split to obtain the multiple subtrees that meet the query granularity.
4. The data query method according to claim 1, characterized in that: The converting, according to all conditions in each target subquery, into a target graph query corresponding to each target subquery comprises: In the case where the designated subquery corresponds to the main query statement, generating a first designated node corresponding to the designated subquery, wherein the designated subquery is any one of the multiple target subqueries; generating a second designated node corresponding to each designated condition of all designated conditions of the designated subquery; According to the query type corresponding to each specified condition, generate an edge corresponding to each specified condition; Each second designated node is connected to the first designated node through the edge corresponding to each second designated node to obtain the target graph query corresponding to the designated subquery.
5. The data query method according to claim 4, characterized in that: The querying in the relational database through each target graph query to obtain a subquery result corresponding to each target graph query includes: Through the node value of each node in the target graph query, a query is performed in the relational database to obtain the subquery result corresponding to the target graph query, wherein the node value of each node is the value of the condition corresponding to each node; Map the subquery result to the root node of the target graph query.
6. The data query method according to any one of claims 1 to 5, characterized in that: The step of obtaining a target query result corresponding to the target SQL query statement based on all sub-query results includes: Merging all sub-query results to obtain a merged data set; Deduplication is performed on the merged data set to obtain the target query result.
7. The data query method according to any one of claims 1 to 5, characterized in that: The target SQL query statement is split to obtain multiple target sub-queries, including: Splitting the target SQL query statement to obtain multiple candidate sub-queries; The multiple candidate sub-queries are deduplicated to obtain the multiple target sub-queries.
8. A data query device, characterized in that: include: A splitting module, used to split the target SQL query statement to obtain multiple target sub-queries, wherein the target SQL query statement is a statement used to query data in a relational database; A conversion module, used for converting, according to all conditions in each target subquery, a target graph query corresponding to each target subquery, wherein the target graph query is a graph structure for indicating a relationship between any two conditions among all conditions in the corresponding target subquery; A query module, used to query the relational database through each target graph query to obtain a subquery result corresponding to each target graph query; The result module is used to obtain the target query result corresponding to the target SQL query statement based on all sub-query results.
9. An electronic device comprising a processor, a communication interface, a memory and a communication bus, wherein: The processor, the communication interface and the memory communicate with each other via the communication bus, wherein: The memory is used to store computer programs; The processor is configured to execute the method steps of any one of claims 1 to 7 by running the computer program stored in the memory.
10. A computer-readable storage medium, characterized in that: The storage medium stores a computer program, wherein the computer program is configured to execute the method steps described in any one of claims 1 to 7 when run.
Citation Information
Cited By
Query optimization method and device for structured query language
CN120448417A