%3CLINGO-SUB%20id%3D%22lingo-sub-721240%22%20slang%3D%22en-US%22%3EPublic%20Preview%20of%20Shortest%20Path%20on%20SQL%20Server%202019%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-721240%22%20slang%3D%22en-US%22%3E%3CP%3ESQL%20Server%202017%20and%20Azure%20SQL%20Database%20introduced%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fsql%2Frelational-databases%2Fgraphs%2Fsql-graph-overview%3Fview%3Dsql-server-2017%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%22%3Enative%20graph%20database%20capabilities%3C%2FA%3E%20used%20to%20model%20many-to-many%20relationships.%20The%20first%20implementation%20of%20SQL%20Graph%20introduced%20support%20for%20nodes%20to%20represent%20entities%2C%20edges%20to%20represent%20relationships%20and%20a%20new%20MATCH%20predicate%20to%20support%20graph%20pattern%20matching%20and%20traversal.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EWe%20are%20expanding%20the%20graph%20database%20capabilities%20with%20several%20new%20features.%20In%20this%20blog%20we%20discuss%20one%20of%20these%20features%20that%20is%20now%20available%20for%20public%20preview%20in%20%3CA%20href%3D%22https%3A%2F%2Fwww.microsoft.com%2Fen-us%2Fsql-server%2Fsql-server-2019%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%22%3ESQL%20Server%202019%3C%2FA%3E%2C%20%3CSTRONG%3EShortest%20Path%3C%2FSTRONG%3E%2C%20which%20can%20be%20used%20to%20find%20a%20shortest%20path%20between%20two%20nodes%20in%26nbsp%3Ba%20graph.%20The%20shortest%20path%20function%20can%20also%20be%20used%20to%20compute%20a%20transitive%20closure%20or%20for%20arbitrary%20length%20traversals.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EWith%20CTP3.1%2C%20we%20are%20introducing%20a%20new%20function%20SHORTEST_PATH%2C%20which%20can%20be%20used%20inside%20MATCH%20to%20find%20a%20shortest%20path%20between%20any%202%20nodes%20in%20a%20graph%20or%20to%20perform%20arbitrary%20length%20traversals.%20Users%20can%20specify%20a%20pattern%20they%20would%20like%20to%20search%20for%20in%20the%20graph%20using%20a%20regular%20expression%20style%20syntax.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ETo%20understand%20the%20new%20syntax%20and%20how%20shortest%20path%20works%2C%20from%20Adventure%20Works%20Cycles%2C%20a%20bicycle%20wholesaler%20that%20sells%20and%20manufactures%20bicycles.%20We%20have%20converted%20their%20bill%20of%20materials%20hierarchy%20into%20a%20graph%2C%20which%20has%20Product%20node%20and%20IsPartOf%20edge%20table.%20Download%20sample%20scripts%20%3CA%20href%3D%22https%3A%2F%2Fgithub.com%2Fmicrosoft%2Fsql-server-samples%2Ftree%2Fmaster%2Fsamples%2Ffeatures%2Fsql-graph%2FShortestPath%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ehere%3C%2FA%3E.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%20style%3D%22text-align%3A%20center%3B%22%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20style%3D%22width%3A%20400px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F120664i2D33EBEBCE319DE4%2Fimage-size%2Fmedium%3Fv%3D1.0%26amp%3Bpx%3D400%22%20alt%3D%22AdventureWorksBOM.png%22%20title%3D%22AdventureWorksBOM.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3ENow%2C%20let%E2%80%99s%20look%20at%20some%20sample%20queries%20to%20see%20how%20shortest%20path%20can%20help%20with%20recursive%20or%20arbitrary%20length%20traversals%20in%20a%20graph%20schema.%20%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CFONT%20size%3D%224%22%20color%3D%22%23003366%22%3E%3CSTRONG%3EFind%20all%20assemblies%20where%20a%20given%20product%20is%20used%3C%2FSTRONG%3E%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%3EImagine%20that%20the%20inventory%20of%20a%20given%20product%20(Bearing%20Ball)%20at%20Adventure%20Works%20Cycles%20is%20running%20low%20and%20that%20they%20would%20like%20to%20find%20all%20the%20assemblies%20in%20their%20manufacturing%20pipeline%2C%20where%20a%20Bearing%20Ball%20is%20used.%20This%20will%20tell%20them%20what%20assemblies%20will%20be%20affected%20due%20to%20the%20scarcity%20of%20a%20given%20product.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThe%20following%20query%20demonstrates%20how%20shortest%20path%20can%20be%20used%20to%20find%20all%20assemblies%20that%20require%20a%20Bearing%20Ball%20in%20the%20bill%20of%20materials%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%3E--%20Find%20all%20the%20assemblies%20where%20Bearing%20Ball%20is%20used%0ASELECT%20%0A%20P1.ProductID%2C%20%0A%20P1.Name%2C%0A%20%3CSTRONG%3ESTRING_AGG%3C%2FSTRONG%3E(P2.Name%2C'-%26gt%3B')%20%3CSTRONG%3EWITHIN%20GROUP%20(GRAPH%20PATH)%3C%2FSTRONG%3E%20AS%20%5BAssembly%5D%2C%0A%20%3CSTRONG%3ELAST_VALUE%3C%2FSTRONG%3E(P2.ProductID)%20%3CSTRONG%3EWITHIN%20GROUP%20(GRAPH%20PATH)%3C%2FSTRONG%3E%20AS%20%5BFinal%20ProductID%5D%0AFROM%0A%20PRODUCT%20P1%2C%0A%20PRODUCT%20FOR%20PATH%20P2%2C%0A%20ISPARTOF%20FOR%20PATH%20IPO%0AWHERE%20%0A%20MATCH(%3CSTRONG%3ESHORTEST_PATH(P1(-(IPO)-%26gt%3BP2)%2B)%3C%2FSTRONG%3E)%0A%20AND%20P1.ProductID%20%3D%202%0A%20%20%20%20%20%20%20ORDER%20BY%20P1.ProductID%20%0A%3C%2FPRE%3E%0A%3CP%20style%3D%22text-align%3A%20left%3B%22%20dir%3D%22ltr%22%3EThis%20query%20starts%20from%20ProductID%20%3D%202%20(Bearing%20Ball)%20and%20keeps%20traversing%20up%20the%20BOM%20tree%20until%20it%20finds%20all%20products%20that%20contain%20a%20Bearing%20Ball.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EHere%20are%20first%20few%20lines%20from%20the%20result%20set%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20style%3D%22width%3A%20400px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F120667iC55678AD8D592C37%2Fimage-size%2Fmedium%3Fv%3D1.0%26amp%3Bpx%3D400%22%20alt%3D%22image.png%22%20title%3D%22image.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThe%20STRING_AGG%20function%20on%20P2.Name%20fetches%20all%20the%20nodes%20that%20were%20traversed%20in%20this%20path%2C%20thereby%2C%20giving%20us%20a%20good%20view%20of%20assembly%20line%20up%20to%20%E2%80%98Touring-3000%20Yellow%2C%2058%E2%80%99%20bike.%20Other%20rows%20in%20the%20result%20set%20show%20the%20other%20assemblies%20which%20have%20dependency%20on%20Bearing%20Ball%20at%20some%20level.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThis%20query%20uses%20a%20lot%20of%20new%20syntax.%20Let%E2%80%99s%20understand%20what%20this%20new%20syntax%20does.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CFONT%20size%3D%223%22%20color%3D%22%23333399%22%3E%3CSTRONG%3ESHORTEST_PATH%3C%2FSTRONG%3E%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%3EThe%20SHORTEST_PATH%20function%20lets%20you%20find%3A%3C%2FP%3E%0A%3COL%3E%0A%3CLI%3EA%20shortest%20path%20between%20two%20given%20nodes%2Fentities%3C%2FLI%3E%0A%3CLI%3ESingle%20source%20shortest%20path(s).%3C%2FLI%3E%0A%3CLI%3EShortest%20path%20from%20multiple%20source%20nodes%20to%20multiple%20target%20nodes.%3C%2FLI%3E%0A%3C%2FOL%3E%0A%3CP%3EIt%20takes%20an%20arbitrary%20length%20pattern%20as%20input%2C%20described%20below.%20It%20returns%20a%20shortest%20path%20that%20exists%20between%20two%20nodes.%26nbsp%3BThis%20function%20can%20only%20be%20used%20inside%20MATCH.%20It%20accepts%20an%20arbitrary%20length%20pattern%20and%20finds%20a%20shortest%20path%20in%20the%20graph%2C%20which%20matches%20that%20pattern.%20If%20there%20exist%20two%20or%20more%20shortest%20paths%20of%20the%20same%20length%20between%20any%20pair%20of%20source%20and%20destination%20node(s)%2C%20the%20function%20will%20return%20the%20one%20that%20was%20found%20first%20during%20traversal.%20Note%20that%2C%20an%20arbitrary%20length%20pattern%20can%20only%20be%20specified%20inside%20a%20SHORTEST_PATH%20function.%20Finding%20weighted%20shortest%20paths%2C%20all%20paths%20or%20all%20shortest%20paths%20is%20not%20supported%20yet.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CFONT%20size%3D%223%22%20color%3D%22%23333399%22%3E%3CSTRONG%3EArbitrary%20Length%20Pattern%3C%2FSTRONG%3E%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%3EThis%20pattern%20includes%20the%20nodes%20and%20edges%20that%20must%20be%20traversed%20repeatedly%20until%20the%20desired%20node%20is%20reached%20or%20until%20the%20maximum%20number%20of%20iterations%20as%20specified%20in%20the%20pattern%20is%20met.%20Each%20time%20the%20query%20is%20executed%2C%20the%20result%20of%20executing%20this%20pattern%20will%20be%20a%20collection%20of%20nodes%20and%20edges%20traversed%20along%20the%20path%20from%20the%20start%20node%20to%20the%20end%20node.%20This%20is%20a%20regular%20expression%20style%20syntax%20pattern%20and%20the%20following%20two%20pattern%20quantifiers%20are%20supported%20in%20this%20release%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSTRONG%3E%E2%80%98%2B%E2%80%99%26nbsp%3B%3C%2FSTRONG%3E%20%3A%26nbsp%3B%26nbsp%3B%20Repeat%20the%20pattern%201%20or%20more%20times.%20Terminate%20as%20soon%20as%20a%20shortest%20path%20is%20found.%3C%2FP%3E%0A%3CP%20dir%3D%22ltr%22%3EWe%20recommend%20that%20users%20provide%20a%20start%20and%20an%20end%20node%2C%20along%20with%20the%20pattern%2C%20whenever%20%E2%80%98%2B%E2%80%99%20is%20used%20in%20the%20pattern%20predicate.%20For%20a%20big%20graph%2C%20a%20query%20with%20%E2%80%98%2B%E2%80%99%20and%20no%20terminating%20condition%20may%20take%20a%20long%20time%20to%20finish.%20Refer%20to%20the%20example%20shown%20in%20%E2%80%98Find%20Shortest%20path%20between%202%20given%20nodes%E2%80%99%20section%20below%2C%20to%20see%20how%20filters%20on%20start%20and%20end%20node%20can%20be%20applied.%3C%2FP%3E%0A%3CP%3E%3CSTRONG%3E%7B1%2Cn%7D%3C%2FSTRONG%3E%20%3A%20Repeat%20the%20pattern%201%20to%20%E2%80%98n%E2%80%99%20times.%20Terminate%20as%20soon%20as%20a%20shortest%20%26nbsp%3Bis%20found%20or%20when%20the%20max%20number%20of%20iterations%20%E2%80%98n%E2%80%99%20specified%20in%20the%20pattern%20is%20reached.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CFONT%20size%3D%223%22%20color%3D%22%23333399%22%3E%3CSTRONG%3EFOR%20PATH%3C%2FSTRONG%3E%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%3EFOR%20PATH%20must%20be%20used%20with%20any%20node%20or%20edge%20table%20name%20in%20the%20FROM%20clause%2C%20which%20will%20participate%20in%20an%20arbitrary%20length%20pattern.%20FOR%20PATH%20tells%20the%20engine%20that%20the%20node%20or%20edge%20table%20will%20return%20an%20ordered%20collection%20representing%20the%20list%20of%20nodes%20or%20edges%20along%20the%20path%20from%20the%20start%20node%20to%20the%20end%20node.%20The%20attributes%20from%20these%20tables%20cannot%20be%20projected%20directly%20in%20the%20SELECT%20clause%20and%20cannot%20be%20filtered%20directly%20in%20the%20WHERE%20clause.%20To%20project%20or%20filter%20attributes%20from%20these%20tables%2C%20%3CSPAN%3E%3CSTRONG%3Egraph%20path%20aggregate%20functions%3C%2FSTRONG%3E%3C%2FSPAN%3E%20must%20be%20used.%3C%2FP%3E%0A%3CP%3E%3CSTRONG%3E%26nbsp%3B%3C%2FSTRONG%3E%3C%2FP%3E%0A%3CP%3E%3CFONT%20size%3D%223%22%20color%3D%22%23333399%22%3E%3CSTRONG%3EGraph%20Path%20Aggregate%20Functions%20%3C%2FSTRONG%3E%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%3ESince%20the%20nodes%20and%20edges%20involved%20in%20arbitrary%20length%20pattern%20return%20a%20collection%20(of%20node(s)%20and%20edge(s)%20traversed%20in%20that%20path)%2C%20users%20cannot%20project%20the%20attributes%20directly%20using%20the%20conventional%20tablename.attributename%20syntax.%20For%20queries%20where%20it%20is%20required%20to%20project%20attribute%20values%20from%20the%20intermediate%20node%20or%20edge%20tables%2C%20in%20the%20path%20traversed%2C%20we%20have%20extended%20aggregate%20functions%20to%20work%20with%20graph%20node%20or%20edge%20tables.%20In%20this%20release%2C%20the%20following%20graph%20path%20aggregate%20functions%20are%20available%3A%20STRING_AGG%2C%20LAST_VALUE%2C%20SUM%2C%20AVG%2C%20MIN%2C%20MAX%20and%20COUNT.%26nbsp%3BThese%20functions%20can%20be%20used%20in%20SELECT%20or%20ORDER%20BY%20clause.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CFONT%20size%3D%224%22%20color%3D%22%23000080%22%3E%3CSTRONG%3EFind%20all%20the%20products%20which%20are%20up%20to%203%20levels%20away%20from%20a%20given%20product.%3C%2FSTRONG%3E%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%3ENow%2C%20if%20Adventure%20Works%20Cycles%20wants%20to%20find%20products%20in%20their%20BOM%2C%20which%20are%201%20to%203%20levels%20away%20from%20Bearing%20Ball%2C%20they%20can%20just%20modify%20the%20query%20shown%20in%20example%20above%20as%20follows%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%3ESELECT%20%0A%20P1.ProductID%2C%20%0A%20P1.Name%2C%0A%20STRING_AGG(P2.Name%2C'-%26gt%3B')%20WITHIN%20GROUP%20(GRAPH%20PATH)%20AS%20%5BAssembly%5D%2C%0A%20LAST_VALUE(P2.ProductID)%20WITHIN%20GROUP%20(GRAPH%20PATH)%20AS%20%5BFinal%20ProductID%5D%0AFROM%0A%20PRODUCT%20P1%2C%0A%20PRODUCT%20FOR%20PATH%20P2%2C%0A%20ISPARTOF%20FOR%20PATH%20IPO%0AWHERE%20%0A%20MATCH(SHORTEST_PATH(P1(-(IPO)-%26gt%3BP2)%7B1%2C3%7D))%0A%20AND%20P1.ProductID%20%3D%202%0AORDER%20BY%20P1.ProductID%20%0A%3C%2FPRE%3E%0A%3CP%3EHere%20is%20a%20sample%20result%20set%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20style%3D%22width%3A%20400px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F120668i56178D07780BFA1C%2Fimage-size%2Fmedium%3Fv%3D1.0%26amp%3Bpx%3D400%22%20alt%3D%22image.png%22%20title%3D%22image.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CFONT%20size%3D%224%22%20color%3D%22%23000080%22%3E%3CSTRONG%3EFind%20Shortest%20Path%20between%202%20given%20nodes%3C%2FSTRONG%3E%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%3EThe%20queries%20shown%20above%2C%20start%20from%20a%20given%20node%20(Bearing%20Ball)%20and%20then%20search%20all%20paths%20in%20the%20graph%20starting%20from%20that%20node%2C%20until%20they%20cannot%20find%20any%20other%20paths%20that%20originate%20from%20this%20node%20and%20end%20at%20other%20nodes.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EImagine%20that%20Adventure%20Works%20Cycles%20wants%20to%20find%20the%20shortest%20path%20between%202%20given%20nodes%20in%20the%20assembly.%20They%20can%20use%20following%20query%20to%20do%20this.%20This%20query%20will%20again%20start%20at%20ProductID%20%3D%202%20(Bearing%20Ball)%20but%20terminates%20as%20soon%20as%20it%20finds%20ProductID%20%3D%20768%20(bike%20%E2%80%98Road-650%20Black%2C%2044%E2%80%99).%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%3ESELECT%0A%20ProductID%2C%20Name%2C%20%5BAssembly%5D%2C%20FinalProductID%2C%20Levels%0AFROM%20(%0A%20SELECT%20%0A%20%20P1.ProductID%2C%20%0A%20%20P1.Name%2C%0A%20%20STRING_AGG(P2.Name%2C'-%26gt%3B')%20WITHIN%20GROUP%20(GRAPH%20PATH)%20AS%20%5BAssembly%5D%2C%0A%20%20LAST_VALUE(P2.ProductID)%20WITHIN%20GROUP%20(GRAPH%20PATH)%20AS%20FinalProductID%2C%0A%20%20COUNT(P2.ProductID)%20WITHIN%20GROUP%20(GRAPH%20PATH)%20AS%20Levels%0A%20FROM%0A%20%20PRODUCT%20P1%2C%0A%20%20PRODUCT%20FOR%20PATH%20P2%2C%0A%20%20ISPARTOF%20FOR%20PATH%20IPO%0A%20WHERE%20%0A%20%20MATCH(SHORTEST_PATH(P1(-(IPO)-%26gt%3BP2)%2B))%0A%20%20AND%20P1.ProductID%20%3D%202%0A%20)%20AS%20Q%0A%20WHERE%20Q.FinalProductID%20%3D%20768%0A%3C%2FPRE%3E%0A%3CP%3ENote%20that%20in%20this%20query%2C%20we%20used%20a%20subquery%20to%20apply%20filter%20on%20the%20last%20node%20traversed%20in%20the%20path%20(FinalProductID%20%3D%20768).%20The%20filter%20on%20the%20FinalProductID%20in%20the%20outer%20query%20is%20pushed%20down%20while%20computing%20the%20shortest%20path.%20Hence%2C%20the%20query%20will%20terminate%20as%20soon%20as%20the%20FinalProductID%20is%20found.%20LAST_VALUE()%20function%2C%20which%20fetches%20the%20attribute%20values%20from%20last%20node%20traversed%2C%20cannot%20be%20used%20in%20the%20WHERE%20clause%2C%20since%20it%20is%20a%20window%20aggregate%20function.%20%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EBy%20using%20the%20COUNT%20graph%20path%20aggregate%20function%20in%20select%20clause%2C%20we%20can%20see%20the%20number%20of%20hops%2Flevels%20between%20the%202%20ProductIDs.%20Here%20is%20the%20result%20set%20of%20this%20query%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20style%3D%22width%3A%20400px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F120669iC581621D0890231B%2Fimage-size%2Fmedium%3Fv%3D1.0%26amp%3Bpx%3D400%22%20alt%3D%22image.png%22%20title%3D%22image.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EIn%20this%20query%2C%20the%20predicate%2Ffilter%20on%20last%20node%20(Q.FinalProductID)%20is%20pushed%20down.%20That%20is%2C%20the%20shortest%20path%20query%20terminates%20as%20soon%20as%20it%20finds%20ProductID%20%3D%20768.%3C%2FP%3E%0A%3CP%3EWriting%20a%20shortest%20path%20query%20with%20a%20terminating%20condition%20on%20the%20last%20node%2C%20as%20shown%20in%20the%20query%20above%2C%20is%20highly%20recommended.%20This%20is%20a%20much%20more%20optimal%20query%20than%20the%20ones%20which%20start%20at%20one%20node%20and%20then%20traverse%20the%20entire%20graph%20to%20find%20all%20the%20connections%20that%20exist%20from%20that%20node%20to%20others.%20For%20huge%20graphs%2C%20finding%20the%20shortest%20path%20from%20a%20given%20start%20node%20to%20all%20the%20other%20nodes%20in%20the%20graph%2C%20will%20be%20a%20very%20expensive%20operation.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CFONT%20size%3D%224%22%20color%3D%22%23000080%22%3E%3CSTRONG%3EConclusion%3C%2FSTRONG%3E%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%3EStarting%20with%20SQL%20Server%202019%20CTP3.1%2C%20you%20can%20use%20SHORTEST_PATH%20function%20to%20compute%20shortest%20path(s)%20between%20nodes%20in%20a%20graph%2C%20using%20a%20regular%20expression%20style%20search%20pattern.%20SHORTEST_PATH%20can%20only%20be%20used%20inside%20a%20MATCH%20function%20and%20works%20only%20with%20graph%20node%20and%20edge%20tables.%20This%20function%20also%20allows%20you%20to%20write%20transitive%20closure%20or%20arbitrary%20length%20traversal%20query%20from%20a%20given%20%E2%80%98start%E2%80%99%20node.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CFONT%20size%3D%223%22%20color%3D%22%23333399%22%3E%3CSTRONG%3ENext%20Steps%3C%2FSTRONG%3E%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%3EPlease%20download%20and%20give%20this%20feature%20a%20try.%26nbsp%3B%20We%20are%20looking%20forward%20to%20hearing%20your%20comments%20or%20feedback%20for%20this%20feature.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-TEASER%20id%3D%22lingo-teaser-721240%22%20slang%3D%22en-US%22%3E%3CP%3EWe%20are%20expanding%20the%20graph%20database%20capabilities%20with%20several%20new%20features.%20In%20this%20blog%20we%20discuss%20one%20of%20these%20features%20that%20is%20now%20available%20for%20public%20preview%20in%20%3CA%20href%3D%22https%3A%2F%2Fwww.microsoft.com%2Fen-us%2Fsql-server%2Fsql-server-2019%22%20rel%3D%22noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%22%20target%3D%22_blank%22%3ESQL%20Server%202019%3C%2FA%3E%2C%20%3CSTRONG%3EShortest%20Path%3C%2FSTRONG%3E%2C%20which%20can%20be%20used%20to%20find%20a%20shortest%20path%20between%20two%20nodes%20in%20a%20graph.%20The%20shortest%20path%20function%20can%20also%20be%20used%20to%20compute%20a%20transitive%20closure%20or%20for%20arbitrary%20length%20traversals.%3C%2FP%3E%0A%3CP%20style%3D%22text-align%3A%20center%3B%22%3E%26nbsp%3B%26nbsp%3B%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20style%3D%22width%3A%20999px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F120670i2924898F19CEAEFC%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20alt%3D%22image.png%22%20title%3D%22image.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%3C%2FLINGO-TEASER%3E%3CLINGO-SUB%20id%3D%22lingo-sub-778493%22%20slang%3D%22en-US%22%3ERe%3A%20Public%20Preview%20of%20Shortest%20Path%20on%20SQL%20Server%202019%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-778493%22%20slang%3D%22en-US%22%3E%3CP%3EThis%20all%20sounds%20very%20promising%2C%20but%20how%20can%20it%20be%20used%20for%20weighted%20paths%2C%20eg%3A%20distances%20on%20a%20map%3F%20It%20seems%20it%20is%20only%20useful%20for%20counting%20nodes%2C%20but%20not%20distances.%20Am%20I%20missing%20something%3F%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-784979%22%20slang%3D%22en-US%22%3ERe%3A%20Public%20Preview%20of%20Shortest%20Path%20on%20SQL%20Server%202019%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-784979%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F384096%22%20target%3D%22_blank%22%3E%40freom%3C%2FA%3E%26nbsp%3BYou%20cannot%20use%20this%20to%20calculate%20a%20weighted%20shortest%20path.%20But%2C%20you%20can%20store%20weights%20as%20edge%20properties%2Fattributes%20and%20calculate%20the%20weight%20for%20the%20shortest%20path%20by%20distance%20using%20the%20aggregate%20functions%20(sum%2C%20in%20this%20case)%20over%20edge%20attributes.%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-840755%22%20slang%3D%22en-US%22%3ERe%3A%20Public%20Preview%20of%20Shortest%20Path%20on%20SQL%20Server%202019%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-840755%22%20slang%3D%22en-US%22%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F73668%22%20target%3D%22_blank%22%3E%40Shreya%20Verma%3C%2FA%3EAre%20there%20any%20plans%20to%20allow%20extra%20edge%20columns%20to%20be%20used%20in%20WHERE%20clauses%3F%20SHORTEST_PATH%20will%20not%20work%20properly%20if%20you%20have%20cycles%20in%20your%20graph%20and%20use%20an%20extra%20column%20(i.e.%20a%20column%20type)%20to%20differentiate%20between%20the%20different%20edge%20types%20(i.e.%20parent%2Fchild)%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1216431%22%20slang%3D%22en-US%22%3ERe%3A%20Public%20Preview%20of%20Shortest%20Path%20on%20SQL%20Server%202019%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1216431%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F73668%22%20target%3D%22_blank%22%3E%40Shreya%20Verma%3C%2FA%3E%26nbsp%3B-%20Thanks%20for%20introducing%20to%20the%20new%20features%20from%20SQL%20Graph%20DB%2C%20I%20have%20been%20trying%20to%20use%20this%20in%20my%20latest%20project%20for%20storing%20hierarchy%20%2F%20tree%20structures%20where%20we%20join%20multiple%20individual%20hierarchies%20to%20create%20a%20combined%20tree%20structure%20with%20intersections%20at%20leaf%20levels%20from%20one%20or%20more%20hierarchies%20to%20store%20the%20Cartesian%20product%20by%20only%20creating%20a%20separate%20Edge%20Table%20to%20store%20that%20relationship%2C%20which%20is%20very%20powerful.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EWhile%20working%20with%20my%20example%20of%20creating%26nbsp%3Bseparate%20Edge%20Table%20to%20store%26nbsp%3B%20combined%20tree%20structure%2C%20I%20have%20observed%20limitations%20of%26nbsp%3B%3CSPAN%3ESHORTEST_PATH%2C%20in%20my%20use%20case%20when%20we%20have%20multiple%20parent%20nodes%20for%20same%20child%20node%2C%20SHORTEST_PATH%20is%20not%20able%20to%20produce%20output%20from%20all%20possible%20child%20nodes%2C%20it%20will%20find%20path%20to%20the%20last%20node%20for%20only%20first%20iteration%20of%20Parent%20to%20Child%2C%20please%20see%20my%20example%20below%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CSPAN%3EIn%20this%20example%20I%20have%202%20tree%20structures%20of%20%22Region%22%20and%20%22Vehicles%22%2C%20purpose%20is%20to%20create%20%22Sales%20by%20Region%22%20hierarchy%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EWhen%26nbsp%3B%3CSPAN%3ESHORTEST_PATH%20query%20is%20executed%2C%20I%20am%20expecting%20to%20get%20full%20path%20from%20all%20nodes%20of%20Region%20to%20all%20possible%20nodes%20of%20Vehicles%2C%20but%20the%20path%20stops%20at%20the%20first%20region%20of%20%22Illinois%22%20and%20does%20not%20show%20data%20for%20%22Wisonsin%22%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CSPAN%3EPlease%20suggest%20any%20corrections%20which%20can%20help%20generate%20full%20path%26nbsp%3B%20..%20many%20thanks%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%3E---Create%20Sample%20Schema%0ACREATE%20SCHEMA%20graph%0AGO%0A%0A--Tree%20Nodes%0ACREATE%20TABLE%20graph.Tree_Nodes%0A(%0A%20tree_id%20bigint%0A%20%2Ctree_parent%20bigint%0A%20%2Ctree_name%20NVARCHAR(50)%0A)%20AS%20NODE%0A%0A%0A--Combined%20Tree%20Edges%0ACREATE%20TABLE%20graph.Combined_Tree_Edges%0A(%0A%20tree_id%20bigint%0A)%20AS%20EDGE%0A%0A--Tree%20Insert%0AINSERT%20INTO%20graph.Tree_Nodes%20VALUES%20(1%2Cnull%2C'Region')%0AINSERT%20INTO%20graph.Tree_Nodes%20VALUES%20(2%2C1%2C'USA')%0AINSERT%20INTO%20graph.Tree_Nodes%20VALUES%20(3%2C2%2C'Illinois')%0AINSERT%20INTO%20graph.Tree_Nodes%20VALUES%20(4%2C2%2C'Wisonsin')%0AINSERT%20INTO%20graph.Tree_Nodes%20VALUES%20(5%2Cnull%2C'Vehicles')%0AINSERT%20INTO%20graph.Tree_Nodes%20VALUES%20(6%2C5%2C'Cars')%0AINSERT%20INTO%20graph.Tree_Nodes%20VALUES%20(7%2C5%2C'Trucks')%0AINSERT%20INTO%20graph.Tree_Nodes%20VALUES%20(8%2C6%2C'BMW')%0AINSERT%20INTO%20graph.Tree_Nodes%20VALUES%20(9%2C6%2C'Merc')%0AINSERT%20INTO%20graph.Tree_Nodes%20VALUES%20(10%2C6%2C'Tesla')%0AINSERT%20INTO%20graph.Tree_Nodes%20VALUES%20(11%2C7%2C'Volvo')%0AINSERT%20INTO%20graph.Tree_Nodes%20VALUES%20(12%2C7%2C'Daimler')%0A%0A%0A--Create%20Edge%20relations%20on%20graph.Combined_Tree_Edges%0AINSERT%20INTO%20graph.Combined_Tree_Edges%20(%24from_id%2C%20%24to_id%2Ctree_id)%20%0Aselect%20%20ParentFromNode.%24node_id%0A%20%20%2CChildToNode.%24node_id%0A%20%20%2CChildToNode.tree_id%0Afrom%20graph.Tree_Nodes%20ParentFromNode%20%0AINNER%20JOIN%20graph.Tree_Nodes%20ChildToNode%20ON%20ChildToNode.tree_parent%3DParentFromNode.tree_id%0AWHERE%20ChildToNode.tree_id%20IN%20(2%2C3%2C4%2C6%2C7%2C8%2C9%2C10%2C11%2C12)%0A%0A--Intersection%20Edges%0AINSERT%20INTO%20graph.Combined_Tree_Edges%20(%24from_id%2C%20%24to_id%2Ctree_id)%20%0AVALUES%0A%20%20(%0A%20%20%20(SELECT%20%24node_id%20FROM%20graph.Tree_Nodes%20WHERE%20tree_id%3D3)%20--Illinois%0A%20%20%20%2C(SELECT%20%24node_id%20FROM%20graph.Tree_Nodes%20WHERE%20tree_id%3D5)%20--Vehicles%0A%20%20%20%2C3%0A%20%20)%0A%0AINSERT%20INTO%20graph.Combined_Tree_Edges%20(%24from_id%2C%20%24to_id%2Ctree_id)%20%0AVALUES%0A%20%20(%0A%20%20%20(SELECT%20%24node_id%20FROM%20graph.Tree_Nodes%20WHERE%20tree_id%3D4)%20--Wisonsin%0A%20%20%20%2C(SELECT%20%24node_id%20FROM%20graph.Tree_Nodes%20WHERE%20tree_id%3D5)%20--Vehicles%0A%20%20%20%2C4%0A%20%20)%0A%0A%0A--Final%20Select%20Query%0ASELECT%0A%20Parent.tree_id%20AS%20ParentId%2C%0A%20Parent.tree_name%20AS%20ParentName%2C%0A%20STRING_AGG(CAST(Child.tree_id%20AS%20NVARCHAR(10))%2C%20'%20%7C%20')%20WITHIN%20GROUP%20(GRAPH%20PATH)%20AS%20PathIds%2C%0A%20STRING_AGG(CAST(Child.tree_name%20AS%20NVARCHAR(10))%2C%20'%20%7C%20')%20WITHIN%20GROUP%20(GRAPH%20PATH)%20AS%20PathNames%2C%0A%20LAST_VALUE(Child.tree_parent)%20WITHIN%20GROUP%20(GRAPH%20PATH)%20AS%20LastNodeParentId%2C%0A%20LAST_VALUE(Child.tree_id)%20WITHIN%20GROUP%20(GRAPH%20PATH)%20AS%20LastNodeId%2C%0A%20LAST_VALUE(Child.tree_name)%20WITHIN%20GROUP%20(GRAPH%20PATH)%20AS%20LastNodeName%0AFROM%0A%20graph.Tree_Nodes%20AS%20Parent%2C%0A%20graph.Combined_Tree_Edges%20FOR%20PATH%20AS%20ChildOf%2C%0A%20graph.Tree_Nodes%20FOR%20PATH%20%20AS%20Child%0AWHERE%20MATCH(SHORTEST_PATH(Parent(-(ChildOf)-%26gt%3BChild)%2B))%0AAND%20Parent.tree_id%20IN%20(1)%20---Region%0A%0A%2F*%0A--Cleanup%0ADROP%20TABLE%20graph.Combined_Tree_Edges%0AGO%0A%0ADROP%20TABLE%20graph.Tree_Nodes%0AGO%0A%0ADROP%20SCHEMA%20graph%0AGO%0A%0A*%2F%3C%2FPRE%3E%3CDIV%20class%3D%22mceNonEditable%20lia-copypaste-placeholder%22%3E%26nbsp%3B%3C%2FDIV%3E%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22final_result.PNG%22%20style%3D%22width%3A%20739px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F175805i5B6E03281CAD6281%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20title%3D%22final_result.PNG%22%20alt%3D%22Final%20Select%20Output%22%20%2F%3E%3CSPAN%20class%3D%22lia-inline-image-caption%22%20onclick%3D%22event.preventDefault()%3B%22%3EFinal%20Select%20Output%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CSPAN%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22excel_example.PNG%22%20style%3D%22width%3A%20610px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F175807i80577A05DF45AB2C%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20title%3D%22excel_example.PNG%22%20alt%3D%22Excel%20Example%22%20%2F%3E%3CSPAN%20class%3D%22lia-inline-image-caption%22%20onclick%3D%22event.preventDefault()%3B%22%3EExcel%20Example%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1543911%22%20slang%3D%22en-US%22%3ERe%3A%20Public%20Preview%20of%20Shortest%20Path%20on%20SQL%20Server%202019%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1543911%22%20slang%3D%22en-US%22%3E%3CP%3EThank%20you%26nbsp%3B%20%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F73668%22%20target%3D%22_blank%22%3E%40Shreya%20Verma%3C%2FA%3E%3CSPAN%20class%3D%22%22%3E%2C%26nbsp%3B%3C%2FSPAN%3Ethis%20is%20a%20great%20article%20and%20inspires%20hopes%20for%20using%20graph%20db%20for%20BOM%20traversing%20in%20SQL%20Server%202019.%3C%2FP%3E%3CP%3EHowever%20in%20all%20my%20testing%20it%20performed%20poorly%20comparing%20to%20using%20recursive-CTE-based%20tree%20explosion.%3C%2FP%3E%3CP%3EThe%20execution%20times%20were%2010%20to%2020%20times%20longer%2C%20output%20Rows%20estimates%204%20to%2010%20times%20worse%2C%20plan%20cost%20comparison%20always%200%25%20for%20recursion%20and%20100%25%20for%20the%20graph%20query%20using%20SHORTEST_PATH().%3C%2FP%3E%3CP%3E---------%3C%2FP%3E%3CP%3EIs%20this%20a%20known%20issue%3F%3C%2FP%3E%3CP%3EAt%20this%20time%20it%20looks%20like%20Graph%20Db%20in%20SQL%20Server%20was%20created%20to%20support%20MATCH%20clause%20rather%20then%20improve%20performance%20of%20hierarchical%20data%20querying.%3C%2FP%3E%3CP%3E---------%3C%2FP%3E%3CP%3ECan%20you%20recommend%20any%20remedies%3F%3C%2FP%3E%3CP%3E-%20I%20played%20with%20different%20indexing%20on%20the%20node%20and%20edge%20tables.%20An%20index%20on%20the%20edge%20(%24from_id%2C%20%24to_id)%20helps%2C%20but%20not%20dramatically.%3C%2FP%3E%3CP%3E-%20OPTION%20(HASH%20JOIN)%20helps%20when%20querying%20for%20All%20product%20trees%2C%20but%20not%20for%20One%20product%20tree.%3C%2FP%3E%3CP%3EThe%20sample%20data%20I%20used%20had%205%20levels%20of%20hierarchy.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1547175%22%20slang%3D%22en-US%22%3ERe%3A%20Public%20Preview%20of%20Shortest%20Path%20on%20SQL%20Server%202019%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1547175%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F739048%22%20target%3D%22_blank%22%3E%40alex_io%3C%2FA%3E%26nbsp%3Bis%20it%20possible%20for%20you%20to%20share%20a%20reproduction%3F%20without%20looking%20at%20the%20query%20it%20is%20hard%20to%20tell%20why%20it%20is%20performing%20poorly%20for%20you.%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThanks%2C%3C%2FP%3E%0A%3CP%3Eshreya%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1548695%22%20slang%3D%22en-US%22%3ERe%3A%20Public%20Preview%20of%20Shortest%20Path%20on%20SQL%20Server%202019%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1548695%22%20slang%3D%22en-US%22%3E%3CP%20class%3D%22syndicated-attribution%22%3E%3CI%3EThis%20post%20has%20been%20republished%20via%20RSS%3B%20it%20originally%20appeared%20at%3A%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fsql-server%2Fpublic-preview-of-shortest-path-on-sql-server-2019%2Fba-p%2F721240%22%20target%3D%22_blank%22%20rel%3D%22noopener%22%3ESQL%20Server%20articles%3C%2FA%3E.%3C%2FI%3E%3C%2FP%3E%3CP%3ESQL%20Server%202017%20and%20Azure%20SQL%20Database%20introduced%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fsql%2Frelational-databases%2Fgraphs%2Fsql-graph-overview%3Fview%3Dsql-server-2017%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%22%3Enative%20graph%20database%20capabilities%3C%2FA%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3Eused%20to%20model%20many-to-many%20relationships.%20The%20first%20implementation%20of%20SQL%20Graph%20introduced%20support%20for%20nodes%20to%20represent%20entities%2C%20edges%20to%20represent%20relationships%20and%20a%20new%20MATCH%20predicate%20to%20support%20graph%20pattern%20matching%20and%20traversal.%3C%2FP%3E%3CDIV%20class%3D%22google-auto-placed%20ap_container%22%3E%26nbsp%3B%3C%2FDIV%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EWe%20are%20expanding%20the%20graph%20database%20capabilities%20with%20several%20new%20features.%20In%20this%20blog%20we%20discuss%20one%20of%20these%20features%20that%20is%20now%20available%20for%20public%20preview%20in%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CA%20href%3D%22https%3A%2F%2Fwww.microsoft.com%2Fen-us%2Fsql-server%2Fsql-server-2019%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%22%3ESQL%20Server%202019%3C%2FA%3E%2C%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CSTRONG%3EShortest%20Path%3C%2FSTRONG%3E%2C%20which%20can%20be%20used%20to%20find%20a%20shortest%20path%20between%20two%20nodes%20in%26nbsp%3Ba%20graph.%20The%20shortest%20path%20function%20can%20also%20be%20used%20to%20compute%20a%20transitive%20closure%20or%20for%20arbitrary%20length%20traversals.%3C%2FP%3E%3CDIV%20class%3D%22google-auto-placed%20ap_container%22%3E%26nbsp%3B%3C%2FDIV%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EWith%20CTP3.1%2C%20we%20are%20introducing%20a%20new%20function%20SHORTEST_PATH%2C%20which%20can%20be%20used%20inside%20MATCH%20to%20find%20a%20shortest%20path%20between%20any%202%20nodes%20in%20a%20graph%20or%20to%20perform%20arbitrary%20length%20traversals.%20Users%20can%20specify%20a%20pattern%20they%20would%20like%20to%20search%20for%20in%20the%20graph%20using%20a%20regular%20expression%20style%20syntax.%3C%2FP%3E%3CDIV%20class%3D%22google-auto-placed%20ap_container%22%3E%26nbsp%3B%3C%2FDIV%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ETo%20understand%20the%20new%20syntax%20and%20how%20shortest%20path%20works%2C%20from%20Adventure%20Works%20Cycles%2C%20a%20bicycle%20wholesaler%20that%20sells%20and%20manufactures%20bicycles.%20We%20have%20converted%20their%20bill%20of%20materials%20hierarchy%20into%20a%20graph%2C%20which%20has%20Product%20node%20and%20IsPartOf%20edge%20table.%20Download%20sample%20scripts%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CA%20href%3D%22https%3A%2F%2Fgithub.com%2Fmicrosoft%2Fsql-server-samples%2Ftree%2Fmaster%2Fsamples%2Ffeatures%2Fsql-graph%2FShortestPath%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ehere%3C%2FA%3E.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CDIV%20class%3D%22mceNonEditable%20lia-copypaste-placeholder%22%3E%26nbsp%3B%3C%2FDIV%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ENow%2C%20let%E2%80%99s%20look%20at%20some%20sample%20queries%20to%20see%20how%20shortest%20path%20can%20help%20with%20recursive%20or%20arbitrary%20length%20traversals%20in%20a%20graph%20schema.%20%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CFONT%20size%3D%224%22%20color%3D%22%23003366%22%3E%3CSTRONG%3EFind%20all%20assemblies%20where%20a%20given%20product%20is%20used%3C%2FSTRONG%3E%3C%2FFONT%3E%3C%2FP%3E%3CP%3EImagine%20that%20the%20inventory%20of%20a%20given%20product%20(Bearing%20Ball)%20at%20Adventure%20Works%20Cycles%20is%20running%20low%20and%20that%20they%20would%20like%20to%20find%20all%20the%20assemblies%20in%20their%20manufacturing%20pipeline%2C%20where%20a%20Bearing%20Ball%20is%20used.%20This%20will%20tell%20them%20what%20assemblies%20will%20be%20affected%20due%20to%20the%20scarcity%20of%20a%20given%20product.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThe%20following%20query%20demonstrates%20how%20shortest%20path%20can%20be%20used%20to%20find%20all%20assemblies%20that%20require%20a%20Bearing%20Ball%20in%20the%20bill%20of%20materials%3A%3C%2FP%3E%3CDIV%20class%3D%22google-auto-placed%20ap_container%22%3E%26nbsp%3B%3C%2FDIV%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%3E--%20Find%20all%20the%20assemblies%20where%20Bearing%20Ball%20is%20used%0ASELECT%20%0A%20P1.ProductID%2C%20%0A%20P1.Name%2C%0A%20%3CSTRONG%3ESTRING_AGG%3C%2FSTRONG%3E(P2.Name%2C'-%26gt%3B')%20%3CSTRONG%3EWITHIN%20GROUP%20(GRAPH%20PATH)%3C%2FSTRONG%3E%20AS%20%5BAssembly%5D%2C%0A%20%3CSTRONG%3ELAST_VALUE%3C%2FSTRONG%3E(P2.ProductID)%20%3CSTRONG%3EWITHIN%20GROUP%20(GRAPH%20PATH)%3C%2FSTRONG%3E%20AS%20%5BFinal%20ProductID%5D%0AFROM%0A%20PRODUCT%20P1%2C%0A%20PRODUCT%20FOR%20PATH%20P2%2C%0A%20ISPARTOF%20FOR%20PATH%20IPO%0AWHERE%20%0A%20MATCH(%3CSTRONG%3ESHORTEST_PATH(P1(-(IPO)-%26gt%3BP2)%2B)%3C%2FSTRONG%3E)%0A%20AND%20P1.ProductID%20%3D%202%0A%20%20%20%20%20%20%20ORDER%20BY%20P1.ProductID%20%3C%2FPRE%3E%3CDIV%20class%3D%22google-auto-placed%20ap_container%22%3E%26nbsp%3B%3C%2FDIV%3E%3CP%3EThis%20query%20starts%20from%20ProductID%20%3D%202%20(Bearing%20Ball)%20and%20keeps%20traversing%20up%20the%20BOM%20tree%20until%20it%20finds%20all%20products%20that%20contain%20a%20Bearing%20Ball.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EHere%20are%20first%20few%20lines%20from%20the%20result%20set%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3C%2FP%3E%3CDIV%20class%3D%22mceNonEditable%20lia-copypaste-placeholder%22%3E%26nbsp%3B%3C%2FDIV%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThe%20STRING_AGG%20function%20on%20P2.Name%20fetches%20all%20the%20nodes%20that%20were%20traversed%20in%20this%20path%2C%20thereby%2C%20giving%20us%20a%20good%20view%20of%20assembly%20line%20up%20to%20%E2%80%98Touring-3000%20Yellow%2C%2058%E2%80%99%20bike.%20Other%20rows%20in%20the%20result%20set%20show%20the%20other%20assemblies%20which%20have%20dependency%20on%20Bearing%20Ball%20at%20some%20level.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThis%20query%20uses%20a%20lot%20of%20new%20syntax.%20Let%E2%80%99s%20understand%20what%20this%20new%20syntax%20does.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CFONT%20size%3D%223%22%20color%3D%22%23333399%22%3E%3CSTRONG%3ESHORTEST_PATH%3C%2FSTRONG%3E%3C%2FFONT%3E%3C%2FP%3E%3CP%3EThe%20SHORTEST_PATH%20function%20lets%20you%20find%3A%3C%2FP%3E%3COL%3E%3CLI%3EA%20shortest%20path%20between%20two%20given%20nodes%2Fentities%3C%2FLI%3E%3CLI%3ESingle%20source%20shortest%20path(s).%3C%2FLI%3E%3CLI%3EShortest%20path%20from%20multiple%20source%20nodes%20to%20multiple%20target%20nodes.%3C%2FLI%3E%3C%2FOL%3E%3CP%3EIt%20takes%20an%20arbitrary%20length%20pattern%20as%20input%2C%20described%20below.%20It%20returns%20a%20shortest%20path%20that%20exists%20between%20two%20nodes.%26nbsp%3BThis%20function%20can%20only%20be%20used%20inside%20MATCH.%20It%20accepts%20an%20arbitrary%20length%20pattern%20and%20finds%20a%20shortest%20path%20in%20the%20graph%2C%20which%20matches%20that%20pattern.%20If%20there%20exist%20two%20or%20more%20shortest%20paths%20of%20the%20same%20length%20between%20any%20pair%20of%20source%20and%20destination%20node(s)%2C%20the%20function%20will%20return%20the%20one%20that%20was%20found%20first%20during%20traversal.%20Note%20that%2C%20an%20arbitrary%20length%20pattern%20can%20only%20be%20specified%20inside%20a%20SHORTEST_PATH%20function.%20Finding%20weighted%20shortest%20paths%2C%20all%20paths%20or%20all%20shortest%20paths%20is%20not%20supported%20yet.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CFONT%20size%3D%223%22%20color%3D%22%23333399%22%3E%3CSTRONG%3EArbitrary%20Length%20Pattern%3C%2FSTRONG%3E%3C%2FFONT%3E%3C%2FP%3E%3CP%3EThis%20pattern%20includes%20the%20nodes%20and%20edges%20that%20must%20be%20traversed%20repeatedly%20until%20the%20desired%20node%20is%20reached%20or%20until%20the%20maximum%20number%20of%20iterations%20as%20specified%20in%20the%20pattern%20is%20met.%20Each%20time%20the%20query%20is%20executed%2C%20the%20result%20of%20executing%20this%20pattern%20will%20be%20a%20collection%20of%20nodes%20and%20edges%20traversed%20along%20the%20path%20from%20the%20start%20node%20to%20the%20end%20node.%20This%20is%20a%20regular%20expression%20style%20syntax%20pattern%20and%20the%20following%20two%20pattern%20quantifiers%20are%20supported%20in%20this%20release%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CSTRONG%3E%E2%80%98%2B%E2%80%99%26nbsp%3B%3C%2FSTRONG%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3A%26nbsp%3B%26nbsp%3B%20Repeat%20the%20pattern%201%20or%20more%20times.%20Terminate%20as%20soon%20as%20a%20shortest%20path%20is%20found.%3C%2FP%3E%3CP%3EWe%20recommend%20that%20users%20provide%20a%20start%20and%20an%20end%20node%2C%20along%20with%20the%20pattern%2C%20whenever%20%E2%80%98%2B%E2%80%99%20is%20used%20in%20the%20pattern%20predicate.%20For%20a%20big%20graph%2C%20a%20query%20with%20%E2%80%98%2B%E2%80%99%20and%20no%20terminating%20condition%20may%20take%20a%20long%20time%20to%20finish.%20Refer%20to%20the%20example%20shown%20in%20%E2%80%98Find%20Shortest%20path%20between%202%20given%20nodes%E2%80%99%20section%20below%2C%20to%20see%20how%20filters%20on%20start%20and%20end%20node%20can%20be%20applied.%3C%2FP%3E%3CP%3E%3CSTRONG%3E%7B1%2Cn%7D%3C%2FSTRONG%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3A%20Repeat%20the%20pattern%201%20to%20%E2%80%98n%E2%80%99%20times.%20Terminate%20as%20soon%20as%20a%20shortest%20%26nbsp%3Bis%20found%20or%20when%20the%20max%20number%20of%20iterations%20%E2%80%98n%E2%80%99%20specified%20in%20the%20pattern%20is%20reached.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CFONT%20size%3D%223%22%20color%3D%22%23333399%22%3E%3CSTRONG%3EFOR%20PATH%3C%2FSTRONG%3E%3C%2FFONT%3E%3C%2FP%3E%3CP%3EFOR%20PATH%20must%20be%20used%20with%20any%20node%20or%20edge%20table%20name%20in%20the%20FROM%20clause%2C%20which%20will%20participate%20in%20an%20arbitrary%20length%20pattern.%20FOR%20PATH%20tells%20the%20engine%20that%20the%20node%20or%20edge%20table%20will%20return%20an%20ordered%20collection%20representing%20the%20list%20of%20nodes%20or%20edges%20along%20the%20path%20from%20the%20start%20node%20to%20the%20end%20node.%20The%20attributes%20from%20these%20tables%20cannot%20be%20projected%20directly%20in%20the%20SELECT%20clause%20and%20cannot%20be%20filtered%20directly%20in%20the%20WHERE%20clause.%20To%20project%20or%20filter%20attributes%20from%20these%20tables%2C%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CSPAN%3E%3CSTRONG%3Egraph%20path%20aggregate%20functions%3C%2FSTRONG%3E%3C%2FSPAN%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3Emust%20be%20used.%3C%2FP%3E%3CP%3E%3CSTRONG%3E%26nbsp%3B%3C%2FSTRONG%3E%3C%2FP%3E%3CP%3E%3CFONT%20size%3D%223%22%20color%3D%22%23333399%22%3E%3CSTRONG%3EGraph%20Path%20Aggregate%20Functions%3C%2FSTRONG%3E%3C%2FFONT%3E%3C%2FP%3E%3CP%3ESince%20the%20nodes%20and%20edges%20involved%20in%20arbitrary%20length%20pattern%20return%20a%20collection%20(of%20node(s)%20and%20edge(s)%20traversed%20in%20that%20path)%2C%20users%20cannot%20project%20the%20attributes%20directly%20using%20the%20conventional%20tablename.attributename%20syntax.%20For%20queries%20where%20it%20is%20required%20to%20project%20attribute%20values%20from%20the%20intermediate%20node%20or%20edge%20tables%2C%20in%20the%20path%20traversed%2C%20we%20have%20extended%20aggregate%20functions%20to%20work%20with%20graph%20node%20or%20edge%20tables.%20In%20this%20release%2C%20the%20following%20graph%20path%20aggregate%20functions%20are%20available%3A%20STRING_AGG%2C%20LAST_VALUE%2C%20SUM%2C%20AVG%2C%20MIN%2C%20MAX%20and%20COUNT.%26nbsp%3BThese%20functions%20can%20be%20used%20in%20SELECT%20or%20ORDER%20BY%20clause.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CFONT%20size%3D%224%22%20color%3D%22%23000080%22%3E%3CSTRONG%3EFind%20all%20the%20products%20which%20are%20up%20to%203%20levels%20away%20from%20a%20given%20product.%3C%2FSTRONG%3E%3C%2FFONT%3E%3C%2FP%3E%3CP%3ENow%2C%20if%20Adventure%20Works%20Cycles%20wants%20to%20find%20products%20in%20their%20BOM%2C%20which%20are%201%20to%203%20levels%20away%20from%20Bearing%20Ball%2C%20they%20can%20just%20modify%20the%20query%20shown%20in%20example%20above%20as%20follows%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%3ESELECT%20%0A%20P1.ProductID%2C%20%0A%20P1.Name%2C%0A%20STRING_AGG(P2.Name%2C'-%26gt%3B')%20WITHIN%20GROUP%20(GRAPH%20PATH)%20AS%20%5BAssembly%5D%2C%0A%20LAST_VALUE(P2.ProductID)%20WITHIN%20GROUP%20(GRAPH%20PATH)%20AS%20%5BFinal%20ProductID%5D%0AFROM%0A%20PRODUCT%20P1%2C%0A%20PRODUCT%20FOR%20PATH%20P2%2C%0A%20ISPARTOF%20FOR%20PATH%20IPO%0AWHERE%20%0A%20MATCH(SHORTEST_PATH(P1(-(IPO)-%26gt%3BP2)%7B1%2C3%7D))%0A%20AND%20P1.ProductID%20%3D%202%0AORDER%20BY%20P1.ProductID%20%3C%2FPRE%3E%3CP%3EHere%20is%20a%20sample%20result%20set%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3C%2FP%3E%3CDIV%20class%3D%22mceNonEditable%20lia-copypaste-placeholder%22%3E%26nbsp%3B%3C%2FDIV%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CFONT%20size%3D%224%22%20color%3D%22%23000080%22%3E%3CSTRONG%3EFind%20Shortest%20Path%20between%202%20given%20nodes%3C%2FSTRONG%3E%3C%2FFONT%3E%3C%2FP%3E%3CP%3EThe%20queries%20shown%20above%2C%20start%20from%20a%20given%20node%20(Bearing%20Ball)%20and%20then%20search%20all%20paths%20in%20the%20graph%20starting%20from%20that%20node%2C%20until%20they%20cannot%20find%20any%20other%20paths%20that%20originate%20from%20this%20node%20and%20end%20at%20other%20nodes.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EImagine%20that%20Adventure%20Works%20Cycles%20wants%20to%20find%20the%20shortest%20path%20between%202%20given%20nodes%20in%20the%20assembly.%20They%20can%20use%20following%20query%20to%20do%20this.%20This%20query%20will%20again%20start%20at%20ProductID%20%3D%202%20(Bearing%20Ball)%20but%20terminates%20as%20soon%20as%20it%20finds%20ProductID%20%3D%20768%20(bike%20%E2%80%98Road-650%20Black%2C%2044%E2%80%99).%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%3ESELECT%0A%20ProductID%2C%20Name%2C%20%5BAssembly%5D%2C%20FinalProductID%2C%20Levels%0AFROM%20(%0A%20SELECT%20%0A%20%20P1.ProductID%2C%20%0A%20%20P1.Name%2C%0A%20%20STRING_AGG(P2.Name%2C'-%26gt%3B')%20WITHIN%20GROUP%20(GRAPH%20PATH)%20AS%20%5BAssembly%5D%2C%0A%20%20LAST_VALUE(P2.ProductID)%20WITHIN%20GROUP%20(GRAPH%20PATH)%20AS%20FinalProductID%2C%0A%20%20COUNT(P2.ProductID)%20WITHIN%20GROUP%20(GRAPH%20PATH)%20AS%20Levels%0A%20FROM%0A%20%20PRODUCT%20P1%2C%0A%20%20PRODUCT%20FOR%20PATH%20P2%2C%0A%20%20ISPARTOF%20FOR%20PATH%20IPO%0A%20WHERE%20%0A%20%20MATCH(SHORTEST_PATH(P1(-(IPO)-%26gt%3BP2)%2B))%0A%20%20AND%20P1.ProductID%20%3D%202%0A%20)%20AS%20Q%0A%20WHERE%20Q.FinalProductID%20%3D%20768%3C%2FPRE%3E%3CP%3ENote%20that%20in%20this%20query%2C%20we%20used%20a%20subquery%20to%20apply%20filter%20on%20the%20last%20node%20traversed%20in%20the%20path%20(FinalProductID%20%3D%20768).%20The%20filter%20on%20the%20FinalProductID%20in%20the%20outer%20query%20is%20pushed%20down%20while%20computing%20the%20shortest%20path.%20Hence%2C%20the%20query%20will%20terminate%20as%20soon%20as%20the%20FinalProductID%20is%20found.%20LAST_VALUE()%20function%2C%20which%20fetches%20the%20attribute%20values%20from%20last%20node%20traversed%2C%20cannot%20be%20used%20in%20the%20WHERE%20clause%2C%20since%20it%20is%20a%20window%20aggregate%20function.%20%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EBy%20using%20the%20COUNT%20graph%20path%20aggregate%20function%20in%20select%20clause%2C%20we%20can%20see%20the%20number%20of%20hops%2Flevels%20between%20the%202%20ProductIDs.%20Here%20is%20the%20result%20set%20of%20this%20query%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3C%2FP%3E%3CDIV%20class%3D%22mceNonEditable%20lia-copypaste-placeholder%22%3E%26nbsp%3B%3C%2FDIV%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EIn%20this%20query%2C%20the%20predicate%2Ffilter%20on%20last%20node%20(Q.FinalProductID)%20is%20pushed%20down.%20That%20is%2C%20the%20shortest%20path%20query%20terminates%20as%20soon%20as%20it%20finds%20ProductID%20%3D%20768.%3C%2FP%3E%3CP%3EWriting%20a%20shortest%20path%20query%20with%20a%20terminating%20condition%20on%20the%20last%20node%2C%20as%20shown%20in%20the%20query%20above%2C%20is%20highly%20recommended.%20This%20is%20a%20much%20more%20optimal%20query%20than%20the%20ones%20which%20start%20at%20one%20node%20and%20then%20traverse%20the%20entire%20graph%20to%20find%20all%20the%20connections%20that%20exist%20from%20that%20node%20to%20others.%20For%20huge%20graphs%2C%20finding%20the%20shortest%20path%20from%20a%20given%20start%20node%20to%20all%20the%20other%20nodes%20in%20the%20graph%2C%20will%20be%20a%20very%20expensive%20operation.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CFONT%20size%3D%224%22%20color%3D%22%23000080%22%3E%3CSTRONG%3EConclusion%3C%2FSTRONG%3E%3C%2FFONT%3E%3C%2FP%3E%3CP%3EStarting%20with%20SQL%20Server%202019%20CTP3.1%2C%20you%20can%20use%20SHORTEST_PATH%20function%20to%20compute%20shortest%20path(s)%20between%20nodes%20in%20a%20graph%2C%20using%20a%20regular%20expression%20style%20search%20pattern.%20SHORTEST_PATH%20can%20only%20be%20used%20inside%20a%20MATCH%20function%20and%20works%20only%20with%20graph%20node%20and%20edge%20tables.%20This%20function%20also%20allows%20you%20to%20write%20transitive%20closure%20or%20arbitrary%20length%20traversal%20query%20from%20a%20given%20%E2%80%98start%E2%80%99%20node.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CFONT%20size%3D%223%22%20color%3D%22%23333399%22%3E%3CSTRONG%3ENext%20Steps%3C%2FSTRONG%3E%3C%2FFONT%3E%3C%2FP%3E%3CP%3EPlease%20download%20and%20give%20this%20feature%20a%20try.%26nbsp%3B%20We%20are%20looking%20forward%20to%20hearing%20your%20comments%20or%20feedback%20for%20this%20feature.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1599735%22%20slang%3D%22en-US%22%3ERe%3A%20Public%20Preview%20of%20Shortest%20Path%20on%20SQL%20Server%202019%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1599735%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F73668%22%20target%3D%22_blank%22%3E%40Shreya%20Verma%3C%2FA%3E%26nbsp%3B%2C%3C%2FP%3E%3CP%3EI%20figured%20out%20my%20main%20mistake%3A%3CBR%20%2F%3EFor%20the%20recursive%20explosion%20I%2C%20of%20course%2C%20filter%20the%20root%20records%20-%20because%20this%20is%20how%20you%20seed%20the%20recursive%20query.%3C%2FP%3E%3CP%3EFor%20the%20Graph%20query%20I%20was%20not%20doing%20that.%26nbsp%3B%3CBR%20%2F%3EWhen%20I%20added%20filtering%20of%20the%20roots%2C%20the%20graph%20query%20performance%20became%20very%20close%20to%20recursive%2C%20thou%20still%20worse.%3C%2FP%3E%3CP%3EMy%20trees%20data%20has%20only%20about%201%25%20or%20products%2Froots%20with%20more%20then%202%20levels%20of%20hierarchy.%20So%20I%20suspect%20when%20I%20test%20with%20more%20levels%20of%20nesting%20for%20most%20of%20the%20data%20the%20performance%20of%20the%20graph%20queries%20may%20win.%3C%2FP%3E%3CP%3EI%20will%20post%20my%20updates.%3C%2FP%3E%3C%2FLINGO-BODY%3E
Microsoft

