Relational database-oriented graph data query method and device
By implementing graph data structuring and query optimization in relational databases, the problem of low graph data query performance is solved, efficiency is improved, transaction consistency is ensured, and an efficient and scalable graph query solution is provided.
Patent Information
- Application Number
- CN202410603077.1
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-05-15
- Publication Date
- 2024-08-27
AI Technical Summary
Relational databases have low performance and efficiency when processing graph data, and migrating data to graph databases has insufficient transaction atomicity and consistency guarantees, and the migration process is time-consuming and costly.
Implement efficient graph algorithms and query optimization strategies in relational databases. By structuring graph data, generate graph vertex tables and graph relationship tables, and use the Lynx graph query framework to convert graph queries into SQL statements and execute them in relational databases. execution, ensuring transaction consistency and low cost.
It improves the efficiency of relational databases in graph queries, ensures transaction consistency and reduces migration costs, and provides an efficient and scalable graph query solution.
Smart Images

Figure CN118550931A
Abstract
Description
Technical Field
[0001] The present invention relates to the fields of graph data, big data technology, database technology, etc., and proposes a graph data query method and device for a relational database. Background Art
[0002] Relational databases play an indispensable role in a wide range of application areas and have become the most mainstream and widely adopted database type. Their application scope covers enterprise-level systems and information management and data storage needs in various industries. Characterized by their structured data storage methods and powerful query languages, relational databases provide reliable and efficient data management solutions for enterprises, government agencies, and various organizations. Even in the current technological environment, relational databases are still the preferred database type for many businesses and organizations, providing a solid foundation for data organization, storage, and retrieval.
[0003] With the continuous evolution of information technology, graph data has gradually shown great social and commercial value. This form of data can not only deeply explore the complex relationships between users in social networks, but also optimize the paths in logistics and transportation, and even perform regression analysis of credit crises in financial networks. In response to this trend, many commercial companies and open source organizations at home and abroad have focused on large-scale graph data storage and processing and other related technologies, and have successfully applied them to information retrieval, recommendation systems, data mining and other fields, and have achieved remarkable results.
[0004] However, although graph data is usually stored in relational databases, relational databases do not directly support efficient graph queries, which leads to performance and efficiency challenges when processing graph data. At the same time, migrating data to a graph database designed specifically for graph queries lacks transaction atomicity and consistency guarantees, and the migration process involves huge time and space costs.
[0005] In this context, there are two approaches in existing research: one approach simply exposes an independent graph query layer to users and implements a translation component that outputs a SQL version of the query with minimal or no modifications to the query processor of the relational database management system. This approach does not focus on performance and is typically used for specific commercial databases. The second approach introduces a new query processor specifically for graphs that coexists with the existing processor of the relational database management system. SQL is extended to use new graph-specific operators such as EdgeScan and PathScan. The parts of the query that reference graph-specific constructs are compiled into these specialized graph operators, while the non-graph parts of the query are compiled into existing operators of the relational database management system. However, there is also the need for users to adapt to the new extended language. And there is no unified standard for the various extended grammar rules. Summary of the invention
[0006] In order to improve the ability of relational databases to process graph queries, this method proposes a graph query acceleration technology based on optimization. This technology accelerates graph query operations by implementing efficient graph algorithms and query optimization strategies in relational databases, and designs query methods to support graph queries, solving the graph query problem of in-situ data stored in relational databases.
[0007] The technical solution of the present invention is as follows:
[0008] First, unlike graph queries on graph databases, relational databases map graph vertex sets and edge sets into tables, i.e., a table in a relational database may contain multiple vertices or multiple relations of a graph. Therefore, the present invention first proposes a method for extracting graph structure from a relational database, the steps of which include:
[0009] 1) Get the number of primary keys in a table in a relational database;
[0010] 2) Obtain the number of foreign keys and referenced fields in a table in a relational database;
[0011] 3) Based on the number of primary keys, foreign keys and reference fields obtained in steps 1) and 2), the table of the relational database is divided into a graph vertex table and a graph relationship table according to the data type stored;
[0012] 4) Generate the graph structure of the current relational database based on the graph vertex table and the graph relationship table.
[0013] In relational databases, the basic structure for storing data is called a table. A table is a two-dimensional structure consisting of rows (records) and columns (fields). Each row represents a data record, and each column represents an attribute of a record. The data in the table is organized through specific constraints and rules to ensure the integrity and accuracy of the data. These rules include, but are not limited to: primary key, foreign key.
[0014] A primary key refers to one or more fields (columns) in a table of a relational database, whose values can uniquely identify each record in the table. The main features and functions of a primary key are as follows:
[0015] 1. Uniqueness: The value of the primary key must be unique in the entire table and cannot be repeated. This ensures that each record can be accurately identified and accessed.
[0016] 2. Non-nullability: The primary key field is not allowed to be empty (NULL), because a null value cannot guarantee uniqueness.
[0017] 3. Index: Usually, the database system will automatically create an index for the primary key to speed up the query.
[0018] 4. Identification function: The primary key is the unique identifier of the record and can be easily associated with other tables through foreign keys.
[0019] A foreign key refers to a field (or combination of fields) in a table of a relational database that references the primary key of another table. The main features and functions of a foreign key are as follows:
[0020] 1. Referential integrity: Foreign key constraints ensure data consistency and referential integrity between tables. If a foreign key value does not exist in the primary key table it references, then the foreign key value cannot be inserted or updated.
[0021] 2. Associative query: Foreign keys enable associative query between data in different tables, and related records can be found quickly through foreign keys.
[0022] 3. Cascade operation: Foreign keys can define cascading update or delete operations. When records in the primary key table are updated or deleted, the associated foreign key records can automatically perform corresponding operations.
[0023] 4. Data constraints: Foreign keys, as a constraint, help maintain the data integrity and accuracy of the database.
[0024] Furthermore, the above method maps the table in the relational database into a graph structure, and the relationship between the data in the table needs to be inferred through the table structure and primary key and foreign key constraints. The table structure refers to a set of related database objects, which may include tables, views, indexes, stored procedures, triggers, and other types of database structures.
[0025] Furthermore, the table of the relational database is divided into a graph vertex table and a graph relationship table according to the data type stored, including:
[0026] 1) Graph vertex table: When a table has a primary key, the table is considered to be a table storing vertex information, namely, a graph vertex table.
[0027] 2) Graph relationship table: a) When a table has a primary key and a foreign key, then this table is considered a graph relationship table, that is, it stores both graph vertex information and graph relationship information; b) When a table has k (k>=2) foreign keys, this table is considered A graph relationship table, where each pair of foreign keys constitutes a graph relationship table.
[0028] Furthermore, step 4) generates the graph structure of the relational database according to the traversal results, and the contents to be recorded are as follows;
[0029] 1) Graph vertex information structure: The url field is used to record the database connection, the table_name field is used to record the table storing the vertex information, the id field is used to record the primary key name of the table storing the vertex information, the label field is used to record the label of the graph vertex, and the properties field is used to record other properties of the table storing the vertex information.
[0030] 2) Graph relationship information structure: The url field is used to record the database connection, the table_name field is used to record the table that stores the relationship information, the src_fk field is used to record the first foreign key, and if the first foreign key is directed, this field corresponds to the starting vertex, the src_table field is used to record the table name corresponding to the first foreign key, the dst_fk field is used to record the second foreign key, and if the second foreign key is directed, this field corresponds to the destination vertex, the dst_table field is used to record the table name corresponding to the second foreign key, the direction field is used to record whether the graph relationship is directed, the label field is used to record the label of the graph vertex, and the properties field is used to record other properties of the table that stores the vertex information.
[0031] Furthermore, in special cases, it is not possible to infer the existence of a relationship between two tables in a relational database based on primary key and foreign key constraints. This can be handled as follows:
[0032] 1) In some cases, there is a logical vertex-edge relationship between two tables in a relational database, but the relationship between the two tables cannot be inferred based on the constraints of the primary key and foreign key of the relational database. In this case, graph relationship information is added after the two specified tables, and an index is added.
[0033] 2) Storing multiple labels in the same table will result in non-unique graph vertex types after the table is converted into a graph structure. In this case, the table will be sharded according to the labels, and then indexes will be added to the sharded parts.
[0034] In a second aspect, the present invention provides a method for performing graph query on a relational database. The method uses the graph structure of the relational database obtained by the above-mentioned method of the present invention to convert the graph query into an SQL query. The specific graph query steps include:
[0035] 1) Query the vertex according to the vertex id
[0036] ① First, convert the graph query language into a physical plan. The physical plan in the present invention refers to a series of specific steps for performing data retrieval, update or modification operations in a relational database. According to the field name of the vertex id, retrieve the id in the generated graph vertex information structure, obtain the table to be retrieved, and in the pre-generated graph vertex information structure, combine the vertex id with the table field name of the relational database, rewrite the physical plan, and generate an SQL statement. The present invention uses Lynx (a general graph query framework) to simplify the query of graph data by converting complex statements into basic graph operations. Lynx does not directly connect to the data source, but retrieves data through the interface implemented by the user for these graph operations.
[0037] ②Submit the SQL statement to the relational data management system to execute the query and return the result.
[0038] 2) Query vertices according to vertex labels
[0039] ① First, convert the graph query language into a physical plan. Search the generated graph vertex information structure according to the vertex label field, determine the table to be searched, and rewrite the physical plan to generate an SQL statement by combining the vertex label in the pre-generated graph vertex structure information with the table field name of the relational database.
[0040] ②Submit the SQL statement to the relational data management system to execute the query.
[0041] 3) Query the relationship by relationship ID
[0042] ① First, convert the graph query language into a physical plan. Search the generated graph relationship information structure according to the name of the relationship id, determine the relationship table to be searched, rewrite the physical plan based on the pre-generated graph relationship information structure, and generate SQL statements based on the specific value of the id.
[0043] ②Submit the SQL statement to the relational data management system to execute the query.
[0044] 4) Query relationships according to relationship vertices
[0045] ① First, query the vertex and query the table where the vertex is located according to steps 1) and 2). After determining the table_name of the two tables, search the graph relationship information structure, and then combine the pre-generated graph relationship information structure to determine the table of the graph structure corresponding to the relationship database, rewrite the physical plan, and generate SQL statements.
[0046] ②Submit the SQL statement to the relational data management system to execute the query.
[0047] In a third aspect, the present invention provides a graph data query device for a relational database, comprising:
[0048] A graph structure extraction unit, used to obtain the graph structure of the relational database using the method of the present invention;
[0049] The graph query unit is used to convert the graph query into an SQL query according to the graph structure of the relational database obtained by the graph structure extraction unit, including querying vertices according to vertex IDs, querying vertices according to vertex labels, querying relationships according to relationship IDs, and querying relationships according to relationship vertices.
[0050] Compared with the prior art, the present invention has the following beneficial effects:
[0051] The present invention combines the characteristics of graph data storage and query, and designs a method for performing graph query based on a relational database, thereby improving the efficiency of performing graph query on relational data. Based on the above design, a method for optimizing graph query in a relational database without intrusion is implemented. Compared with the method of migrating relational data to a graph database, the atomicity and consistency of transactions are guaranteed, and the time and storage space costs are reduced.
[0052] The present invention proposes a method that can give full play to the advantages of relational databases and improve the efficiency of graph queries, and ensure the versatility of various relational databases. Because the existing SQL language cannot meet the complex graph query requirements, the present invention proposes a graph query method based on an in-situ relational database. And combined with a series of optimization and indexing technologies, it makes it more efficient to perform complex query operations on graph data. This method can make full use of the transaction management and data consistency features provided by relational databases, while solving the problem of insufficient performance of relational databases in graph data processing. Overall, this method provides a more efficient and scalable solution for graph query to meet the growing graph query needs of graph data stored in relational CNC libraries. BRIEF DESCRIPTION OF THE DRAWINGS
[0053] The present invention is further described below in conjunction with the accompanying drawings and embodiments:
[0054] Figure 1 is a schematic diagram of the process of this method.
[0055] Figure 2 It is a schematic diagram of table data in a relational database.
[0056] Figure 3 It is a schematic diagram of the table structure of a relational database. DETAILED DESCRIPTION
[0057] In order to make the above features and advantages of the present invention more clearly understood, the following is a detailed description with reference to the accompanying drawings.
[0058] The present invention proposes a method for querying graph data in a relational database. The method flow chart is as follows: Figure 1 As shown in the figure. The Schema (graph structure) generator obtains the Schema (graph structure) of the current relational database by reading the constraint relationship between the tables of the relational database. Then, SQL query statements are generated based on the Schema to implement graph data query on the relational database, and the relational database is further optimized non-invasively based on the Schema, so that the speed of graph query is optimized. Figure 1 In the database optimizer, the database optimizer is used to paginate and fragment the relational database according to the schema and add index operations, and the SQL generator is used to generate SQL statements when performing graph queries.
[0059] A specific embodiment is given below:
[0060] Example data of relational data is as follows Figure 2 As shown, it contains three tables: Person, Knows, and Place.
[0061] The graph structure of data between tables in a relational database is as follows: Figure 3 shown.
[0062] The specific implementation steps of this embodiment are as follows:
[0063] Step 1. Read the constraints in the three tables. The foreign keys P1_id and P2_id in the Knows table are foreign keys that reference the primary key id field of Person.
[0064] Step 2. Generate a Schema (graph structure) based on the constraint relationship, where the Person table and Place table are regarded as Nodes (graph vertices), and the Person table and Knows table are regarded as Relationships (graph relationships).
[0065] Step 3. Build an index mapping relationship between the liveIn (residence table) field and the primary key id in the Place table.
[0066] Step 4. When there is too much data in the Knows table, reduce the amount of data read and perform paging according to the date field in the Knows table.
[0067] Step 5. Generate the modified Schema.
[0068] The following is an example of a graph query on a relational database:
[0069] 1) Example 1. Query the Person vertex based on the vertex ID
[0070] ① After generating a physical plan from the graph query statement, filter non-candidate tables in the table corresponding to the Node of the Schema according to the attributes and id corresponding fields, and return the candidate table name Person.
[0071] ② Generate SQL query results by querying the candidate tables in turn, and return the corresponding graph query results generated by the system.
[0072] 2) Example 2. Query label vertex based on vertex ID
[0073] ① After generating a physical plan from the graph query statement, query the corresponding Label in the table corresponding to the Node of the Schema and return the table name Person to be queried.
[0074] ② Generate SQL query results by querying the candidate tables in turn, process the query results, generate corresponding Nodes, and return the graph query results.
[0075] 3) Example 3. Query the relationship according to the Id of the relationship Knows;
[0076] ① After generating a physical plan from the graph query statement, query the corresponding id field in the table corresponding to the Relationship in the Schema and return the table name Knows to be queried.
[0077] ②Submit the SQL to the relational data management system to execute the query, obtain the SQL query results, process the query results, generate the corresponding Relationship, and return the graph query results.
[0078] 4) Query the relationship according to the relationship vertex Person;
[0079] ① After generating a physical plan from the graph query statement, query the corresponding src_table field in the table corresponding to the Relationship of the Schema, confirm that the Relationship data of the Schema exists in the Knows table, determine the corresponding relationship between Node-Relationship-Node, join the Person, Know, and Person tables, and generate a temporary table.
[0080] ② Generate SQL query statements, process the query results, generate corresponding Nodes, and return graph query results.
[0081] Another embodiment of the present invention provides a graph data query device for a relational database, comprising:
[0082] A graph structure extraction unit, used to obtain the graph structure of the relational database using the method of the present invention;
[0083] The graph query unit is used to convert the graph query into an SQL query according to the graph structure of the relational database obtained by the graph structure extraction unit, including querying vertices according to vertex IDs, querying vertices according to vertex labels, querying relationships according to relationship IDs, and querying relationships according to relationship vertices.
[0084] The specific implementation process of the graph structure extraction unit and the graph query unit refers to the above description of the method of the present invention.
[0085] Another embodiment of the present invention provides a computer device (computer, server, smart phone, etc.), which includes a memory and a processor, the memory stores a computer program, the computer program is configured to be executed by the processor, and the computer program includes instructions for executing each step in the method of the present invention.
[0086] Another embodiment of the present invention provides a computer-readable storage medium (such as ROM / RAM, magnetic disk, optical disk), wherein the computer-readable storage medium stores a computer program, and when the computer program is executed by a computer, the steps of the method of the present invention are implemented.
[0087] The specific embodiments of the present invention disclosed above are intended to help understand the content of the present invention and implement it accordingly. It can be understood by those skilled in the art that various replacements, changes and modifications are possible without departing from the spirit and scope of the present invention. The present invention should not be limited to the contents disclosed in the embodiments of this specification, and the scope of protection of the present invention shall be subject to the scope defined in the claims.
Claims
1. A method for extracting graph structure from a relational database, It is characterized in that The following steps are involved: Get the number of primary keys in a table in a relational database; Get the number of foreign keys and referenced fields in a table in a relational database; Based on the number of primary keys, foreign keys, and reference fields, the relational database table is divided into graph vertex table and graph relationship table according to the data type stored; Generate the graph structure of the relational database based on the graph vertex table and the graph relationship table.
2. The method according to claim 1, It is characterized in that The table of the relational database is divided into a graph vertex table and a graph relationship table according to the data type stored, including: Graph vertex table: When a table has a primary key, the table is considered to be a table storing vertex information, namely, a graph vertex table; Graph relation table: When a table has a primary key and a foreign key, the table is considered a graph relation table, that is, it stores both graph vertex information and graph relation information; when a table has k foreign keys, where k>=2, the table is considered A graph relationship table, where each pair of foreign keys constitutes a graph relationship table.
3. The method according to claim 1, It is characterized in that The graph structure of the generated relational database includes a structure for recording graph vertex information and a structure for recording graph relationship information; In the graph vertex information structure, the url field is used to record the database connection, the table_name field is used to record the table storing the vertex information, the id field is used to record the primary key name of the table storing the vertex information, the label field is used to record the label of the graph vertex, and the properties field is used to record other properties of the table storing the vertex information; In the graph relationship information structure, the url field is used to record the database connection, the table_name field is used to record the table that stores the relationship information, the src_fk field is used to record the first foreign key, and if the first foreign key is directed, this field corresponds to the starting vertex, the src_table field is used to record the table name corresponding to the first foreign key, the dst_fk field is used to record the second foreign key, and if the second foreign key is directed, this field corresponds to the destination vertex, the dst_table field is used to record the table name corresponding to the second foreign key, the direction field is used to record whether the graph relationship is directed, the label field is used to record the label of the graph vertex, and the properties field is used to record other properties of the table that stores the vertex information.
4. The method according to claim 1, It is characterized in that If there is a logical vertex-edge relationship between two tables in a relational database, but the relationship between the two tables cannot be inferred based on the primary key and foreign key constraints of the relational database, add graph relationship information after the two specified tables and add indexes; If multiple labels are stored in the same table, resulting in non-unique graph vertex types after the table is converted into a graph structure, the table is sharded according to the labels, and indexes are added to the sharded parts.
5. A method for performing graph query on a relational database. It is characterized in that The method converts the graph query into an SQL query based on the graph structure of the relational database obtained by the method described in any one of claims 1 to 4, including querying vertices according to vertex IDs, querying vertices according to vertex labels, querying relationships according to relationship IDs, and querying relationships according to relationship vertices.
6. The method according to claim 5, It is characterized in that The querying of vertices according to vertex IDs includes: Convert the graph query language into a physical plan. According to the field name of the vertex id, retrieve the id in the generated graph vertex information structure to obtain the table to be retrieved. In the pre-generated graph vertex structure information, combine the vertex id with the table field name of the relational database, rewrite the physical plan, and generate SQL statements. Submit the SQL statement to the relational data management system to execute the query and return the result.
7. The method according to claim 5, It is characterized in that The querying of vertices according to vertex labels includes: Convert the graph query language into a physical plan, search in the generated graph vertex information structure according to the vertex label field, determine the table to be searched, combine the vertex label in the graph vertex structure information with the table field name of the relational database, rewrite the physical plan, and generate SQL statements; Submit SQL statements to the relational data management system to execute queries.
8. The method according to claim 5, It is characterized in that The querying of a relationship according to the relationship ID includes: First, convert the graph query language into a physical plan, search the generated graph relationship information structure according to the name of the relationship id, determine the relationship table to be searched, rewrite the query based on the pre-generated graph relationship information structure, and generate an SQL statement based on the specific value of the id; Submit SQL statements to the relational data management system to execute queries.
9. The method according to claim 5, It is characterized in that The querying of relations according to relation vertices includes: Query the table where the vertex is located. After determining the table_name of the two tables, search the graph relationship information structure, and then determine the table of the relationship database corresponding to the relationship in the graph structure in combination with the graph relationship information structure, rewrite the physical plan, and generate SQL statements; Submit SQL statements to the relational data management system to execute queries.
10. A graph data query device for relational databases, It is characterized in that include: A graph structure extraction unit, for obtaining a graph structure of a relational database using the method according to any one of claims 1 to 4; The graph query unit is used to convert the graph query into an SQL query according to the graph structure of the relational database obtained by the graph structure extraction unit, including querying vertices according to vertex IDs, querying vertices according to vertex labels, querying relationships according to relationship IDs, and querying relationships according to relationship vertices.
Citation Information
Patent Citations
Service encapsulation system and method of relational data
CN102841892A
Ship industry knowledge graph construction and analysis method
CN113987210A
Knowledge graph processing method and device based on distributed database
CN114020932A
Data query method and device based on knowledge graph cluster
CN115617859A
Document type database-oriented graph query method, system and device and medium
CN117932078A
