{"id":19995,"date":"2017-04-20T09:00:00","date_gmt":"2017-04-20T16:00:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/dataplatforminsider\/?p=19995"},"modified":"2024-01-22T22:50:54","modified_gmt":"2024-01-23T06:50:54","slug":"graph-data-processing-with-sql-server-2017","status":"publish","type":"post","link":"https:\/\/www.microsoft.com\/en-us\/sql-server\/blog\/2017\/04\/20\/graph-data-processing-with-sql-server-2017\/","title":{"rendered":"Graph Data Processing with SQL Server 2017 and Azure SQL Database"},"content":{"rendered":"
SQL Server is trusted by many customers for enterprise-grade, mission-critical workloads that store and process large volumes of data. Technologies like in-memory OLTP and columnstore have also helped our customers to improve application performance many times over. But when it comes to hierarchical data with complex relationships or data that share multiple relationships, users might find themselves struggling with a good schema design to represent all the entities and relationships, and writing optimal queries to analyze complex data and relationships between the tables. SQL Server uses foreign keys and joins to handle relationships between entities or tables. Foreign keys only represent one-to-many relationships and hence, to model many-to-many relationships, a common approach is to introduce a table that holds such relationships. For example, Student and Course in a school share a many-to-many relationship; a Student takes multiple Courses and a Course is taken by multiple Students. To represent this kind of relationship one can create an \u201cAttends\u201d table to hold information about all the Courses a Student is taking. The \u201cAttends\u201d table can then store some extra information like the dates when a given Student took this Course, etc.<\/p>\n
Over time applications tend to evolve and get more complex. For example, a Student can start \u201cVolunteering\u201d in a Course or start mentoring \u201cMentoring\u201d others. This will add new types of relationships to the database. With this type of approach, it is not always easy to modify existing tables to accommodate evolving relationships. To analyze data connected by means of foreign keys or multiple junction tables involves writing complex queries with joins across multiple tables, and this is no trivial task. The queries can quickly get complex, resulting in complex execution plans and degraded query performance over time.<\/p>\n
We live in an era of big data and connected information; people, machines, devices, businesses across the continents are connected to each other more than ever before. Analyzing connected information is becoming critical for businesses to achieve operational agility. Users are finding it easier to model data and complex relationships with the help of graph databases. Native graph databases have risen in popularity, being used for social networks, transportation networks, logistics, and much more. Graph database scenarios can easily be found across several business disciplines, including supply chain management, computer or telecommunication networks, detecting fraud attacks, and recommendation engines.<\/p>\n
At Microsoft, we believe that there should be no need for our customers to turn to a new system just to meet their new or evolving graph database requirements. SQL Server is already trusted by millions of customers for mission-critical workloads, and with graph extensions in SQL Server 2017 and Azure SQL Database, customers get the best of both relational and graph databases in a single product, including the ability to query across all data using a single platform. Users can also benefit from other cutting-edge technologies already available in SQL Server, such as columnstore indexes, advanced analytics using SQL Server R Services, high availability, and more.\n<\/p>\n
A graph schema or database in SQL Server is a collection of node and edge tables. A node represents an entity\u2014for example, a person or an organization\u2014and an edge represents a relationship between the two nodes it connects. Figure 1 shows the architecture of a graph database in SQL Server.<\/p>\n
<\/a><\/p>\n Figure 1: SQL graph database architecture<\/p>\n With the help of T-SQL extensions to DDL, users can create node or edge tables. Both nodes and edges can have properties associated to them. Users can model many-to-many relationships using edge tables. A single edge type can connect multiple type of nodes with each other, in contrast to foreign keys in relational tables. Figure 2 shows how a node and edge table are stored internally in the database. Since nodes and edges are stored as tables, most of the operations supported on tables are available on node or edge tables, too.<\/p>\n <\/a><\/p>\n Figure 2: Person Node and Friends Edge table.<\/p>\n The CREATE TABLE<\/a> syntax guide shows the supported syntax for creation of node and edge tables.<\/p>\n To help search a pattern or traverse through the graph, a new MATCH clause is introduced that uses ASCII-art syntax for pattern matching and navigation. For example, consider the Person and Friends node tables shown in Figure 2; the following query will return friends of \u201cJohn\u201d:<\/p>\n SELECT<\/span> Person2.Name<\/span><\/span> The MATCH clause is taking a search pattern as input. This pattern traverses the graph from one node to another via an edge. Edges appear inside parentheses and nodes appear at the ends of the arrow. Please refer to MATCH<\/a> syntax guide to find out more ways in which MATCH can be used.<\/p>\n Graph extensions are fully integrated in the SQL Server engine. Node and edge tables are just new types of tables in the database. The same storage engine, metadata, query processor, etc., is used to store and query graph data. All security and compliance features are also supported. Other cutting-edge technologies like columnstore, ML using R Services, HA, and more can also be combined with graph capabilities to achieve more. Since graphs are fully integrated in the engine, users can query across their relational and graph data in a single system.<\/p>\n Users benefit from the existing tools and ecosystem that SQL Server offers. Tools like backup and restore, import and export, BCP, and SSMS \u201cjust work\u201d out of the box.<\/p>\n How can I ingest unstructured data?<\/b><\/p>\n Since we are storing data in tables, users must know the schema at the time of creation. Users can always add new types of nodes or edges to their schema. But if they want to modify an existing node or edge table, they can use ALTER TABLE to add or delete attributes. If you expect any unknown attributes in your schema, you could either use sparse columns or create a column to hold JSON strings and use that as a placeholder for unknown attributes.<\/p>\n Do you maintain an adjacency list for faster lookups?<\/b><\/p>\n No. We are not maintaining an adjacency list on every node; instead we are storing edge data in tables. Because it is a relational database, storing data in the form of tables was a more natural choice for us. In native-directed graph databases with an adjacency list, you can only traverse in one direction. If you need to traverse in the reverse direction, you need to maintain an adjacency list at the remote node too. Also, with adjacency lists, in a big graph for a large query that spawns across your graph, you are essentially always doing a nested loop lookup: for every node, find all the edges, from there find all the connected nodes and edges, and so on.<\/p>\n Storing edge data in a separate table allows us to benefit from the query optimizer, which can pick the optimal join strategy for large queries. Depending on the complexity of query and data statistics, the optimizer can pick a nested loop join, hash join, or other join strategies \u2014 as opposed to always using nested loop join, as in the case of an adjacency list. Each edge table has two implicit columns, $from_id and $to_id, which store information about the nodes that it connects. For OLTP scenarios, we recommend that users create indexes on these columns ($from_id, $to_id) for faster lookups in the direction of the edge. If your application needs to perform traversals in reverse direction of an edge, you can create an index on ($to_id, $from_id).<\/p>\n Is the new MATCH syntax supported on relational tables?<\/b><\/p>\n No. MATCH clause works only on graph node and edge tables.<\/p>\n Can I alter an existing table into a node or edge table?<\/b><\/p>\n No. In the first release, ALTER TABLE to convert an existing relational table into a node or edge table is not supported. Users can create a node table and use INSERT INTO \u2026 SELECT FROM to populate data into the node table. To populate an edge table from an existing table, proper $from_id and $to_id values must be obtained from the node tables.<\/p>\n What are some table operations that are not supported on node or edge tables?<\/b><\/p>\n In the first release, node or edge tables cannot be created as memory-optimized, system-versioned, or temporary tables. Stretching or creating a node or edge table as external table (PolyBase) is also not supported in this release.<\/p>\n How do I find a node connected to me, arbitrary number of hops away, in my graph?<\/b><\/p>\n The ability to recurse through a combination of nodes and edges, an arbitrary number of times, is called transitive closure. For example, find all the people connected to me through three levels of indirections or find the employee chain for a given employee in an organization. Transitive closure is not supported in the first release. A recursive CTE or a T-SQL loop may be used to work around these types of queries.<\/p>\n How do I find ANY Node connected to me in my graph?<\/b><\/p>\n The ability to find any type of node connected to a given node in a graph is called polymorphism. SQL graph does not support polymorphism in the first release. A possible workaround is to write queries with UNION clause over a known set of node and edge types. However, this workaround is good for a small set of node and edge types.<\/p>\n Are there special graph analytics functions introduced?<\/b><\/p>\n Some graph databases provide dedicated graph analytical functions like \u201cshortest path\u201d or \u201cpage rank.\u201d SQL Graph does not provide any such functions in this release. Again, T-SQL loops and temp tables may be used to write a workaround for these scenarios.<\/p>\n Thank you for reading this post! We are excited to announce the first version of graph extensions to SQL Server. To learn more, see this article on Graph processing with SQL Server 2017<\/a>. Stay tuned for more blog posts and updates on SQL graph database!<\/p>\n Get started with the preview of SQL Server 2017 on macOS, Docker, Windows, and Linux using these links:<\/p>\n SQL Server is trusted by many customers for enterprise-grade, mission-critical workloads that store and process large volumes of data. Technologies like in-memory OLTP and columnstore have also helped our customers to improve application performance many times over. But when it comes to hierarchical data with complex relationships or data that share multiple relationships, users might<\/p>\n","protected":false},"author":1457,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"ep_exclude_from_search":false,"_classifai_error":"","footnotes":""},"post_tag":[],"product":[5226,2391,5227,2406],"content-type":[2424],"topic":[2466],"coauthors":[2487],"class_list":["post-19995","post","type-post","status-publish","format-standard","hentry","product-azure","product-azure-sql-database","product-sql","product-sql-server-2017","content-type-best-practices","topic-developer"],"yoast_head":"\nCreate graph objects<\/h3>\n
Query language extensions<\/h3>\n
\nFROM<\/span> Person Person1, Friends, Person Person2<\/span>
\nWHERE MATCH<\/span>(Person1-(Friends)->Person2)<\/span>
\nAND<\/span> Person1.Name<\/span> = ‘John’;<\/span><\/p>\nFully integrated in SQL Server engine<\/h3>\n
Tooling and ecosystem<\/h3>\n
FAQs<\/h2>\n
\nTry SQL Server 2017<\/b><\/b>
\n<\/h2>\n\n