SQL Server 2017 and Azure SQL Database introduced native graph database capabilities used to model many-to-many relationships. The first implementation of SQL Graph introduced support for nodes to represent entities, edges to represent relationships and a new MATCH predicate to support graph pattern matching and traversal.

 

We are expanding the graph database capabilities with several new features. In this blog we discuss one of these features that is now available for public preview in SQL Server 2019, Shortest Path, which can be used to find a shortest path between two nodes in a graph. The shortest path function can also be used to compute a transitive closure or for arbitrary length traversals.

 

With CTP3.1, we are introducing a new function SHORTEST_PATH, which can be used inside MATCH to find a shortest path between any 2 nodes in a graph or to perform arbitrary length traversals. Users can specify a pattern they would like to search for in the graph using a regular expression style syntax.

 

To understand the new syntax and how shortest path works, from Adventure Works Cycles, a bicycle wholesaler that sells and manufactures bicycles. We have converted their bill of materials hierarchy into a graph, which has Product node and IsPartOf edge table. Download sample scripts here.

 

AdventureWorksBOM.png

Now, let’s look at some sample queries to see how shortest path can help with recursive or arbitrary length traversals in a graph schema.  

 

Find all assemblies where a given product is used

Imagine that the inventory of a given product (Bearing Ball) at Adventure Works Cycles is running low and that they would like to find all the assemblies in their manufacturing pipeline, where a Bearing Ball is used. This will tell them what assemblies will be affected due to the scarcity of a given product.

 

