Query optimization method, electronic equipment, storage medium and computer program product
By using the schema defined in the graph database to supplement the missing constraints in the query statement, the graph database query is optimized, and the problem of inefficient query is solved and more efficient query processing is achieved.
Patent Information
- Application Number
- CN202410983777.8
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-07-19
- Publication Date
- 2024-11-12
AI Technical Summary
The prior art has failed to effectively solve this problem due to the lack of constraints in the query statement when querying in the graph database.
By obtaining the schema defined in the graph database, the missing constraints in the original query statement are supplemented, the search space is reduced, and the invalid traversal of vertices and edges that do not conform to the definition of the schema are avoided, and the query efficiency is improved.
By supplementing the constraints in the query conditions, the query process is optimized, which significantly improves the query efficiency of the graph database and reduces resource consumption.
Smart Images

Figure CN118939845A_ABST
Abstract
Description
Technical Field
[0001] One or more embodiments of the present specification relate to the field of graph database technology, and in particular, to a query optimization method and electronic device, storage medium, and computer program product. Background Art
[0002] Graph databases are widely used in many fields such as social network analysis, recommendation systems, and bioinformatics due to their powerful semantic expression capabilities and flexibility. They can effectively store and query data with complex relationships and high interconnectivity. With the advent of the big data era, graph databases have shown unique advantages in processing large-scale graph-structured data and have become an important tool for data management and analysis.
[0003] As the demand for large data graphs grows, efficient query processing in graph databases becomes particularly important. In related technologies, query optimization is an important component of the database management system of graph databases, and its role is to improve the efficiency and performance of data queries. Specifically, the optimizer inside the database management system automatically implements query optimization according to a predefined internal algorithm. For example, the optimizer considers multiple possible execution plans for the received query statement, and then selects a plan with the lowest cost to execute the query.
[0004] However, for the situation where there may be missing constraints in the query statement, the relevant technology has not yet proposed an effective solution. Summary of the invention
[0005] In view of this, one or more embodiments of this specification provide the following technical solutions:
[0006] According to a first aspect of one or more embodiments of this specification, a query optimization method is proposed, including:
[0007] Get the original query statement for the graph database;
[0008] The query condition indicated by the original query statement is optimized according to the pattern information schema defined in the graph database to supplement at least one missing constraint on the nodes and / or edges in the query condition.
[0009] According to a second aspect of one or more embodiments of this specification, an electronic device is proposed, comprising: a processor; a memory for storing processor-executable instructions; wherein the processor implements the steps of the method described in the first aspect by running the executable instructions.
[0010] According to a third aspect of one or more embodiments of the present specification, a computer-readable storage medium is provided, on which computer instructions are stored, and when the instructions are executed by a processor, the steps of the method described in the first aspect are implemented.
[0011] According to a fourth aspect of one or more embodiments of the present specification, a computer program product is provided, comprising a computer program / instruction, which implements the steps of the method described in the first aspect when executed by a processor.
[0012] It can be seen from the above embodiments that the present specification, by obtaining the schema defined in the graph database, can utilize the constraints pre-defined in the schema to supplement at least one constraint missing in the query conditions indicated by the original query statement, thereby relatively reducing the search space when querying the graph database, avoiding invalid traversal of vertices and edges that do not conform to the constraints defined in the schema, thereby helping to improve query efficiency. BRIEF DESCRIPTION OF THE DRAWINGS
[0013] Figure 1 It is a schematic diagram of the architecture of a graph database system provided by an exemplary embodiment.
[0014] Figure 2 It is a schematic diagram of the architecture of a query engine in the related technology.
[0015] Figure 3 It is a schematic diagram of a schema defined in a graph database in the related art.
[0016] Figure 4 It is a schematic diagram of the architecture of a query engine provided by an exemplary embodiment.
[0017] Figure 5 It is a flowchart of a query optimization method provided by an exemplary embodiment.
[0018] Figure 6 It is a schematic diagram of a Pattern Graph containing only edges of fixed length provided by an exemplary embodiment.
[0019] Figure 7 This is a schematic diagram of a Pattern Graph including edges of variable lengths provided by an exemplary embodiment.
[0020] Figure 8 yes Figure 7 Schematic diagram of the two expanded original paths corresponding to the Pattern Graph shown.
[0021] Fig. 9 It is a structural schematic diagram of a device provided by an exemplary embodiment.
[0022] Fig.10 It is a block diagram of a query optimization device provided by an exemplary embodiment. DETAILED DESCRIPTION
[0023] The data involved in this manual (including but not limited to data used for analysis, data stored, data displayed, etc.) are all data authorized by the user or fully authorized by all parties, and the collection, use and processing of relevant data must comply with the relevant laws, regulations and standards of relevant countries and regions, and provide corresponding operation entrances for users to choose to authorize or refuse.
[0024] A graph database is a database that uses a graph structure for semantic queries. It uses "vertices" to represent entities and "edges" to represent relationships between entities. It is a type of NoSQL (non-relational database). Figure 1 First, we introduce the architecture of the graph database system. Figure 1 FIG. 1 is a schematic diagram of the architecture of a graph database system provided by an exemplary embodiment. Figure 1 As shown, the system may include a front end 11 , a query service end 12 and a storage end 13 .
[0025] The front end 11 can be deployed on various types of electronic devices, such as PC (Personal Computer), mobile phones, tablet devices, laptops, PDAs (Personal Digital Assistants), wearable devices (such as smart glasses, smart watches, etc.), etc., and one or more embodiments of this specification do not limit this. During operation, the front end 11 can receive the user's query request and send it to the query server 12, and receive the query results returned by the server 12 to provide to the user for viewing. The front end 11 can also be used by the user to implement other functions, such as configuring and adjusting the query server 12, and managing the data stored in the storage end 13, etc., which are not listed here one by one. Among them, the front end 11 can specifically take the form of a client, which can be a native application installed on the electronic device, or the program on the client side can be a small program, a quick application or other similar forms. The front end 11 can also be in the form of a web page. For example, when using web page technologies such as HTML5 or similar, the relevant functions can be implemented through the page displayed by the browser. The browser here can be an independent browser application or a browser module embedded in certain applications.
[0026] The query server 12 can be deployed on a physical server including an independent host, or a virtual server hosted by a host cluster. During operation, the query server 12 can query the data stored in the storage end 13 according to the query request initiated by the front end 11, and return the query result to the front end 11. In some cases, based on the pre-set processing logic, the query server 12 can also manage the storage end 13 by itself. The query server 12 can also respond to other requests initiated by the front end 11, such as adjusting the configuration of the query server 12.
[0027] The storage end 13 can be deployed on any type of storage device, such as an independent host, or a storage cluster composed of several hosts, etc., for storing data specifically maintained in the graph database, etc. There can be various forms of specific architectures between the query server 12 and the storage end 13. For example, in some smaller-scale deployments, a direct-attached storage (DAS) architecture can be used between the query server 12 and the storage end 13, such as both are deployed in the same physical server. For example, in some more complex scenarios, the storage end 13 can be deployed as a network attached storage (NAS) or a storage area network (SAN), and connected to the device to which the query server 12 belongs through a high-speed network (such as Fibre Channel or Ethernet). In larger-scale deployments, the storage end 13 can be deployed as a distributed file system or object storage, so that data can be redundantly stored on multiple storage devices (such as storage servers), and the query server 12 may need to remotely access these data through specific protocols, which are not listed here one by one.
[0028] At the query server 12, it is usually necessary to deploy a database management system (DBMS), which is specifically a software system that enables users to define, create, retrieve and manage databases. As the core software, DBMS provides an interface for interaction between users and graph databases, and supports data storage, organization, update, retrieval and management functions. Generally, a database management system can include multiple components such as a query engine, a transaction manager, and a storage manager, which work together to efficiently process query requests submitted by users and ensure data consistency and efficient access.
[0029] The query engine is the core component of a database management system. Figure 2 This is a schematic diagram of the architecture of a query engine in the related art. Figure 2As shown, the query engine may include a parser 21, a query optimizer 22, an execution plan generator 23, and an executor 24, etc. The parser 21, the query optimizer 22, and the execution plan generator 23 may also be collectively referred to as a generator 20.
[0030] The parser 21 is used to obtain a query statement for the graph database, which corresponds to a query request initiated by a user. The parser 21 usually parses the query statement into an internal structure that the system can understand and process, such as an Abstract Syntax Tree (AST).
[0031] Based on the AST generated by the parser 21 and the statistical information of the database (such as the size of the table, the distribution of indexes, etc.), the query optimizer 22 transforms the query logic, including selecting appropriate indexes, adjusting the connection order, determining the data access path, etc., in order to explore one or more possible execution plans. The query optimizer 22 does not directly generate the final execution plan, but explores and evaluates different execution strategies.
[0032] The execution plan generator 23 selects an optimal (or relatively optimal) query logic, such as the query logic with the lowest cost (such as the shortest expected execution time or the least resource consumption, etc.), based on the multiple query logics provided by the query optimizer 22, and generates a corresponding execution plan. At this time, the output of the query optimizer 22 (the optimized query logic) is converted into a physical execution plan, which includes a specific sequence of database operation instructions, such as table scans, index searches, join operations, etc.
[0033] The executor 24 actually executes the query operation according to the execution plan generated by the execution plan generator 23. This includes interaction with the storage engine, such as reading indexes, scanning tables, processing data, etc., to obtain query results.
[0034] Similar to relational databases, graph databases also have their own graph query languages to form the query statements required by the query engine. For example, Gremlin is a common graph query language, which is a functional data flow language that allows users to express complex property graph traversal or query in a concise way. It can be written in an imperative (procedural) way, a declarative (descriptive) way, or a hybrid of imperative and declarative. For another example, GSQL is another common graph query language that supports conditional branches, loops, and storage variables. It is a Turing-complete declarative language. Similar to GSQL, Cypher is another common declarative language with the characteristics of humanized design and concise syntax.
[0035] Since Cypher is more concise and easy to understand than other graph query languages, for ease of understanding, the following mainly uses Cypher as an example to describe the query statements and their optimization process involved in this specification. Of course, this does not limit the language type applicable to the technical solutions in this specification. At the same time, for the exemplary query statements listed, this specification will be explained in conjunction with the graph database defined by the SNB (Social Network Benchmark) benchmark test of LDBC (Linked Data Benchmark Council). For example, Figure 3 The following figure shows the schema defined in the graph database used by LDBC's SNB. The schema defines 8 types of vertices and 21 types of edges. For details about the types of vertices, edges, and their relationships, please refer to Figure 3 The instructions in the figure will not be repeated here one by one.
[0036] It should be noted that: this embodiment specifically takes the property graph database as an example, and lists the schema defined therein. In a property graph database, vertex labels (Labels, also called types, etc.), edge types (Edge Types, also called labels, etc.), vertex or label attributes, and data types, etc. can usually be defined in the schema. Of course, in addition to property graph databases, other types of databases can also have schemas. For example, in an RDF (Resource Description Framework) graph database, the embodiment of the schema is usually defined by RDFS (RDF Schema) or OWL (Web Ontology Language). These technologies allow the definition of categories (Classes), properties (Properties), and the relationships between them, thereby forming an ontology (Ontology) as the skeleton of the data model. Of course, for ease of understanding, the technical solution of this specification is described below using a property graph database as an example.
[0037] Assume there is a query statement as follows:
[0038] MATCH p=(n1:Comment)-[e1]->(n2)-[e2:workAt]->(n3),(n2)-[e3*1..2]->(n4:Tagclass)
[0039] RETURN n1,n3,n4 LIMIT 30
[0040] In this query statement, a path p is defined, which contains 2 branches. Branch 1 contains vertices n1, n2, and n3, with edge e1 between vertices n1 and n2 and edge e2 between vertices n2 and n3. Branch 2 contains vertices n2 and n4, with edge e3 between vertices n2 and n4. This query statement ultimately needs to output the values of vertices n1, n3, and n4 on the path that meet the conditions, and the number is limited to 30. In the query conditions indicated by this query statement, there are missing constraints on vertices and edges, including: vertices n2, n3 and edges e1, e3 are all missing type labels, and edge e3 is a variable-length edge with a length of 1 or 2.
[0041] As mentioned above, the query optimizer 22 in the related art will provide the query logic with the lowest cost relatively on the basis of the above query statement, so that the execution plan generator 23 can generate the corresponding execution plan. However, the above process is implemented under the premise that there are missing constraints in the query statement, resulting in the execution plan finally generated having tentative query operations due to the missing constraints, resulting in a waste of relevant query resources and affecting the query efficiency.
[0042] Specifically, for branch 1 of the above query statement, the execution plan generated by the related technology usually adopts the following operation logic: since the type of vertex n1 has been determined to be Comment, it usually uses the NodeByLabelScan operator, which binds all vertices matching the Comment type in the graph database to the starting vertex variable; then, since edge e1 lacks a type label, the Expand operator is usually used to traverse the edges related to all vertices bound to the starting vertex variable. Figure 3 It can be seen that there are four types of edges with Comment as the starting vertex, namely replyOf, commentisLocatedin, commentHasCreator, and commentHasTag, and there are five types of connected vertices, namely Comment, Post, Place, Person, and Tag.
[0043] However, by combining the edge e2 of type workAt and Figure 3 From the schema shown, we can see that only vertices of type Person and Organisation are connected by edges of type workAt, so vertex n2 can only be of type Person and vertex n3 can only be of type Organisation. Accordingly, we can infer that edge e1 can only be an edge of type commentHasCreator between Comment and Person, and cannot be any of the other three types mentioned above, and the corresponding traversal process in the above operation logic is unnecessary.
[0044] Furthermore, for branch 2 of the above query statement, the execution plan generated by the relevant technology usually adopts the following operation logic: First, based on branch 1, it can be determined that the label type of vertex n2 is specifically Person, but because edge e3 is a variable-length edge, when expanding the edge based on this, it is necessary to traverse all possible edge lengths separately. For example, the edge length of edge e3 may be 1 or 2, and they need to be traversed separately.
[0045] However, combined with the vertex n2 being of type Person, n4 being of type Tagclass, and Figure 3 From the schema shown, it can be seen that Person and Tagclass must be connected through a vertex of type Tag, that is, the length of edge e3 must be 2, and the specific path must be extended from Person to Tag through the outgoing relationship hasInterest, and then from Tag to Tagclass through the outgoing relationship hasType. Therefore, it is unnecessary to traverse the edge of length 1 in the related technology.
[0046] It can be seen that the query optimizer 22 in the related art can only perform a certain degree of query optimization based on the constraints contained in the query statement, but cannot eliminate the unnecessary consumption caused by the lack of constraints, such as the lack of type constraints, edge length constraints, etc., which may cause problems such as waste of resources and low query efficiency. The technical solution of this specification solves the above-mentioned deficiencies in the related art by rationally using the schema.
[0047] This specification proposes architectural improvements for the query engine. Figure 4 Provide explanation. Figure 4 FIG. 1 is a schematic diagram of the architecture of a query engine provided by an exemplary embodiment. Figure 4 As shown, the related art provided such as Figure 2 Based on the query engine shown, the query engine of this specification further includes a schema-guided optimizer 41. Specifically, the schema-guided optimizer 41 can form an optimized generator 40 together with the generator 20 in the related art.
[0048] The pattern-guided optimizer 41 has two inputs. One input is the schema defined in the graph database, and the other input is the pattern graph or execution plan generated by the generator 20; wherein, the Pattern Graph and the execution plan are both generated by the generator 20 for the original query statement. Accordingly, the pattern-guided optimizer 41 can optimize the Pattern Graph according to the schema to obtain the optimized Pattern Graph, and rewrite it into an optimized query statement to replace the original query statement for re-query. Alternatively, the pattern-guided optimizer 41 can optimize the execution plan according to the schema to obtain the optimized execution plan, and then provide it to the executor 24 for processing.
[0049] The following will describe in detail how the schema-guided optimizer 41 implements relevant optimization based on the schema.
[0050] Figure 5 FIG. 1 is a flowchart of a query optimization method provided by an exemplary embodiment. Figure 5 As shown, the method may include the following steps:
[0051] Step 502, obtain the original query statement for the graph database.
[0052] The original query statement corresponds to the user through the front end 11 (see Figure 1 ). For example, the original query statement may be included in the query request. For another example, the query request itself may not include the original query statement, and the original query statement may be generated by the query server 12 based on the query request, or by the query server 12 by calling other services and the other services generating the query statement based on the query request. For example, the query request may include query description information provided by the user in text, voice or any other form, and the query server 12 or other services called by it may generate the original query statement based on the query description information. Of course, the original query statement may also be obtained in other ways, and this specification does not limit this.
[0053] The original query statement refers to the query statement initially formed, which is used to distinguish from the optimized query statement mentioned in some embodiments below. Both the original query statement and the optimized query statement are query statements for the graph database, and meet the requirements of relevant statement structure and grammar.
[0054] Step 504: Optimize the query condition indicated by the original query statement according to the pattern information schema defined in the graph database to supplement at least one missing constraint on vertices and / or edges in the query condition.
[0055] For the missing constraints in the query conditions, such as vertex labels (i.e. vertex labels; also called vertex types or vertex types), edge labels (i.e. edge labels; also called edge types or edge types), and the length of an edge is not fixed (i.e. the edge is a variable-length edge), etc., since the schema defined in the graph database may have clearly defined the relevant constraints, it is possible to match them in the schema based on the query conditions to determine the constraints originally missing in the query conditions, and supplement the query conditions based on the constraints.
[0056] Of course, in some embodiments, if the constraints defined by the schema are comprehensive and all missing constraints can be matched from the schema, then the missing constraints of the query conditions can be supplemented. In other embodiments, since the constraints defined by the schema may not be comprehensive or appropriate constraints may not be matched from the schema, it may not be possible to supplement all the missing constraints of the query conditions, and only some of the constraints can be supplemented, but it can still reduce unnecessary resource consumption to a certain extent and improve query efficiency.
[0057] The schema defined in the graph database can be a global schema, that is, the schema is applicable to all data in the graph database, and the schema based on which the above optimization is performed can be the global schema. Of course, the schema defined in the graph database is not necessarily a global schema. For example, for different data sets in the graph database, a separate applicable schema may be defined for one or more data sets, or even a separate applicable schema may be defined for each data set. In this case, if the query scope of the original query statement is the target data set in the graph database, and the target data set is defined with an independent schema, then the schema based on which the optimization is performed can be the independent schema corresponding to the target data set. If the graph database defines both a global schema and an independent schema, the applicable schema can be selected at will. Or, generally speaking, the constraints defined by the independent schema are fewer than those of the global schema, and the matching operations performed when supplementing the query conditions are relatively fewer, so the independent schema can be used to supplement the query conditions first, in order to complete the matching and supplementation of the constraints more quickly.
[0058] The process of optimizing the query conditions indicated by the original query statement according to the pattern information schema defined in the graph database can actually be understood as follows: the original query statement defines the target original path, but there are missing constraints on the vertices and / or edges in the target original path. Therefore, based on the information contained in the target original path, a matching specialized path can be found from the schema to replace the target original path. On the one hand, the specialized path conforms to the original constraints of the target original path, including vertex labels, edge labels, and the connection relationship between vertices and edges. On the other hand, the vertices and edges of the specialized path all adopt the constraints defined in the schema, which is equivalent to supplementing at least a part of the constraints missing from the target original path. The specialized path is the supplemented path, which can be used to optimize the query conditions indicated by the query statement.
[0059] There are many ways to implement the above matching process from the schema. For example, you can obtain the query optimizer (for example, you can refer to Figure 2 The query optimizer 22 shown in the figure processes the original query statement to obtain a pattern graph PatternGraph, from which the target original path can be determined, and then a matching specialized path can be determined from the schema for the target original path.
[0060] The Pattern Graph can be understood as a graphical expression obtained by converting a query statement (such as the original query statement), so that the entities involved in the query (such as vertices) and the relationship between entities (such as edges) can be intuitively reflected. For example, based on the query statement listed above, it is assumed that a slight change is made as follows and used as the original query statement:
[0061] MATCH p=(n1:Comment)-[e1]->(n2)-[e2:workAt]->(n3)
[0062] RETURN n1,n3 LIMIT 30
[0063] The entities defined by the original query statement include vertices n1, n2, and n3, and the relationships between the entities include edges e1 and e2. Among them, vertex n1 is extended to vertex n2 through the out-relationship formed by edge e1, and vertex n2 is extended to vertex n3 through the out-relationship formed by edge e2. Specifically, if it is represented as a Pattern Graph and presented in a visual form, you can refer to Figure 6 , and based on this, the target original path formed by vertices n1, n2, n3 and edges e1, e2 can be formed.
[0064] In the above example of the original query statement, only branch 1 in the query statement example mentioned above is actually considered, and branch 2 is not considered. Assume that a complete query statement containing both branch 1 and branch 2 is considered as the above original query statement. Specifically, if it is represented as a Pattern Graph and presented in a visual form, you can refer to Figure 7 However, in the target original path extracted based on the Pattern Graph, edge e3 is a variable-length edge, which is not conducive to further matching a specialized path from the schema.
[0065] The situation at this time can be understood as follows: for the original query statement, the missing constraints in the query conditions indicated by it include: at least one edge in the target original path is a variable-length edge. Then, according to the optional value of the edge length of each variable-length edge, the target original path can be expanded into multiple extended original paths, each extended original path contains a fixed-length edge corresponding to the variable-length edge, the edge length of the fixed-length edge is one of the optional values of the edge length of the corresponding variable-length edge, and the multiple extended original paths cover all optional value combinations of the edge length of the variable-length edge contained in the target original path; further, specialized paths that match the multiple extended original paths respectively can be determined from the schema as specialized paths that match the target original path.
[0066] Combination Figure 7 In the embodiment shown, the edge e3 is a variable length edge, and the optional values of its edge length are 1 and 2. Since there is only one variable length edge in this embodiment, there are only two optional value combinations of the edge length of the variable length edge contained in the target original path, which correspond to the above two optional values. Accordingly, the target original path can be expanded into two expanded original paths, which correspond to the two optional values. Specifically, Figure 8 The two expanded original paths are presented graphically. Figure 8 (a) in FIG. 1 illustrates the case where the optional value is 1, where vertex n2 is directly extended to vertex n4 through edge e3_0 with an outgoing relationship; and Figure 8 (b) in FIG. 1 illustrates a case where the optional value is 2, where vertex n2 is extended to vertex n5 through edge e3_1 with an out-relationship, and vertex n5 is extended to vertex n4 through edge e3_2 with an out-relationship.
[0067] In more complex cases, if there are multiple variable-length edges in the target original path, it is necessary to consider the combinations of the optional values of each of these variable-length edges, and form an extended original path for each optional value combination. For example, assuming that the target original path contains variable-length edges e_m and variable-length edges e_n, there are x optional values for the length of edge e_m, and there are y optional values for the length of edge e_n, then the number of optional value combinations formed is x*y. For example, when x is 2 and y is 3, the optional value combination is 2*3=6. Then, it is necessary to generate x*y extended original paths corresponding to the target original path for the above optional value combinations.
[0068] Then, no matter it is the target original path or the expanded original path, once the influence of the variable-length edges is eliminated, that is, the edges in the original path are all fixed-length edges, the corresponding specialized paths can be matched from the schema in a unified way. Taking any original path as an example, when the edges of any original path are all fixed-length edges, the specialized path matching any original path can be determined from the schema in the following way.
[0069] First, match the vertices. You can recursively match each vertex in any original path with the vertex labels defined in the schema. For each vertex in any original path, there may be two situations:
[0070] In case 1, the vertex does not contain a vertex label, that is, the vertex has a missing constraint, specifically, its label is missing. Therefore, based on the original constraint related to the vertex in any original path, the vertex label that meets the original constraint can be determined from the schema, and the determined vertex label can be recorded as associated with the vertex in the summary corresponding to any original path.
[0071] In case 2, the vertex contains a vertex label, that is, there is no missing constraint on the vertex, then the same vertex label defined in the schema can be determined, and the determined vertex label is recorded as associated with the vertex in the summary corresponding to any original path.
[0072] It should be noted that the above summary can also be called a specialized pattern summary. Each original path corresponds to a summary, which is used to describe or characterize the information obtained by matching the corresponding original path in the schema, that is, the information of the matched specialized path. The specific definition of the summary can be adopted in various different ways, and this specification does not limit this. However, for ease of understanding, a reference example is provided below.
[0073] Before defining the data structure of the summary, first define the data structure of the graph database and schema.
[0074] Regarding the graph database. Assume that the graph recorded therein is defined by a tuple G(N, E, ρ, λ, π), where:
[0075] -N is a finite set of vertices;
[0076] -E is a finite set of edges such that
[0077] -ρ is a total function that maps edges to vertex pairs, which can be expressed by the formula: E→(N×N)
[0078] -λ is a total function that maps vertices and edges to label sets (including the empty set), which can be expressed by the formula: (N∪E)→Ρ(L), where Ρ(*) represents the power set of the set *;
[0079] -π is a partial function that maps vertex / edge attributes to value sets, which can be expressed by the formula: (N∪E)×P→Ρ(V).
[0080] Among them, L, P, V, and T represent the label set, attribute name set, attribute value set, and data type set, respectively.
[0081] About schema. It is defined by tuple S = (TN, TE, β, δ), where:
[0082] -TN is a finite set of labels representing vertex types, and satisfies
[0083] -TE is a finite set of labels representing edge types, satisfying And TE and TN do not intersect;
[0084] -β is a local function that defines the attributes of vertex type and edge type, as well as the data type of the corresponding value, which can be expressed by the formula: (TN∪TE)×P→T;
[0085] -δ is a local function that defines the edge types allowed between a given pair of vertex types and can be expressed by the formula: (TN, TN)→Ρ(TE).
[0086] Furthermore, G is considered valid for S if the following conditions are met, that is, the corresponding schema is defined for the graph database, and in this specification, the query statement initiated for the graph database can be optimized based on the schema:
[0087] Applies to
[0088] Applies to
[0089] ρ(e)=(n,n′), satisfying the existence of le∈δ(t,t′) such that le∈λ(e), t∈λ(n) and t′∈λ(n′).
[0090] For any attribute op of any vertex or edge o, let π(o, op) = v∈V, satisfying that there exists to∈λ(o) such that β(to, p) = type(v)
[0091] Based on the above definition, assuming that the Pattern Graph is represented by parameter Q, its corresponding summary can be defined as a tuple R(MN, ME, Ψ, MP), where:
[0092] -MN is the total function that maps vertex variables in Q to vertex types in S, that is, assigning a unique label type to each vertex variable in Q, which can be expressed by the formula: QN→TN, where QN represents the vertex variable in Q; in other words, MN is the summary variable corresponding to the vertex in the summary;
[0093] -ME is a partial function that maps edge variables in Q to a subset of edge types in S, which can be expressed as: QE→Ρ(TE), where QE represents the edge variables in Q; in other words, ME is the summary variable corresponding to the fixed-length edge in the summary;
[0094] -Ψ is the set of all fixed-length path segments Ψ obtained by mapping the variable-length edges in Q;
[0095] -MP is a partial function that maps variable-length edges in Q to a subset of all fixed-length path segments Ψ in this summary, which can be expressed by the formula: QE→Ρ(Ψ); in other words, MP is the summary variable corresponding to the variable-length edges (i.e., variable-length edges) in the summary.
[0096] Based on the above definition, the above text determines the vertex labels corresponding to each vertex after recursively matching each vertex in any original path with the schema, and records them in the MN in the summary R corresponding to the original path. Furthermore, after the recursive matching is completed, for each edge contained in the original path: if the edge does not contain an edge label, the edge label defined for the corresponding vertex label is determined from the schema based on the vertex labels recorded in the summary of the two vertices connected by the edge; if the edge contains an edge label, the same edge label defined in the schema is determined, such as ME which can be specifically recorded in the summary R corresponding to the original path.
[0097] For ease of understanding, the following Figure 6 and Figure 7 Taking the Pattern Graph shown in the figure as an example, the matching process of the corresponding original path in the schema is introduced in detail.
[0098] First introduce Figure 6 The embodiment shown.
[0099] In a simpler case, the target original path formed by the Pattern Graph may have only one, for example Figure 6 The embodiment shown is such a case. However, in the case where the Pattern Graph is relatively complex, even if there are no variable-length edges, it may still be split into multiple target original paths, so that the processing of each target original path will be simpler, and avoiding overly complex paths will affect the subsequent analysis efficiency. For example, the weakly connected areas in the Pattern Graph can be searched by a DFS (Deep First Search) algorithm, and each weakly connected area can be divided into an independent target original path. Of course, other methods can also be applied to the technical solution of this specification to determine the target original path, and this specification does not limit this.
[0100] Then, the target original path needs to be matched in the schema, and a summary corresponding to the target original path is generated based on the matching result. The summary is used to represent the specialized path that the target original path matches in the schema. Figure 3 and Figure 6 , the process may include:
[0101] Assume that Figure 6 The vertex n1 shown in the figure is used as the starting point. The constraints on vertex n1 in the original query statement include: the vertex label is Comment, that is, vertex n1 itself has a corresponding vertex label. Figure 3 The schema shown can match a vertex of the Comment type, so a corresponding summary variable can be generated for the vertex n1, such as R.MN(n1)=Comment, and added to the summary corresponding to the target original path, indicating that the vertex n1 can be mapped to a vertex of the Comment type in the schema.
[0102] Based on the aforementioned recursive matching principle, we further match the adjacent vertex n2 of vertex n1. There is no predefined vertex label for vertex n2, that is, vertex n2 actually lacks direct constraints based on vertex labels in the original query statement. However, there are still other forms of original constraints for vertex n2 in the original query statement, including: ① Comment type vertices can be extended to vertex n2 through out-relationships; ② Vertex n2 can be extended to vertex n3 through out-relationships of workAt type. Accordingly, based on Figure 3 It can be seen that: based on condition ①, the vertices of the Comment type can be extended to vertices of the Comment, Post, Place, Person, Tag and other types through out-relationships, but if further combined with condition ②, it can be known that in fact only vertices of the Person type can be extended to other nodes through out-relationships of the workAt type. Therefore, by combining conditions ① and ②, the type of vertex n2 can be determined as Person, and other types can be excluded. Therefore, the corresponding summary variable can be generated for the vertex n2, such as R.MN(n2)=Person, and added to the summary corresponding to the target original path, indicating that the vertex n2 can be mapped to the vertex of the Person type in the schema.
[0103] Based on the aforementioned recursive matching principle, further matching is performed on the adjacent vertex n3 of vertex n2. Vertex n3 also does not have a pre-defined vertex label, that is, vertex n3 actually lacks direct constraints based on vertex labels in the original query statement. However, there are still other forms of original constraints for vertex n3 in the original query statement, including: other vertices can be extended to the vertex n3 through an out-relationship of the workAt type. At the same time, the reason for adopting the recursive matching method is that the matching results obtained previously can be applied to subsequent matching processes. Therefore, the aforementioned matching results for vertices n1 and n2 can actually be used as constraints for vertex n3, so as to match vertex n3 in the schema. For example, here the vertex type Person matched to vertex n2 can be used as a new condition, combined with the aforementioned original constraints, the final conditions formed include: vertices of type Person can be extended to vertex n3 through an out-relationship of type workAt. Of course, in Figure 3In the defined schema, only vertices of type Person are actually extended to vertices of type Organisation through an out-relationship of type workAt. Therefore, even without combining the vertex type Person matched to the aforementioned vertex n2, based only on the out-relationship of type workAt in the aforementioned original constraint, it can be determined that vertex n3 matches a vertex of type Organisation. However, in some more complex scenarios, if in addition to vertices of type Person being extended to vertices of type Organisation through an out-relationship of type workAt, there is also a vertex of type T different from Person that can be extended to a vertex of type W different from Organisation through an out-relationship of type workAt, the type matched by vertex n3 cannot be directly determined based on the out-relationship of type workAt alone. Instead, it is necessary to further combine the type of vertex n2 as Person to determine that vertex n3 matches a vertex of type Organisation. Therefore, a corresponding summary variable can be generated for the vertex n3, such as R.MN(n3)=Organisation, and added to the summary corresponding to the target original path, indicating that the vertex n3 can be mapped to a vertex of type Organisation in the schema.
[0104] So far, the matching operation of all vertices in the target original path has been completed, and then the edges in the target original path need to be processed.
[0105] For edge e1, the vertices on both sides are n1 and n2 respectively, and specifically vertex n1 is extended to vertex n2 based on edge e1. Since the edge label of edge e1 is not defined in the original query statement, it is necessary to use the aforementioned vertex matching results. Specifically, vertex n1 matches the vertex of Comment type in the schema, vertex n2 matches the vertex of Person type in the schema, and the vertex of Comment type in the schema is extended to the vertex of Person type through the edge of commentHasCreator type, so it can be determined that edge e1 matches the edge of commentHasCreator type in the schema. Therefore, the corresponding edge variable can be generated for the edge e1, such as R.ME(e1) = commentHasCreator, and added to the summary corresponding to the target original path, indicating that the edge e1 can be mapped to the edge of commentHasCreator type in the schema.
[0106] For edge e2, the vertices on both sides are n2 and n3 respectively, and specifically vertex n2 is extended to vertex n3 based on edge e2. The edge label of edge e2 has been defined as workAt in the original query statement. Since vertex n2 matches the vertex of type Person in the schema, vertex n3 matches the vertex of type Organisation in the schema, and the vertex of type Person in the schema is extended to the vertex of type Organisation through the edge of type workAt, it can be determined that edge e2 matches the edge of type workAt in the schema. Therefore, the corresponding edge variable can be generated for edge e2, such as R.ME(e2)=workAt, and added to the summary corresponding to the target original path, indicating that edge e2 can be mapped to the edge of type workAt in the schema.
[0107] because Figure 6 The illustrated embodiment does not involve variable-length edges, so its summary only includes variables MN and ME, but does not involve variables Ψ and MP. The variable MN specifically includes the aforementioned R.MN(n1), R.MN(n2) and R.MN(n3), and the variable ME includes the aforementioned R.ME(e1) and R.ME(e2). Based on the content of the summary, the corresponding specialized path can be determined in the schema, and the specialized path includes: extending from a vertex of the Comment type to a vertex of the Person type through an out-relationship of the commentHasCreator type, and further extending from a vertex of the Person type to a vertex of the Organisation type through an out-relationship of the workAt type.
[0108] Next, let’s introduce Figure 7 The embodiment shown.
[0109] Based on the method described above, Figure 7 The target original path determined in the Pattern Graph shown is only one, and its structure can be borrowed Figure 7 The content shown is represented, that is, the path formed by vertices n1, n2, n3 and n4 and edges e1, e2 and e3, specifically including branch 1 composed of vertices n1, n2 and n3 and edges e1, e2, and branch 2 composed of vertices n2, n4 and edge e3.
[0110] However, since it involves variable length edges, it is necessary to generate paths corresponding to each optional value combination according to the optional value combination of the variable length edges. Specifically, the optional values of the variable length edge e3 are 1 and 2, and the above target original path can be expanded to Figure 8 The expanded original path shown includes Figure 8(a) in FIG. 1 shows the case where the optional value is 1, where vertex n2 is directly extended to vertex n4 through edge e3_0 with an outgoing relationship, and Figure 8 (b) in the figure illustrates a case where the optional value is 2, where vertex n2 is extended to vertex n5 through edge e3_1 with an out-relationship, and vertex n5 is extended to vertex n4 through edge e3_2 with an out-relationship.
[0111] In this scenario, it is necessary to match each extended original path separately to obtain the digest corresponding to each extended original path. Then, the digest corresponding to the target original path actually changes into a digest set, which contains the digests determined by each extended original path corresponding to the target original path.
[0112] against Figure 8 The matching process of the expanded original path 1 corresponding to (a) is as follows:
[0113] Assuming that vertex n1 is taken as the starting point, the matching process for vertex n1 is the same as above. Figure 6 Based on the matching result, a corresponding summary variable can be generated for the vertex n1, such as R.MN(n1)=Comment, and added to the summary corresponding to the expanded original path 1, indicating that the vertex n1 can be mapped to a vertex of the Comment type in the schema.
[0114] Based on the aforementioned recursive matching principle, we further match the adjacent vertex n2 of vertex n1. There is no predefined vertex label for vertex n2, that is, vertex n2 actually lacks direct constraints based on vertex labels in the original query statement. However, there are still other forms of original constraints for vertex n2 in the original query statement, including: ① Comment-type vertices can be extended to vertex n2 through out-relationships; ② Vertex n2 can be extended to vertex n3 through out-relationships of workAt type; ③ Vertex n2 can be extended to Tagclass-type vertices through out-relationships. Accordingly, based on Figure 3 It can be seen that: although based on condition ①, the Comment type vertex can be extended to Comment, Post, Place, Person, Tag and other types of vertices through out-relationships, if further combined with condition ②, it can be known that in fact only the Person type vertex can be extended to other nodes through the workAt type out-relationship. However, further combined with condition ③, that is, requiring the Person type vertex to be connected to the Tagclass type vertex through only one hop, but based on Figure 3It can be seen that: the vertex of type Person needs at least 2 hops to connect to the vertex of type Tagclass, which needs to pass through the vertex of type Tag as a transfer. Therefore, the match for vertex n2 fails, and there is no vertex type matching vertex n2 in the schema.
[0115] Therefore, the match for the extended original path 1 fails, and there is no specialized path matching it in the schema. The digest for the extended original path 1 described above is illegal, so the digest corresponding to the extended original path 1 will not be obtained in the end.
[0116] Next, for Figure 8 The matching process of the expanded original path 2 corresponding to (b) is as follows:
[0117] Assuming that vertex n1 is taken as the starting point, the matching process for vertex n1 is the same as above. Figure 6 Based on the matching result, a corresponding summary variable can be generated for the vertex n1, such as R.MN(n1)=Comment, and added to the summary corresponding to the expanded original path 2, indicating that the vertex n1 can be mapped to a vertex of the Comment type in the schema.
[0118] Based on the aforementioned recursive matching principle, the adjacent vertex n2 of vertex n1 is further matched. There is no pre-defined vertex label for vertex n2, that is, vertex n2 actually lacks direct constraints based on vertex labels in the original query statement. However, there are still other forms of original constraints for vertex n2 in the original query statement, including: ① Comment-type vertices can be extended to vertex n2 through out-relationships; ② Vertex n2 can be extended to vertex n3 through out-relationships of workAt type; ③ Vertex n2 can be extended to Tagclass-type vertices through 2-hop out-relationships (since the types of vertex n5 and edges e3_1 and e3_2 are unknown, only the number of hops and the direction of the edges need to be considered here). Accordingly, based on Figure 3 It can be seen that: although based on condition ①, the Comment type vertex can be extended to Comment, Post, Place, Person, Tag and other types of vertices through outgoing relationships, if further combined with condition ②, it can be known that in fact only the Person type vertex can be extended to other nodes through the workAt type outgoing relationship. Further combined with condition ③, that is, requiring the Person type vertex to be connected to the Tagclass type vertex through 2 hops, based on Figure 3It can be seen that a vertex of type Person can be connected to a vertex of type Tagclass via 2 hops, so by combining conditions ①, ②, and ③, the type of vertex n2 can be determined as Person, while other types are excluded. Therefore, a corresponding summary variable can be generated for the vertex n2, such as R.MN(n2) = Person, and added to the summary corresponding to the original path 2 after expansion, indicating that the vertex n2 can be mapped to a vertex of type Person in the schema.
[0119] Based on the aforementioned recursive matching principle, we further match the adjacent vertex n3 of vertex n2. The matching process for vertex n2 is the same as that described above. Figure 6 Therefore, a corresponding summary variable can be generated for the vertex n3, such as R.MN(n3)=Organisation, and added to the summary corresponding to the expanded original path 2, indicating that the vertex n3 can be mapped to a vertex of the Organisation type in the schema.
[0120] Based on the aforementioned recursive matching principle, further matching is performed on the adjacent vertex n5 of vertex n2. There is no pre-defined vertex label for vertex n5, that is, vertex n5 actually lacks direct constraints based on vertex labels in the original query statement. However, there are still other forms of original constraints for vertex n5 in the query conditions formed by the original query statement, including: vertex n2 can be extended to vertex n5 through an out-relationship, and vertex n5 can be extended to vertices of the Tagclass type through an out-relationship. At the same time, the vertex type Person matched to vertex n2 can be used as a new condition. Combined with the aforementioned original constraints, the final conditions formed include: ① Vertices of the Person type can be extended to the vertex n5 through an out-relationship; ② The vertex n5 can be extended to vertices of the Tagclass type through an out-relationship. Accordingly, based on Figure 3 It can be seen that: although based on condition ①, the vertex of type Person can be extended to vertices of types such as Comment, Post, Place, Person, Tag, Organisation, etc. through out-relationships, if further combined with condition ②, it can be known that in fact only vertices of type Tag and Tagclass can be extended to vertices of type Tagclass through out-relationships. Therefore, by combining conditions ① and ②, the type of vertex n5 can be determined as Tag, and other types can be excluded. Therefore, a corresponding summary variable can be generated for the vertex n5, such as R.MN(n5) = Tag, and added to the summary corresponding to the original path 2 after expansion, indicating that the vertex n5 can be mapped to the Tag type vertex in the schema.
[0121] Based on the aforementioned recursive matching principle, the adjacent vertex n4 of vertex n5 is further matched. Since vertex n4 itself has a vertex tag Tagclass, its matching process can refer to the aforementioned vertex n1 and will not be repeated here. Based on the matching results, a corresponding summary variable can be generated for the vertex n4, such as R.MN(n4) = Tagclass, and added to the summary corresponding to the expanded original path 2, indicating that the vertex n4 can be mapped to a vertex of the Tagclass type in the schema.
[0122] So far, the matching operation of all vertices in the expanded original path 2 has been completed, and then the edges in the expanded original path 2 need to be processed.
[0123] For the processing of edges e1 and e2, please refer to Figure 6 The illustrated embodiment will not be described in detail here. Based on the processing results, a corresponding edge variable can be generated for the edge e1, such as R.ME(e1)=commentHasCreator, and added to the summary corresponding to the original path 2 after expansion, indicating that the edge e1 can be mapped to a vertex of the commentHasCreator type in the schema. In addition, a corresponding edge variable can be generated for the edge e2, such as R.ME(e2)=workAt, and added to the summary corresponding to the original path 2 after expansion, indicating that the edge e2 can be mapped to an edge of the workAt type in the schema.
[0124] For edge e3_1, the vertices on both sides are n2 and n5 respectively, and specifically vertex n2 is extended to vertex n5 based on edge e3_1. Since the edge label of edge e3_1 is not defined in the original query statement, it is necessary to use the aforementioned vertex matching results. Specifically, vertex n2 matches the vertex of type Person in the schema, vertex n5 matches the vertex of type Tag in the schema, and the vertex of type Person in the schema is extended to the vertex of type Tag through the edge of type hasInterest, so it can be determined that edge e3_1 matches the edge of type hasInterest in the schema. Therefore, the corresponding edge variable can be generated for edge e3_1, such as R.ME(e3_1) = hasInterest, and added to the summary corresponding to the original path 2 after expansion, indicating that edge e3_1 can be mapped to the edge of type hasInterest in the schema.
[0125] For edge e3_2, the vertices on both sides are n5 and n4 respectively, and specifically vertex n5 is extended to vertex n4 based on edge e3_2. Since the edge label of edge e3_2 is not defined in the original query statement, it is necessary to use the aforementioned vertex matching results. Specifically, vertex n5 matches the vertex of Tag type in the schema, vertex n4 matches the vertex of Tagclass type in the schema, and the vertex of Tag type in the schema is extended to the vertex of Tagclass type through the edge of hasType type, so it can be determined that edge e3_2 matches the edge of hasType type in the schema. Therefore, the corresponding edge variable can be generated for the edge e3_2, such as R.ME(e3_2) = hasType, and added to the summary corresponding to the original path 2 after expansion, indicating that the edge e3_2 can be mapped to the edge of hasType type in the schema.
[0126] because Figure 8 The illustrated embodiment involves variable-length edges, so its summary includes, in addition to variables MN and ME, variables Ψ and MP related to the variable-length edges. For the above-mentioned extended original path 2, the variable MN specifically includes the aforementioned R.MN(n1), R.MN(n2), R.MN(n3), R.MN(n4) and R.MN(n5). The variable ME includes the aforementioned R.ME(e1), R.ME(e2), R.ME(e3_1) and R.ME(e3_2). The variable Ψ is used to record the fixed-length path segment corresponding to the variable-length edge e3 in the extended original path 2 after expansion, specifically including the edge e3_1, vertex n5 and edge e3_2. The variable MP is used to record the labels involved in the fixed-length path segments in the variable Ψ, specifically involving the labels of the edge e3_1, vertex n5 and edge e3_2. Since during the matching process, the label corresponding to vertex n5 is recorded as R.MN(n5) in the variable MN, and the labels corresponding to edges e3_1 and e3_2 are recorded as R.ME(e3_1) and R.ME(e3_2) in the variable ME, it is necessary to move R.MN(n5), R.ME(e3_1) and R.ME(e3_2) from variables MN and ME to variables MP respectively.
[0127] Therefore, after adjustment, the summary corresponding to the expanded original path 2 finally records:
[0128] Variable MN: R.MN(n1), R.MN(n2), R.MN(n3), R.MN(n4);
[0129] Variable ME: R.ME(e1), R.ME(e2);
[0130] Variable Ψ: the fixed-length path segment described by edge e3_1, vertex n5, and edge e3_2;
[0131] Variable MP: R.ME(e3_1), R.MN(n5), R.ME(e3_2).
[0132] Since the extended original path 1 fails to match, the summary corresponding to the target original path is the summary corresponding to the extended original path 2. Based on the content of the summary, the corresponding specialized path can be determined in the schema, which includes: the vertex of Comment type is extended to the vertex of Person type through the out-relationship of commentHasCreator type, and the vertex of Person type is further extended to the vertex of Organisation type through the out-relationship of workAt type; and the vertex of Person type is also extended to the vertex of Tag type through the out-relationship of hasInterest type, and the vertex of Tag type is extended to the vertex of Tagclass type through the out-relationship of hasType type.
[0133] In some cases, multiple extended original paths may successfully match corresponding specialized paths in the schema, or more than three extended original paths may successfully match at least two specialized paths in the schema, thereby obtaining at least two summaries. Among them, any summary records: summary variables corresponding to vertices, used to record the vertex labels corresponding to each vertex in the target original path in the corresponding specialized path; summary variables corresponding to fixed-length edges, used to record the edge labels corresponding to each fixed-length edge in the target original path in the corresponding specialized path; summary variables corresponding to variable-length edges, used to record the labels involved in the fixed-length path segments corresponding to the variable-length edges in the target original path in the corresponding specialized path.
[0134] In one embodiment, these summaries may be determined to correspond to the original target paths respectively, and the optimization processing solution of this specification may be implemented independently based on each summary.
[0135] In other embodiments, the summaries may be compared: if the summary variables corresponding to the vertices and the summary variables corresponding to the fixed-length edges in at least two summaries are the same, the at least two summaries are merged, so as to perform union processing on the summary variables corresponding to the vertices, fixed-length edges and variable-length edges in the at least two summaries respectively. After such processing, the same MNs and MEs in different summaries can be merged and deduplicated, and then in the subsequent optimization process, multiple repeated searches for the corresponding vertices and edges can be avoided, which helps to reduce resource consumption and improve query efficiency.
[0136] For example, suppose that Figure 3 The schema shown in the figure is modified so that there is an edge of type X between the vertices of type Person and the vertices of type Tagclass, so that the vertices of type Person can be extended to the vertices of type Tagclass through this imaginary edge. Then, reconsider Figure 8 The expanded original path 1 corresponding to (a) is:
[0137] Assuming that vertex n1 is taken as the starting point, the matching process for vertex n1 is the same as above. Figure 6 Based on the matching result, a corresponding summary variable can be generated for the vertex n1, such as R.MN(n1)=Comment, and added to the summary corresponding to the expanded original path 1, indicating that the vertex n1 can be mapped to a vertex of the Comment type in the schema.
[0138] Based on the aforementioned recursive matching principle, we further match the adjacent vertex n2 of vertex n1. There is no predefined vertex label for vertex n2, that is, vertex n2 actually lacks direct constraints based on vertex labels in the original query statement. However, there are still other forms of original constraints for vertex n2 in the original query statement, including: ① Comment-type vertices can be extended to vertex n2 through out-relationships; ② Vertex n2 can be extended to vertex n3 through out-relationships of workAt type; ③ Vertex n2 can be extended to Tagclass-type vertices through out-relationships. Accordingly, based on Figure 3 It can be seen that: although based on condition ①, the vertex of Comment type can be extended to vertices of Comment, Post, Place, Person, Tag and other types through out-relationships, if further combined with condition ②, it can be known that in fact only vertices of Person type can be extended to other nodes through out-relationships of workAt type. Further combined with condition ③, that is, it is required that the vertex of Person type is connected to the vertex of Tagclass type through only one hop, and based on the above modification, it can be known that the vertex of Person type can be extended to the vertex of Tagclass type through the edge of type X. Therefore, combining conditions ①, ② and ③ can determine the type of vertex n2 as Person, and exclude other types. Therefore, the corresponding summary variable can be generated for the vertex n2, such as R.MN(n2)=Person, and added to the summary corresponding to the original path 1 after expansion, indicating that the vertex n2 can be mapped to the vertex of Person type in the schema.
[0139] Based on the aforementioned recursive matching principle, we further match the adjacent vertex n3 of vertex n2. The matching process for vertex n2 is the same as that described above. Figure 6 Therefore, a corresponding summary variable can be generated for the vertex n3, such as R.MN(n3)=Organisation, and added to the summary corresponding to the expanded original path 1, indicating that the vertex n3 can be mapped to a vertex of the Organisation type in the schema.
[0140] Based on the aforementioned recursive matching principle, the adjacent vertex n4 of vertex n2 is further matched. Since vertex n4 itself has a vertex tag Tagclass, its matching process can refer to the aforementioned vertex n1 and will not be repeated here. Based on the matching results, a corresponding summary variable can be generated for the vertex n4, such as R.MN(n4) = Tagclass, and added to the summary corresponding to the expanded original path 1, indicating that the vertex n4 can be mapped to a vertex of the Tagclass type in the schema.
[0141] So far, the matching operation of all vertices in the expanded original path 1 has been completed, and then the edges in the expanded original path 1 need to be processed.
[0142] For the processing of edges e1 and e2, please refer to Figure 6 The illustrated embodiment will not be described in detail here. Based on the processing results, a corresponding edge variable can be generated for the edge e1, such as R.ME(e1)=commentHasCreator, and added to the summary corresponding to the original path 1 after expansion, indicating that the edge e1 can be mapped to a vertex of the commentHasCreator type in the schema. In addition, a corresponding edge variable can be generated for the edge e2, such as R.ME(e2)=workAt, and added to the summary corresponding to the original path 1 after expansion, indicating that the edge e2 can be mapped to an edge of the workAt type in the schema.
[0143] For edge e3_0, the vertices on both sides are n2 and n4 respectively, and specifically vertex n2 is extended to vertex n4 based on edge e3_0. Since the edge label of edge e3_0 is not defined in the original query statement, it is necessary to use the aforementioned vertex matching results. Specifically, vertex n2 matches the vertex of Person type in the schema, vertex n4 matches the vertex of Tagclass type in the schema, and the vertex of Person type in the schema is extended to the vertex of Tagclass type through the edge of type X, so it can be determined that edge e3_0 matches the edge of type X in the schema. Therefore, the corresponding edge variable can be generated for the edge e3_0, such as R.ME(e3_0) = X, and added to the summary corresponding to the original path 1 after expansion, indicating that the edge e3_0 can be mapped to the edge of type X in the schema.
[0144] As mentioned earlier, due to Figure 8 The illustrated embodiment involves variable-length edges, so its summary includes not only variables MN and ME, but also variables Ψ and MP related to the variable-length edges. For the above-mentioned expanded original path 1 and the modified schema, the variable MN specifically includes the aforementioned R.MN(n1), R.MN(n2), R.MN(n3) and R.MN(n4). The variable ME includes the aforementioned R.ME(e1), R.ME(e2) and R.ME(e3_0). The variable Ψ is used to record the fixed-length path segment corresponding to the variable-length edge e3 in the expanded original path 1 after expansion, specifically including the edge e3_0. The variable MP is used to record the labels involved in the fixed-length path segments in the variable Ψ, specifically the label of the edge e3_0. Since the label corresponding to the edge e3_0 is recorded as R.ME(e3_0) in the variable ME during the matching process, it is necessary to move R.MN(e3_0) from the variable ME to the variable MP respectively.
[0145] Therefore, after adjustment, the summary corresponding to the expanded original path 1 finally records:
[0146] Variable MN: R.MN(n1), R.MN(n2), R.MN(n3), R.MN(n4);
[0147] Variable ME: R.ME(e1), R.ME(e2);
[0148] Variable Ψ: the fixed-length path segment described by edge e3_0;
[0149] Variable MP: R.ME(e3_0).
[0150] Combining the summary of the expanded original path 2 mentioned above, and comparing it with the summary of the expanded original path 1 here, it can be seen that the variables MN and ME of the two are the same, and only the variables Ψ and MP are different. Therefore, the summaries of the two expanded original paths can be merged, which specifically involves performing union processing on each summary variable separately.
[0151] Regarding variable MN: Since the variable MN of the two summaries is the same, the variable MN after the union process remains unchanged. For example, in the merged summary, the variable MN is: R.MN(n1), R.MN(n2), R.MN(n3), R.MN(n4);
[0152] Regarding the variable ME: Since the variable ME of the two summaries is the same, the variable ME after the union process remains unchanged. For example, in the merged summary, the variable ME is: R.ME(e1), R.ME(e2);
[0153] Regarding variable Ψ: the variables Ψ of the two summaries are different, so the variable Ψ after the union processing contains the contents recorded in the variables Ψ in the two original summaries. For example, in the merged summary, variable Ψ is: {fixed-length path segment described by edge e3_1, vertex n5 and edge e3_2}, {fixed-length path segment described by edge e3_0};
[0154] Regarding variable MP: the variable MP of the two summaries is different, so the variable MP after the union processing contains the contents recorded in the variable MP of the two original summaries. For example, in the merged summary, the variable MP is: {R.ME(e3_1), R.MN(n5), R.ME(e3_2)}, {R.ME(e3_0)}.
[0155] The Pattern Graph is optimized based on the merged summary, so that when the corresponding execution plan is finally executed, repeated searches for vertices and edges in the variable MN and the variable ME can be avoided. In other words, if no merging is performed, each summary will be used to optimize the Pattern Graph separately, so that when the corresponding execution plan is finally executed, it is necessary to repeatedly search for vertices and edges in the variable MN and the variable ME separately. Of course, even if the summaries are not merged, since the variable-length edges in the target original path can be converted into corresponding fixed-length edges in the expanded original path in this specification, and illegal summaries can be excluded in the process of generating summaries corresponding to each expanded original path, it is possible to avoid searching for vertices and edges on the paths corresponding to these illegal summaries during the execution of the subsequent execution plan, thereby reducing resource consumption and improving query efficiency to a certain extent.
[0156] Based on the above technical solution, a specialized path that matches the original target path can be determined from the schema. Further, as mentioned above, the Pattern Graph can be optimized based on the specialized path, and the optimized Pattern Graph is used to rewrite the optimized query statement to replace the original query statement for re-querying, so that the query engine can process the optimized query statement in the manner of the relevant technology. Then, it is equivalent to adding a query statement before the query engine. Figure 4 The optimized generator 40 shown in the figure can generate optimized query statements based on the technical solution of this specification, while the query engine in the query server can still adopt the structure in the related technology (such as Figure 2 As shown in the figure), no changes are required, so the code intrusion on the query server can be minimized.
[0157] As mentioned above, the query engine in the query server can also be directly replaced or optimized, for example, Figure 4 The structure shown in this example will cause a greater code intrusion to the query server, but the optimized generator 40 can directly output the optimized execution plan, which will be directly executed by the executor 24 without rewriting the query statement, re-entering the query engine for optimization and generating the execution plan, thereby achieving a relatively higher execution efficiency.
[0158] Of course, in the implementation of a specific product, only any one of the above solutions may be provided, or both solutions may be provided at the same time for the user to select the actual solution according to needs, and this specification does not limit this.
[0159] To summarize, this specification, by obtaining the schema defined in the graph database, can utilize the pre-defined constraints in the schema to supplement at least one missing constraint in the query conditions indicated by the original query statement, thereby relatively reducing the search space when querying the graph database, avoiding invalid traversal of vertices and edges that do not conform to the constraints defined in the schema, and thus helping to improve query efficiency.
[0160] Fig. 9 is a schematic structural diagram of a device provided by an exemplary embodiment. Fig. 9At the hardware level, the device includes a processor 902, an internal bus 904, a network interface 906, a memory 908, and a non-volatile memory 910, and may also include hardware required for other functions. One or more embodiments of this specification may be implemented based on software, such as the processor 902 reading the corresponding computer program from the non-volatile memory 910 into the memory 908 and then running it. Of course, in addition to the software implementation, one or more embodiments of this specification do not exclude other implementations, such as logic devices or a combination of software and hardware, etc., that is, the execution subject of the following processing flow is not limited to each logic unit, but can also be hardware or logic devices.
[0161] Please refer to Fig.10 , the query optimizer can be applied to Fig. 9 The device shown in the figure is used to implement the technical solution of this specification. The query optimization device may include:
[0162] A statement acquisition unit 1001 acquires an original query statement for a graph database;
[0163] The constraint supplementation unit 1002 optimizes the query condition indicated by the original query statement according to the pattern information schema defined in the graph database to supplement at least one missing constraint on vertices and / or edges in the query condition.
[0164] Optional,
[0165] When the schema information defined in the graph database is a global schema, the schema based on which the optimization is performed is the global schema;
[0166] The query scope of the original query statement is the target data set in the graph database, and the target data set is defined with an independent schema; wherein the schema based on which the optimization is based is the independent schema corresponding to the target data set.
[0167] Optionally, the constraint supplementing unit 1002 is specifically used for:
[0168] Obtaining a pattern graph Pattern Graph obtained by the query optimizer processing the original query statement;
[0169] For a target original path in which vertices and / or edges in the pattern graph have missing constraints, determine a specialized path matching the target original path from the schema, wherein the vertices and edges of the specialized path all adopt the constraints defined in the schema, and the specialized path complies with the original constraints of the target original path;
[0170] The query condition indicated by the original query statement is optimized according to the specialized path.
[0171] Optionally, when the edges of any original path are all fixed-length edges, determining a specialized path matching any original path from the schema includes:
[0172] Recursively match each vertex contained in any of the original paths with the vertex labels defined in the schema in turn; wherein, for each vertex in any of the original paths: if the vertex does not contain a vertex label, determine a vertex label in the schema that satisfies the original constraint associated with the vertex; if the vertex contains a vertex label, determine the same vertex label defined in the schema, and record the determined vertex label as associated with the vertex in the summary corresponding to the any of the original paths;
[0173] After the recursive matching is completed, for each edge contained in any of the original paths: if the edge does not contain an edge label, then according to the vertex labels recorded in the summary of the two vertices connected by the edge, the edge label defined for the corresponding vertex label is determined from the schema; if the edge contains an edge label, then the same edge label defined in the schema is determined, and the determined edge label type is recorded as associated with the edge in the summary corresponding to the any of the original paths;
[0174] The summary is used to describe the specialized path that matches the corresponding original path.
[0175] Optionally, determining from the schema a specialized path that matches the target original path includes:
[0176] In the case where the constraint missing includes that at least one edge in the target original path is a variable-length edge, according to the optional value of the edge length of each variable-length edge, the target original path is expanded into a plurality of expanded original paths, each of which includes a fixed-length edge corresponding to the variable-length edge, the edge length of the fixed-length edge is one of the optional values of the edge length of the corresponding variable-length edge, and the plurality of expanded original paths cover all optional value combinations of the edge lengths of the variable-length edges contained in the target original path;
[0177] Specialized paths that respectively match the multiple expanded original paths are determined from the schema as specialized paths that match the target original path.
[0178] Optionally, also include:
[0179] A summary acquisition unit matches the multiple extended original paths with the schema respectively, and obtains summaries for characterizing each matched specialized path; wherein any summary records: summary variables corresponding to vertices, for recording vertex labels corresponding to each vertex in the target original path in the corresponding specialized path; summary variables corresponding to fixed-length edges, for recording edge labels corresponding to each fixed-length edge in the target original path in the corresponding specialized path; summary variables corresponding to variable-length edges, for recording labels involved in fixed-length path segments corresponding to variable-length edges in the target original path in the corresponding specialized path;
[0180] The summary merging unit merges the at least two summaries if the summary variables corresponding to the vertices and the summary variables corresponding to the fixed-length edges in at least two summaries are the same, so as to respectively perform union processing on the summary variables corresponding to the vertices, the fixed-length edges and the variable-length edges in the at least two summaries.
[0181] Optionally, the constraint supplementing unit 1002 is specifically used for:
[0182] Optimizing the Pattern Graph generated by the query optimizer when processing the original query statement; wherein the optimized Pattern Graph is used to rewrite the optimized query statement to replace the original query statement for re-querying;
[0183] Alternatively, the execution plan generated by the execution plan generator based on the original query statement is optimized to obtain an optimized execution plan as an input of the executor.
[0184] Based on the same concept as the above method, this specification also provides an electronic device, including: a processor; a memory for storing processor executable instructions; wherein the processor implements the steps of the method described in any of the above embodiments by running the executable instructions.
[0185] Based on the same concept as the above method, this specification also provides a computer-readable storage medium on which computer instructions are stored. When the instructions are executed by a processor, the steps of the method described in any of the above embodiments are implemented.
[0186] Based on the same concept as the above method, this specification also provides a computer program product, including a computer program / instruction, which implements the steps of the method described in any of the above embodiments when executed by a processor.
Claims
1. A query optimization method, comprising: Get the original query statement for the graph database; The query condition indicated by the original query statement is optimized according to the pattern information schema defined in the graph database to supplement at least one missing constraint on vertices and / or edges in the query condition.
2. The method according to claim 1, When the schema information defined in the graph database is a global schema, the schema based on which the optimization is performed is the global schema; The query scope of the original query statement is the target data set in the graph database, and the target data set is defined with an independent schema; wherein, The schema based on which the optimization is performed is the independent schema corresponding to the target dataset.
3. According to the method of claim 1, the step of optimizing the query condition indicated by the original query statement according to the schema information schema defined in the graph database comprises: Obtaining a pattern graph Pattern Graph obtained by the query optimizer processing the original query statement; For a target original path in which vertices and / or edges in the pattern graph have missing constraints, determine a specialized path matching the target original path from the schema, wherein the vertices and edges of the specialized path all adopt the constraints defined in the schema, and the specialized path complies with the original constraints of the target original path; The query condition indicated by the original query statement is optimized according to the specialized path.
4. The method according to claim 3, wherein, when the edges of any original path are all fixed-length edges, determining a specialized path matching any original path from the schema comprises: Recursively match each vertex contained in any of the original paths with the vertex labels defined in the schema in turn; wherein, for each vertex in any of the original paths: if the vertex does not contain a vertex label, determine a vertex label in the schema that satisfies the original constraint associated with the vertex; if the vertex contains a vertex label, determine the same vertex label defined in the schema, and record the determined vertex label as associated with the vertex in the summary corresponding to the any of the original paths; After the recursive matching is completed, for each edge contained in any of the original paths: if the edge does not contain an edge label, then according to the vertex labels recorded in the summary of the two vertices connected by the edge, the edge label defined for the corresponding vertex label is determined from the schema; if the edge contains an edge label, then the same edge label defined in the schema is determined, and the determined edge label type is recorded as associated with the edge in the summary corresponding to the any of the original paths; The summary is used to describe the specialized path that matches the corresponding original path.
5. According to the method of claim 4, determining from the schema a specialized path that matches the target original path comprises: In the case where the constraint missing includes that at least one edge in the target original path is a variable-length edge, according to the optional value of the edge length of each variable-length edge, the target original path is expanded into a plurality of expanded original paths, each of which includes a fixed-length edge corresponding to the variable-length edge, the edge length of the fixed-length edge is one of the optional values of the edge length of the corresponding variable-length edge, and the plurality of expanded original paths cover all optional value combinations of the edge lengths of the variable-length edges contained in the target original path; Specialized paths that respectively match the multiple expanded original paths are determined from the schema as specialized paths that match the target original path.
6. The method according to claim 5, further comprising: Match the multiple extended original paths with the schema respectively, and obtain summaries for characterizing each matched specialized path respectively; wherein any summary records: summary variables corresponding to vertices, used to record vertex labels corresponding to each vertex in the target original path in the corresponding specialized path; summary variables corresponding to fixed-length edges, used to record edge labels corresponding to each fixed-length edge in the target original path in the corresponding specialized path; summary variables corresponding to variable-length edges, used to record labels involved in fixed-length path segments corresponding to variable-length edges in the target original path in the corresponding specialized path; If summary variables corresponding to vertices and summary variables corresponding to fixed-length edges in at least two summaries are the same, the at least two summaries are merged to respectively perform union processing on summary variables corresponding to vertices, fixed-length edges and variable-length edges in the at least two summaries.
7. According to the method of claim 1, optimizing the query condition indicated by the original query statement according to the schema information schema defined in the graph database comprises: Optimizing the Pattern Graph generated by the query optimizer when processing the original query statement; wherein the optimized Pattern Graph is used to rewrite the optimized query statement to replace the original query statement for re-querying; Alternatively, the execution plan generated by the execution plan generator based on the original query statement is optimized to obtain an optimized execution plan as an input of the executor.
8. An electronic device, comprising: processor; A memory for storing processor-executable instructions; wherein the processor implements the steps of the method according to any one of claims 1 to 7 by executing the executable instructions.
9. A computer-readable storage medium having computer instructions stored thereon, which, when executed by a processor, implement the steps of the method according to any one of claims 1 to 7.
10. A computer program product, comprising a computer program / instruction, which, when executed by a processor, implements the steps of the method according to any one of claims 1 to 7.
Citation Information
Patent Citations
Data read-write method, device and equipment of graph database and storage medium
CN114817275A
Making graph pattern queries bounded in big graphs
US20170308620A1
Efficient compilation of bounded recursive graph queries on top of SQL based relational engine
US20240126764A1