The following query demonstrates how shortest path can be used to find all assemblies that require a Bearing Ball in the bill of materials:

 

-- Find all the assemblies where Bearing Ball is used
SELECT 
	P1.ProductID, 
	P1.Name,
	STRING_AGG(P2.Name,'->') WITHIN GROUP (GRAPH PATH) AS [Assembly],
	LAST_VALUE(P2.ProductID) WITHIN GROUP (GRAPH PATH) AS [Final ProductID]
FROM
	PRODUCT P1,
	PRODUCT FOR PATH P2,
	ISPARTOF FOR PATH IPO
WHERE 
	MATCH(SHORTEST_PATH(P1(-(IPO)->P2)+))
	AND P1.ProductID = 2
       ORDER BY P1.ProductID	

This query starts from ProductID = 2 (Bearing Ball) and keeps traversing up the BOM tree until it finds all products that contain a Bearing Ball.

 

Here are first few lines from the result set:

      image.png

 

The STRING_AGG function on P2.Name fetches all the nodes that were traversed in this path, thereby, giving us a good view of assembly line up to ‘Touring-3000 Yellow, 58’ bike. Other rows in the result set show the other assemblies which have dependency on Bearing Ball at some level.

 

This query uses a lot of new syntax. Let’s understand what this new syntax does.

 

SHORTEST_PATH

The SHORTEST_PATH function lets you find:

  1. A shortest path between two given nodes/entities
  2. Single source shortest path(s).
  3. Shortest path from multiple source nodes to multiple target nodes.

It takes an arbitrary length pattern as input, described below. It returns a shortest path that exists between two nodes. This function can only be used inside MATCH. It accepts an arbitrary length pattern and finds a shortest path in the graph, which matches that pattern. If there exist two or more shortest paths of the same length between any pair of source and destination node(s), the function will return the one that was found first during traversal. Note that, an arbitrary length pattern can only be specified inside a SHORTEST_PATH function. Finding weighted shortest paths, all paths or all shortest paths is not supported yet.

 

Arbitrary Length Pattern

This pattern includes the nodes and edges that must be traversed repeatedly until the desired node is reached or until the maximum number of iterations as specified in the pattern is met. Each time the query is executed, the result of executing this pattern will be a collection of nodes and edges traversed along the path from the start node to the end node. This is a regular expression style syntax pattern and the following two pattern quantifiers are supported in this release:

 

‘+’  :   Repeat the pattern 1 or more times. Terminate as soon as a shortest path is found.

We recommend that users provide a start and an end node, along with the pattern, whenever ‘+’ is used in the pattern predicate. For a big graph, a query with ‘+’ and no terminating condition may take a long time to finish. Refer to the example shown in ‘Find Shortest path between 2 given nodes’ section below, to see how filters on start and end node can be applied.

{1,n} : Repeat the pattern 1 to ‘n’ times. Terminate as soon as a shortest  is found or when the max number of iterations ‘n’ specified in the pattern is reached.

 

FOR PATH

FOR PATH must be used with any node or edge table name in the FROM clause, which will participate in an arbitrary length pattern. FOR PATH tells the engine that the node or edge table will return an ordered collection representing the list of nodes or edges along the path from the start node to the end node. The attributes from these tables cannot be projected directly in the SELECT clause and cannot be filtered directly in the WHERE clause. To project or filter attributes from these tables, graph path aggregate functions must be used.

 

Graph Path Aggregate Functions

Since the nodes and edges involved in arbitrary length pattern return a collection (of node(s) and edge(s) traversed in that path), users cannot project the attributes directly using the conventional tablename.attributename syntax. For queries where it is required to project attribute values from the intermediate node or edge tables, in the path traversed, we have extended aggregate functions to work with graph node or edge tables. In this release, the following graph path aggregate functions are available: STRING_AGG, LAST_VALUE, SUM, AVG, MIN, MAX and COUNT. These functions can be used in SELECT or ORDER BY clause.

 

Find all the products which are up to 3 levels away from a given product.

Now, if Adventure Works Cycles wants to find products in their BOM, which are 1 to 3 levels away from Bearing Ball, they can just modify the query shown in example above as follows:

 

SELECT 
	P1.ProductID, 
	P1.Name,
	STRING_AGG(P2.Name,'->') WITHIN GROUP (GRAPH PATH) AS [Assembly],
	LAST_VALUE(P2.ProductID) WITHIN GROUP (GRAPH PATH) AS [Final ProductID]
FROM
	PRODUCT P1,
	PRODUCT FOR PATH P2,
	ISPARTOF FOR PATH IPO
WHERE 
	MATCH(SHORTEST_PATH(P1(-(IPO)->P2){1,3}))
	AND P1.ProductID = 2
ORDER BY P1.ProductID	

Here is a sample result set:

      image.png

 

Find Shortest Path between 2 given nodes

The queries shown above, start from a given node (Bearing Ball) and then search all paths in the graph starting from that node, until they cannot find any other paths that originate from this node and end at other nodes.

 

Imagine that Adventure Works Cycles wants to find the shortest path between 2 given nodes in the assembly. They can use following query to do this. This query will again start at ProductID = 2 (Bearing Ball) but terminates as soon as it finds ProductID = 768 (bike ‘Road-650 Black, 44’).

 

SELECT
	ProductID, Name, [Assembly], FinalProductID, Levels
FROM (
	SELECT 
		P1.ProductID, 
		P1.Name,
		STRING_AGG(P2.Name,'->') WITHIN GROUP (GRAPH PATH) AS [Assembly],
		LAST_VALUE(P2.ProductID) WITHIN GROUP (GRAPH PATH) AS FinalProductID,
		COUNT(P2.ProductID) WITHIN GROUP (GRAPH PATH) AS Levels
	FROM
		PRODUCT P1,
		PRODUCT FOR PATH P2,
		ISPARTOF FOR PATH IPO
	WHERE 
		MATCH(SHORTEST_PATH(P1(-(IPO)->P2)+))
		AND P1.ProductID = 2
 ) AS Q
 WHERE Q.FinalProductID = 768

Note that in this query, we used a subquery to apply filter on the last node traversed in the path (FinalProductID = 768). The filter on the FinalProductID in the outer query is pushed down while computing the shortest path. Hence, the query will terminate as soon as the FinalProductID is found. LAST_VALUE() function, which fetches the attribute values from last node traversed, cannot be used in the WHERE clause, since it is a window aggregate function.  

 

By using the COUNT graph path aggregate function in select clause, we can see the number of hops/levels between the 2 ProductIDs. Here is the result set of this query:

 

      image.png

 

In this query, the predicate/filter on last node (Q.FinalProductID) is pushed down. That is, the shortest path query terminates as soon as it finds ProductID = 768.

Writing a shortest path query with a terminating condition on the last node, as shown in the query above, is highly recommended. This is a much more optimal query than the ones which start at one node and then traverse the entire graph to find all the connections that exist from that node to others. For huge graphs, finding the shortest path from a given start node to all the other nodes in the graph, will be a very expensive operation.

 

Conclusion

Starting with SQL Server 2019 CTP3.1, you can use SHORTEST_PATH function to compute shortest path(s) between nodes in a graph, using a regular expression style search pattern. SHORTEST_PATH can only be used inside a MATCH function and works only with graph node and edge tables. This function also allows you to write transitive closure or arbitrary length traversal query from a given ‘start’ node.

 

Next Steps

Please download and give this feature a try.  We are looking forward to hearing your comments or feedback for this feature.

 

7 Comments
Occasional Visitor

This all sounds very promising, but how can it be used for weighted paths, eg: distances on a map? It seems it is only useful for counting nodes, but not distances. Am I missing something?

 

Microsoft

@freom You cannot use this to calculate a weighted shortest path. But, you can store weights as edge properties/attributes and calculate the weight for the shortest path by distance using the aggregate functions (sum, in this case) over edge attributes. 

Occasional Visitor
@Shreya Verma Are there any plans to allow extra edge columns to be used in WHERE clauses? SHORTEST_PATH will not work properly if you have cycles in your graph and use an extra column (i.e. a column type) to differentiate between the different edge types (i.e. parent/child)
Occasional Visitor

@Shreya Verma - Thanks for introducing to the new features from SQL Graph DB, I have been trying to use this in my latest project for storing hierarchy / tree structures where we join multiple individual hierarchies to create a combined tree structure with intersections at leaf levels from one or more hierarchies to store the Cartesian product by only creating a separate Edge Table to store that relationship, which is very powerful.

 

While working with my example of creating separate Edge Table to store  combined tree structure, I have observed limitations of SHORTEST_PATH, in my use case when we have multiple parent nodes for same child node, SHORTEST_PATH is not able to produce output from all possible child nodes, it will find path to the last node for only first iteration of Parent to Child, please see my example below

 

In this example I have 2 tree structures of "Region" and "Vehicles", purpose is to create "Sales by Region" hierarchy

 

When SHORTEST_PATH query is executed, I am expecting to get full path from all nodes of Region to all possible nodes of Vehicles, but the path stops at the first region of "Illinois" and does not show data for "Wisonsin"

 

Please suggest any corrections which can help generate full path  .. many thanks

 

---Create Sample Schema
CREATE SCHEMA graph
GO

--Tree Nodes
CREATE TABLE graph.Tree_Nodes
(
	tree_id bigint
	,tree_parent bigint
	,tree_name NVARCHAR(50)
) AS NODE


--Combined Tree Edges
CREATE TABLE graph.Combined_Tree_Edges
(
	tree_id bigint
) AS EDGE

--Tree Insert
INSERT INTO graph.Tree_Nodes VALUES (1,null,'Region')
INSERT INTO graph.Tree_Nodes VALUES (2,1,'USA')
INSERT INTO graph.Tree_Nodes VALUES (3,2,'Illinois')
INSERT INTO graph.Tree_Nodes VALUES (4,2,'Wisonsin')
INSERT INTO graph.Tree_Nodes VALUES (5,null,'Vehicles')
INSERT INTO graph.Tree_Nodes VALUES (6,5,'Cars')
INSERT INTO graph.Tree_Nodes VALUES (7,5,'Trucks')
INSERT INTO graph.Tree_Nodes VALUES (8,6,'BMW')
INSERT INTO graph.Tree_Nodes VALUES (9,6,'Merc')
INSERT INTO graph.Tree_Nodes VALUES (10,6,'Tesla')
INSERT INTO graph.Tree_Nodes VALUES (11,7,'Volvo')
INSERT INTO graph.Tree_Nodes VALUES (12,7,'Daimler')


--Create Edge relations on graph.Combined_Tree_Edges
INSERT INTO graph.Combined_Tree_Edges ($from_id, $to_id,tree_id) 
select	 ParentFromNode.$node_id
		,ChildToNode.$node_id
		,ChildToNode.tree_id
from graph.Tree_Nodes ParentFromNode 
INNER JOIN graph.Tree_Nodes ChildToNode ON ChildToNode.tree_parent=ParentFromNode.tree_id
WHERE ChildToNode.tree_id IN (2,3,4,6,7,8,9,10,11,12)

--Intersection Edges
INSERT INTO graph.Combined_Tree_Edges ($from_id, $to_id,tree_id) 
VALUES
		(
			(SELECT $node_id FROM graph.Tree_Nodes WHERE tree_id=3) --Illinois
			,(SELECT $node_id FROM graph.Tree_Nodes WHERE tree_id=5) --Vehicles
			,3
		)

INSERT INTO graph.Combined_Tree_Edges ($from_id, $to_id,tree_id) 
VALUES
		(
			(SELECT $node_id FROM graph.Tree_Nodes WHERE tree_id=4) --Wisonsin
			,(SELECT $node_id FROM graph.Tree_Nodes WHERE tree_id=5) --Vehicles
			,4
		)


--Final Select Query
SELECT
	Parent.tree_id AS ParentId,
	Parent.tree_name AS ParentName,
	STRING_AGG(CAST(Child.tree_id AS NVARCHAR(10)), ' | ') WITHIN GROUP (GRAPH PATH) AS PathIds,
	STRING_AGG(CAST(Child.tree_name AS NVARCHAR(10)), ' | ') WITHIN GROUP (GRAPH PATH) AS PathNames,
	LAST_VALUE(Child.tree_parent) WITHIN GROUP (GRAPH PATH) AS LastNodeParentId,
	LAST_VALUE(Child.tree_id) WITHIN GROUP (GRAPH PATH) AS LastNodeId,
	LAST_VALUE(Child.tree_name) WITHIN GROUP (GRAPH PATH) AS LastNodeName
FROM
	graph.Tree_Nodes AS Parent,
	graph.Combined_Tree_Edges FOR PATH AS ChildOf,
	graph.Tree_Nodes FOR PATH  AS Child
WHERE MATCH(SHORTEST_PATH(Parent(-(ChildOf)->Child)+))
AND Parent.tree_id IN (1) ---Region

/*
--Cleanup
DROP TABLE graph.Combined_Tree_Edges
GO

DROP TABLE graph.Tree_Nodes
GO

DROP SCHEMA graph
GO

*/
 

Final Select OutputFinal Select Output

 

Excel ExampleExcel Example

 

Occasional Visitor

Thank you  @Shreya Vermathis is a great article and inspires hopes for using graph db for BOM traversing in SQL Server 2019.

However in all my testing it performed poorly comparing to using recursive-CTE-based tree explosion.

The execution times were 10 to 20 times longer, output Rows estimates 4 to 10 times worse, plan cost comparison always 0% for recursion and 100% for the graph query using SHORTEST_PATH().

---------

Is this a known issue?

At this time it looks like Graph Db in SQL Server was created to support MATCH clause rather then improve performance of hierarchical data querying.

---------

Can you recommend any remedies?

- I played with different indexing on the node and edge tables. An index on the edge ($from_id, $to_id) helps, but not dramatically.

- OPTION (HASH JOIN) helps when querying for All product trees, but not for One product tree.

The sample data I used had 5 levels of hierarchy.

Microsoft

@alex_io is it possible for you to share a reproduction? without looking at the query it is hard to tell why it is performing poorly for you. 

 

Thanks,

shreya

Occasional Visitor

@Shreya Verma ,

I figured out my main mistake:
For the recursive explosion I, of course, filter the root records - because this is how you seed the recursive query.

For the Graph query I was not doing that. 
When I added filtering of the roots, the graph query performance became very close to recursive, thou still worse.

My trees data has only about 1% or products/roots with more then 2 levels of hierarchy. So I suspect when I test with more levels of nesting for most of the data the performance of the graph queries may win.

I will post my updates.