ldbc_bi.slt 223 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667
  1. # Copyright Materialize, Inc. and contributors. All rights reserved.
  2. #
  3. # Use of this software is governed by the Business Source License
  4. # included in the LICENSE file at the root of this repository.
  5. #
  6. # As of the Change Date specified in that file, in accordance with
  7. # the Business Source License, use of this software will be governed
  8. # by the Apache License, Version 2.0.
  9. # The underlying LDBC SNB BI benchmark is under the Apache 2.0 license
  10. # as well; see materialize/test/ldbc-bi/LICENSE.txt.
  11. ######################################################################
  12. # TABLE DEFINITIONS
  13. ######################################################################
  14. # PRIMARY KEY annotations (which are in the spec) are currently
  15. # removed from this slt, because we don't support them at the moment.
  16. # (Note that _in slts_ they are actually supported, but it's better to
  17. # match the plans of real runs more closely.)
  18. statement ok
  19. CREATE TABLE Organisation (
  20. id bigint,
  21. type text NOT NULL,
  22. name text NOT NULL,
  23. url text NOT NULL,
  24. LocationPlaceId bigint NOT NULL
  25. )
  26. statement ok
  27. CREATE INDEX Organisation_id ON Organisation (id)
  28. statement ok
  29. CREATE TABLE Place (
  30. id bigint,
  31. name text NOT NULL,
  32. url text NOT NULL,
  33. type text NOT NULL,
  34. PartOfPlaceId bigint -- null for continents
  35. )
  36. statement ok
  37. CREATE INDEX Place_id ON Place (id)
  38. statement ok
  39. CREATE TABLE Tag (
  40. id bigint,
  41. name text NOT NULL,
  42. url text NOT NULL,
  43. TypeTagClassId bigint NOT NULL
  44. )
  45. statement ok
  46. CREATE INDEX Tag_id ON Tag (id)
  47. statement ok
  48. CREATE INDEX Tag_name ON Tag (name)
  49. statement ok
  50. CREATE INDEX Tag_TypeTagClassId ON Tag (TypeTagClassId)
  51. statement ok
  52. CREATE TABLE TagClass (
  53. id bigint,
  54. name text NOT NULL,
  55. url text NOT NULL,
  56. SubclassOfTagClassId bigint -- null for the root TagClass (Thing)
  57. )
  58. statement ok
  59. CREATE INDEX TagClass_id ON TagClass (id)
  60. statement ok
  61. CREATE INDEX TagClass_name ON TagClass (name)
  62. statement ok
  63. CREATE TABLE Comment (
  64. creationDate timestamp with time zone NOT NULL,
  65. id bigint NOT NULL,
  66. locationIP text NOT NULL,
  67. browserUsed text NOT NULL,
  68. content text NOT NULL,
  69. length int NOT NULL,
  70. CreatorPersonId bigint NOT NULL,
  71. LocationCountryId bigint NOT NULL,
  72. ParentPostId bigint,
  73. ParentCommentId bigint
  74. )
  75. statement ok
  76. CREATE INDEX Comment_id ON Comment (id)
  77. statement ok
  78. CREATE TABLE Forum (
  79. creationDate timestamp with time zone NOT NULL,
  80. id bigint,
  81. title text NOT NULL,
  82. ModeratorPersonId bigint -- can be null as its cardinality is 0..1
  83. )
  84. statement ok
  85. CREATE INDEX Forum_id ON Forum (id)
  86. statement ok
  87. CREATE INDEX Forum_ModeratorPersonId on Forum (ModeratorPersonId)
  88. statement ok
  89. CREATE TABLE Post (
  90. creationDate timestamp with time zone NOT NULL,
  91. id bigint NOT NULL,
  92. imageFile text,
  93. locationIP text NOT NULL,
  94. browserUsed text NOT NULL,
  95. language text,
  96. content text,
  97. length int NOT NULL,
  98. CreatorPersonId bigint NOT NULL,
  99. ContainerForumId bigint NOT NULL,
  100. LocationCountryId bigint NOT NULL
  101. )
  102. statement ok
  103. CREATE INDEX Post_id ON Post (id)
  104. statement ok
  105. CREATE TABLE Person (
  106. creationDate timestamp with time zone NOT NULL,
  107. id bigint,
  108. firstName text NOT NULL,
  109. lastName text NOT NULL,
  110. gender text NOT NULL,
  111. birthday date NOT NULL,
  112. locationIP text NOT NULL,
  113. browserUsed text NOT NULL,
  114. LocationCityId bigint NOT NULL,
  115. speaks text NOT NULL,
  116. email text NOT NULL
  117. )
  118. statement ok
  119. CREATE INDEX Person_id ON Person (id)
  120. statement ok
  121. CREATE INDEX Person_LocationCityId ON Person (LocationCityId)
  122. statement ok
  123. CREATE TABLE Comment_hasTag_Tag (
  124. creationDate timestamp with time zone NOT NULL,
  125. CommentId bigint NOT NULL,
  126. TagId bigint NOT NULL
  127. )
  128. statement ok
  129. CREATE TABLE Post_hasTag_Tag (
  130. creationDate timestamp with time zone NOT NULL,
  131. PostId bigint NOT NULL,
  132. TagId bigint NOT NULL
  133. )
  134. statement ok
  135. CREATE TABLE Forum_hasMember_Person (
  136. creationDate timestamp with time zone NOT NULL,
  137. ForumId bigint NOT NULL,
  138. PersonId bigint NOT NULL
  139. )
  140. statement ok
  141. CREATE INDEX Forum_hasMember_Person_ForumId ON Forum_hasMember_Person (ForumId)
  142. statement ok
  143. CREATE INDEX Forum_hasMember_Person_PersonId ON Forum_hasMember_Person (PersonId)
  144. statement ok
  145. CREATE TABLE Forum_hasTag_Tag (
  146. creationDate timestamp with time zone NOT NULL,
  147. ForumId bigint NOT NULL,
  148. TagId bigint NOT NULL
  149. )
  150. statement ok
  151. CREATE TABLE Person_hasInterest_Tag (
  152. creationDate timestamp with time zone NOT NULL,
  153. PersonId bigint NOT NULL,
  154. TagId bigint NOT NULL
  155. )
  156. statement ok
  157. CREATE INDEX Person_hasInterest_Tag_TagId ON Person_hasInterest_Tag (TagId)
  158. statement ok
  159. CREATE TABLE Person_likes_Comment (
  160. creationDate timestamp with time zone NOT NULL,
  161. PersonId bigint NOT NULL,
  162. CommentId bigint NOT NULL
  163. )
  164. statement ok
  165. CREATE TABLE Person_likes_Post (
  166. creationDate timestamp with time zone NOT NULL,
  167. PersonId bigint NOT NULL,
  168. PostId bigint NOT NULL
  169. )
  170. statement ok
  171. CREATE TABLE Person_studyAt_University (
  172. creationDate timestamp with time zone NOT NULL,
  173. PersonId bigint NOT NULL,
  174. UniversityId bigint NOT NULL,
  175. classYear int NOT NULL
  176. )
  177. statement ok
  178. CREATE INDEX Person_studyAt_University_PersonId ON Person_studyAt_University (PersonId)
  179. statement ok
  180. CREATE INDEX Person_studyAt_University_UniversityId ON Person_studyAt_University (UniversityId)
  181. statement ok
  182. CREATE TABLE Person_workAt_Company (
  183. creationDate timestamp with time zone NOT NULL,
  184. PersonId bigint NOT NULL,
  185. CompanyId bigint NOT NULL,
  186. workFrom int NOT NULL
  187. )
  188. statement ok
  189. CREATE INDEX Person_workAt_Company_PersonId ON Person_workAt_Company (PersonId)
  190. statement ok
  191. CREATE INDEX Person_workAt_Company_CompanyId ON Person_workAt_Company (CompanyId)
  192. statement ok
  193. CREATE TABLE Person_knows_Person (
  194. creationDate timestamp with time zone NOT NULL,
  195. Person1id bigint NOT NULL,
  196. Person2id bigint NOT NULL
  197. )
  198. statement ok
  199. CREATE INDEX Person_knows_Person_Person1id ON Person_knows_Person (Person1id)
  200. statement ok
  201. CREATE INDEX Person_knows_Person_Person2id ON person_knows_person (Person2id)
  202. statement ok
  203. CREATE INDEX Person_knows_Person_Person1id_Person2id ON Person_knows_Person (Person1id, Person2id)
  204. ######################################################################
  205. # VIEWS
  206. ######################################################################
  207. statement ok
  208. CREATE OR REPLACE MATERIALIZED VIEW Country AS
  209. SELECT id, name, url, PartOfPlaceId AS PartOfContinentId
  210. FROM Place
  211. WHERE type = 'Country'
  212. statement ok
  213. CREATE INDEX Country_id ON Country (id)
  214. statement ok
  215. CREATE OR REPLACE MATERIALIZED VIEW City AS
  216. SELECT id, name, url, PartOfPlaceId AS PartOfCountryId
  217. FROM Place
  218. WHERE type = 'City'
  219. statement ok
  220. CREATE INDEX City_id ON City (id)
  221. statement ok
  222. CREATE INDEX City_PartOfCountryId ON City (PartOfCountryId)
  223. statement ok
  224. CREATE OR REPLACE MATERIALIZED VIEW Company AS
  225. SELECT id, name, url, LocationPlaceId AS LocatedInCountryId
  226. FROM Organisation
  227. WHERE type = 'Company'
  228. statement ok
  229. CREATE INDEX Company_id ON Company (id)
  230. statement ok
  231. CREATE OR REPLACE MATERIALIZED VIEW University AS
  232. SELECT id, name, url, LocationPlaceId AS LocatedInCityId
  233. FROM Organisation
  234. WHERE type = 'University'
  235. statement ok
  236. CREATE INDEX University_id ON University (id)
  237. statement ok
  238. CREATE OR REPLACE MATERIALIZED VIEW Message_hasTag_Tag AS
  239. (SELECT creationDate, CommentId as MessageId, TagId FROM Comment_hasTag_Tag)
  240. UNION
  241. (SELECT creationDate, PostId as MessageId, TagId FROM Post_hasTag_Tag)
  242. statement ok
  243. CREATE INDEX Message_hasTag_Tag_MessageId ON Message_hasTag_Tag (MessageId)
  244. statement ok
  245. CREATE INDEX Message_hasTag_Tag_TagId ON Message_hasTag_Tag (TagId)
  246. statement ok
  247. CREATE OR REPLACE MATERIALIZED VIEW Person_likes_Message AS
  248. (SELECT creationDate, PersonId, CommentId as MessageId FROM Person_likes_Comment)
  249. UNION
  250. (SELECT creationDate, PersonId, PostId as MessageId FROM Person_likes_Post)
  251. statement ok
  252. CREATE INDEX Person_likes_Message_PersonId ON Person_likes_Message (PersonId)
  253. statement ok
  254. CREATE INDEX Person_likes_Message_MessageId ON Person_likes_Message (MessageId)
  255. statement ok
  256. CREATE OR REPLACE MATERIALIZED VIEW Message AS
  257. WITH MUTUALLY RECURSIVE
  258. -- compute the transitive closure (with root information) using minimnal info
  259. roots (MessageId bigint, RootPostId bigint, RootPostLanguage text, ContainerForumId bigint, ParentMessageId bigint) AS
  260. ( SELECT id AS MessageId, id AS RootPostId, language AS RootPostLanguage, ContainerForumId, NULL::bigint AS ParentMessageId FROM Post
  261. UNION SELECT
  262. Comment.id AS MessageId,
  263. ParentPostId AS RootPostId,
  264. language AS RootPostLanguage,
  265. Post.ContainerForumId AS ContainerForumId,
  266. ParentPostId AS ParentMessageId
  267. FROM Comment
  268. JOIN Post
  269. ON Comment.ParentPostId = Post.id),
  270. ms (MessageId bigint, RootPostId bigint, RootPostLanguage text, ContainerForumId bigint, ParentMessageId bigint) AS
  271. ( SELECT *
  272. FROM roots
  273. UNION SELECT
  274. Comment.id AS MessageId,
  275. ms.RootPostId AS RootPostId,
  276. ms.RootPostLanguage AS RootPostLanguage,
  277. ms.ContainerForumId AS ContainerForumId,
  278. ParentCommentId AS ParentMessageId
  279. FROM Comment
  280. JOIN ms
  281. ON ParentCommentId = ms.MessageId)
  282. -- now do the late materialization
  283. ( SELECT
  284. creationDate,
  285. id AS MessageId,
  286. id AS RootPostId,
  287. language AS RootPostLanguage,
  288. content,
  289. imageFile,
  290. locationIP,
  291. browserUsed,
  292. length,
  293. CreatorPersonId,
  294. ContainerForumId,
  295. LocationCountryId,
  296. NULL::bigint AS ParentMessageId
  297. FROM Post
  298. UNION (SELECT
  299. Comment.creationDate AS creationDate,
  300. Comment.id AS MessageId,
  301. ms.RootPostId AS RootPostId,
  302. ms.RootPostLanguage AS RootPostLanguage,
  303. Comment.content AS content,
  304. NULL::text AS imageFile,
  305. Comment.locationIP AS locationIP,
  306. Comment.browserUsed AS browserUsed,
  307. Comment.length AS length,
  308. Comment.CreatorPersonId AS CreatorPersonId,
  309. ms.ContainerForumId AS ContainerForumId,
  310. Comment.LocationCountryId AS LocationCityId,
  311. ms.ParentMessageId AS ParentMessageId
  312. FROM Comment
  313. JOIN ms
  314. ON Comment.id = ms.MessageId))
  315. statement ok
  316. CREATE INDEX Message_MessageId ON Message (MessageId)
  317. statement ok
  318. CREATE INDEX Message_ContainerForumId ON Message (ContainerForumId)
  319. statement ok
  320. CREATE INDEX Message_ParentMessageId ON Message (ParentMessageId)
  321. statement ok
  322. CREATE INDEX Message_CreatorPersonId ON Message (CreatorPersonId)
  323. statement ok
  324. CREATE INDEX Message_RootPostLanguage ON Message (RootPostLanguage)
  325. statement ok
  326. CREATE OR REPLACE VIEW Comment_View AS
  327. SELECT creationDate, MessageId AS id, locationIP, browserUsed, content, length, CreatorPersonId, LocationCountryId, ParentMessageId
  328. FROM Message
  329. WHERE ParentMessageId IS NOT NULL
  330. statement ok
  331. CREATE OR REPLACE VIEW Post_View AS
  332. SELECT creationDate, MessageId AS id, imageFile, locationIP, browserUsed, RootPostLanguage, content, length, CreatorPersonId, ContainerForumId, LocationCountryId
  333. FROM Message
  334. WHERE ParentMessageId IS NULL
  335. ######################################################################
  336. # QUERY 01
  337. ######################################################################
  338. # \set datetime '\'2010-06-11T09:21:46.000+00:00\'::TIMESTAMP'
  339. query T multiline
  340. EXPLAIN OPTIMIZED PLAN WITH(humanized expressions, arity, join implementations) AS VERBOSE TEXT FOR WITH
  341. message_count AS (
  342. SELECT 0.0 + count(*) AS cnt
  343. FROM Message
  344. WHERE creationDate < '2010-06-11T09:21:46.000+00:00'::TIMESTAMP
  345. )
  346. , message_prep AS (
  347. SELECT extract(year from creationDate) AS messageYear
  348. , ParentMessageId IS NOT NULL AS isComment
  349. , CASE
  350. WHEN length < 40 THEN 0 -- short
  351. WHEN length < 80 THEN 1 -- one liner
  352. WHEN length < 160 THEN 2 -- tweet
  353. ELSE 3 -- long
  354. END AS lengthCategory
  355. , length
  356. FROM Message
  357. WHERE creationDate < '2010-06-11T09:21:46.000+00:00'::TIMESTAMP
  358. AND content IS NOT NULL
  359. )
  360. SELECT messageYear, isComment, lengthCategory
  361. , count(*) AS messageCount
  362. , avg(length::bigint) AS averageMessageLength
  363. , sum(length::bigint) AS sumMessageLength
  364. , count(*) / mc.cnt AS percentageOfMessages
  365. FROM message_prep
  366. , message_count mc
  367. GROUP BY messageYear, isComment, lengthCategory, mc.cnt
  368. ORDER BY messageYear DESC, isComment ASC, lengthCategory ASC
  369. ----
  370. Explained Query:
  371. Finish order_by=[#0 desc nulls_first, #1 asc nulls_last, #2 asc nulls_last] output=[#0..=#6]
  372. With
  373. cte l0 =
  374. Reduce aggregates=[count(*)] // { arity: 1 }
  375. Project () // { arity: 0 }
  376. Filter (#0{creationdate} < 2010-06-11 09:21:46 UTC) // { arity: 13 }
  377. ReadIndex on=message message_messageid=[*** full scan ***] // { arity: 13 }
  378. Return // { arity: 7 }
  379. Project (#0..=#2, #4{count}, #7, #5{sum}, #8) // { arity: 7 }
  380. Map ((#5{sum} / bigint_to_numeric(case when (#6{count} = 0) then null else #6{count} end)), (bigint_to_numeric(#4{count}) / #3{cnt})) // { arity: 9 }
  381. Reduce group_by=[#1..=#4] aggregates=[count(*), sum(integer_to_bigint(#0{length})), count(integer_to_bigint(#0{length}))] // { arity: 7 }
  382. CrossJoin type=differential // { arity: 5 }
  383. implementation
  384. %1[×]U » %0:message[×]if
  385. ArrangeBy keys=[[]] // { arity: 4 }
  386. Project (#8{length}, #13..=#15) // { arity: 4 }
  387. Filter (#0{creationdate} < 2010-06-11 09:21:46 UTC) AND (#4{content}) IS NOT NULL // { arity: 16 }
  388. Map (extract_year_tstz(#0{creationdate}), (#12{parentmessageid}) IS NOT NULL, case when (#8{length} < 40) then 0 else case when (#8{length} < 80) then 1 else case when (#8{length} < 160) then 2 else 3 end end end) // { arity: 16 }
  389. ReadIndex on=message message_messageid=[*** full scan ***] // { arity: 13 }
  390. ArrangeBy keys=[[]] // { arity: 1 }
  391. Project (#1) // { arity: 1 }
  392. Map ((0 + bigint_to_numeric(#0{count}))) // { arity: 2 }
  393. Union // { arity: 1 }
  394. Get l0 // { arity: 1 }
  395. Map (0) // { arity: 1 }
  396. Union // { arity: 0 }
  397. Negate // { arity: 0 }
  398. Project () // { arity: 0 }
  399. Get l0 // { arity: 1 }
  400. Constant // { arity: 0 }
  401. - ()
  402. Used Indexes:
  403. - materialize.public.message_messageid (*** full scan ***)
  404. Target cluster: quickstart
  405. EOF
  406. ######################################################################
  407. # QUERY 02
  408. ######################################################################
  409. # \set date '\'2010-06-08\'::TIMESTAMP'
  410. # \set tagClass '\'ChristianBishop\''
  411. query T multiline
  412. EXPLAIN OPTIMIZED PLAN WITH(humanized expressions, arity, join implementations) AS VERBOSE TEXT FOR WITH
  413. MyTag AS (
  414. SELECT Tag.id AS id, Tag.name AS name
  415. FROM TagClass
  416. JOIN Tag
  417. ON Tag.TypeTagClassId = TagClass.id
  418. WHERE TagClass.name = 'ChristianBishop'
  419. ),
  420. detail AS (
  421. SELECT t.id as TagId
  422. , count(CASE WHEN Message.creationDate < '2010-06-08'::TIMESTAMP + INTERVAL '100 days' THEN Message.MessageId ELSE NULL END) AS countMonth1
  423. , count(CASE WHEN Message.creationDate >= '2010-06-08'::TIMESTAMP + INTERVAL '100 days' THEN Message.MessageId ELSE NULL END) AS countMonth2
  424. FROM MyTag t
  425. JOIN Message_hasTag_Tag
  426. ON Message_hasTag_tag.TagId = t.id
  427. JOIN Message
  428. ON Message.MessageId = Message_hasTag_tag.MessageId
  429. AND Message.creationDate >= '2010-06-08'::TIMESTAMP
  430. AND Message.creationDate < '2010-06-08'::TIMESTAMP + INTERVAL '200 days'
  431. GROUP BY t.id
  432. )
  433. SELECT t.name AS "tag.name"
  434. , coalesce(countMonth1, 0)
  435. , coalesce(countMonth2, 0)
  436. , abs(coalesce(countMonth1, 0)-coalesce(countMonth2, 0)) AS diff
  437. FROM MyTag t LEFT JOIN detail ON t.id = detail.TagId
  438. ORDER BY diff desc, t.name
  439. LIMIT 100
  440. ----
  441. Explained Query:
  442. Finish order_by=[#3 desc nulls_first, #0{name} asc nulls_last] limit=100 output=[#0..=#3]
  443. With
  444. cte l0 =
  445. Project (#5{id}, #6{name}) // { arity: 2 }
  446. Join on=(#0{id} = #8{typetagclassid}) type=differential // { arity: 9 }
  447. implementation
  448. %0:tagclass[#0{id}]KAe » %1:tag[#3{typetagclassid}]KAe
  449. ArrangeBy keys=[[#0{id}]] // { arity: 5 }
  450. ReadIndex on=materialize.public.tagclass tagclass_name=[lookup value=("ChristianBishop")] // { arity: 5 }
  451. ArrangeBy keys=[[#3{typetagclassid}]] // { arity: 4 }
  452. ReadIndex on=tag tag_typetagclassid=[differential join] // { arity: 4 }
  453. cte l1 =
  454. Filter (#0{id}) IS NOT NULL // { arity: 2 }
  455. Get l0 // { arity: 2 }
  456. cte l2 =
  457. Project (#1{name}, #3{count}, #4{count}) // { arity: 3 }
  458. Join on=(#0{id} = #2{id}) type=differential // { arity: 5 }
  459. implementation
  460. %1[#0]UKA » %0:l1[#0{id}]K
  461. ArrangeBy keys=[[#0{id}]] // { arity: 2 }
  462. Get l1 // { arity: 2 }
  463. ArrangeBy keys=[[#0{id}]] // { arity: 3 }
  464. Reduce group_by=[#0{id}] aggregates=[count(case when (#2{creationdate} < 2010-09-16 00:00:00 UTC) then #1{messageid} else null end), count(case when (#2{creationdate} >= 2010-09-16 00:00:00 UTC) then #1{messageid} else null end)] // { arity: 3 }
  465. Project (#0{id}, #2{messageid}, #4{creationdate}) // { arity: 3 }
  466. Filter (#4{creationdate} < 2010-12-25 00:00:00 UTC) AND (#4{creationdate} >= 2010-06-08 00:00:00 UTC) // { arity: 17 }
  467. Join on=(#0{id} = #3{tagid} AND #2{messageid} = #5{messageid}) type=delta // { arity: 17 }
  468. implementation
  469. %0:l1 » %1:message_hastag_tag[#2{tagid}]KA » %2:message[#1{messageid}]KAiif
  470. %1:message_hastag_tag » %2:message[#1{messageid}]KAiif » %0:l1[#0{id}]K
  471. %2:message » %1:message_hastag_tag[#1{messageid}]KA » %0:l1[#0{id}]K
  472. ArrangeBy keys=[[#0{id}]] // { arity: 1 }
  473. Project (#0{id}) // { arity: 1 }
  474. Get l1 // { arity: 2 }
  475. ArrangeBy keys=[[#1{messageid}], [#2{tagid}]] // { arity: 3 }
  476. ReadIndex on=message_hastag_tag message_hastag_tag_messageid=[delta join lookup] message_hastag_tag_tagid=[delta join lookup] // { arity: 3 }
  477. ArrangeBy keys=[[#1{messageid}]] // { arity: 13 }
  478. ReadIndex on=message message_messageid=[delta join lookup] // { arity: 13 }
  479. Return // { arity: 4 }
  480. Project (#0{name}, #3..=#5) // { arity: 4 }
  481. Map (coalesce(#1{count}, 0), coalesce(#2{count}, 0), abs((#3{"?column?"} - #4{"?column?"}))) // { arity: 6 }
  482. Union // { arity: 3 }
  483. Map (null, null) // { arity: 3 }
  484. Union // { arity: 1 }
  485. Negate // { arity: 1 }
  486. Project (#0{name}) // { arity: 1 }
  487. Get l2 // { arity: 3 }
  488. Project (#1{name}) // { arity: 1 }
  489. Get l0 // { arity: 2 }
  490. Get l2 // { arity: 3 }
  491. Used Indexes:
  492. - materialize.public.tag_typetagclassid (differential join)
  493. - materialize.public.tagclass_name (lookup)
  494. - materialize.public.message_hastag_tag_messageid (delta join lookup)
  495. - materialize.public.message_hastag_tag_tagid (delta join lookup)
  496. - materialize.public.message_messageid (delta join lookup)
  497. Target cluster: quickstart
  498. EOF
  499. ######################################################################
  500. # QUERY 03
  501. ######################################################################
  502. # \set tagClass '\'Philosopher\''
  503. # \set country '\'China\''
  504. query T multiline
  505. EXPLAIN OPTIMIZED PLAN WITH(humanized expressions, arity, join implementations) AS VERBOSE TEXT FOR
  506. SELECT Forum.id AS "forum.id"
  507. , Forum.title AS "forum.title"
  508. , Forum.creationDate AS "forum.creationDate"
  509. , Forum.ModeratorPersonId AS "person.id"
  510. , count(Message.MessageId) AS messageCount
  511. FROM Message
  512. JOIN Forum
  513. ON Forum.id = Message.ContainerForumId
  514. JOIN Person AS ModeratorPerson
  515. ON ModeratorPerson.id = Forum.ModeratorPersonId
  516. JOIN City
  517. ON City.id = ModeratorPerson.LocationCityId
  518. JOIN Country
  519. ON Country.id = City.PartOfCountryId
  520. AND Country.name = 'China'
  521. WHERE EXISTS (
  522. SELECT 1
  523. FROM TagClass
  524. JOIN Tag
  525. ON Tag.TypeTagClassId = TagClass.id
  526. JOIN Message_hasTag_Tag
  527. ON Message_hasTag_Tag.TagId = Tag.id
  528. WHERE Message.MessageId = Message_hasTag_Tag.MessageId AND TagClass.name = 'Philosopher')
  529. GROUP BY Forum.id, Forum.title, Forum.creationDate, Forum.ModeratorPersonId
  530. ORDER BY messageCount DESC, Forum.id
  531. LIMIT 20
  532. ----
  533. Explained Query:
  534. Finish order_by=[#4{count} desc nulls_first, #0{containerforumid} asc nulls_last] limit=20 output=[#0..=#4]
  535. Reduce group_by=[#0{containerforumid}, #2{title}, #1{creationdate}, #3{moderatorpersonid}] aggregates=[count(*)] // { arity: 5 }
  536. Project (#10{containerforumid}, #13{creationdate}, #15{title}, #16{moderatorpersonid}) // { arity: 4 }
  537. Filter (#33{name} = "China") AND (#16{moderatorpersonid}) IS NOT NULL AND (#31{partofcountryid}) IS NOT NULL // { arity: 37 }
  538. Join on=(#1{messageid} = #36{messageid} AND #10{containerforumid} = #14{id} AND #16{moderatorpersonid} = #18{id} AND #25{locationcityid} = #28{id} AND #31{partofcountryid} = #32{id}) type=delta // { arity: 37 }
  539. implementation
  540. %0:message » %5[#0]UKA » %1:forum[#1{id}]KA » %2:person[#1{id}]KA » %3:city[#0{id}]KA » %4:country[#0{id}]KAef
  541. %1:forum » %0:message[#10{containerforumid}]KA » %5[#0]UKA » %2:person[#1{id}]KA » %3:city[#0{id}]KA » %4:country[#0{id}]KAef
  542. %2:person » %1:forum[#3{moderatorpersonid}]KA » %0:message[#10{containerforumid}]KA » %5[#0]UKA » %3:city[#0{id}]KA » %4:country[#0{id}]KAef
  543. %3:city » %4:country[#0{id}]KAef » %2:person[#8{locationcityid}]KA » %1:forum[#3{moderatorpersonid}]KA » %0:message[#10{containerforumid}]KA » %5[#0]UKA
  544. %4:country » %3:city[#3{partofcountryid}]KA » %2:person[#8{locationcityid}]KA » %1:forum[#3{moderatorpersonid}]KA » %0:message[#10{containerforumid}]KA » %5[#0]UKA
  545. %5 » %0:message[#1{messageid}]KA » %1:forum[#1{id}]KA » %2:person[#1{id}]KA » %3:city[#0{id}]KA » %4:country[#0{id}]KAef
  546. ArrangeBy keys=[[#1{messageid}], [#10{containerforumid}]] // { arity: 13 }
  547. ReadIndex on=message message_messageid=[delta join 1st input (full scan)] message_containerforumid=[delta join lookup] // { arity: 13 }
  548. ArrangeBy keys=[[#1{id}], [#3{moderatorpersonid}]] // { arity: 4 }
  549. ReadIndex on=forum forum_id=[delta join lookup] forum_moderatorpersonid=[delta join lookup] // { arity: 4 }
  550. ArrangeBy keys=[[#1{id}], [#8{locationcityid}]] // { arity: 11 }
  551. ReadIndex on=person person_id=[delta join lookup] person_locationcityid=[delta join lookup] // { arity: 11 }
  552. ArrangeBy keys=[[#0{id}], [#3{partofcountryid}]] // { arity: 4 }
  553. ReadIndex on=city city_id=[delta join lookup] city_partofcountryid=[delta join lookup] // { arity: 4 }
  554. ArrangeBy keys=[[#0{id}]] // { arity: 4 }
  555. ReadIndex on=country country_id=[delta join lookup] // { arity: 4 }
  556. ArrangeBy keys=[[#0{messageid}]] // { arity: 1 }
  557. Distinct project=[#0{messageid}] // { arity: 1 }
  558. Project (#10{messageid}) // { arity: 1 }
  559. Join on=(#0{id} = #8{typetagclassid} AND #5{id} = #11{tagid}) type=delta // { arity: 12 }
  560. implementation
  561. %0:tagclass » %1:tag[#3{typetagclassid}]KA » %2:message_hastag_tag[#2{tagid}]KA
  562. %1:tag » %0:tagclass[#0{id}]KAe » %2:message_hastag_tag[#2{tagid}]KA
  563. %2:message_hastag_tag » %1:tag[#0{id}]KA » %0:tagclass[#0{id}]KAe
  564. ArrangeBy keys=[[#0{id}]] // { arity: 5 }
  565. ReadIndex on=materialize.public.tagclass tagclass_name=[lookup value=("Philosopher")] // { arity: 5 }
  566. ArrangeBy keys=[[#0{id}], [#3{typetagclassid}]] // { arity: 4 }
  567. ReadIndex on=tag tag_id=[delta join lookup] tag_typetagclassid=[delta join lookup] // { arity: 4 }
  568. ArrangeBy keys=[[#2{tagid}]] // { arity: 3 }
  569. ReadIndex on=message_hastag_tag message_hastag_tag_tagid=[delta join lookup] // { arity: 3 }
  570. Used Indexes:
  571. - materialize.public.tag_id (delta join lookup)
  572. - materialize.public.tag_typetagclassid (delta join lookup)
  573. - materialize.public.tagclass_name (lookup)
  574. - materialize.public.forum_id (delta join lookup)
  575. - materialize.public.forum_moderatorpersonid (delta join lookup)
  576. - materialize.public.person_id (delta join lookup)
  577. - materialize.public.person_locationcityid (delta join lookup)
  578. - materialize.public.country_id (delta join lookup)
  579. - materialize.public.city_id (delta join lookup)
  580. - materialize.public.city_partofcountryid (delta join lookup)
  581. - materialize.public.message_hastag_tag_tagid (delta join lookup)
  582. - materialize.public.message_messageid (delta join 1st input (full scan))
  583. - materialize.public.message_containerforumid (delta join lookup)
  584. Target cluster: quickstart
  585. EOF
  586. ######################################################################
  587. # QUERY 04
  588. ######################################################################
  589. # \set date '\'2010-02-12\''::timestamp
  590. statement ok
  591. CREATE OR REPLACE MATERIALIZED VIEW Top100PopularForumsQ04 AS
  592. SELECT
  593. T.id AS id,
  594. Forum.creationdate AS creationDate,
  595. T.maxNumberOfMembers AS maxNumberOfMembers
  596. FROM (SELECT
  597. ForumId AS id,
  598. MAX(numberOfMembers) AS maxNumberOfMembers
  599. FROM (SELECT
  600. Forum_hasMember_Person.ForumId AS ForumId,
  601. count(Person.id) AS numberOfMembers,
  602. City.PartOfCountryId AS CountryId
  603. FROM Forum_hasMember_Person
  604. JOIN Person
  605. ON Person.id = Forum_hasMember_Person.PersonId
  606. JOIN City
  607. ON City.id = Person.LocationCityId
  608. GROUP BY City.PartOfCountryId, Forum_hasMember_Person.ForumId)
  609. ForumMembershipPerCountry
  610. GROUP BY ForumId) T, Forum
  611. WHERE T.id = Forum.id
  612. statement ok
  613. CREATE INDEX Top100PopularForumsQ04_id ON Top100PopularForumsQ04 (id);
  614. query T multiline
  615. EXPLAIN OPTIMIZED PLAN WITH(humanized expressions, arity, join implementations) AS VERBOSE TEXT FOR WITH
  616. Top100_Popular_Forums AS (
  617. SELECT id, creationDate, maxNumberOfMembers
  618. FROM Top100PopularForumsQ04
  619. WHERE creationDate > '2010-02-12'::timestamp
  620. ORDER BY maxNumberOfMembers DESC, id
  621. LIMIT 100
  622. ),
  623. au AS (
  624. SELECT *
  625. FROM Person
  626. WHERE EXISTS (SELECT 1
  627. FROM Top100_Popular_Forums
  628. INNER JOIN Forum_hasMember_Person
  629. ON Forum_hasMember_Person.ForumId = Top100_Popular_Forums.id
  630. WHERE Forum_hasMember_Person.PersonId = Person.id)
  631. ),
  632. Top100_Message AS (
  633. SELECT MessageId,
  634. CreatorPersonId
  635. FROM Message
  636. WHERE Message.ContainerForumId IN (SELECT id FROM Top100_Popular_Forums)
  637. )
  638. SELECT au.id AS "person.id"
  639. , au.firstName AS "person.firstName"
  640. , au.lastName AS "person.lastName"
  641. , au.creationDate
  642. -- a single person might be member of more than 1 of the top100 forums, so their messages should be DISTINCT counted
  643. , COUNT(Top100_Message.MessageId) AS messageCount
  644. FROM au
  645. LEFT JOIN Top100_Message
  646. ON au.id = Top100_Message.CreatorPersonId
  647. GROUP BY au.id, au.firstName, au.lastName, au.creationDate
  648. ORDER BY messageCount DESC, au.id
  649. LIMIT 100
  650. ----
  651. Explained Query:
  652. Finish order_by=[#4{count_messageid} desc nulls_first, #0{id} asc nulls_last] limit=100 output=[#0..=#4]
  653. With
  654. cte l0 =
  655. Project (#0{id}) // { arity: 1 }
  656. TopK order_by=[#1{maxnumberofmembers} desc nulls_first, #0{id} asc nulls_last] limit=100 // { arity: 2 }
  657. Project (#0{id}, #2{maxnumberofmembers}) // { arity: 2 }
  658. Filter (#1{creationdate} > 2010-02-12 00:00:00 UTC) // { arity: 3 }
  659. ReadIndex on=top100popularforumsq04 top100popularforumsq04_id=[*** full scan ***] // { arity: 3 }
  660. cte l1 =
  661. Project (#0{creationdate}..=#3{lastname}) // { arity: 4 }
  662. Join on=(#1{id} = #11{personid}) type=differential // { arity: 12 }
  663. implementation
  664. %1[#0]UKA » %0:person[#1{id}]KA
  665. ArrangeBy keys=[[#1{id}]] // { arity: 11 }
  666. ReadIndex on=person person_id=[differential join] // { arity: 11 }
  667. ArrangeBy keys=[[#0{personid}]] // { arity: 1 }
  668. Distinct project=[#0{personid}] // { arity: 1 }
  669. Project (#3{personid}) // { arity: 1 }
  670. Join on=(#0{id} = #2{forumid}) type=differential // { arity: 4 }
  671. implementation
  672. %1:forum_hasmember_person[#1{forumid}]KA » %0:l0[#0{id}]K
  673. ArrangeBy keys=[[#0{id}]] // { arity: 1 }
  674. Get l0 // { arity: 1 }
  675. ArrangeBy keys=[[#1{forumid}]] // { arity: 3 }
  676. ReadIndex on=forum_hasmember_person forum_hasmember_person_forumid=[differential join] // { arity: 3 }
  677. cte l2 =
  678. ArrangeBy keys=[[#1{id}]] // { arity: 4 }
  679. Get l1 // { arity: 4 }
  680. cte l3 =
  681. Project (#0{creationdate}..=#3{lastname}, #5{messageid}) // { arity: 5 }
  682. Join on=(#1{id} = #13{creatorpersonid} AND #14{containerforumid} = #17{id}) type=delta // { arity: 18 }
  683. implementation
  684. %0:l2 » %1:message[#9{creatorpersonid}]KA » %2[#0]UKA
  685. %1:message » %2[#0]UKA » %0:l2[#1{id}]K
  686. %2 » %1:message[#10{containerforumid}]KA » %0:l2[#1{id}]K
  687. Get l2 // { arity: 4 }
  688. ArrangeBy keys=[[#9{creatorpersonid}], [#10{containerforumid}]] // { arity: 13 }
  689. ReadIndex on=message message_containerforumid=[delta join lookup] message_creatorpersonid=[delta join lookup] // { arity: 13 }
  690. ArrangeBy keys=[[#0{id}]] // { arity: 1 }
  691. Distinct project=[#0{id}] // { arity: 1 }
  692. Get l0 // { arity: 1 }
  693. Return // { arity: 5 }
  694. Reduce group_by=[#1{id}..=#3{lastname}, #0{creationdate}] aggregates=[count(#4{messageid})] // { arity: 5 }
  695. Union // { arity: 5 }
  696. Map (null) // { arity: 5 }
  697. Union // { arity: 4 }
  698. Negate // { arity: 4 }
  699. Project (#0{creationdate}..=#3{lastname}) // { arity: 4 }
  700. Join on=(#1{id} = #4{id}) type=differential // { arity: 5 }
  701. implementation
  702. %1[#0]UKA » %0:l2[#1{id}]K
  703. Get l2 // { arity: 4 }
  704. ArrangeBy keys=[[#0{id}]] // { arity: 1 }
  705. Distinct project=[#0{id}] // { arity: 1 }
  706. Project (#1{id}) // { arity: 1 }
  707. Get l3 // { arity: 5 }
  708. Get l1 // { arity: 4 }
  709. Get l3 // { arity: 5 }
  710. Used Indexes:
  711. - materialize.public.person_id (differential join)
  712. - materialize.public.forum_hasmember_person_forumid (differential join)
  713. - materialize.public.message_containerforumid (delta join lookup)
  714. - materialize.public.message_creatorpersonid (delta join lookup)
  715. - materialize.public.top100popularforumsq04_id (*** full scan ***)
  716. Target cluster: quickstart
  717. EOF
  718. ######################################################################
  719. # QUERY 05
  720. ######################################################################
  721. # \set tag '\'Sikh_Empire\''
  722. # TODO(mgree) predicate push down anomaly on Tag.name
  723. query T multiline
  724. EXPLAIN OPTIMIZED PLAN WITH(humanized expressions, arity, join implementations) AS VERBOSE TEXT FOR WITH detail AS (
  725. SELECT Message.CreatorPersonId AS CreatorPersonId
  726. , sum(coalesce(Cs.c, 0)) AS replyCount
  727. , sum(coalesce(Plm.c, 0)) AS likeCount
  728. , count(Message.MessageId) AS messageCount
  729. FROM Tag
  730. JOIN Message_hasTag_Tag
  731. ON Message_hasTag_Tag.TagId = Tag.id
  732. JOIN Message
  733. ON Message.MessageId = Message_hasTag_Tag.MessageId
  734. LEFT JOIN (SELECT ParentMessageId, count(*) FROM Message c WHERE ParentMessageId IS NOT NULL GROUP BY ParentMessageId) Cs(id, c) ON Cs.id = Message.MessageId
  735. LEFT JOIN (SELECT MessageId, count(*) FROM Person_likes_Message GROUP BY MessageId) Plm(id, c) ON Plm.id = Message.MessageId
  736. WHERE Tag.name = 'Sikh_Empire'
  737. GROUP BY Message.CreatorPersonId
  738. )
  739. SELECT CreatorPersonId AS "person.id"
  740. , replyCount
  741. , likeCount
  742. , messageCount
  743. , 1*messageCount + 2*replyCount + 10*likeCount AS score
  744. FROM detail
  745. ORDER BY score DESC, CreatorPersonId
  746. LIMIT 100
  747. ----
  748. Explained Query:
  749. Finish order_by=[#4 desc nulls_first, #0{creatorpersonid} asc nulls_last] limit=100 output=[#0..=#4]
  750. With
  751. cte l0 =
  752. Project (#1{name}, #5{messageid}, #16{creatorpersonid}) // { arity: 3 }
  753. Join on=(#0{id} = #6{tagid} AND #5{messageid} = #8{messageid}) type=delta // { arity: 20 }
  754. implementation
  755. %0:tag » %1:message_hastag_tag[#2{tagid}]KA » %2:message[#1{messageid}]KA
  756. %1:message_hastag_tag » %0:tag[#0{id}]KA » %2:message[#1{messageid}]KA
  757. %2:message » %1:message_hastag_tag[#1{messageid}]KA » %0:tag[#0{id}]KA
  758. ArrangeBy keys=[[#0{id}]] // { arity: 4 }
  759. ReadIndex on=tag tag_id=[delta join 1st input (full scan)] // { arity: 4 }
  760. ArrangeBy keys=[[#1{messageid}], [#2{tagid}]] // { arity: 3 }
  761. ReadIndex on=message_hastag_tag message_hastag_tag_messageid=[delta join lookup] message_hastag_tag_tagid=[delta join lookup] // { arity: 3 }
  762. ArrangeBy keys=[[#1{messageid}]] // { arity: 13 }
  763. ReadIndex on=message message_messageid=[delta join lookup] // { arity: 13 }
  764. cte l1 =
  765. Reduce group_by=[#0{parentmessageid}] aggregates=[count(*)] // { arity: 2 }
  766. Project (#12{parentmessageid}) // { arity: 1 }
  767. Filter (#12{parentmessageid}) IS NOT NULL // { arity: 13 }
  768. ReadIndex on=message message_messageid=[*** full scan ***] // { arity: 13 }
  769. cte l2 =
  770. Reduce group_by=[#0{messageid}] aggregates=[count(*)] // { arity: 2 }
  771. Project (#2{messageid}) // { arity: 1 }
  772. ReadIndex on=person_likes_message person_likes_message_personid=[*** full scan ***] // { arity: 3 }
  773. cte l3 =
  774. Distinct project=[#0{messageid}] // { arity: 1 }
  775. Project (#1{messageid}) // { arity: 1 }
  776. Get l0 // { arity: 3 }
  777. Return // { arity: 5 }
  778. Map (((bigint_to_numeric((1 * #3{count})) + (2 * #1{sum})) + (10 * #2{sum}))) // { arity: 5 }
  779. Reduce group_by=[#0{creatorpersonid}] aggregates=[sum(coalesce(case when (#2) IS NULL then null else #1{count} end, 0)), sum(coalesce(case when (#4) IS NULL then null else #3{count} end, 0)), count(*)] // { arity: 4 }
  780. Project (#1{creatorpersonid}, #3{count}, #4, #6{count}, #7) // { arity: 5 }
  781. Join on=(#0{messageid} = #2{parentmessageid} = #5{messageid}) type=delta // { arity: 8 }
  782. implementation
  783. %0:l0 » %1[#0]K » %2[#0]K
  784. %1 » %0:l0[#0]Kef » %2[#0]K
  785. %2 » %0:l0[#0]Kef » %1[#0]K
  786. ArrangeBy keys=[[#0{messageid}]] // { arity: 2 }
  787. Project (#1{messageid}, #2{creatorpersonid}) // { arity: 2 }
  788. Filter (#0{name} = "Sikh_Empire") // { arity: 3 }
  789. Get l0 // { arity: 3 }
  790. ArrangeBy keys=[[#0{parentmessageid}]] // { arity: 3 }
  791. Union // { arity: 3 }
  792. Map (true) // { arity: 3 }
  793. Get l1 // { arity: 2 }
  794. Map (null, null) // { arity: 3 }
  795. Threshold // { arity: 1 }
  796. Union // { arity: 1 }
  797. Negate // { arity: 1 }
  798. Project (#0{parentmessageid}) // { arity: 1 }
  799. Get l1 // { arity: 2 }
  800. Get l3 // { arity: 1 }
  801. ArrangeBy keys=[[#0{messageid}]] // { arity: 3 }
  802. Union // { arity: 3 }
  803. Map (true) // { arity: 3 }
  804. Get l2 // { arity: 2 }
  805. Map (null, null) // { arity: 3 }
  806. Threshold // { arity: 1 }
  807. Union // { arity: 1 }
  808. Negate // { arity: 1 }
  809. Project (#0{messageid}) // { arity: 1 }
  810. Get l2 // { arity: 2 }
  811. Get l3 // { arity: 1 }
  812. Used Indexes:
  813. - materialize.public.tag_id (delta join 1st input (full scan))
  814. - materialize.public.message_hastag_tag_messageid (delta join lookup)
  815. - materialize.public.message_hastag_tag_tagid (delta join lookup)
  816. - materialize.public.person_likes_message_personid (*** full scan ***)
  817. - materialize.public.message_messageid (*** full scan ***, delta join lookup)
  818. Target cluster: quickstart
  819. EOF
  820. ######################################################################
  821. # QUERY 06
  822. ######################################################################
  823. # \set tag '\'Bob_Geldof\''
  824. statement ok
  825. CREATE OR REPLACE MATERIALIZED VIEW PopularityScoreQ06 AS
  826. SELECT
  827. message2.CreatorPersonId AS person2id,
  828. count(*) AS popularityScore
  829. FROM Message message2
  830. JOIN Person_likes_Message like2
  831. ON like2.MessageId = message2.MessageId
  832. GROUP BY message2.CreatorPersonId;
  833. statement ok
  834. CREATE INDEX PopularityScoreQ06_person2id ON PopularityScoreQ06 (person2id);
  835. # rewritten query to manually push filter down
  836. query T multiline
  837. EXPLAIN OPTIMIZED PLAN WITH(humanized expressions, arity, join implementations) AS VERBOSE TEXT FOR
  838. WITH applicable_posts AS (
  839. SELECT message1.MessageId,
  840. message1.CreatorPersonId AS person1id
  841. FROM Tag
  842. JOIN Message_hasTag_Tag
  843. ON Message_hasTag_Tag.TagId = Tag.id
  844. JOIN Message message1
  845. ON message1.MessageId = Message_hasTag_Tag.MessageId
  846. WHERE Tag.name = 'Bob_Geldof'
  847. ),
  848. poster_w_liker AS (
  849. SELECT DISTINCT
  850. message1.person1id,
  851. like2.PersonId AS person2id
  852. FROM applicable_posts message1
  853. LEFT JOIN Person_likes_Message like2
  854. ON like2.MessageId = message1.MessageId
  855. -- we don't need the Person itself as its ID is in the like
  856. )
  857. SELECT pl.person1id AS "person1.id",
  858. sum(coalesce(ps.popularityScore, 0)) AS authorityScore
  859. FROM poster_w_liker pl
  860. LEFT JOIN PopularityScoreQ06 ps
  861. ON ps.person2id = pl.person2id
  862. GROUP BY pl.person1id
  863. ORDER BY authorityScore DESC, pl.person1id ASC
  864. LIMIT 100
  865. ;
  866. ----
  867. Explained Query:
  868. Finish order_by=[#1{sum} desc nulls_first, #0{creatorpersonid} asc nulls_last] limit=100 output=[#0, #1]
  869. With
  870. cte l0 =
  871. Project (#6{messageid}, #17{creatorpersonid}) // { arity: 2 }
  872. Join on=(#0{id} = #7{tagid} AND #6{messageid} = #9{messageid}) type=delta // { arity: 21 }
  873. implementation
  874. %0:tag » %1:message_hastag_tag[#2{tagid}]KA » %2:message[#1{messageid}]KA
  875. %1:message_hastag_tag » %0:tag[#0{id}]KAe » %2:message[#1{messageid}]KA
  876. %2:message » %1:message_hastag_tag[#1{messageid}]KA » %0:tag[#0{id}]KAe
  877. ArrangeBy keys=[[#0{id}]] // { arity: 5 }
  878. ReadIndex on=materialize.public.tag tag_name=[lookup value=("Bob_Geldof")] // { arity: 5 }
  879. ArrangeBy keys=[[#1{messageid}], [#2{tagid}]] // { arity: 3 }
  880. ReadIndex on=message_hastag_tag message_hastag_tag_messageid=[delta join lookup] message_hastag_tag_tagid=[delta join lookup] // { arity: 3 }
  881. ArrangeBy keys=[[#1{messageid}]] // { arity: 13 }
  882. ReadIndex on=message message_messageid=[delta join lookup] // { arity: 13 }
  883. cte l1 =
  884. ArrangeBy keys=[[#0{messageid}]] // { arity: 2 }
  885. Get l0 // { arity: 2 }
  886. cte l2 =
  887. Project (#0{messageid}, #1{creatorpersonid}, #3{personid}) // { arity: 3 }
  888. Join on=(#0{messageid} = #4{messageid}) type=differential // { arity: 5 }
  889. implementation
  890. %1:person_likes_message[#2{messageid}]KA » %0:l1[#0{messageid}]K
  891. Get l1 // { arity: 2 }
  892. ArrangeBy keys=[[#2{messageid}]] // { arity: 3 }
  893. ReadIndex on=person_likes_message person_likes_message_messageid=[differential join] // { arity: 3 }
  894. cte l3 =
  895. Distinct project=[#0{creatorpersonid}, #1{personid}] // { arity: 2 }
  896. Union // { arity: 2 }
  897. Map (null) // { arity: 2 }
  898. Union // { arity: 1 }
  899. Negate // { arity: 1 }
  900. Project (#1{creatorpersonid}) // { arity: 1 }
  901. Join on=(#0{messageid} = #2{messageid}) type=differential // { arity: 3 }
  902. implementation
  903. %1[#0]UKA » %0:l1[#0{messageid}]K
  904. Get l1 // { arity: 2 }
  905. ArrangeBy keys=[[#0{messageid}]] // { arity: 1 }
  906. Distinct project=[#0{messageid}] // { arity: 1 }
  907. Project (#0{messageid}) // { arity: 1 }
  908. Get l2 // { arity: 3 }
  909. Project (#1{creatorpersonid}) // { arity: 1 }
  910. Get l0 // { arity: 2 }
  911. Project (#1{creatorpersonid}, #2{personid}) // { arity: 2 }
  912. Get l2 // { arity: 3 }
  913. cte l4 =
  914. Project (#0{creatorpersonid}, #3{popularityscore}) // { arity: 2 }
  915. Join on=(#1{personid} = #2{person2id}) type=differential // { arity: 4 }
  916. implementation
  917. %1:popularityscoreq06[#0]UKA » %0:l3[#1{person2id}]K
  918. ArrangeBy keys=[[#1{personid}]] // { arity: 2 }
  919. Filter (#1{personid}) IS NOT NULL // { arity: 2 }
  920. Get l3 // { arity: 2 }
  921. ArrangeBy keys=[[#0{person2id}]] // { arity: 2 }
  922. ReadIndex on=popularityscoreq06 popularityscoreq06_person2id=[differential join] // { arity: 2 }
  923. Return // { arity: 2 }
  924. Reduce group_by=[#0{creatorpersonid}] aggregates=[sum(coalesce(#1{popularityscore}, 0))] // { arity: 2 }
  925. Union // { arity: 2 }
  926. Map (null) // { arity: 2 }
  927. Union // { arity: 1 }
  928. Negate // { arity: 1 }
  929. Project (#0{creatorpersonid}) // { arity: 1 }
  930. Get l4 // { arity: 2 }
  931. Project (#0{creatorpersonid}) // { arity: 1 }
  932. Get l3 // { arity: 2 }
  933. Get l4 // { arity: 2 }
  934. Used Indexes:
  935. - materialize.public.tag_name (lookup)
  936. - materialize.public.message_hastag_tag_messageid (delta join lookup)
  937. - materialize.public.message_hastag_tag_tagid (delta join lookup)
  938. - materialize.public.person_likes_message_messageid (differential join)
  939. - materialize.public.message_messageid (delta join lookup)
  940. - materialize.public.popularityscoreq06_person2id (differential join)
  941. Target cluster: quickstart
  942. EOF
  943. # Gábor's version, yields identical output
  944. query T multiline
  945. EXPLAIN OPTIMIZED PLAN WITH(humanized expressions, arity, join implementations) AS VERBOSE TEXT FOR
  946. WITH poster_w_liker AS (
  947. SELECT DISTINCT
  948. message1.CreatorPersonId AS person1id,
  949. like2.PersonId AS person2id
  950. FROM (SELECT id FROM Tag WHERE Tag.name = 'Bob_Geldof') AS Tag_filtered
  951. JOIN Message_hasTag_Tag
  952. ON Message_hasTag_Tag.TagId = Tag_filtered.id
  953. JOIN Message message1
  954. ON message1.MessageId = Message_hasTag_Tag.MessageId
  955. LEFT JOIN Person_likes_Message like2
  956. ON like2.MessageId = message1.MessageId
  957. -- we don't need the Person itself as its ID is in the like
  958. )
  959. SELECT pl.person1id AS "person1.id",
  960. sum(coalesce(ps.popularityScore, 0)) AS authorityScore
  961. FROM poster_w_liker pl
  962. LEFT JOIN PopularityScoreQ06 ps
  963. ON ps.person2id = pl.person2id
  964. GROUP BY pl.person1id
  965. ORDER BY authorityScore DESC, pl.person1id ASC
  966. LIMIT 100
  967. ----
  968. Explained Query:
  969. Finish order_by=[#1{sum} desc nulls_first, #0{creatorpersonid} asc nulls_last] limit=100 output=[#0, #1]
  970. With
  971. cte l0 =
  972. Project (#6{messageid}, #17{creatorpersonid}) // { arity: 2 }
  973. Join on=(#0{id} = #7{tagid} AND #6{messageid} = #9{messageid}) type=delta // { arity: 21 }
  974. implementation
  975. %0:tag » %1:message_hastag_tag[#2{tagid}]KA » %2:message[#1{messageid}]KA
  976. %1:message_hastag_tag » %0:tag[#0{id}]KAe » %2:message[#1{messageid}]KA
  977. %2:message » %1:message_hastag_tag[#1{messageid}]KA » %0:tag[#0{id}]KAe
  978. ArrangeBy keys=[[#0{id}]] // { arity: 5 }
  979. ReadIndex on=materialize.public.tag tag_name=[lookup value=("Bob_Geldof")] // { arity: 5 }
  980. ArrangeBy keys=[[#1{messageid}], [#2{tagid}]] // { arity: 3 }
  981. ReadIndex on=message_hastag_tag message_hastag_tag_messageid=[delta join lookup] message_hastag_tag_tagid=[delta join lookup] // { arity: 3 }
  982. ArrangeBy keys=[[#1{messageid}]] // { arity: 13 }
  983. ReadIndex on=message message_messageid=[delta join lookup] // { arity: 13 }
  984. cte l1 =
  985. ArrangeBy keys=[[#0{messageid}]] // { arity: 2 }
  986. Get l0 // { arity: 2 }
  987. cte l2 =
  988. Project (#0{messageid}, #1{creatorpersonid}, #3{personid}) // { arity: 3 }
  989. Join on=(#0{messageid} = #4{messageid}) type=differential // { arity: 5 }
  990. implementation
  991. %1:person_likes_message[#2{messageid}]KA » %0:l1[#0{messageid}]K
  992. Get l1 // { arity: 2 }
  993. ArrangeBy keys=[[#2{messageid}]] // { arity: 3 }
  994. ReadIndex on=person_likes_message person_likes_message_messageid=[differential join] // { arity: 3 }
  995. cte l3 =
  996. Distinct project=[#0{creatorpersonid}, #1{personid}] // { arity: 2 }
  997. Union // { arity: 2 }
  998. Map (null) // { arity: 2 }
  999. Union // { arity: 1 }
  1000. Negate // { arity: 1 }
  1001. Project (#1{creatorpersonid}) // { arity: 1 }
  1002. Join on=(#0{messageid} = #2{messageid}) type=differential // { arity: 3 }
  1003. implementation
  1004. %1[#0]UKA » %0:l1[#0{messageid}]K
  1005. Get l1 // { arity: 2 }
  1006. ArrangeBy keys=[[#0{messageid}]] // { arity: 1 }
  1007. Distinct project=[#0{messageid}] // { arity: 1 }
  1008. Project (#0{messageid}) // { arity: 1 }
  1009. Get l2 // { arity: 3 }
  1010. Project (#1{creatorpersonid}) // { arity: 1 }
  1011. Get l0 // { arity: 2 }
  1012. Project (#1{creatorpersonid}, #2{personid}) // { arity: 2 }
  1013. Get l2 // { arity: 3 }
  1014. cte l4 =
  1015. Project (#0{creatorpersonid}, #3{popularityscore}) // { arity: 2 }
  1016. Join on=(#1{personid} = #2{person2id}) type=differential // { arity: 4 }
  1017. implementation
  1018. %1:popularityscoreq06[#0]UKA » %0:l3[#1{person2id}]K
  1019. ArrangeBy keys=[[#1{personid}]] // { arity: 2 }
  1020. Filter (#1{personid}) IS NOT NULL // { arity: 2 }
  1021. Get l3 // { arity: 2 }
  1022. ArrangeBy keys=[[#0{person2id}]] // { arity: 2 }
  1023. ReadIndex on=popularityscoreq06 popularityscoreq06_person2id=[differential join] // { arity: 2 }
  1024. Return // { arity: 2 }
  1025. Reduce group_by=[#0{creatorpersonid}] aggregates=[sum(coalesce(#1{popularityscore}, 0))] // { arity: 2 }
  1026. Union // { arity: 2 }
  1027. Map (null) // { arity: 2 }
  1028. Union // { arity: 1 }
  1029. Negate // { arity: 1 }
  1030. Project (#0{creatorpersonid}) // { arity: 1 }
  1031. Get l4 // { arity: 2 }
  1032. Project (#0{creatorpersonid}) // { arity: 1 }
  1033. Get l3 // { arity: 2 }
  1034. Get l4 // { arity: 2 }
  1035. Used Indexes:
  1036. - materialize.public.tag_name (lookup)
  1037. - materialize.public.message_hastag_tag_messageid (delta join lookup)
  1038. - materialize.public.message_hastag_tag_tagid (delta join lookup)
  1039. - materialize.public.person_likes_message_messageid (differential join)
  1040. - materialize.public.message_messageid (delta join lookup)
  1041. - materialize.public.popularityscoreq06_person2id (differential join)
  1042. Target cluster: quickstart
  1043. EOF
  1044. # TODO(mgree) predicate push down anomaly on Tag.name
  1045. # original umbra query
  1046. query T multiline
  1047. EXPLAIN OPTIMIZED PLAN WITH(humanized expressions, arity, join implementations) AS VERBOSE TEXT FOR
  1048. WITH poster_w_liker AS (
  1049. SELECT DISTINCT
  1050. message1.CreatorPersonId AS person1id,
  1051. like2.PersonId AS person2id
  1052. FROM Tag
  1053. JOIN Message_hasTag_Tag
  1054. ON Message_hasTag_Tag.TagId = Tag.id
  1055. JOIN Message message1
  1056. ON message1.MessageId = Message_hasTag_Tag.MessageId
  1057. LEFT JOIN Person_likes_Message like2
  1058. ON like2.MessageId = message1.MessageId
  1059. -- we don't need the Person itself as its ID is in the like
  1060. WHERE Tag.name = 'Bob_Geldof'
  1061. )
  1062. SELECT pl.person1id AS "person1.id",
  1063. sum(coalesce(ps.popularityScore, 0)) AS authorityScore
  1064. FROM poster_w_liker pl
  1065. LEFT JOIN PopularityScoreQ06 ps
  1066. ON ps.person2id = pl.person2id
  1067. GROUP BY pl.person1id
  1068. ORDER BY authorityScore DESC, pl.person1id ASC
  1069. LIMIT 100
  1070. ----
  1071. Explained Query:
  1072. Finish order_by=[#1{sum} desc nulls_first, #0{creatorpersonid} asc nulls_last] limit=100 output=[#0, #1]
  1073. With
  1074. cte l0 =
  1075. Project (#1{name}, #5{messageid}, #16{creatorpersonid}) // { arity: 3 }
  1076. Join on=(#0{id} = #6{tagid} AND #5{messageid} = #8{messageid}) type=delta // { arity: 20 }
  1077. implementation
  1078. %0:tag » %1:message_hastag_tag[#2{tagid}]KA » %2:message[#1{messageid}]KA
  1079. %1:message_hastag_tag » %0:tag[#0{id}]KA » %2:message[#1{messageid}]KA
  1080. %2:message » %1:message_hastag_tag[#1{messageid}]KA » %0:tag[#0{id}]KA
  1081. ArrangeBy keys=[[#0{id}]] // { arity: 4 }
  1082. ReadIndex on=tag tag_id=[delta join 1st input (full scan)] // { arity: 4 }
  1083. ArrangeBy keys=[[#1{messageid}], [#2{tagid}]] // { arity: 3 }
  1084. ReadIndex on=message_hastag_tag message_hastag_tag_messageid=[delta join lookup] message_hastag_tag_tagid=[delta join lookup] // { arity: 3 }
  1085. ArrangeBy keys=[[#1{messageid}]] // { arity: 13 }
  1086. ReadIndex on=message message_messageid=[delta join lookup] // { arity: 13 }
  1087. cte l1 =
  1088. Project (#0{name}..=#2{creatorpersonid}, #4{personid}) // { arity: 4 }
  1089. Join on=(#1{messageid} = #5{messageid}) type=differential // { arity: 6 }
  1090. implementation
  1091. %1:person_likes_message[#2{messageid}]KA » %0:l0[#1{messageid}]K
  1092. ArrangeBy keys=[[#1{messageid}]] // { arity: 3 }
  1093. Get l0 // { arity: 3 }
  1094. ArrangeBy keys=[[#2{messageid}]] // { arity: 3 }
  1095. ReadIndex on=person_likes_message person_likes_message_messageid=[differential join] // { arity: 3 }
  1096. cte l2 =
  1097. Project (#1{messageid}, #2{creatorpersonid}) // { arity: 2 }
  1098. Filter (#0{name} = "Bob_Geldof") // { arity: 3 }
  1099. Get l0 // { arity: 3 }
  1100. cte l3 =
  1101. Distinct project=[#0{creatorpersonid}, #1{personid}] // { arity: 2 }
  1102. Union // { arity: 2 }
  1103. Map (null) // { arity: 2 }
  1104. Union // { arity: 1 }
  1105. Negate // { arity: 1 }
  1106. Project (#1{creatorpersonid}) // { arity: 1 }
  1107. Join on=(#0{messageid} = #2{messageid}) type=differential // { arity: 3 }
  1108. implementation
  1109. %1[#0]UKA » %0:l2[#0{messageid}]Kef
  1110. ArrangeBy keys=[[#0{messageid}]] // { arity: 2 }
  1111. Get l2 // { arity: 2 }
  1112. ArrangeBy keys=[[#0{messageid}]] // { arity: 1 }
  1113. Distinct project=[#0{messageid}] // { arity: 1 }
  1114. Project (#1{messageid}) // { arity: 1 }
  1115. Get l1 // { arity: 4 }
  1116. Project (#1{creatorpersonid}) // { arity: 1 }
  1117. Get l2 // { arity: 2 }
  1118. Project (#2{creatorpersonid}, #3{personid}) // { arity: 2 }
  1119. Filter (#0{name} = "Bob_Geldof") // { arity: 4 }
  1120. Get l1 // { arity: 4 }
  1121. cte l4 =
  1122. Project (#0{creatorpersonid}, #3{popularityscore}) // { arity: 2 }
  1123. Join on=(#1{personid} = #2{person2id}) type=differential // { arity: 4 }
  1124. implementation
  1125. %1:popularityscoreq06[#0]UKA » %0:l3[#1{person2id}]K
  1126. ArrangeBy keys=[[#1{personid}]] // { arity: 2 }
  1127. Filter (#1{personid}) IS NOT NULL // { arity: 2 }
  1128. Get l3 // { arity: 2 }
  1129. ArrangeBy keys=[[#0{person2id}]] // { arity: 2 }
  1130. ReadIndex on=popularityscoreq06 popularityscoreq06_person2id=[differential join] // { arity: 2 }
  1131. Return // { arity: 2 }
  1132. Reduce group_by=[#0{creatorpersonid}] aggregates=[sum(coalesce(#1{popularityscore}, 0))] // { arity: 2 }
  1133. Union // { arity: 2 }
  1134. Map (null) // { arity: 2 }
  1135. Union // { arity: 1 }
  1136. Negate // { arity: 1 }
  1137. Project (#0{creatorpersonid}) // { arity: 1 }
  1138. Get l4 // { arity: 2 }
  1139. Project (#0{creatorpersonid}) // { arity: 1 }
  1140. Get l3 // { arity: 2 }
  1141. Get l4 // { arity: 2 }
  1142. Used Indexes:
  1143. - materialize.public.tag_id (delta join 1st input (full scan))
  1144. - materialize.public.message_hastag_tag_messageid (delta join lookup)
  1145. - materialize.public.message_hastag_tag_tagid (delta join lookup)
  1146. - materialize.public.person_likes_message_messageid (differential join)
  1147. - materialize.public.message_messageid (delta join lookup)
  1148. - materialize.public.popularityscoreq06_person2id (differential join)
  1149. Target cluster: quickstart
  1150. EOF
  1151. ######################################################################
  1152. # QUERY 07
  1153. ######################################################################
  1154. # \set tag '\'Slovenia\''
  1155. # TODO(mgree) predicate push down anomaly on Tag.name
  1156. query T multiline
  1157. EXPLAIN OPTIMIZED PLAN WITH(humanized expressions, arity, join implementations) AS VERBOSE TEXT FOR WITH MyMessage AS (
  1158. SELECT m.MessageId
  1159. FROM Message_hasTag_Tag m, Tag
  1160. WHERE Tag.name = 'Slovenia' and m.TagId = Tag.Id
  1161. )
  1162. SELECT RelatedTag.name AS "relatedTag.name"
  1163. , count(*) AS count
  1164. FROM MyMessage ParentMessage_HasTag_Tag
  1165. -- as an optimization, we don't need message here as it's ID is in ParentMessage_HasTag_Tag
  1166. -- so proceed to the comment directly
  1167. INNER JOIN Message Comment
  1168. ON ParentMessage_HasTag_Tag.MessageId = Comment.ParentMessageId
  1169. -- comment's tag
  1170. LEFT JOIN Message_hasTag_Tag ct
  1171. ON Comment.MessageId = ct.MessageId
  1172. INNER JOIN Tag RelatedTag
  1173. ON RelatedTag.id = ct.TagId
  1174. WHERE TRUE
  1175. -- comment doesn't have the given tag
  1176. AND Comment.MessageId NOT In (SELECT MessageId FROM MyMessage)
  1177. AND Comment.ParentMessageId IS NOT NULL
  1178. GROUP BY RelatedTag.Name
  1179. ORDER BY count DESC, RelatedTag.name
  1180. LIMIT 100
  1181. ----
  1182. Explained Query:
  1183. Finish order_by=[#1{count} desc nulls_first, #0{name} asc nulls_last] limit=100 output=[#0, #1]
  1184. With
  1185. cte l0 =
  1186. Project (#1{messageid}) // { arity: 1 }
  1187. Join on=(#2{tagid} = #3{id}) type=differential // { arity: 8 }
  1188. implementation
  1189. %1:tag[#0{id}]KAe » %0:message_hastag_tag[#2{tagid}]KAe
  1190. ArrangeBy keys=[[#2{tagid}]] // { arity: 3 }
  1191. ReadIndex on=message_hastag_tag message_hastag_tag_tagid=[differential join] // { arity: 3 }
  1192. ArrangeBy keys=[[#0{id}]] // { arity: 5 }
  1193. ReadIndex on=materialize.public.tag tag_name=[lookup value=("Slovenia")] // { arity: 5 }
  1194. cte l1 =
  1195. Project (#2{messageid}, #18{name}) // { arity: 2 }
  1196. Join on=(#0{messageid} = #13{parentmessageid} AND #2{messageid} = #15{messageid} AND #16{tagid} = #17{id}) type=delta // { arity: 21 }
  1197. implementation
  1198. %0:l0 » %1:message[#12{parentmessageid}]KA » %2:message_hastag_tag[#1{messageid}]KA » %3:tag[#0{id}]KA
  1199. %1:message » %2:message_hastag_tag[#1{messageid}]KA » %3:tag[#0{id}]KA » %0:l0[#0{messageid}]K
  1200. %2:message_hastag_tag » %1:message[#1{messageid}]KA » %3:tag[#0{id}]KA » %0:l0[#0{messageid}]K
  1201. %3:tag » %2:message_hastag_tag[#2{tagid}]KA » %1:message[#1{messageid}]KA » %0:l0[#0{messageid}]K
  1202. ArrangeBy keys=[[#0{messageid}]] // { arity: 1 }
  1203. Get l0 // { arity: 1 }
  1204. ArrangeBy keys=[[#1{messageid}], [#12{parentmessageid}]] // { arity: 13 }
  1205. ReadIndex on=message message_messageid=[delta join lookup] message_parentmessageid=[delta join lookup] // { arity: 13 }
  1206. ArrangeBy keys=[[#1{messageid}], [#2{tagid}]] // { arity: 3 }
  1207. ReadIndex on=message_hastag_tag message_hastag_tag_messageid=[delta join lookup] message_hastag_tag_tagid=[delta join lookup] // { arity: 3 }
  1208. ArrangeBy keys=[[#0{id}]] // { arity: 4 }
  1209. ReadIndex on=tag tag_id=[delta join lookup] // { arity: 4 }
  1210. cte l2 =
  1211. Distinct project=[#0{messageid}] // { arity: 1 }
  1212. Project (#0{messageid}) // { arity: 1 }
  1213. Get l1 // { arity: 2 }
  1214. Return // { arity: 2 }
  1215. Reduce group_by=[#0{name}] aggregates=[count(*)] // { arity: 2 }
  1216. Project (#1{name}) // { arity: 1 }
  1217. Join on=(#0{messageid} = #2{messageid}) type=differential // { arity: 3 }
  1218. implementation
  1219. %0:l1[#0]K » %1[#0]K
  1220. ArrangeBy keys=[[#0{messageid}]] // { arity: 2 }
  1221. Get l1 // { arity: 2 }
  1222. ArrangeBy keys=[[#0{messageid}]] // { arity: 1 }
  1223. Union // { arity: 1 }
  1224. Negate // { arity: 1 }
  1225. Project (#0{messageid}) // { arity: 1 }
  1226. Join on=(#0{messageid} = #1{messageid}) type=differential // { arity: 2 }
  1227. implementation
  1228. %0:l2[#0]UKA » %1[#0]UKA
  1229. ArrangeBy keys=[[#0{messageid}]] // { arity: 1 }
  1230. Get l2 // { arity: 1 }
  1231. ArrangeBy keys=[[#0{messageid}]] // { arity: 1 }
  1232. Distinct project=[#0{messageid}] // { arity: 1 }
  1233. Get l0 // { arity: 1 }
  1234. Get l2 // { arity: 1 }
  1235. Used Indexes:
  1236. - materialize.public.tag_id (delta join lookup)
  1237. - materialize.public.tag_name (lookup)
  1238. - materialize.public.message_hastag_tag_messageid (delta join lookup)
  1239. - materialize.public.message_hastag_tag_tagid (differential join, delta join lookup)
  1240. - materialize.public.message_messageid (delta join lookup)
  1241. - materialize.public.message_parentmessageid (delta join lookup)
  1242. Target cluster: quickstart
  1243. EOF
  1244. ######################################################################
  1245. # QUERY 08
  1246. ######################################################################
  1247. # \set tag '\'Abbas_I_of_Persia\''
  1248. # \set startDate '\'2010-06-14\''::timestamp
  1249. # \set endDate '\'2010-06-28\''::timestamp
  1250. query T multiline
  1251. EXPLAIN OPTIMIZED PLAN WITH(humanized expressions, arity, join implementations) AS VERBOSE TEXT FOR WITH Person_interested_in_Tag AS (
  1252. SELECT Person.id AS PersonId
  1253. FROM Person
  1254. JOIN Person_hasInterest_Tag
  1255. ON Person_hasInterest_Tag.PersonId = Person.id
  1256. JOIN Tag
  1257. ON Tag.id = Person_hasInterest_Tag.TagId
  1258. AND Tag.name = 'Abbas_I_of_Persia'
  1259. )
  1260. , Person_Message_score AS (
  1261. SELECT Person.id AS PersonId
  1262. , count(*) AS message_score
  1263. FROM Tag
  1264. JOIN Message_hasTag_Tag
  1265. ON Message_hasTag_Tag.TagId = Tag.id
  1266. JOIN Message
  1267. ON Message_hasTag_Tag.MessageId = Message.MessageId
  1268. AND '2010-06-14'::TIMESTAMP < Message.creationDate
  1269. JOIN Person
  1270. ON Person.id = Message.CreatorPersonId
  1271. WHERE Tag.name = 'Abbas_I_of_Persia'
  1272. AND Message.creationDate < '2010-06-28'::TIMESTAMP
  1273. GROUP BY Person.id
  1274. )
  1275. , Person_score AS (
  1276. SELECT coalesce(Person_interested_in_Tag.PersonId, pms.PersonId) AS PersonId
  1277. , CASE WHEN Person_interested_in_Tag.PersonId IS NULL then 0 ELSE 100 END -- scored from interest in the given tag
  1278. + coalesce(pms.message_score, 0) AS score
  1279. FROM Person_interested_in_Tag
  1280. FULL JOIN Person_Message_score pms
  1281. ON Person_interested_in_Tag.PersonId = pms.PersonId
  1282. )
  1283. SELECT p.PersonId AS "person.id"
  1284. , p.score AS score
  1285. , coalesce(sum(f.score), 0) AS friendsScore
  1286. FROM Person_score p
  1287. LEFT JOIN Person_knows_Person
  1288. ON Person_knows_Person.Person1Id = p.PersonId
  1289. LEFT JOIN Person_score f -- the friend
  1290. ON f.PersonId = Person_knows_Person.Person2Id
  1291. GROUP BY p.PersonId, p.score
  1292. ORDER BY p.score + coalesce(sum(f.score), 0) DESC, p.PersonId
  1293. LIMIT 100
  1294. ----
  1295. Explained Query:
  1296. Finish order_by=[#4 desc nulls_first, #0 asc nulls_last] limit=100 output=[#0, #1, #3]
  1297. With
  1298. cte l0 =
  1299. ArrangeBy keys=[[#1{id}]] // { arity: 11 }
  1300. ReadIndex on=person person_id=[delta join lookup, delta join 1st input (full scan)] // { arity: 11 }
  1301. cte l1 =
  1302. ArrangeBy keys=[[#0{id}]] // { arity: 5 }
  1303. ReadIndex on=materialize.public.tag tag_name=[lookup value=("Abbas_I_of_Persia")] // { arity: 5 }
  1304. cte l2 =
  1305. Project (#1{id}) // { arity: 1 }
  1306. Join on=(#1{id} = #11{personid} AND #12{tagid} = #13{id}) type=delta // { arity: 18 }
  1307. implementation
  1308. %0:l0 » %1:person_hasinterest_tag[#0{personid}]K » %2:l1[#0{id}]KAe
  1309. %1:person_hasinterest_tag » %2:l1[#0{id}]KAe » %0:l0[#1{id}]KA
  1310. %2:l1 » %1:person_hasinterest_tag[#1{tagid}]KA » %0:l0[#1{id}]KA
  1311. Get l0 // { arity: 11 }
  1312. ArrangeBy keys=[[#0{personid}], [#1{tagid}]] // { arity: 2 }
  1313. Project (#1{personid}, #2{tagid}) // { arity: 2 }
  1314. ReadIndex on=person_hasinterest_tag person_hasinterest_tag_tagid=[*** full scan ***] // { arity: 3 }
  1315. Get l1 // { arity: 5 }
  1316. cte l3 =
  1317. Reduce group_by=[#0{creatorpersonid}] aggregates=[count(*)] // { arity: 2 }
  1318. Project (#17{creatorpersonid}) // { arity: 1 }
  1319. Filter (#8{creationdate} < 2010-06-28 00:00:00 UTC) AND (2010-06-14 00:00:00 UTC < #8{creationdate}) // { arity: 32 }
  1320. Join on=(#0{id} = #7{tagid} AND #6{messageid} = #9{messageid} AND #17{creatorpersonid} = #22{id}) type=delta // { arity: 32 }
  1321. implementation
  1322. %0:l1 » %1:message_hastag_tag[#2{tagid}]KA » %2:message[#1{messageid}]KAiif » %3:l0[#1{id}]KA
  1323. %1:message_hastag_tag » %0:l1[#0{id}]KAe » %2:message[#1{messageid}]KAiif » %3:l0[#1{id}]KA
  1324. %2:message » %1:message_hastag_tag[#1{messageid}]KA » %0:l1[#0{id}]KAe » %3:l0[#1{id}]KA
  1325. %3:l0 » %2:message[#9{creatorpersonid}]KAiif » %1:message_hastag_tag[#1{messageid}]KA » %0:l1[#0{id}]KAe
  1326. Get l1 // { arity: 5 }
  1327. ArrangeBy keys=[[#1{messageid}], [#2{tagid}]] // { arity: 3 }
  1328. ReadIndex on=message_hastag_tag message_hastag_tag_messageid=[delta join lookup] message_hastag_tag_tagid=[delta join lookup] // { arity: 3 }
  1329. ArrangeBy keys=[[#1{messageid}], [#9{creatorpersonid}]] // { arity: 13 }
  1330. ReadIndex on=message message_messageid=[delta join lookup] message_creatorpersonid=[delta join lookup] // { arity: 13 }
  1331. Get l0 // { arity: 11 }
  1332. cte l4 =
  1333. ArrangeBy keys=[[#0{creatorpersonid}]] // { arity: 2 }
  1334. Get l3 // { arity: 2 }
  1335. cte l5 =
  1336. Project (#0{id}, #2{count}) // { arity: 2 }
  1337. Join on=(#0{id} = #1{creatorpersonid}) type=differential // { arity: 3 }
  1338. implementation
  1339. %1:l4[#0{personid}]UKA » %0:l2[#0{personid}]K
  1340. ArrangeBy keys=[[#0{id}]] // { arity: 1 }
  1341. Get l2 // { arity: 1 }
  1342. Get l4 // { arity: 2 }
  1343. cte l6 =
  1344. Project (#0{id}) // { arity: 1 }
  1345. Get l5 // { arity: 2 }
  1346. cte l7 =
  1347. Project (#3, #4) // { arity: 2 }
  1348. Map (coalesce(#0{id}, #1{creatorpersonid}), (integer_to_bigint(case when (#0{id}) IS NULL then 0 else 100 end) + coalesce(#2{count}, 0))) // { arity: 5 }
  1349. Union // { arity: 3 }
  1350. Project (#2, #0{creatorpersonid}, #1{count}) // { arity: 3 }
  1351. Map (null) // { arity: 3 }
  1352. Union // { arity: 2 }
  1353. Negate // { arity: 2 }
  1354. Project (#0{creatorpersonid}, #1{count}) // { arity: 2 }
  1355. Join on=(#0{creatorpersonid} = #2{id}) type=differential // { arity: 3 }
  1356. implementation
  1357. %0:l4[#0{personid}]UKA » %1[#0]UKA
  1358. Get l4 // { arity: 2 }
  1359. ArrangeBy keys=[[#0{id}]] // { arity: 1 }
  1360. Distinct project=[#0{id}] // { arity: 1 }
  1361. Get l6 // { arity: 1 }
  1362. Get l3 // { arity: 2 }
  1363. Map (null, null) // { arity: 3 }
  1364. Union // { arity: 1 }
  1365. Negate // { arity: 1 }
  1366. Get l6 // { arity: 1 }
  1367. Get l2 // { arity: 1 }
  1368. Project (#0{id}, #0{id}, #1{count}) // { arity: 3 }
  1369. Get l5 // { arity: 2 }
  1370. Return // { arity: 5 }
  1371. Map (coalesce(#2{sum}, 0), (bigint_to_numeric(#1{score}) + #3{"?column?"})) // { arity: 5 }
  1372. Reduce group_by=[#0, #1] aggregates=[sum(case when (#3) IS NULL then null else #2 end)] // { arity: 3 }
  1373. Project (#0, #1, #6, #7) // { arity: 4 }
  1374. Join on=(#0 = #2{person1id} AND #5{person2id} = case when (#4) IS NULL then null else #3{person2id} end) type=delta // { arity: 8 }
  1375. implementation
  1376. %0:l7 » %1[#0]K » %2[#0]K
  1377. %1 » %0:l7[#0]K » %2[#0]K
  1378. %2 » %1[case when (#2) IS NULL then null else #1 end]K » %0:l7[#0]K
  1379. ArrangeBy keys=[[#0]] // { arity: 2 }
  1380. Get l7 // { arity: 2 }
  1381. ArrangeBy keys=[[#0{person1id}], [case when (#2) IS NULL then null else #1{person2id} end]] // { arity: 3 }
  1382. Union // { arity: 3 }
  1383. Project (#1{person1id}..=#3) // { arity: 3 }
  1384. Map (true) // { arity: 4 }
  1385. ReadIndex on=person_knows_person person_knows_person_person1id=[*** full scan ***] // { arity: 3 }
  1386. Map (null, null) // { arity: 3 }
  1387. Threshold // { arity: 1 }
  1388. Union // { arity: 1 }
  1389. Negate // { arity: 1 }
  1390. Project (#1{person1id}) // { arity: 1 }
  1391. ReadIndex on=person_knows_person person_knows_person_person1id=[*** full scan ***] // { arity: 3 }
  1392. Distinct project=[#0] // { arity: 1 }
  1393. Union // { arity: 1 }
  1394. Project (#0) // { arity: 1 }
  1395. Get l7 // { arity: 2 }
  1396. Constant // { arity: 1 }
  1397. - (null)
  1398. ArrangeBy keys=[[#0{person2id}]] // { arity: 3 }
  1399. Union // { arity: 3 }
  1400. Filter (#0) IS NOT NULL // { arity: 3 }
  1401. Map (true) // { arity: 3 }
  1402. Get l7 // { arity: 2 }
  1403. Map (null, null) // { arity: 3 }
  1404. Threshold // { arity: 1 }
  1405. Union // { arity: 1 }
  1406. Negate // { arity: 1 }
  1407. Project (#0) // { arity: 1 }
  1408. Filter (#0) IS NOT NULL // { arity: 2 }
  1409. Get l7 // { arity: 2 }
  1410. Distinct project=[#0{person2id}] // { arity: 1 }
  1411. Union // { arity: 1 }
  1412. Project (#2{person2id}) // { arity: 1 }
  1413. ReadIndex on=person_knows_person person_knows_person_person1id=[*** full scan ***] // { arity: 3 }
  1414. Constant // { arity: 1 }
  1415. - (null)
  1416. Used Indexes:
  1417. - materialize.public.tag_name (lookup)
  1418. - materialize.public.person_id (delta join lookup, delta join 1st input (full scan))
  1419. - materialize.public.person_hasinterest_tag_tagid (*** full scan ***)
  1420. - materialize.public.person_knows_person_person1id (*** full scan ***)
  1421. - materialize.public.message_hastag_tag_messageid (delta join lookup)
  1422. - materialize.public.message_hastag_tag_tagid (delta join lookup)
  1423. - materialize.public.message_messageid (delta join lookup)
  1424. - materialize.public.message_creatorpersonid (delta join lookup)
  1425. Target cluster: quickstart
  1426. EOF
  1427. ######################################################################
  1428. # QUERY 09
  1429. ######################################################################
  1430. # \set startDate '\'2012-08-29\''::timestamp
  1431. # \set endDate '\'2012-11-24\''::timestamp
  1432. query T multiline
  1433. EXPLAIN OPTIMIZED PLAN WITH(humanized expressions, arity, join implementations) AS VERBOSE TEXT FOR WITH
  1434. MPP AS (SELECT RootPostId, count(*) as MessageCount FROM Message WHERE Message.creationDate BETWEEN '2012-08-29'::TIMESTAMP AND '2012-11-24'::TIMESTAMP GROUP BY RootPostId)
  1435. SELECT Person.id AS "person.id"
  1436. , Person.firstName AS "person.firstName"
  1437. , Person.lastName AS "person.lastName"
  1438. , count(Post.id) AS threadCount
  1439. , sum(MPP.MessageCount) AS messageCount
  1440. FROM Person
  1441. JOIN Post_View Post
  1442. ON Person.id = Post.CreatorPersonId
  1443. JOIN MPP
  1444. ON Post.id = MPP.RootPostId
  1445. WHERE Post.creationDate BETWEEN '2012-08-29'::TIMESTAMP AND '2012-11-24'::TIMESTAMP
  1446. GROUP BY Person.id, Person.firstName, Person.lastName
  1447. ORDER BY messageCount DESC, Person.id
  1448. LIMIT 100
  1449. ----
  1450. Explained Query:
  1451. Finish order_by=[#4{sum_count} desc nulls_first, #0{id} asc nulls_last] limit=100 output=[#0..=#4]
  1452. Reduce group_by=[#0{id}..=#2{lastname}] aggregates=[count(*), sum(#3{count})] // { arity: 5 }
  1453. Project (#1{id}..=#3{lastname}, #25{count}) // { arity: 4 }
  1454. Filter (#23{parentmessageid}) IS NULL AND (#11{creationdate} <= 2012-11-24 00:00:00 UTC) AND (#11{creationdate} >= 2012-08-29 00:00:00 UTC) // { arity: 26 }
  1455. Join on=(#1{id} = #20{creatorpersonid} AND #12{messageid} = #24{rootpostid}) type=delta // { arity: 26 }
  1456. implementation
  1457. %0:person » %1:message[#9{creatorpersonid}]KAniif » %2[#0]UKA
  1458. %1:message » %2[#0]UKA » %0:person[#1{id}]KA
  1459. %2 » %1:message[#1{messageid}]KAniif » %0:person[#1{id}]KA
  1460. ArrangeBy keys=[[#1{id}]] // { arity: 11 }
  1461. ReadIndex on=person person_id=[delta join 1st input (full scan)] // { arity: 11 }
  1462. ArrangeBy keys=[[#1{messageid}], [#9{creatorpersonid}]] // { arity: 13 }
  1463. ReadIndex on=message message_messageid=[delta join lookup] message_creatorpersonid=[delta join lookup] // { arity: 13 }
  1464. ArrangeBy keys=[[#0{rootpostid}]] // { arity: 2 }
  1465. Reduce group_by=[#0{rootpostid}] aggregates=[count(*)] // { arity: 2 }
  1466. Project (#2{rootpostid}) // { arity: 1 }
  1467. Filter (#0{creationdate} <= 2012-11-24 00:00:00 UTC) AND (#0{creationdate} >= 2012-08-29 00:00:00 UTC) // { arity: 13 }
  1468. ReadIndex on=message message_messageid=[*** full scan ***] // { arity: 13 }
  1469. Used Indexes:
  1470. - materialize.public.person_id (delta join 1st input (full scan))
  1471. - materialize.public.message_messageid (*** full scan ***, delta join lookup)
  1472. - materialize.public.message_creatorpersonid (delta join lookup)
  1473. Target cluster: quickstart
  1474. EOF
  1475. ######################################################################
  1476. # QUERY 10
  1477. ######################################################################
  1478. # \set personId 6597069770479
  1479. # \set country '\'Italy\''
  1480. # \set tagClass '\'Thing\''
  1481. # \set minPathDistance 3 -- fixed value
  1482. # \set maxPathDistance 4 -- fixed value
  1483. query T multiline
  1484. EXPLAIN OPTIMIZED PLAN WITH(humanized expressions, arity, join implementations) AS VERBOSE TEXT FOR WITH friends AS
  1485. (SELECT Person2Id
  1486. FROM Person_knows_Person
  1487. WHERE Person1Id = 6597069770479
  1488. )
  1489. , friends_of_friends AS
  1490. (SELECT knowsB.Person2Id AS Person2Id
  1491. FROM friends
  1492. JOIN Person_knows_Person knowsB
  1493. ON friends.Person2Id = knowsB.Person1Id
  1494. )
  1495. , friends_and_friends_of_friends AS
  1496. (SELECT Person2Id
  1497. FROM friends
  1498. UNION -- using plain UNION to eliminate duplicates
  1499. SELECT Person2Id
  1500. FROM friends_of_friends
  1501. )
  1502. , friends_between_3_and_4_hops AS (
  1503. -- people reachable through 1..4 hops
  1504. (SELECT DISTINCT knowsD.Person2Id AS Person2Id
  1505. FROM friends_and_friends_of_friends ffoaf
  1506. JOIN Person_knows_Person knowsC
  1507. ON knowsC.Person1Id = ffoaf.Person2Id
  1508. JOIN Person_knows_Person knowsD
  1509. ON knowsD.Person1Id = knowsC.Person2Id
  1510. )
  1511. -- removing people reachable through 1..2 hops, yielding the ones reachable through 3..4 hops
  1512. EXCEPT
  1513. (SELECT Person2Id
  1514. FROM friends_and_friends_of_friends
  1515. )
  1516. )
  1517. , friend_list AS (
  1518. SELECT f.person2Id AS friendId
  1519. FROM friends_between_3_and_4_hops f
  1520. JOIN Person tf -- the friend's person record
  1521. ON tf.id = f.person2Id
  1522. JOIN City
  1523. ON City.id = tf.LocationCityId
  1524. JOIN Country
  1525. ON Country.id = City.PartOfCountryId
  1526. AND Country.name = 'Italy'
  1527. )
  1528. , messages_of_tagclass_by_friends AS (
  1529. SELECT DISTINCT f.friendId
  1530. , Message.MessageId AS messageid
  1531. FROM friend_list f
  1532. JOIN Message
  1533. ON Message.CreatorPersonId = f.friendId
  1534. JOIN Message_hasTag_Tag
  1535. ON Message_hasTag_Tag.MessageId = Message.MessageId
  1536. JOIN Tag
  1537. ON Tag.id = Message_hasTag_Tag.TagId
  1538. JOIN TagClass
  1539. ON TagClass.id = Tag.TypeTagClassId
  1540. WHERE TagClass.name = 'Thing'
  1541. )
  1542. SELECT m.friendId AS "person.id"
  1543. , Tag.name AS "tag.name"
  1544. , count(*) AS messageCount
  1545. FROM messages_of_tagclass_by_friends m
  1546. JOIN Message_hasTag_Tag
  1547. ON Message_hasTag_Tag.MessageId = m.MessageId
  1548. JOIN Tag
  1549. ON Tag.id = Message_hasTag_Tag.TagId
  1550. GROUP BY m.friendId, Tag.name
  1551. ORDER BY messageCount DESC, Tag.name, m.friendId
  1552. LIMIT 100
  1553. ----
  1554. Explained Query:
  1555. Finish order_by=[#2{count} desc nulls_first, #1{name} asc nulls_last, #0{person2id} asc nulls_last] limit=100 output=[#0..=#2]
  1556. With
  1557. cte l0 =
  1558. ArrangeBy keys=[[#1{person1id}]] // { arity: 3 }
  1559. ReadIndex on=person_knows_person person_knows_person_person1id=[differential join, delta join lookup, lookup] // { arity: 3 }
  1560. cte l1 =
  1561. ReadIndex on=materialize.public.person_knows_person person_knows_person_person1id=[lookup value=(6597069770479)] // { arity: 4 }
  1562. cte l2 =
  1563. Distinct project=[#0{person2id}] // { arity: 1 }
  1564. Union // { arity: 1 }
  1565. Project (#2{person2id}) // { arity: 1 }
  1566. Get l1 // { arity: 4 }
  1567. Project (#6{person2id}) // { arity: 1 }
  1568. Join on=(#2{person2id} = #5{person1id}) type=differential // { arity: 7 }
  1569. implementation
  1570. %0:l1[#2{person2id}]KAe » %1:l0[#1{person1id}]KAe
  1571. ArrangeBy keys=[[#2{person2id}]] // { arity: 4 }
  1572. Get l1 // { arity: 4 }
  1573. Get l0 // { arity: 3 }
  1574. Return // { arity: 3 }
  1575. Reduce group_by=[#0{person2id}, #1{name}] aggregates=[count(*)] // { arity: 3 }
  1576. Project (#0{person2id}, #9{name}) // { arity: 2 }
  1577. Join on=(#0{person2id} = #1{id} = #2{creatorpersonid} AND #3{messageid} = #4{messageid} = #6{messageid} AND #7{tagid} = #8{id}) type=delta // { arity: 12 }
  1578. implementation
  1579. %0 » %1[#0]UKA » %2[#0]K » %3[#0]UKA » %4:message_hastag_tag[#1{messageid}]KA » %5:tag[#0{id}]KA
  1580. %1 » %0[#0]UKA » %2[#0]K » %3[#0]UKA » %4:message_hastag_tag[#1{messageid}]KA » %5:tag[#0{id}]KA
  1581. %2 » %0[#0]UKA » %1[#0]UKA » %3[#0]UKA » %4:message_hastag_tag[#1{messageid}]KA » %5:tag[#0{id}]KA
  1582. %3 » %4:message_hastag_tag[#1{messageid}]KA » %5:tag[#0{id}]KA » %2[#1]K » %0[#0]UKA » %1[#0]UKA
  1583. %4:message_hastag_tag » %3[#0]UKA » %5:tag[#0{id}]KA » %2[#1]K » %0[#0]UKA » %1[#0]UKA
  1584. %5:tag » %4:message_hastag_tag[#2{tagid}]KA » %3[#0]UKA » %2[#1]K » %0[#0]UKA » %1[#0]UKA
  1585. ArrangeBy keys=[[#0{person2id}]] // { arity: 1 }
  1586. Distinct project=[#0{person2id}] // { arity: 1 }
  1587. Threshold // { arity: 1 }
  1588. Union // { arity: 1 }
  1589. Distinct project=[#0{person2id}] // { arity: 1 }
  1590. Project (#6{person2id}) // { arity: 1 }
  1591. Join on=(#0{person2id} = #2{person1id} AND #3{person2id} = #5{person1id}) type=delta // { arity: 7 }
  1592. implementation
  1593. %0:l2 » %1:person_knows_person[#1{person1id}]KA » %2:l0[#1{person1id}]KA
  1594. %1:person_knows_person » %0:l2[#0]UKA » %2:l0[#1{person1id}]KA
  1595. %2:l0 » %1:person_knows_person[#2{person2id}]KA » %0:l2[#0]UKA
  1596. ArrangeBy keys=[[#0{person2id}]] // { arity: 1 }
  1597. Get l2 // { arity: 1 }
  1598. ArrangeBy keys=[[#1{person1id}], [#2{person2id}]] // { arity: 3 }
  1599. ReadIndex on=person_knows_person person_knows_person_person1id=[delta join lookup] person_knows_person_person2id=[delta join lookup] // { arity: 3 }
  1600. Get l0 // { arity: 3 }
  1601. Negate // { arity: 1 }
  1602. Get l2 // { arity: 1 }
  1603. ArrangeBy keys=[[#0{id}]] // { arity: 1 }
  1604. Distinct project=[#0{id}] // { arity: 1 }
  1605. Project (#1{id}) // { arity: 1 }
  1606. Filter (#16{name} = "Italy") AND (#1{id}) IS NOT NULL AND (#14{partofcountryid}) IS NOT NULL // { arity: 19 }
  1607. Join on=(#8{locationcityid} = #11{id} AND #14{partofcountryid} = #15{id}) type=delta // { arity: 19 }
  1608. implementation
  1609. %0:person » %1:city[#0{id}]KA » %2:country[#0{id}]KAef
  1610. %1:city » %2:country[#0{id}]KAef » %0:person[#8{locationcityid}]KA
  1611. %2:country » %1:city[#3{partofcountryid}]KA » %0:person[#8{locationcityid}]KA
  1612. ArrangeBy keys=[[#8{locationcityid}]] // { arity: 11 }
  1613. ReadIndex on=person person_locationcityid=[delta join 1st input (full scan)] // { arity: 11 }
  1614. ArrangeBy keys=[[#0{id}], [#3{partofcountryid}]] // { arity: 4 }
  1615. ReadIndex on=city city_id=[delta join lookup] city_partofcountryid=[delta join lookup] // { arity: 4 }
  1616. ArrangeBy keys=[[#0{id}]] // { arity: 4 }
  1617. ReadIndex on=country country_id=[delta join lookup] // { arity: 4 }
  1618. ArrangeBy keys=[[#0{creatorpersonid}], [#1{messageid}]] // { arity: 2 }
  1619. Distinct project=[#1{creatorpersonid}, #0{messageid}] // { arity: 2 }
  1620. Project (#1{messageid}, #9{creatorpersonid}) // { arity: 2 }
  1621. ReadIndex on=message message_messageid=[*** full scan ***] // { arity: 13 }
  1622. ArrangeBy keys=[[#0{messageid}]] // { arity: 1 }
  1623. Distinct project=[#0{messageid}] // { arity: 1 }
  1624. Project (#1{messageid}) // { arity: 1 }
  1625. Join on=(#2{tagid} = #3{id} AND #6{typetagclassid} = #7{id}) type=delta // { arity: 12 }
  1626. implementation
  1627. %0:message_hastag_tag » %1:tag[#0{id}]KA » %2:tagclass[#0{id}]KAe
  1628. %1:tag » %2:tagclass[#0{id}]KAe » %0:message_hastag_tag[#2{tagid}]KA
  1629. %2:tagclass » %1:tag[#3{typetagclassid}]KA » %0:message_hastag_tag[#2{tagid}]KA
  1630. ArrangeBy keys=[[#2{tagid}]] // { arity: 3 }
  1631. ReadIndex on=message_hastag_tag message_hastag_tag_tagid=[delta join 1st input (full scan)] // { arity: 3 }
  1632. ArrangeBy keys=[[#0{id}], [#3{typetagclassid}]] // { arity: 4 }
  1633. ReadIndex on=tag tag_id=[delta join lookup] tag_typetagclassid=[delta join lookup] // { arity: 4 }
  1634. ArrangeBy keys=[[#0{id}]] // { arity: 5 }
  1635. ReadIndex on=materialize.public.tagclass tagclass_name=[lookup value=("Thing")] // { arity: 5 }
  1636. ArrangeBy keys=[[#1{messageid}], [#2{tagid}]] // { arity: 3 }
  1637. ReadIndex on=message_hastag_tag message_hastag_tag_messageid=[delta join lookup] message_hastag_tag_tagid=[delta join lookup] // { arity: 3 }
  1638. ArrangeBy keys=[[#0{id}]] // { arity: 4 }
  1639. ReadIndex on=tag tag_id=[delta join lookup] // { arity: 4 }
  1640. Used Indexes:
  1641. - materialize.public.tag_id (delta join lookup)
  1642. - materialize.public.tag_typetagclassid (delta join lookup)
  1643. - materialize.public.tagclass_name (lookup)
  1644. - materialize.public.person_locationcityid (delta join 1st input (full scan))
  1645. - materialize.public.person_knows_person_person1id (differential join, delta join lookup, lookup)
  1646. - materialize.public.person_knows_person_person2id (delta join lookup)
  1647. - materialize.public.country_id (delta join lookup)
  1648. - materialize.public.city_id (delta join lookup)
  1649. - materialize.public.city_partofcountryid (delta join lookup)
  1650. - materialize.public.message_hastag_tag_messageid (delta join lookup)
  1651. - materialize.public.message_hastag_tag_tagid (delta join lookup, delta join 1st input (full scan))
  1652. - materialize.public.message_messageid (*** full scan ***)
  1653. Target cluster: quickstart
  1654. EOF
  1655. ######################################################################
  1656. # QUERY 11
  1657. ######################################################################
  1658. # \set country '\'India\''
  1659. # \set startDate '\'2012-09-28\''::timestamp
  1660. # \set endDate '\'2013-01-10\''::timestamp
  1661. query T multiline
  1662. EXPLAIN OPTIMIZED PLAN WITH(humanized expressions, arity, join implementations) AS VERBOSE TEXT FOR WITH Persons_of_country_w_friends AS (
  1663. SELECT Person.id AS PersonId
  1664. , Person_knows_Person.Person2Id AS FriendId
  1665. , Person_knows_Person.creationDate AS creationDate
  1666. FROM Person
  1667. JOIN City
  1668. ON City.id = Person.LocationCityId
  1669. JOIN Country
  1670. ON Country.id = City.PartOfCountryId
  1671. AND Country.name = 'India'
  1672. JOIN Person_knows_Person
  1673. ON Person_knows_Person.Person1Id = Person.id
  1674. )
  1675. SELECT count(*)
  1676. FROM Persons_of_country_w_friends p1
  1677. JOIN Persons_of_country_w_friends p2
  1678. ON p1.FriendId = p2.PersonId
  1679. JOIN Persons_of_country_w_friends p3
  1680. ON p2.FriendId = p3.PersonId
  1681. AND p3.FriendId = p1.PersonId
  1682. WHERE true
  1683. -- filter: unique triangles only
  1684. AND p1.PersonId < p2.PersonId
  1685. AND p2.PersonId < p3.PersonId
  1686. -- filter: only edges created after :startDate
  1687. AND '2012-09-28'::TIMESTAMP <= p1.creationDate AND p1.creationDate <= '2013-01-10'::TIMESTAMP
  1688. AND '2012-09-28'::TIMESTAMP <= p2.creationDate AND p2.creationDate <= '2013-01-10'::TIMESTAMP
  1689. AND '2012-09-28'::TIMESTAMP <= p3.creationDate AND p3.creationDate <= '2013-01-10'::TIMESTAMP
  1690. ----
  1691. Explained Query:
  1692. With
  1693. cte l0 =
  1694. Project (#1{id}, #21{person2id}) // { arity: 2 }
  1695. Filter (#16{name} = "India") AND (#19{creationdate} <= 2013-01-10 00:00:00 UTC) AND (2012-09-28 00:00:00 UTC <= #19{creationdate}) AND (#14{partofcountryid}) IS NOT NULL // { arity: 22 }
  1696. Join on=(#1{id} = #20{person1id} AND #8{locationcityid} = #11{id} AND #14{partofcountryid} = #15{id}) type=delta // { arity: 22 }
  1697. implementation
  1698. %0:person » %3:person_knows_person[#1{person1id}]KAiif » %1:city[#0{id}]KA » %2:country[#0{id}]KAef
  1699. %1:city » %2:country[#0{id}]KAef » %0:person[#8{locationcityid}]KA » %3:person_knows_person[#1{person1id}]KAiif
  1700. %2:country » %1:city[#3{partofcountryid}]KA » %0:person[#8{locationcityid}]KA » %3:person_knows_person[#1{person1id}]KAiif
  1701. %3:person_knows_person » %0:person[#1{id}]KA » %1:city[#0{id}]KA » %2:country[#0{id}]KAef
  1702. ArrangeBy keys=[[#1{id}], [#8{locationcityid}]] // { arity: 11 }
  1703. ReadIndex on=person person_id=[delta join 1st input (full scan)] person_locationcityid=[delta join lookup] // { arity: 11 }
  1704. ArrangeBy keys=[[#0{id}], [#3{partofcountryid}]] // { arity: 4 }
  1705. ReadIndex on=city city_id=[delta join lookup] city_partofcountryid=[delta join lookup] // { arity: 4 }
  1706. ArrangeBy keys=[[#0{id}]] // { arity: 4 }
  1707. ReadIndex on=country country_id=[delta join lookup] // { arity: 4 }
  1708. ArrangeBy keys=[[#1{person1id}]] // { arity: 3 }
  1709. ReadIndex on=person_knows_person person_knows_person_person1id=[delta join lookup] // { arity: 3 }
  1710. cte l1 =
  1711. Filter (#0{id} < #1{person2id}) // { arity: 2 }
  1712. Get l0 // { arity: 2 }
  1713. cte l2 =
  1714. Reduce aggregates=[count(*)] // { arity: 1 }
  1715. Project () // { arity: 0 }
  1716. Join on=(#0{id} = #5{person2id} AND #1{person2id} = #2{id} AND #3{person2id} = #4{id}) type=differential // { arity: 6 }
  1717. implementation
  1718. %0:l1[#1{friendid}]Kf » %1:l1[#0{personid}]Kf » %2:l0[#0{personid}, #1{friendid}]KKf
  1719. ArrangeBy keys=[[#1{person2id}]] // { arity: 2 }
  1720. Get l1 // { arity: 2 }
  1721. ArrangeBy keys=[[#0{id}]] // { arity: 2 }
  1722. Get l1 // { arity: 2 }
  1723. ArrangeBy keys=[[#0{id}, #1{person2id}]] // { arity: 2 }
  1724. Get l0 // { arity: 2 }
  1725. Return // { arity: 1 }
  1726. Union // { arity: 1 }
  1727. Get l2 // { arity: 1 }
  1728. Map (0) // { arity: 1 }
  1729. Union // { arity: 0 }
  1730. Negate // { arity: 0 }
  1731. Project () // { arity: 0 }
  1732. Get l2 // { arity: 1 }
  1733. Constant // { arity: 0 }
  1734. - ()
  1735. Used Indexes:
  1736. - materialize.public.person_id (delta join 1st input (full scan))
  1737. - materialize.public.person_locationcityid (delta join lookup)
  1738. - materialize.public.person_knows_person_person1id (delta join lookup)
  1739. - materialize.public.country_id (delta join lookup)
  1740. - materialize.public.city_id (delta join lookup)
  1741. - materialize.public.city_partofcountryid (delta join lookup)
  1742. Target cluster: quickstart
  1743. EOF
  1744. ######################################################################
  1745. # QUERY 12
  1746. ######################################################################
  1747. # \set startDate '\'2012-06-03\''::timestamp
  1748. # \set lengthThreshold '120'
  1749. # \set languages '\'{es, ta, pt}\''::varchar[]
  1750. query T multiline
  1751. EXPLAIN OPTIMIZED PLAN WITH(humanized expressions, arity, join implementations) AS VERBOSE TEXT FOR WITH
  1752. matching_message AS (
  1753. SELECT MessageId,
  1754. CreatorPersonId
  1755. FROM Message
  1756. WHERE Message.content IS NOT NULL
  1757. AND Message.length < 120
  1758. AND Message.creationDate > '2012-06-03'::TIMESTAMP
  1759. AND Message.RootPostLanguage IN ('es', 'ta', 'pt') -- MZ change to use postgres containment check
  1760. ),
  1761. person_w_posts AS (
  1762. SELECT Person.id, count(matching_message.MessageId) as messageCount
  1763. FROM Person
  1764. LEFT JOIN matching_message
  1765. ON Person.id = matching_message.CreatorPersonId
  1766. GROUP BY Person.id
  1767. ),
  1768. message_count_distribution AS (
  1769. SELECT pp.messageCount, count(*) as personCount
  1770. FROM person_w_posts pp
  1771. GROUP BY pp.messageCount
  1772. ORDER BY personCount DESC, messageCount DESC
  1773. )
  1774. SELECT *
  1775. FROM message_count_distribution
  1776. ORDER BY personCount DESC, messageCount DESC
  1777. ----
  1778. Explained Query:
  1779. Finish order_by=[#1{count} desc nulls_first, #0{count_messageid} desc nulls_first] output=[#0, #1]
  1780. With
  1781. cte l0 =
  1782. ArrangeBy keys=[[#1{id}]] // { arity: 11 }
  1783. ReadIndex on=person person_id=[differential join] // { arity: 11 }
  1784. cte l1 =
  1785. Project (#1{id}, #12{messageid}) // { arity: 2 }
  1786. Filter (#19{length} < 120) AND (#11{creationdate} > 2012-06-03 00:00:00 UTC) AND (#15{content}) IS NOT NULL // { arity: 25 }
  1787. Join on=(#1{id} = #20{creatorpersonid}) type=differential // { arity: 25 }
  1788. implementation
  1789. %1:message[#9{creatorpersonid}]KAeiif » %0:l0[#1{id}]KAeiif
  1790. Get l0 // { arity: 11 }
  1791. ArrangeBy keys=[[#9{creatorpersonid}]] // { arity: 14 }
  1792. ReadIndex on=materialize.public.message message_rootpostlanguage=[lookup values=[("es"); ("pt"); ("ta")]] // { arity: 14 }
  1793. Return // { arity: 2 }
  1794. Reduce group_by=[#0{count_messageid}] aggregates=[count(*)] // { arity: 2 }
  1795. Project (#1{count_messageid}) // { arity: 1 }
  1796. Reduce group_by=[#0{id}] aggregates=[count(#1{messageid})] // { arity: 2 }
  1797. Union // { arity: 2 }
  1798. Map (null) // { arity: 2 }
  1799. Union // { arity: 1 }
  1800. Negate // { arity: 1 }
  1801. Project (#1{id}) // { arity: 1 }
  1802. Join on=(#1{id} = #11{id}) type=differential // { arity: 12 }
  1803. implementation
  1804. %1[#0]UKA » %0:l0[#1{id}]KA
  1805. Get l0 // { arity: 11 }
  1806. ArrangeBy keys=[[#0{id}]] // { arity: 1 }
  1807. Distinct project=[#0{id}] // { arity: 1 }
  1808. Project (#0{id}) // { arity: 1 }
  1809. Get l1 // { arity: 2 }
  1810. Project (#1{id}) // { arity: 1 }
  1811. ReadIndex on=person person_id=[*** full scan ***] // { arity: 11 }
  1812. Get l1 // { arity: 2 }
  1813. Used Indexes:
  1814. - materialize.public.person_id (*** full scan ***, differential join)
  1815. - materialize.public.message_rootpostlanguage (lookup)
  1816. Target cluster: quickstart
  1817. EOF
  1818. # original version
  1819. query T multiline
  1820. EXPLAIN OPTIMIZED PLAN WITH(humanized expressions, arity, join implementations) AS VERBOSE TEXT FOR WITH person_w_posts AS (
  1821. SELECT Person.id, count(Message.MessageId) as messageCount
  1822. FROM Person
  1823. LEFT JOIN Message
  1824. ON Person.id = Message.CreatorPersonId
  1825. AND Message.content IS NOT NULL
  1826. AND Message.length < 120
  1827. AND Message.creationDate > '2012-06-03'::TIMESTAMP
  1828. AND Message.RootPostLanguage = ANY ('{es, ta, pt}'::varchar[]) -- MZ change to use postgres containment check
  1829. GROUP BY Person.id
  1830. )
  1831. , message_count_distribution AS (
  1832. SELECT pp.messageCount, count(*) as personCount
  1833. FROM person_w_posts pp
  1834. GROUP BY pp.messageCount
  1835. ORDER BY personCount DESC, messageCount DESC
  1836. )
  1837. SELECT *
  1838. FROM message_count_distribution
  1839. ORDER BY personCount DESC, messageCount DESC
  1840. ----
  1841. Explained Query:
  1842. Finish order_by=[#1{count} desc nulls_first, #0{count_messageid} desc nulls_first] output=[#0, #1]
  1843. With
  1844. cte l0 =
  1845. CrossJoin type=differential // { arity: 17 }
  1846. implementation
  1847. %0:person[×] » %1:message[×]
  1848. ArrangeBy keys=[[]] // { arity: 11 }
  1849. ReadIndex on=person person_id=[*** full scan ***] // { arity: 11 }
  1850. ArrangeBy keys=[[]] // { arity: 6 }
  1851. Project (#0{creationdate}, #1{messageid}, #3{rootpostlanguage}, #4{content}, #8{length}, #9{creatorpersonid}) // { arity: 6 }
  1852. ReadIndex on=message message_messageid=[*** full scan ***] // { arity: 13 }
  1853. cte l1 =
  1854. Project (#0{creationdate}..=#11{messageid}) // { arity: 12 }
  1855. Join on=(#12{rootpostlanguage} = #13{rootpostlanguage}) type=differential // { arity: 14 }
  1856. implementation
  1857. %1[#0]UKA » %0:l0[#12]Kiif
  1858. ArrangeBy keys=[[#12{rootpostlanguage}]] // { arity: 13 }
  1859. Project (#0{creationdate}..=#10{email}, #12{messageid}, #13{rootpostlanguage}) // { arity: 13 }
  1860. Filter (#15{length} < 120) AND (#11{creationdate} > 2012-06-03 00:00:00 UTC) AND (#14{content}) IS NOT NULL AND (#1{id} = #16{creatorpersonid}) // { arity: 17 }
  1861. Get l0 // { arity: 17 }
  1862. ArrangeBy keys=[[#0{rootpostlanguage}]] // { arity: 1 }
  1863. Distinct project=[#0{rootpostlanguage}] // { arity: 1 }
  1864. Project (#0{rootpostlanguage}) // { arity: 1 }
  1865. Filter (#0{rootpostlanguage} = varchar_to_text(#1{right_col0_0})) // { arity: 2 }
  1866. FlatMap unnest_array({"es", "ta", "pt"}) // { arity: 2 }
  1867. Distinct project=[#0{rootpostlanguage}] // { arity: 1 }
  1868. Project (#13{rootpostlanguage}) // { arity: 1 }
  1869. Get l0 // { arity: 17 }
  1870. Return // { arity: 2 }
  1871. Reduce group_by=[#0{count_messageid}] aggregates=[count(*)] // { arity: 2 }
  1872. Project (#1{count_messageid}) // { arity: 1 }
  1873. Reduce group_by=[#0{id}] aggregates=[count(#1{messageid})] // { arity: 2 }
  1874. Union // { arity: 2 }
  1875. Project (#1{id}, #11{messageid}) // { arity: 2 }
  1876. Get l1 // { arity: 12 }
  1877. Project (#1{id}, #22) // { arity: 2 }
  1878. Map (null) // { arity: 23 }
  1879. Join on=(#0{creationdate} = #11{creationdate} AND #1{id} = #12{id} AND #2{firstname} = #13{firstname} AND #3{lastname} = #14{lastname} AND #4{gender} = #15{gender} AND #5{birthday} = #16{birthday} AND #6{locationip} = #17{locationip} AND #7{browserused} = #18{browserused} AND #8{locationcityid} = #19{locationcityid} AND #9{speaks} = #20{speaks} AND #10{email} = #21{email}) type=differential // { arity: 22 }
  1880. implementation
  1881. %0[#0..=#10]KKKKKKKKKKK » %1:person[#0..=#10]KKKKKKKKKKK
  1882. ArrangeBy keys=[[#0{creationdate}..=#10{email}]] // { arity: 11 }
  1883. Union // { arity: 11 }
  1884. Negate // { arity: 11 }
  1885. Distinct project=[#0{creationdate}..=#10{email}] // { arity: 11 }
  1886. Project (#0{creationdate}..=#10{email}) // { arity: 11 }
  1887. Get l1 // { arity: 12 }
  1888. Distinct project=[#0{creationdate}..=#10{email}] // { arity: 11 }
  1889. ReadIndex on=person person_id=[*** full scan ***] // { arity: 11 }
  1890. ArrangeBy keys=[[#0{creationdate}..=#10{email}]] // { arity: 11 }
  1891. ReadIndex on=person person_id=[*** full scan ***] // { arity: 11 }
  1892. Used Indexes:
  1893. - materialize.public.person_id (*** full scan ***)
  1894. - materialize.public.message_messageid (*** full scan ***)
  1895. Target cluster: quickstart
  1896. EOF
  1897. ######################################################################
  1898. # QUERY 13
  1899. ######################################################################
  1900. # \set country '\'India\''
  1901. # \set endDate '\'2012-11-09\''::timestamp
  1902. query T multiline
  1903. EXPLAIN OPTIMIZED PLAN WITH(humanized expressions, arity, join implementations) AS VERBOSE TEXT FOR WITH Zombies AS (
  1904. SELECT Person.id AS zombieid
  1905. FROM Country
  1906. JOIN City
  1907. ON City.PartOfCountryId = Country.id
  1908. JOIN Person
  1909. ON Person.LocationCityId = City.id
  1910. LEFT JOIN Message
  1911. ON Person.id = Message.CreatorPersonId
  1912. AND Message.creationDate BETWEEN Person.creationDate AND '2012-11-09'::TIMESTAMP -- the lower bound is an optmization to prune messages
  1913. WHERE Country.name = 'India'
  1914. AND Person.creationDate < '2012-11-09'::TIMESTAMP
  1915. GROUP BY Person.id, Person.creationDate
  1916. -- average of [0, 1) messages per month is equivalent with having less messages than the month span between person creationDate and parameter '2012-11-09'::TIMESTAMP
  1917. HAVING count(Message.MessageId) < 12*extract(YEAR FROM '2012-11-09'::TIMESTAMP) + extract(MONTH FROM '2012-11-09'::TIMESTAMP)
  1918. - (12*extract(YEAR FROM Person.creationDate) + extract(MONTH FROM Person.creationDate))
  1919. + 1
  1920. )
  1921. SELECT Z.zombieid AS "zombie.id"
  1922. , coalesce(t.zombieLikeCount, 0) AS zombieLikeCount
  1923. , coalesce(t.totalLikeCount, 0) AS totalLikeCount
  1924. , CASE WHEN t.totalLikeCount > 0 THEN t.zombieLikeCount::float/t.totalLikeCount ELSE 0 END AS zombieScore
  1925. FROM Zombies Z LEFT JOIN (
  1926. SELECT Z.zombieid, count(*) as totalLikeCount, sum(case when exists (SELECT 1 FROM Zombies ZL WHERE ZL.zombieid = p.id) then 1 else 0 end) AS zombieLikeCount
  1927. FROM Person p, Person_likes_Message plm, Message m, Zombies Z
  1928. WHERE Z.zombieid = m.CreatorPersonId AND p.creationDate < '2012-11-09'::TIMESTAMP
  1929. AND p.id = plm.PersonId AND m.MessageId = plm.MessageId
  1930. GROUP BY Z.zombieid
  1931. ) t ON (Z.zombieid = t.zombieid)
  1932. ORDER BY zombieScore DESC, Z.zombieid
  1933. LIMIT 100
  1934. ----
  1935. Explained Query:
  1936. Finish order_by=[#3 desc nulls_first, #0{id} asc nulls_last] limit=100 output=[#0..=#3]
  1937. With
  1938. cte l0 =
  1939. Project (#0{id}, #2{url}..=#6{url}, #8{creationdate}..=#15{browserused}, #17{speaks}, #18{email}) // { arity: 16 }
  1940. Filter (#1{name} = "India") AND (#8{creationdate} < 2012-11-09 00:00:00 UTC) AND (#0{id}) IS NOT NULL // { arity: 19 }
  1941. Join on=(#0{id} = #7{partofcountryid} AND #4{id} = #16{locationcityid}) type=delta // { arity: 19 }
  1942. implementation
  1943. %0:country » %1:city[#3{partofcountryid}]KA » %2:person[#8{locationcityid}]KAif
  1944. %1:city » %0:country[#0{id}]KAef » %2:person[#8{locationcityid}]KAif
  1945. %2:person » %1:city[#0{id}]KA » %0:country[#0{id}]KAef
  1946. ArrangeBy keys=[[#0{id}]] // { arity: 4 }
  1947. ReadIndex on=country country_id=[delta join 1st input (full scan)] // { arity: 4 }
  1948. ArrangeBy keys=[[#0{id}], [#3{partofcountryid}]] // { arity: 4 }
  1949. ReadIndex on=city city_id=[delta join lookup] city_partofcountryid=[delta join lookup] // { arity: 4 }
  1950. ArrangeBy keys=[[#8{locationcityid}]] // { arity: 11 }
  1951. ReadIndex on=person person_locationcityid=[delta join lookup] // { arity: 11 }
  1952. cte l1 =
  1953. Project (#0{id}..=#15{email}, #17{messageid}) // { arity: 17 }
  1954. Filter (#16{creationdate} <= 2012-11-09 00:00:00 UTC) AND (#16{creationdate} >= #6{creationdate}) // { arity: 29 }
  1955. Join on=(#7{id} = #25{creatorpersonid}) type=differential // { arity: 29 }
  1956. implementation
  1957. %1:message[#9{creatorpersonid}]KAif » %0:l0[#7{id}]Kif
  1958. ArrangeBy keys=[[#7{id}]] // { arity: 16 }
  1959. Filter (#7{id}) IS NOT NULL // { arity: 16 }
  1960. Get l0 // { arity: 16 }
  1961. ArrangeBy keys=[[#9{creatorpersonid}]] // { arity: 13 }
  1962. ReadIndex on=message message_creatorpersonid=[differential join] // { arity: 13 }
  1963. cte l2 =
  1964. Project (#0{id}) // { arity: 1 }
  1965. Filter (bigint_to_numeric(#2{count_messageid}) < ((24155 - ((12 * extract_year_tstz(#1{creationdate})) + extract_month_tstz(#1{creationdate}))) + 1)) // { arity: 3 }
  1966. Reduce group_by=[#1{id}, #0{creationdate}] aggregates=[count(#2{messageid})] // { arity: 3 }
  1967. Union // { arity: 3 }
  1968. Project (#6{creationdate}, #7{id}, #16{messageid}) // { arity: 3 }
  1969. Get l1 // { arity: 17 }
  1970. Project (#6{creationdate}, #7{id}, #32) // { arity: 3 }
  1971. Map (null) // { arity: 33 }
  1972. Join on=(#0{id} = #16{id} AND #1{url} = #17{url} AND #2{partofcontinentid} = #18{partofcontinentid} AND #3{id} = #19{id} AND #4{name} = #20{name} AND #5{url} = #21{url} AND #6{creationdate} = #22{creationdate} AND #7{id} = #23{id} AND #8{firstname} = #24{firstname} AND #9{lastname} = #25{lastname} AND #10{gender} = #26{gender} AND #11{birthday} = #27{birthday} AND #12{locationip} = #28{locationip} AND #13{browserused} = #29{browserused} AND #14{speaks} = #30{speaks} AND #15{email} = #31{email}) type=differential // { arity: 32 }
  1973. implementation
  1974. %0[#0..=#15]KKKKKKKKKKKKKKKK » %1:l0[#0..=#15]KKKKKKKKKKKKKKKK
  1975. ArrangeBy keys=[[#0{id}..=#15{email}]] // { arity: 16 }
  1976. Union // { arity: 16 }
  1977. Negate // { arity: 16 }
  1978. Distinct project=[#0{id}..=#15{email}] // { arity: 16 }
  1979. Project (#0{id}..=#15{email}) // { arity: 16 }
  1980. Filter (#0{id} = #0{id}) AND (#3{id} = #3{id}) // { arity: 17 }
  1981. Get l1 // { arity: 17 }
  1982. Distinct project=[#0{id}..=#15{email}] // { arity: 16 }
  1983. Filter (#0{id} = #0{id}) AND (#3{id} = #3{id}) // { arity: 16 }
  1984. Get l0 // { arity: 16 }
  1985. ArrangeBy keys=[[#0{id}..=#15{email}]] // { arity: 16 }
  1986. Get l0 // { arity: 16 }
  1987. cte l3 =
  1988. Filter (#0{id}) IS NOT NULL // { arity: 1 }
  1989. Get l2 // { arity: 1 }
  1990. cte l4 =
  1991. ArrangeBy keys=[[#0{id}]] // { arity: 1 }
  1992. Get l3 // { arity: 1 }
  1993. cte l5 =
  1994. Project (#1{id}, #23{creatorpersonid}) // { arity: 2 }
  1995. Filter (#0{creationdate} < 2012-11-09 00:00:00 UTC) // { arity: 28 }
  1996. Join on=(#1{id} = #12{personid} AND #13{messageid} = #15{messageid} AND #23{creatorpersonid} = #27{id}) type=delta // { arity: 28 }
  1997. implementation
  1998. %0:person » %1:person_likes_message[#1{personid}]KA » %2:message[#1{messageid}]KA » %3:l4[#0{zombieid}]K
  1999. %1:person_likes_message » %0:person[#1{id}]KAif » %2:message[#1{messageid}]KA » %3:l4[#0{zombieid}]K
  2000. %2:message » %1:person_likes_message[#2{messageid}]KA » %0:person[#1{id}]KAif » %3:l4[#0{zombieid}]K
  2001. %3:l4 » %2:message[#9{creatorpersonid}]KA » %1:person_likes_message[#2{messageid}]KA » %0:person[#1{id}]KAif
  2002. ArrangeBy keys=[[#1{id}]] // { arity: 11 }
  2003. ReadIndex on=person person_id=[delta join 1st input (full scan)] // { arity: 11 }
  2004. ArrangeBy keys=[[#1{personid}], [#2{messageid}]] // { arity: 3 }
  2005. ReadIndex on=person_likes_message person_likes_message_personid=[delta join lookup] person_likes_message_messageid=[delta join lookup] // { arity: 3 }
  2006. ArrangeBy keys=[[#1{messageid}], [#9{creatorpersonid}]] // { arity: 13 }
  2007. ReadIndex on=message message_messageid=[delta join lookup] message_creatorpersonid=[delta join lookup] // { arity: 13 }
  2008. Get l4 // { arity: 1 }
  2009. cte l6 =
  2010. Distinct project=[#0{id}] // { arity: 1 }
  2011. Project (#0{id}) // { arity: 1 }
  2012. Get l5 // { arity: 2 }
  2013. cte l7 =
  2014. Project (#0{id}) // { arity: 1 }
  2015. Join on=(#0{id} = #1{id}) type=differential // { arity: 2 }
  2016. implementation
  2017. %0:l6[#0]UKA » %1[#0]UKA
  2018. ArrangeBy keys=[[#0{id}]] // { arity: 1 }
  2019. Get l6 // { arity: 1 }
  2020. ArrangeBy keys=[[#0{id}]] // { arity: 1 }
  2021. Distinct project=[#0{id}] // { arity: 1 }
  2022. Get l3 // { arity: 1 }
  2023. cte l8 =
  2024. Project (#0{id}, #2{count}, #3{sum}) // { arity: 3 }
  2025. Join on=(#0{id} = #1{creatorpersonid}) type=differential // { arity: 4 }
  2026. implementation
  2027. %1[#0]UKA » %0:l4[#0{zombieid}]K
  2028. Get l4 // { arity: 1 }
  2029. ArrangeBy keys=[[#0{creatorpersonid}]] // { arity: 3 }
  2030. Reduce group_by=[#0{creatorpersonid}] aggregates=[count(*), sum(case when #1 then 1 else 0 end)] // { arity: 3 }
  2031. Project (#1{creatorpersonid}, #3) // { arity: 2 }
  2032. Join on=(#0{id} = #2{id}) type=differential // { arity: 4 }
  2033. implementation
  2034. %0:l5[#0]K » %1[#0]K
  2035. ArrangeBy keys=[[#0{id}]] // { arity: 2 }
  2036. Get l5 // { arity: 2 }
  2037. ArrangeBy keys=[[#0{id}]] // { arity: 2 }
  2038. Union // { arity: 2 }
  2039. Map (true) // { arity: 2 }
  2040. Get l7 // { arity: 1 }
  2041. Map (false) // { arity: 2 }
  2042. Union // { arity: 1 }
  2043. Negate // { arity: 1 }
  2044. Get l7 // { arity: 1 }
  2045. Get l6 // { arity: 1 }
  2046. Return // { arity: 4 }
  2047. Project (#0{id}, #3..=#5) // { arity: 4 }
  2048. Map (coalesce(#2{sum}, 0), coalesce(#1{count}, 0), case when (#1{count} > 0) then (bigint_to_double(#2{sum}) / bigint_to_double(#1{count})) else 0 end) // { arity: 6 }
  2049. Union // { arity: 3 }
  2050. Map (null, null) // { arity: 3 }
  2051. Union // { arity: 1 }
  2052. Negate // { arity: 1 }
  2053. Project (#0{id}) // { arity: 1 }
  2054. Get l8 // { arity: 3 }
  2055. Get l2 // { arity: 1 }
  2056. Get l8 // { arity: 3 }
  2057. Used Indexes:
  2058. - materialize.public.person_id (delta join 1st input (full scan))
  2059. - materialize.public.person_locationcityid (delta join lookup)
  2060. - materialize.public.country_id (delta join 1st input (full scan))
  2061. - materialize.public.city_id (delta join lookup)
  2062. - materialize.public.city_partofcountryid (delta join lookup)
  2063. - materialize.public.person_likes_message_personid (delta join lookup)
  2064. - materialize.public.person_likes_message_messageid (delta join lookup)
  2065. - materialize.public.message_messageid (delta join lookup)
  2066. - materialize.public.message_creatorpersonid (differential join, delta join lookup)
  2067. Target cluster: quickstart
  2068. EOF
  2069. ######################################################################
  2070. # QUERY 14
  2071. ######################################################################
  2072. # \set country1 '\'Philippines\''
  2073. # \set country2 '\'Taiwan\''
  2074. query T multiline
  2075. EXPLAIN OPTIMIZED PLAN WITH(humanized expressions, arity, join implementations) AS VERBOSE TEXT FOR WITH PersonPairCandidates AS (
  2076. SELECT Person1.id AS Person1Id
  2077. , Person2.id AS Person2Id
  2078. , City1.id AS cityId
  2079. , City1.name AS cityName
  2080. FROM Country Country1
  2081. JOIN City City1
  2082. ON City1.PartOfCountryId = Country1.id
  2083. JOIN Person Person1
  2084. ON Person1.LocationCityId = City1.id
  2085. JOIN Person_knows_Person
  2086. ON Person_knows_Person.Person1Id = Person1.id
  2087. JOIN Person Person2
  2088. ON Person2.id = Person_knows_Person.Person2Id
  2089. JOIN City City2
  2090. ON Person2.LocationCityId = City2.id
  2091. JOIN Country Country2
  2092. ON Country2.id = City2.PartOfCountryId
  2093. WHERE Country1.name = 'Philippines'
  2094. AND Country2.name = 'Taiwan'
  2095. )
  2096. , PPC(Person1Id, Person2Id, Flipped) AS (
  2097. SELECT Person1Id AS Person1Id, Person2Id AS Person2Id, false AS Flipped FROM PersonPairCandidates
  2098. UNION ALL
  2099. SELECT Person2Id AS Person1Id, Person1Id AS Person2Id, true As Flipped FROM PersonPairCandidates
  2100. )
  2101. , pair_scores AS (
  2102. SELECT CASE WHEN Flipped THEN Person2Id ELSE Person1Id END AS Person1Id,
  2103. CASE WHEN Flipped THEN Person1Id ELSE Person2Id END AS Person2Id,
  2104. (
  2105. CASE WHEN EXISTS (SELECT 1 FROM Message m, Message r WHERE m.MessageId = r.ParentMessageId AND Person1Id = r.CreatorPersonId AND Person2Id = m.CreatorPersonId AND EXISTS (SELECT 1 FROM PPC x WHERE x.Person1Id = r.CreatorPersonId)) THEN (CASE WHEN Flipped THEN 1 ELSE 4 END) ELSE 0 END +
  2106. CASE WHEN EXISTS (SELECT 1 FROM Message m, Person_likes_Message l WHERE Person2Id = m.CreatorPersonId AND m.MessageId = l.MessageId AND l.PersonId = Person1Id AND EXISTS (SELECT 1 FROM PPC x WHERE x.Person1Id = l.PersonId)) THEN (CASE WHEN Flipped THEN 1 ELSE 10 END) ELSE 0 END
  2107. ) as score
  2108. FROM PPC
  2109. )
  2110. , pair_scoresX AS (
  2111. SELECT Person1Id, Person2Id, sum(score) as score
  2112. FROM pair_scores
  2113. GROUP BY Person1Id, Person2Id
  2114. )
  2115. , score_ranks AS (
  2116. SELECT DISTINCT ON (cityId)
  2117. PersonPairCandidates.Person1Id, PersonPairCandidates.Person2Id, cityId, cityName
  2118. , s.score AS score
  2119. FROM PersonPairCandidates
  2120. LEFT JOIN pair_scoresX s
  2121. ON s.Person1Id = PersonPairCandidates.Person1Id
  2122. AND s.person2Id = PersonPairCandidates.Person2Id
  2123. ORDER BY cityId, s.score DESC, PersonPairCandidates.Person1Id, PersonPairCandidates.Person2Id
  2124. )
  2125. SELECT score_ranks.Person1Id AS "person1.id"
  2126. , score_ranks.Person2Id AS "person2.id"
  2127. , score_ranks.cityName AS "city1.name"
  2128. , score_ranks.score
  2129. FROM score_ranks
  2130. ORDER BY score_ranks.score DESC, score_ranks.Person1Id, score_ranks.Person2Id
  2131. LIMIT 100
  2132. ----
  2133. Explained Query:
  2134. Finish order_by=[#3{sum} desc nulls_first, #0{id} asc nulls_last, #1{person2id} asc nulls_last] limit=100 output=[#0..=#3]
  2135. With
  2136. cte l0 =
  2137. ArrangeBy keys=[[#0{id}]] // { arity: 4 }
  2138. ReadIndex on=country country_id=[delta join lookup, delta join 1st input (full scan)] // { arity: 4 }
  2139. cte l1 =
  2140. ArrangeBy keys=[[#0{id}], [#3{partofcountryid}]] // { arity: 4 }
  2141. ReadIndex on=city city_id=[delta join lookup] city_partofcountryid=[delta join lookup] // { arity: 4 }
  2142. cte l2 =
  2143. ArrangeBy keys=[[#1{id}], [#8{locationcityid}]] // { arity: 11 }
  2144. ReadIndex on=person person_id=[delta join lookup] person_locationcityid=[delta join lookup] // { arity: 11 }
  2145. cte l3 =
  2146. Project (#4{id}, #5{name}, #9{id}, #21{person2id}) // { arity: 4 }
  2147. Filter (#1{name} = "Philippines") AND (#38{name} = "Taiwan") AND (#0{id}) IS NOT NULL AND (#36{partofcountryid}) IS NOT NULL // { arity: 41 }
  2148. Join on=(#0{id} = #7{partofcountryid} AND #4{id} = #16{locationcityid} AND #9{id} = #20{person1id} AND #21{person2id} = #23{id} AND #30{locationcityid} = #33{id} AND #36{partofcountryid} = #37{id}) type=delta // { arity: 41 }
  2149. implementation
  2150. %0:l0 » %1:l1[#3{partofcountryid}]KA » %2:l2[#8{locationcityid}]KA » %3:person_knows_person[#1{person1id}]KA » %4:l2[#1{id}]KA » %5:l1[#0{id}]KA » %6:l0[#0{id}]KAef
  2151. %1:l1 » %0:l0[#0{id}]KAef » %2:l2[#8{locationcityid}]KA » %3:person_knows_person[#1{person1id}]KA » %4:l2[#1{id}]KA » %5:l1[#0{id}]KA » %6:l0[#0{id}]KAef
  2152. %2:l2 » %1:l1[#0{id}]KA » %0:l0[#0{id}]KAef » %3:person_knows_person[#1{person1id}]KA » %4:l2[#1{id}]KA » %5:l1[#0{id}]KA » %6:l0[#0{id}]KAef
  2153. %3:person_knows_person » %2:l2[#1{id}]KA » %1:l1[#0{id}]KA » %0:l0[#0{id}]KAef » %4:l2[#1{id}]KA » %5:l1[#0{id}]KA » %6:l0[#0{id}]KAef
  2154. %4:l2 » %3:person_knows_person[#2{person2id}]KA » %2:l2[#1{id}]KA » %1:l1[#0{id}]KA » %0:l0[#0{id}]KAef » %5:l1[#0{id}]KA » %6:l0[#0{id}]KAef
  2155. %5:l1 » %6:l0[#0{id}]KAef » %4:l2[#8{locationcityid}]KA » %3:person_knows_person[#2{person2id}]KA » %2:l2[#1{id}]KA » %1:l1[#0{id}]KA » %0:l0[#0{id}]KAef
  2156. %6:l0 » %5:l1[#3{partofcountryid}]KA » %4:l2[#8{locationcityid}]KA » %3:person_knows_person[#2{person2id}]KA » %2:l2[#1{id}]KA » %1:l1[#0{id}]KA » %0:l0[#0{id}]KAef
  2157. Get l0 // { arity: 4 }
  2158. Get l1 // { arity: 4 }
  2159. Get l2 // { arity: 11 }
  2160. ArrangeBy keys=[[#1{person1id}], [#2{person2id}]] // { arity: 3 }
  2161. ReadIndex on=person_knows_person person_knows_person_person1id=[delta join lookup] person_knows_person_person2id=[delta join lookup] // { arity: 3 }
  2162. Get l2 // { arity: 11 }
  2163. Get l1 // { arity: 4 }
  2164. Get l0 // { arity: 4 }
  2165. cte l4 =
  2166. Map (case when #2{flipped} then #1{person2id} else #0{id} end, case when #2{flipped} then #0{id} else #1{person2id} end) // { arity: 5 }
  2167. Union // { arity: 3 }
  2168. Project (#2{id}..=#4) // { arity: 3 }
  2169. Map (false) // { arity: 5 }
  2170. Get l3 // { arity: 4 }
  2171. Project (#3{person2id}, #2{id}, #4) // { arity: 3 }
  2172. Map (true) // { arity: 5 }
  2173. Get l3 // { arity: 4 }
  2174. cte l5 =
  2175. Distinct project=[#0{id}, #1{person2id}] // { arity: 2 }
  2176. Project (#0{id}, #1{person2id}) // { arity: 2 }
  2177. Get l4 // { arity: 5 }
  2178. cte l6 =
  2179. ArrangeBy keys=[[#1{messageid}]] // { arity: 13 }
  2180. ReadIndex on=message message_messageid=[differential join] // { arity: 13 }
  2181. cte l7 =
  2182. Project (#0{id}, #1{person2id}) // { arity: 2 }
  2183. Join on=(#0{id} = #2{creatorpersonid} AND #1{person2id} = #3{creatorpersonid}) type=differential // { arity: 4 }
  2184. implementation
  2185. %0:l5[#0, #1]UKKA » %1[#0, #1]UKKA
  2186. ArrangeBy keys=[[#0{id}, #1{person2id}]] // { arity: 2 }
  2187. Get l5 // { arity: 2 }
  2188. ArrangeBy keys=[[#0{creatorpersonid}, #1{creatorpersonid}]] // { arity: 2 }
  2189. Distinct project=[#1{creatorpersonid}, #0{creatorpersonid}] // { arity: 2 }
  2190. Project (#9{creatorpersonid}, #22{creatorpersonid}) // { arity: 2 }
  2191. Join on=(#1{messageid} = #25{parentmessageid}) type=differential // { arity: 26 }
  2192. implementation
  2193. %0:l6[#1{messageid}]KA » %1:message[#12{parentmessageid}]KA
  2194. Get l6 // { arity: 13 }
  2195. ArrangeBy keys=[[#12{parentmessageid}]] // { arity: 13 }
  2196. ReadIndex on=message message_parentmessageid=[differential join] // { arity: 13 }
  2197. cte l8 =
  2198. Project (#0{id}..=#4, #7) // { arity: 6 }
  2199. Join on=(#0{id} = #5{id} AND #1{person2id} = #6{person2id}) type=differential // { arity: 8 }
  2200. implementation
  2201. %0:l4[#0, #1]KK » %1[#0, #1]KK
  2202. ArrangeBy keys=[[#0{id}, #1{person2id}]] // { arity: 5 }
  2203. Get l4 // { arity: 5 }
  2204. ArrangeBy keys=[[#0{id}, #1{person2id}]] // { arity: 3 }
  2205. Union // { arity: 3 }
  2206. Map (true) // { arity: 3 }
  2207. Get l7 // { arity: 2 }
  2208. Map (false) // { arity: 3 }
  2209. Union // { arity: 2 }
  2210. Negate // { arity: 2 }
  2211. Get l7 // { arity: 2 }
  2212. Get l5 // { arity: 2 }
  2213. cte l9 =
  2214. Distinct project=[#0{id}, #1{person2id}] // { arity: 2 }
  2215. Project (#0{id}, #1{person2id}) // { arity: 2 }
  2216. Get l8 // { arity: 6 }
  2217. cte l10 =
  2218. Project (#0{id}, #1{person2id}) // { arity: 2 }
  2219. Join on=(#0{id} = #2{personid} AND #1{person2id} = #3{creatorpersonid}) type=differential // { arity: 4 }
  2220. implementation
  2221. %0:l9[#0, #1]UKKA » %1[#0, #1]UKKA
  2222. ArrangeBy keys=[[#0{id}, #1{person2id}]] // { arity: 2 }
  2223. Get l9 // { arity: 2 }
  2224. ArrangeBy keys=[[#0{personid}, #1{creatorpersonid}]] // { arity: 2 }
  2225. Distinct project=[#1{personid}, #0{creatorpersonid}] // { arity: 2 }
  2226. Project (#9{creatorpersonid}, #14{personid}) // { arity: 2 }
  2227. Join on=(#1{messageid} = #15{messageid}) type=differential // { arity: 16 }
  2228. implementation
  2229. %0:l6[#1{messageid}]KA » %1:person_likes_message[#2{messageid}]KA
  2230. Get l6 // { arity: 13 }
  2231. ArrangeBy keys=[[#2{messageid}]] // { arity: 3 }
  2232. ReadIndex on=person_likes_message person_likes_message_messageid=[differential join] // { arity: 3 }
  2233. cte l11 =
  2234. Project (#0{id}..=#3{person2id}, #6{sum}) // { arity: 5 }
  2235. Join on=(#2{id} = #4 AND #3{person2id} = #5) type=differential // { arity: 7 }
  2236. implementation
  2237. %1[#0, #1]UKKA » %0:l3[#2{person1id}, #3{person2id}]KK
  2238. ArrangeBy keys=[[#2{id}, #3{person2id}]] // { arity: 4 }
  2239. Get l3 // { arity: 4 }
  2240. ArrangeBy keys=[[#0, #1]] // { arity: 3 }
  2241. Reduce group_by=[#1, #2] aggregates=[sum((case when #3 then case when #0{flipped} then 1 else 4 end else 0 end + case when #4 then case when #0{flipped} then 1 else 10 end else 0 end))] // { arity: 3 }
  2242. Project (#2..=#5, #8) // { arity: 5 }
  2243. Join on=(#0{id} = #6{id} AND #1{person2id} = #7{person2id}) type=differential // { arity: 9 }
  2244. implementation
  2245. %0:l8[#0, #1]KK » %1[#0, #1]KK
  2246. ArrangeBy keys=[[#0{id}, #1{person2id}]] // { arity: 6 }
  2247. Get l8 // { arity: 6 }
  2248. ArrangeBy keys=[[#0{id}, #1{person2id}]] // { arity: 3 }
  2249. Union // { arity: 3 }
  2250. Map (true) // { arity: 3 }
  2251. Get l10 // { arity: 2 }
  2252. Map (false) // { arity: 3 }
  2253. Union // { arity: 2 }
  2254. Negate // { arity: 2 }
  2255. Get l10 // { arity: 2 }
  2256. Get l9 // { arity: 2 }
  2257. Return // { arity: 4 }
  2258. Project (#0{id}, #1{person2id}, #3{name}, #4{sum}) // { arity: 4 }
  2259. TopK group_by=[#2{id}] order_by=[#4{sum} desc nulls_first, #0{id} asc nulls_last, #1{person2id} asc nulls_last] limit=1 // { arity: 5 }
  2260. Union // { arity: 5 }
  2261. Map (null) // { arity: 5 }
  2262. Union // { arity: 4 }
  2263. Negate // { arity: 4 }
  2264. Project (#2{id}, #3{person2id}, #0{id}, #1{name}) // { arity: 4 }
  2265. Get l11 // { arity: 5 }
  2266. Project (#2{id}, #3{person2id}, #0{id}, #1{name}) // { arity: 4 }
  2267. Get l3 // { arity: 4 }
  2268. Project (#2{id}, #3{person2id}, #0{id}, #1{name}, #4{sum}) // { arity: 5 }
  2269. Get l11 // { arity: 5 }
  2270. Used Indexes:
  2271. - materialize.public.person_id (delta join lookup)
  2272. - materialize.public.person_locationcityid (delta join lookup)
  2273. - materialize.public.person_knows_person_person1id (delta join lookup)
  2274. - materialize.public.person_knows_person_person2id (delta join lookup)
  2275. - materialize.public.country_id (delta join lookup, delta join 1st input (full scan))
  2276. - materialize.public.city_id (delta join lookup)
  2277. - materialize.public.city_partofcountryid (delta join lookup)
  2278. - materialize.public.person_likes_message_messageid (differential join)
  2279. - materialize.public.message_messageid (differential join)
  2280. - materialize.public.message_parentmessageid (differential join)
  2281. Target cluster: quickstart
  2282. EOF
  2283. ######################################################################
  2284. # QUERY 15
  2285. ######################################################################
  2286. # \set person1Id 1450::bigint
  2287. # \set person2Id 15393162796819
  2288. # \set startDate '\'2012-11-06\''::timestamp
  2289. # \set endDate '\'2012-11-10\''::timestamp
  2290. query T multiline
  2291. EXPLAIN OPTIMIZED PLAN WITH(humanized expressions, arity, join implementations) AS VERBOSE TEXT FOR WITH
  2292. -- forums within the date range
  2293. myForums AS (
  2294. SELECT id FROM Forum f WHERE f.creationDate BETWEEN '2012-11-06'::TIMESTAMP AND '2012-11-10'::TIMESTAMP
  2295. ),
  2296. -- the (inverse) interaction scores between folks who know each other
  2297. mm AS (
  2298. SELECT least(msg.CreatorPersonId, reply.CreatorPersonId) AS src,
  2299. greatest(msg.CreatorPersonId, reply.CreatorPersonId) AS dst,
  2300. sum(case when msg.ParentMessageId is null then 10 else 5 end) AS w
  2301. FROM Person_knows_Person pp, Message msg, Message reply
  2302. WHERE true
  2303. AND pp.person1id = msg.CreatorPersonId
  2304. AND pp.person2id = reply.CreatorPersonId
  2305. AND reply.ParentMessageId = msg.MessageId
  2306. AND EXISTS (SELECT * FROM myForums f WHERE f.id = msg.containerforumid)
  2307. AND EXISTS (SELECT * FROM myForums f WHERE f.id = reply.containerforumid)
  2308. GROUP BY src, dst
  2309. ),
  2310. -- the true interaction scores, with 0 default for folks with no interactions
  2311. edge AS (
  2312. SELECT pp.person1id AS src,
  2313. pp.person2id AS dst,
  2314. 10::double precision / (coalesce(w, 0) + 10) AS w
  2315. FROM Person_knows_Person pp
  2316. LEFT JOIN mm
  2317. ON least(pp.person1id, pp.person2id) = mm.src
  2318. AND greatest(pp.person1id, pp.person2id) = mm.dst
  2319. ),
  2320. completed_paths AS (
  2321. WITH MUTUALLY RECURSIVE
  2322. paths (src bigint, dst bigint, w double precision) AS (
  2323. SELECT 1450::bigint AS src, 1450::bigint AS dst, 0::double precision AS w
  2324. UNION
  2325. SELECT paths1.src, paths2.dst, paths1.w + paths2.w
  2326. FROM minimal_paths paths1
  2327. JOIN edge paths2 -- step-transitive closure
  2328. ON paths1.dst = paths2.src
  2329. ),
  2330. minimal_paths (src bigint, dst bigint, w double precision) AS (
  2331. SELECT src, dst, min(w)
  2332. FROM paths
  2333. GROUP BY src, dst
  2334. )
  2335. SELECT src, dst, w
  2336. FROM minimal_paths
  2337. WHERE dst = 15393162796819),
  2338. results AS (
  2339. SELECT dst, w
  2340. FROM completed_paths
  2341. WHERE w IN (SELECT min(w) FROM completed_paths)
  2342. )
  2343. SELECT coalesce(w, -1) FROM results ORDER BY w ASC LIMIT 20
  2344. ----
  2345. Explained Query:
  2346. Finish order_by=[#1{min} asc nulls_last] limit=20 output=[#2]
  2347. With
  2348. cte l0 =
  2349. Project (#1{person1id}, #2{person2id}) // { arity: 2 }
  2350. ReadIndex on=person_knows_person person_knows_person_person1id=[*** full scan ***] // { arity: 3 }
  2351. cte l1 =
  2352. ArrangeBy keys=[[greatest(#0{person1id}, #1{person2id}), least(#0{person1id}, #1{person2id})]] // { arity: 2 }
  2353. Get l0 // { arity: 2 }
  2354. cte l2 =
  2355. ArrangeBy keys=[[#0{id}]] // { arity: 1 }
  2356. Distinct project=[#0{id}] // { arity: 1 }
  2357. Project (#1{id}) // { arity: 1 }
  2358. Filter (#0{creationdate} <= 2012-11-10 00:00:00 UTC) AND (#0{creationdate} >= 2012-11-06 00:00:00 UTC) AND (#1{id}) IS NOT NULL // { arity: 4 }
  2359. ReadIndex on=forum forum_id=[*** full scan ***] // { arity: 4 }
  2360. cte l3 =
  2361. Join on=(#2{src} = least(#0{person1id}, #1{person2id}) AND #3{dst} = greatest(#0{person1id}, #1{person2id})) type=differential // { arity: 5 }
  2362. implementation
  2363. %1[#1{dst}, #0{src}]UKK » %0:l1[greatest(#0{person1id}, #1{person2id}), least(#0{person1id}, #1{person2id})]KK
  2364. Get l1 // { arity: 2 }
  2365. ArrangeBy keys=[[#1{dst}, #0{src}]] // { arity: 3 }
  2366. Reduce group_by=[least(#0{person1id}, #1{person2id}), greatest(#0{person1id}, #1{person2id})] aggregates=[sum(case when (#2{parentmessageid}) IS NULL then 10 else 5 end)] // { arity: 3 }
  2367. Project (#1{person1id}, #2{person2id}, #6{parentmessageid}) // { arity: 3 }
  2368. Join on=(#1{person1id} = #4{creatorpersonid} AND #2{person2id} = #7{creatorpersonid} AND #3{messageid} = #9{parentmessageid} AND #5{containerforumid} = #10{id} AND #8{containerforumid} = #11{id}) type=delta // { arity: 12 }
  2369. implementation
  2370. %0:person_knows_person » %1:message[#1{creatorpersonid}]KA » %3:l2[#0]UKA » %2:message[#0{creatorpersonid}, #2{parentmessageid}]KK » %4:l2[#0]UKA
  2371. %1:message » %3:l2[#0]UKA » %0:person_knows_person[#1{person1id}]KA » %2:message[#0{creatorpersonid}, #2{parentmessageid}]KK » %4:l2[#0]UKA
  2372. %2:message » %4:l2[#0]UKA » %0:person_knows_person[#2{person2id}]KA » %1:message[#0{messageid}, #1{creatorpersonid}]KK » %3:l2[#0]UKA
  2373. %3:l2 » %1:message[#2{containerforumid}]KA » %0:person_knows_person[#1{person1id}]KA » %2:message[#0{creatorpersonid}, #2{parentmessageid}]KK » %4:l2[#0]UKA
  2374. %4:l2 » %2:message[#1{containerforumid}]KA » %0:person_knows_person[#2{person2id}]KA » %1:message[#0{messageid}, #1{creatorpersonid}]KK » %3:l2[#0]UKA
  2375. ArrangeBy keys=[[#1{person1id}], [#2{person2id}]] // { arity: 3 }
  2376. ReadIndex on=person_knows_person person_knows_person_person1id=[delta join 1st input (full scan)] person_knows_person_person2id=[delta join lookup] // { arity: 3 }
  2377. ArrangeBy keys=[[#0{messageid}, #1{creatorpersonid}], [#1{creatorpersonid}], [#2{containerforumid}]] // { arity: 4 }
  2378. Project (#1{messageid}, #9{creatorpersonid}, #10{containerforumid}, #12{parentmessageid}) // { arity: 4 }
  2379. ReadIndex on=message message_messageid=[*** full scan ***] // { arity: 13 }
  2380. ArrangeBy keys=[[#0{creatorpersonid}, #2{parentmessageid}], [#1{containerforumid}]] // { arity: 3 }
  2381. Project (#9{creatorpersonid}, #10{containerforumid}, #12{parentmessageid}) // { arity: 3 }
  2382. Filter (#12{parentmessageid}) IS NOT NULL // { arity: 13 }
  2383. ReadIndex on=message message_messageid=[*** full scan ***] // { arity: 13 }
  2384. Get l2 // { arity: 1 }
  2385. Get l2 // { arity: 1 }
  2386. Return // { arity: 3 }
  2387. With Mutually Recursive
  2388. cte l4 =
  2389. Project (#2, #0{person2id}, #1{min}) // { arity: 3 }
  2390. Map (1450) // { arity: 3 }
  2391. Reduce group_by=[#0{person2id}] aggregates=[min(#1{w})] // { arity: 2 }
  2392. Distinct project=[#0{person2id}, #1] // { arity: 2 }
  2393. Union // { arity: 2 }
  2394. Project (#3{person2id}, #5) // { arity: 2 }
  2395. Map ((#1{w} + #4{w})) // { arity: 6 }
  2396. Join on=(#0{dst} = #2{person1id}) type=differential // { arity: 5 }
  2397. implementation
  2398. %0:l4[#0{dst}]UK » %1[#0{src}]K
  2399. ArrangeBy keys=[[#0{dst}]] // { arity: 2 }
  2400. Project (#1{person2id}, #2{min}) // { arity: 2 }
  2401. Get l4 // { arity: 3 }
  2402. ArrangeBy keys=[[#0{person1id}]] // { arity: 3 }
  2403. Project (#0{person1id}, #1{person2id}, #3) // { arity: 3 }
  2404. Map ((10 / bigint_to_double((coalesce(#2{sum}, 0) + 10)))) // { arity: 4 }
  2405. Union // { arity: 3 }
  2406. Map (null) // { arity: 3 }
  2407. Union // { arity: 2 }
  2408. Negate // { arity: 2 }
  2409. Project (#0{person1id}, #1{person2id}) // { arity: 2 }
  2410. Join on=(#2 = least(#0{person1id}, #1{person2id}) AND #3 = greatest(#0{person1id}, #1{person2id})) type=differential // { arity: 4 }
  2411. implementation
  2412. %1[#1, #0]UKK » %0:l1[greatest(#0{person1id}, #1{person2id}), least(#0{person1id}, #1{person2id})]KK
  2413. Get l1 // { arity: 2 }
  2414. ArrangeBy keys=[[#1, #0]] // { arity: 2 }
  2415. Distinct project=[#0, #1] // { arity: 2 }
  2416. Project (#2, #3) // { arity: 2 }
  2417. Get l3 // { arity: 5 }
  2418. Get l0 // { arity: 2 }
  2419. Project (#0{person1id}, #1{person2id}, #4{sum}) // { arity: 3 }
  2420. Get l3 // { arity: 5 }
  2421. Constant // { arity: 2 }
  2422. - (1450, 0)
  2423. Return // { arity: 3 }
  2424. Project (#1{person2id}, #2{min}, #2{min}) // { arity: 3 }
  2425. Filter (#1{person2id} = 15393162796819) AND (#2{min} = #2{min}) // { arity: 3 }
  2426. Get l4 // { arity: 3 }
  2427. Used Indexes:
  2428. - materialize.public.forum_id (*** full scan ***)
  2429. - materialize.public.person_knows_person_person1id (*** full scan ***, delta join 1st input (full scan))
  2430. - materialize.public.person_knows_person_person2id (delta join lookup)
  2431. - materialize.public.message_messageid (*** full scan ***)
  2432. Target cluster: quickstart
  2433. EOF
  2434. # original, w/crossjoins
  2435. query T multiline
  2436. EXPLAIN OPTIMIZED PLAN WITH(humanized expressions, arity, join implementations) AS VERBOSE TEXT FOR WITH MUTUALLY RECURSIVE
  2437. srcs (f bigint) AS (SELECT 1450::bigint),
  2438. dsts (t bigint) AS (SELECT 15393162796819),
  2439. myForums (id bigint) AS (
  2440. SELECT id FROM Forum f WHERE f.creationDate BETWEEN '2012-11-06'::TIMESTAMP AND '2012-11-10'::TIMESTAMP
  2441. ),
  2442. mm (src bigint, dst bigint, w bigint) AS (
  2443. SELECT least(msg.CreatorPersonId, reply.CreatorPersonId) AS src,
  2444. greatest(msg.CreatorPersonId, reply.CreatorPersonId) AS dst,
  2445. sum(case when msg.ParentMessageId is null then 10 else 5 end) AS w
  2446. FROM Person_knows_Person pp, Message msg, Message reply
  2447. WHERE true
  2448. AND pp.person1id = msg.CreatorPersonId
  2449. AND pp.person2id = reply.CreatorPersonId
  2450. AND reply.ParentMessageId = msg.MessageId
  2451. AND EXISTS (SELECT * FROM myForums f WHERE f.id = msg.containerforumid)
  2452. AND EXISTS (SELECT * FROM myForums f WHERE f.id = reply.containerforumid)
  2453. GROUP BY src, dst
  2454. ),
  2455. path (src bigint, dst bigint, w double precision) AS (
  2456. SELECT pp.person1id, pp.person2id, 10::double precision / (coalesce(w, 0) + 10)
  2457. FROM Person_knows_Person pp left join mm on least(pp.person1id, pp.person2id) = mm.src AND greatest(pp.person1id, pp.person2id) = mm.dst
  2458. ),
  2459. -- bidirectional bfs for nonexistant paths
  2460. pexists (src bigint, dir bool) AS (
  2461. (
  2462. SELECT f, true FROM srcs
  2463. UNION
  2464. SELECT t, false FROM dsts
  2465. )
  2466. UNION
  2467. (
  2468. WITH
  2469. ss (src, dir) AS (SELECT src, dir FROM pexists),
  2470. ns (src, dir) AS (SELECT p.dst, ss.dir FROM ss, path p WHERE ss.src = p.src),
  2471. bb (src, dir) AS (SELECT src, dir FROM ns UNION ALL SELECT src, dir FROM ss),
  2472. found (found) AS (
  2473. SELECT 1 AS found
  2474. FROM bb b1, bb b2
  2475. WHERE b1.dir AND (NOT b2.dir) AND b1.src = b2.src
  2476. )
  2477. SELECT src, dir
  2478. FROM ns
  2479. WHERE NOT EXISTS (SELECT 1 FROM found)
  2480. UNION
  2481. SELECT -1, true
  2482. WHERE EXISTS (SELECT 1 FROM found)
  2483. )
  2484. ),
  2485. pathfound (c bool) AS (
  2486. SELECT true AS c
  2487. FROM pexists
  2488. WHERE src = -1 AND dir
  2489. ),
  2490. shorts (dir bool, gsrc bigint, dst bigint, w double precision, dead bool, iter bigint) AS (
  2491. (
  2492. SELECT false, f, f, 0::double precision, false, 0 FROM srcs WHERE EXISTS (SELECT 1 FROM pathfound)
  2493. UNION ALL
  2494. SELECT true, t, t, 0::double precision, false, 0 FROM dsts WHERE EXISTS (SELECT 1 FROM pathfound)
  2495. )
  2496. UNION
  2497. (
  2498. WITH
  2499. ss (dir, gsrc, dst, w, dead, iter) AS
  2500. (SELECT * FROM shorts),
  2501. toExplore (dir, gsrc, dst, w, dead, iter) AS
  2502. (SELECT * FROM ss WHERE dead = false ORDER BY w limit 1000),
  2503. -- assumes graph is undirected
  2504. newPoints (dir, gsrc, dst, w, dead) AS (
  2505. SELECT e.dir, e.gsrc AS gsrc, p.dst AS dst, e.w + p.w AS w, false AS dead
  2506. FROM path p join toExplore e on (e.dst = p.src)
  2507. UNION ALL
  2508. SELECT dir, gsrc, dst, w, dead OR EXISTS (SELECT * FROM toExplore e WHERE e.dir = o.dir AND e.gsrc = o.gsrc AND e.dst = o.dst) FROM ss o
  2509. ),
  2510. fullTable (dir, gsrc, dst, w, dead) AS (
  2511. SELECT DISTINCT ON(dir, gsrc, dst) dir, gsrc, dst, w, dead
  2512. FROM newPoints
  2513. ORDER BY dir, gsrc, dst, w, dead DESC
  2514. ),
  2515. found AS (
  2516. SELECT min(l.w + r.w) AS w
  2517. FROM fullTable l, fullTable r
  2518. WHERE l.dir = false AND r.dir = true AND l.dst = r.dst
  2519. )
  2520. SELECT dir,
  2521. gsrc,
  2522. dst,
  2523. w,
  2524. dead OR (coalesce(t.w > (SELECT f.w/2 FROM found f), false)),
  2525. e.iter + 1 AS iter
  2526. FROM fullTable t, (SELECT iter FROM toExplore limit 1) e
  2527. )
  2528. ),
  2529. ss (dir bool, gsrc bigint, dst bigint, w double precision, iter bigint) AS (
  2530. SELECT dir, gsrc, dst, w, iter FROM shorts WHERE iter = (SELECT max(iter) FROM shorts)
  2531. ),
  2532. results(f bigint, t bigint, w double precision) AS (
  2533. SELECT l.gsrc, r.gsrc, min(l.w + r.w)
  2534. FROM ss l, ss r
  2535. WHERE l.dir = false AND r.dir = true AND l.dst = r.dst
  2536. GROUP BY l.gsrc, r.gsrc
  2537. )
  2538. SELECT coalesce(min(w), -1) FROM results
  2539. ----
  2540. Explained Query:
  2541. With
  2542. cte l0 =
  2543. Project (#1{person1id}, #2{person2id}) // { arity: 2 }
  2544. ReadIndex on=person_knows_person person_knows_person_person1id=[*** full scan ***] // { arity: 3 }
  2545. cte l1 =
  2546. ArrangeBy keys=[[greatest(#0{person1id}, #1{person2id}), least(#0{person1id}, #1{person2id})]] // { arity: 2 }
  2547. Get l0 // { arity: 2 }
  2548. cte l2 =
  2549. ArrangeBy keys=[[#0{id}]] // { arity: 1 }
  2550. Distinct project=[#0{id}] // { arity: 1 }
  2551. Project (#1{id}) // { arity: 1 }
  2552. Filter (#0{creationdate} <= 2012-11-10 00:00:00 UTC) AND (#0{creationdate} >= 2012-11-06 00:00:00 UTC) AND (#1{id}) IS NOT NULL // { arity: 4 }
  2553. ReadIndex on=forum forum_id=[*** full scan ***] // { arity: 4 }
  2554. cte l3 =
  2555. Join on=(#2{src} = least(#0{person1id}, #1{person2id}) AND #3{dst} = greatest(#0{person1id}, #1{person2id})) type=differential // { arity: 5 }
  2556. implementation
  2557. %1[#1{dst}, #0{src}]UKK » %0:l1[greatest(#0{person1id}, #1{person2id}), least(#0{person1id}, #1{person2id})]KK
  2558. Get l1 // { arity: 2 }
  2559. ArrangeBy keys=[[#1{dst}, #0{src}]] // { arity: 3 }
  2560. Reduce group_by=[least(#0{person1id}, #1{person2id}), greatest(#0{person1id}, #1{person2id})] aggregates=[sum(case when (#2{parentmessageid}) IS NULL then 10 else 5 end)] // { arity: 3 }
  2561. Project (#1{person1id}, #2{person2id}, #6{parentmessageid}) // { arity: 3 }
  2562. Join on=(#1{person1id} = #4{creatorpersonid} AND #2{person2id} = #7{creatorpersonid} AND #3{messageid} = #9{parentmessageid} AND #5{containerforumid} = #10{id} AND #8{containerforumid} = #11{id}) type=delta // { arity: 12 }
  2563. implementation
  2564. %0:person_knows_person » %1:message[#1{creatorpersonid}]KA » %3:l2[#0]UKA » %2:message[#0{creatorpersonid}, #2{parentmessageid}]KK » %4:l2[#0]UKA
  2565. %1:message » %3:l2[#0]UKA » %0:person_knows_person[#1{person1id}]KA » %2:message[#0{creatorpersonid}, #2{parentmessageid}]KK » %4:l2[#0]UKA
  2566. %2:message » %4:l2[#0]UKA » %0:person_knows_person[#2{person2id}]KA » %1:message[#0{messageid}, #1{creatorpersonid}]KK » %3:l2[#0]UKA
  2567. %3:l2 » %1:message[#2{containerforumid}]KA » %0:person_knows_person[#1{person1id}]KA » %2:message[#0{creatorpersonid}, #2{parentmessageid}]KK » %4:l2[#0]UKA
  2568. %4:l2 » %2:message[#1{containerforumid}]KA » %0:person_knows_person[#2{person2id}]KA » %1:message[#0{messageid}, #1{creatorpersonid}]KK » %3:l2[#0]UKA
  2569. ArrangeBy keys=[[#1{person1id}], [#2{person2id}]] // { arity: 3 }
  2570. ReadIndex on=person_knows_person person_knows_person_person1id=[delta join 1st input (full scan)] person_knows_person_person2id=[delta join lookup] // { arity: 3 }
  2571. ArrangeBy keys=[[#0{messageid}, #1{creatorpersonid}], [#1{creatorpersonid}], [#2{containerforumid}]] // { arity: 4 }
  2572. Project (#1{messageid}, #9{creatorpersonid}, #10{containerforumid}, #12{parentmessageid}) // { arity: 4 }
  2573. ReadIndex on=message message_messageid=[*** full scan ***] // { arity: 13 }
  2574. ArrangeBy keys=[[#0{creatorpersonid}, #2{parentmessageid}], [#1{containerforumid}]] // { arity: 3 }
  2575. Project (#9{creatorpersonid}, #10{containerforumid}, #12{parentmessageid}) // { arity: 3 }
  2576. Filter (#12{parentmessageid}) IS NOT NULL // { arity: 13 }
  2577. ReadIndex on=message message_messageid=[*** full scan ***] // { arity: 13 }
  2578. Get l2 // { arity: 1 }
  2579. Get l2 // { arity: 1 }
  2580. cte l4 =
  2581. Project (#0{person1id}, #1{person2id}, #3) // { arity: 3 }
  2582. Map ((10 / bigint_to_double((coalesce(#2{sum}, 0) + 10)))) // { arity: 4 }
  2583. Union // { arity: 3 }
  2584. Map (null) // { arity: 3 }
  2585. Union // { arity: 2 }
  2586. Negate // { arity: 2 }
  2587. Project (#0{person1id}, #1{person2id}) // { arity: 2 }
  2588. Join on=(#2 = least(#0{person1id}, #1{person2id}) AND #3 = greatest(#0{person1id}, #1{person2id})) type=differential // { arity: 4 }
  2589. implementation
  2590. %1[#1, #0]UKK » %0:l1[greatest(#0{person1id}, #1{person2id}), least(#0{person1id}, #1{person2id})]KK
  2591. Get l1 // { arity: 2 }
  2592. ArrangeBy keys=[[#1, #0]] // { arity: 2 }
  2593. Distinct project=[#0, #1] // { arity: 2 }
  2594. Project (#2, #3) // { arity: 2 }
  2595. Get l3 // { arity: 5 }
  2596. Get l0 // { arity: 2 }
  2597. Project (#0{person1id}, #1{person2id}, #4{sum}) // { arity: 3 }
  2598. Get l3 // { arity: 5 }
  2599. Return // { arity: 1 }
  2600. With Mutually Recursive
  2601. cte l5 =
  2602. Project (#1, #3{person2id}) // { arity: 2 }
  2603. Join on=(#0{src} = #2{person1id}) type=differential // { arity: 4 }
  2604. implementation
  2605. %0:l8[#0{src}]K » %1:l4[#0{src}]K
  2606. ArrangeBy keys=[[#0{person2id}]] // { arity: 2 }
  2607. Get l8 // { arity: 2 }
  2608. ArrangeBy keys=[[#0{person1id}]] // { arity: 2 }
  2609. Project (#0{person1id}, #1{person2id}) // { arity: 2 }
  2610. Get l4 // { arity: 3 }
  2611. cte l6 =
  2612. Union // { arity: 2 }
  2613. Project (#1{person2id}, #0) // { arity: 2 }
  2614. Get l5 // { arity: 2 }
  2615. Get l8 // { arity: 2 }
  2616. cte l7 =
  2617. Distinct project=[] // { arity: 0 }
  2618. Project () // { arity: 0 }
  2619. Join on=(#0{person2id} = #1{person2id}) type=differential // { arity: 2 }
  2620. implementation
  2621. %0:l6[#0{src}]Kf » %1:l6[#0{src}]Kf
  2622. ArrangeBy keys=[[#0{person2id}]] // { arity: 1 }
  2623. Project (#0{person2id}) // { arity: 1 }
  2624. Filter #1{dir} // { arity: 2 }
  2625. Get l6 // { arity: 2 }
  2626. ArrangeBy keys=[[#0{person2id}]] // { arity: 1 }
  2627. Project (#0{person2id}) // { arity: 1 }
  2628. Filter NOT(#1{dir}) // { arity: 2 }
  2629. Get l6 // { arity: 2 }
  2630. cte l8 =
  2631. Distinct project=[#0{person2id}, #1] // { arity: 2 }
  2632. Union // { arity: 2 }
  2633. Project (#1{person2id}, #0) // { arity: 2 }
  2634. CrossJoin type=differential // { arity: 2 }
  2635. implementation
  2636. %0:l5[×] » %1[×]
  2637. ArrangeBy keys=[[]] // { arity: 2 }
  2638. Get l5 // { arity: 2 }
  2639. ArrangeBy keys=[[]] // { arity: 0 }
  2640. Union // { arity: 0 }
  2641. Negate // { arity: 0 }
  2642. Get l7 // { arity: 0 }
  2643. Constant // { arity: 0 }
  2644. - ()
  2645. Project (#1, #0) // { arity: 2 }
  2646. Map (true, -1) // { arity: 2 }
  2647. Get l7 // { arity: 0 }
  2648. Constant // { arity: 2 }
  2649. - (1450, true)
  2650. - (15393162796819, false)
  2651. cte l9 =
  2652. TopK order_by=[#3 asc nulls_last] limit=1000 // { arity: 5 }
  2653. Project (#0..=#3, #5) // { arity: 5 }
  2654. Filter (#4{dead} = false) // { arity: 6 }
  2655. Get l17 // { arity: 6 }
  2656. cte l10 =
  2657. Distinct project=[#0..=#2] // { arity: 3 }
  2658. Project (#0..=#2{person2id}) // { arity: 3 }
  2659. Get l17 // { arity: 6 }
  2660. cte l11 =
  2661. ArrangeBy keys=[[#0..=#2]] // { arity: 3 }
  2662. Get l10 // { arity: 3 }
  2663. cte l12 =
  2664. Project (#0..=#2) // { arity: 3 }
  2665. Join on=(#0 = #3{dir} AND #1 = #4{gsrc} AND #2 = #5{dst}) type=differential // { arity: 6 }
  2666. implementation
  2667. %1[#0..=#2]UKKKA » %0:l11[#0..=#2]UKKK
  2668. Get l11 // { arity: 3 }
  2669. ArrangeBy keys=[[#0{dir}..=#2{dst}]] // { arity: 3 }
  2670. Distinct project=[#0{dir}..=#2{dst}] // { arity: 3 }
  2671. Project (#0..=#2) // { arity: 3 }
  2672. Get l9 // { arity: 5 }
  2673. cte l13 =
  2674. TopK group_by=[#0, #1, #2{person2id}] order_by=[#3 asc nulls_last, #4 desc nulls_first] limit=1 // { arity: 5 }
  2675. Union // { arity: 5 }
  2676. Project (#3, #4, #1{person2id}, #7, #8) // { arity: 5 }
  2677. Map ((#6{w} + #2{w}), false) // { arity: 9 }
  2678. Join on=(#0{person1id} = #5{dst}) type=differential // { arity: 7 }
  2679. implementation
  2680. %0:l4[#0{src}]K » %1:l9[#2{dst}]K
  2681. ArrangeBy keys=[[#0{person1id}]] // { arity: 3 }
  2682. Get l4 // { arity: 3 }
  2683. ArrangeBy keys=[[#2{dst}]] // { arity: 4 }
  2684. Project (#0..=#3) // { arity: 4 }
  2685. Get l9 // { arity: 5 }
  2686. Project (#0..=#3, #9) // { arity: 5 }
  2687. Map ((#4{dead} OR #8)) // { arity: 10 }
  2688. Join on=(#0 = #5 AND #1 = #6 AND #2 = #7) type=differential // { arity: 9 }
  2689. implementation
  2690. %0:l17[#0..=#2]KKK » %1[#0..=#2]KKK
  2691. ArrangeBy keys=[[#0..=#2]] // { arity: 5 }
  2692. Project (#0..=#4) // { arity: 5 }
  2693. Get l17 // { arity: 6 }
  2694. ArrangeBy keys=[[#0..=#2]] // { arity: 4 }
  2695. Union // { arity: 4 }
  2696. Map (true) // { arity: 4 }
  2697. Get l12 // { arity: 3 }
  2698. Project (#0..=#2, #6) // { arity: 4 }
  2699. Map (false) // { arity: 7 }
  2700. Join on=(#0 = #3 AND #1 = #4 AND #2 = #5) type=differential // { arity: 6 }
  2701. implementation
  2702. %1:l11[#0..=#2]UKKK » %0[#0..=#2]KKK
  2703. ArrangeBy keys=[[#0..=#2]] // { arity: 3 }
  2704. Union // { arity: 3 }
  2705. Negate // { arity: 3 }
  2706. Get l12 // { arity: 3 }
  2707. Get l10 // { arity: 3 }
  2708. Get l11 // { arity: 3 }
  2709. cte l14 =
  2710. Reduce aggregates=[min((#0{w} + #1{w}))] // { arity: 1 }
  2711. Project (#1, #3) // { arity: 2 }
  2712. Join on=(#0{person2id} = #2{person2id}) type=differential // { arity: 4 }
  2713. implementation
  2714. %0:l13[#0{dst}]Kef » %1:l13[#0{dst}]Kef
  2715. ArrangeBy keys=[[#0{person2id}]] // { arity: 2 }
  2716. Project (#2{person2id}, #3) // { arity: 2 }
  2717. Filter (#0{dir} = false) // { arity: 5 }
  2718. Get l13 // { arity: 5 }
  2719. ArrangeBy keys=[[#0{person2id}]] // { arity: 2 }
  2720. Project (#2{person2id}, #3) // { arity: 2 }
  2721. Filter (#0{dir} = true) // { arity: 5 }
  2722. Get l13 // { arity: 5 }
  2723. cte l15 =
  2724. Project (#1) // { arity: 1 }
  2725. Map ((#0{min} / 2)) // { arity: 2 }
  2726. Union // { arity: 1 }
  2727. Get l14 // { arity: 1 }
  2728. Map (null) // { arity: 1 }
  2729. Union // { arity: 0 }
  2730. Negate // { arity: 0 }
  2731. Project () // { arity: 0 }
  2732. Get l14 // { arity: 1 }
  2733. Constant // { arity: 0 }
  2734. - ()
  2735. cte l16 =
  2736. Distinct project=[] // { arity: 0 }
  2737. Project () // { arity: 0 }
  2738. Filter #1{dir} AND (#0{person2id} = -1) // { arity: 2 }
  2739. Get l8 // { arity: 2 }
  2740. cte l17 =
  2741. Distinct project=[#0..=#5] // { arity: 6 }
  2742. Union // { arity: 6 }
  2743. Project (#1, #0, #0, #2..=#4) // { arity: 6 }
  2744. Map (0, false, 0) // { arity: 5 }
  2745. Union // { arity: 2 }
  2746. Map (1450, false) // { arity: 2 }
  2747. Get l16 // { arity: 0 }
  2748. Map (15393162796819, true) // { arity: 2 }
  2749. Get l16 // { arity: 0 }
  2750. Project (#0..=#3, #7, #8) // { arity: 6 }
  2751. Map ((#4{dead} OR coalesce((#3{w} > #6), false)), (#5{iter} + 1)) // { arity: 9 }
  2752. CrossJoin type=delta // { arity: 7 }
  2753. implementation
  2754. %0:l13 » %1[×]U » %2[×]U
  2755. %1 » %2[×]U » %0:l13[×]
  2756. %2 » %1[×]U » %0:l13[×]
  2757. ArrangeBy keys=[[]] // { arity: 5 }
  2758. Get l13 // { arity: 5 }
  2759. ArrangeBy keys=[[]] // { arity: 1 }
  2760. TopK limit=1 // { arity: 1 }
  2761. Project (#4) // { arity: 1 }
  2762. Get l9 // { arity: 5 }
  2763. ArrangeBy keys=[[]] // { arity: 1 }
  2764. Union // { arity: 1 }
  2765. Get l15 // { arity: 1 }
  2766. Map (null) // { arity: 1 }
  2767. Union // { arity: 0 }
  2768. Negate // { arity: 0 }
  2769. Project () // { arity: 0 }
  2770. Get l15 // { arity: 1 }
  2771. Constant // { arity: 0 }
  2772. - ()
  2773. Return // { arity: 1 }
  2774. With
  2775. cte l18 =
  2776. Project (#0..=#3) // { arity: 4 }
  2777. Join on=(#4{iter} = #5{max}) type=differential // { arity: 6 }
  2778. implementation
  2779. %1[#0]UK » %0:l17[#4{iter}]K
  2780. ArrangeBy keys=[[#4{iter}]] // { arity: 5 }
  2781. Project (#0..=#3, #5) // { arity: 5 }
  2782. Get l17 // { arity: 6 }
  2783. ArrangeBy keys=[[#0{max}]] // { arity: 1 }
  2784. Reduce aggregates=[max(#0{iter})] // { arity: 1 }
  2785. Project (#5) // { arity: 1 }
  2786. Get l17 // { arity: 6 }
  2787. cte l19 =
  2788. Reduce aggregates=[min(#0{min})] // { arity: 1 }
  2789. Project (#2{min}) // { arity: 1 }
  2790. Reduce group_by=[#0, #2] aggregates=[min((#1{w} + #3{w}))] // { arity: 3 }
  2791. Project (#0, #2, #3, #5) // { arity: 4 }
  2792. Join on=(#1{person2id} = #4{person2id}) type=differential // { arity: 6 }
  2793. implementation
  2794. %0:l18[#1{dst}]Kef » %1:l18[#1{dst}]Kef
  2795. ArrangeBy keys=[[#1{person2id}]] // { arity: 3 }
  2796. Project (#1..=#3) // { arity: 3 }
  2797. Filter (#0{dir} = false) // { arity: 4 }
  2798. Get l18 // { arity: 4 }
  2799. ArrangeBy keys=[[#1{person2id}]] // { arity: 3 }
  2800. Project (#1..=#3) // { arity: 3 }
  2801. Filter (#0{dir} = true) // { arity: 4 }
  2802. Get l18 // { arity: 4 }
  2803. Return // { arity: 1 }
  2804. Project (#1) // { arity: 1 }
  2805. Map (coalesce(#0{min_min}, -1)) // { arity: 2 }
  2806. Union // { arity: 1 }
  2807. Get l19 // { arity: 1 }
  2808. Map (null) // { arity: 1 }
  2809. Union // { arity: 0 }
  2810. Negate // { arity: 0 }
  2811. Project () // { arity: 0 }
  2812. Get l19 // { arity: 1 }
  2813. Constant // { arity: 0 }
  2814. - ()
  2815. Used Indexes:
  2816. - materialize.public.forum_id (*** full scan ***)
  2817. - materialize.public.person_knows_person_person1id (*** full scan ***, delta join 1st input (full scan))
  2818. - materialize.public.person_knows_person_person2id (delta join lookup)
  2819. - materialize.public.message_messageid (*** full scan ***)
  2820. Target cluster: quickstart
  2821. EOF
  2822. ######################################################################
  2823. # QUERY 16
  2824. ######################################################################
  2825. # \set tagA '\'Diosdado_Macapagal\''
  2826. # \set dateA '\'2012-10-07\''::timestamp
  2827. # \set tagB '\'Thailand_Noriega\''
  2828. # \set dateB '\'2012-12-14\''::timestamp
  2829. # \set maxKnowsLimit '5'
  2830. # TODO(mgree) predicate push down anomaly on Tag.name
  2831. query T multiline
  2832. EXPLAIN OPTIMIZED PLAN WITH(humanized expressions, arity, join implementations) AS VERBOSE TEXT FOR WITH
  2833. subgraphA AS (
  2834. SELECT DISTINCT Person.id AS PersonId, Message.MessageId AS MessageId
  2835. FROM Person
  2836. JOIN Message
  2837. ON Message.CreatorPersonId = Person.id
  2838. AND Message.creationDate::date = '2012-10-07'::TIMESTAMP
  2839. JOIN Message_hasTag_Tag
  2840. ON Message_hasTag_Tag.MessageId = Message.MessageId
  2841. JOIN Tag
  2842. ON Tag.id = Message_hasTag_Tag.TagId
  2843. AND Tag.name = 'Diosdado_Macapagal'
  2844. ),
  2845. personA AS (
  2846. SELECT
  2847. subgraphA1.PersonId,
  2848. count(DISTINCT subgraphA1.MessageId) AS cm,
  2849. count(DISTINCT Person_knows_Person.Person2Id) AS cp2
  2850. FROM subgraphA subgraphA1
  2851. LEFT JOIN Person_knows_Person
  2852. ON Person_knows_Person.Person1Id = subgraphA1.PersonId
  2853. AND Person_knows_Person.Person2Id IN (SELECT PersonId FROM subgraphA)
  2854. GROUP BY subgraphA1.PersonId
  2855. HAVING count(DISTINCT Person_knows_Person.Person2Id) <= 5
  2856. ORDER BY subgraphA1.PersonId ASC
  2857. ),
  2858. subgraphB AS (
  2859. SELECT DISTINCT Person.id AS PersonId, Message.MessageId AS MessageId
  2860. FROM Person
  2861. JOIN Message
  2862. ON Message.CreatorPersonId = Person.id
  2863. AND Message.creationDate::date = '2012-12-14'::TIMESTAMP
  2864. JOIN Message_hasTag_Tag
  2865. ON Message_hasTag_Tag.MessageId = Message.MessageId
  2866. JOIN Tag
  2867. ON Tag.id = Message_hasTag_Tag.TagId
  2868. AND Tag.name = 'Thailand_Noriega'
  2869. ),
  2870. personB AS (
  2871. SELECT
  2872. subgraphB1.PersonId,
  2873. count(DISTINCT subgraphB1.MessageId) AS cm,
  2874. count(DISTINCT Person_knows_Person.Person2Id) AS cp2
  2875. FROM subgraphB subgraphB1
  2876. LEFT JOIN Person_knows_Person
  2877. ON Person_knows_Person.Person1Id = subgraphB1.PersonId
  2878. AND Person_knows_Person.Person2Id IN (SELECT PersonId FROM subgraphB)
  2879. GROUP BY subgraphB1.PersonId
  2880. HAVING count(DISTINCT Person_knows_Person.Person2Id) <= 5
  2881. ORDER BY subgraphB1.PersonId ASC
  2882. )
  2883. SELECT
  2884. personA.PersonId AS PersonId,
  2885. personA.cm AS messageCountA,
  2886. personB.cm AS messageCountB
  2887. FROM personA
  2888. JOIN personB
  2889. ON personB.PersonId = personA.PersonId
  2890. ORDER BY personA.cm + personB.cm DESC, PersonId ASC
  2891. LIMIT 20
  2892. ----
  2893. Explained Query:
  2894. Finish order_by=[#6 desc nulls_first, #0{id} asc nulls_last] limit=20 output=[#0, #1, #4]
  2895. With
  2896. cte l0 =
  2897. ArrangeBy keys=[[#0{id}]] // { arity: 1 }
  2898. Distinct project=[#0{id}] // { arity: 1 }
  2899. Project (#1{id}) // { arity: 1 }
  2900. Filter (#1{id}) IS NOT NULL // { arity: 11 }
  2901. ReadIndex on=person person_id=[*** full scan ***] // { arity: 11 }
  2902. cte l1 =
  2903. ArrangeBy keys=[[#2{tagid}]] // { arity: 3 }
  2904. ReadIndex on=message_hastag_tag message_hastag_tag_tagid=[differential join] // { arity: 3 }
  2905. cte l2 =
  2906. ArrangeBy keys=[[#1{name}]] // { arity: 4 }
  2907. ReadIndex on=tag tag_name=[lookup] // { arity: 4 }
  2908. cte l3 =
  2909. Project (#0{id}, #2{messageid}) // { arity: 2 }
  2910. Join on=(#0{id} = #1{creatorpersonid} AND #2{messageid} = #3{messageid}) type=delta // { arity: 4 }
  2911. implementation
  2912. %0:l0 » %1[#0]K » %2[#0]UKA
  2913. %1 » %0:l0[#0]UKA » %2[#0]UKA
  2914. %2 » %1[#1]K » %0:l0[#0]UKA
  2915. Get l0 // { arity: 1 }
  2916. ArrangeBy keys=[[#0{creatorpersonid}], [#1{messageid}]] // { arity: 2 }
  2917. Distinct project=[#1{creatorpersonid}, #0{messageid}] // { arity: 2 }
  2918. Project (#1{messageid}, #9{creatorpersonid}) // { arity: 2 }
  2919. Filter (2012-10-07 00:00:00 = date_to_timestamp(timestamp_with_time_zone_to_date(#0{creationdate}))) // { arity: 13 }
  2920. ReadIndex on=message message_messageid=[*** full scan ***] // { arity: 13 }
  2921. ArrangeBy keys=[[#0{messageid}]] // { arity: 1 }
  2922. Distinct project=[#0{messageid}] // { arity: 1 }
  2923. Project (#1{messageid}) // { arity: 1 }
  2924. Join on=(#2{tagid} = #3{id}) type=differential // { arity: 8 }
  2925. implementation
  2926. %1:tag[#0{id}]KAe » %0:l1[#2{tagid}]KAe
  2927. Get l1 // { arity: 3 }
  2928. ArrangeBy keys=[[#0{id}]] // { arity: 5 }
  2929. ReadIndex on=materialize.public.tag tag_name=[lookup value=("Diosdado_Macapagal")] // { arity: 5 }
  2930. cte l4 =
  2931. ArrangeBy keys=[[#1{person1id}], [#2{person2id}]] // { arity: 3 }
  2932. ReadIndex on=person_knows_person person_knows_person_person1id=[delta join lookup] person_knows_person_person2id=[delta join lookup] // { arity: 3 }
  2933. cte l5 =
  2934. Project (#0{id}, #1{messageid}, #4{person2id}) // { arity: 3 }
  2935. Join on=(#0{id} = #3{person1id} AND #4{person2id} = #5{id}) type=delta // { arity: 6 }
  2936. implementation
  2937. %0:l3 » %1:l4[#1{person1id}]KA » %2[#0]UKA
  2938. %1:l4 » %2[#0]UKA » %0:l3[#0]K
  2939. %2 » %1:l4[#2{person2id}]KA » %0:l3[#0]K
  2940. ArrangeBy keys=[[#0{id}]] // { arity: 2 }
  2941. Get l3 // { arity: 2 }
  2942. Get l4 // { arity: 3 }
  2943. ArrangeBy keys=[[#0{id}]] // { arity: 1 }
  2944. Distinct project=[#0{id}] // { arity: 1 }
  2945. Project (#0{id}) // { arity: 1 }
  2946. Get l3 // { arity: 2 }
  2947. cte l6 =
  2948. Project (#0{id}, #2{messageid}) // { arity: 2 }
  2949. Join on=(#0{id} = #1{creatorpersonid} AND #2{messageid} = #3{messageid}) type=delta // { arity: 4 }
  2950. implementation
  2951. %0:l0 » %1[#0]K » %2[#0]UKA
  2952. %1 » %0:l0[#0]UKA » %2[#0]UKA
  2953. %2 » %1[#1]K » %0:l0[#0]UKA
  2954. Get l0 // { arity: 1 }
  2955. ArrangeBy keys=[[#0{creatorpersonid}], [#1{messageid}]] // { arity: 2 }
  2956. Distinct project=[#1{creatorpersonid}, #0{messageid}] // { arity: 2 }
  2957. Project (#1{messageid}, #9{creatorpersonid}) // { arity: 2 }
  2958. Filter (2012-12-14 00:00:00 = date_to_timestamp(timestamp_with_time_zone_to_date(#0{creationdate}))) // { arity: 13 }
  2959. ReadIndex on=message message_messageid=[*** full scan ***] // { arity: 13 }
  2960. ArrangeBy keys=[[#0{messageid}]] // { arity: 1 }
  2961. Distinct project=[#0{messageid}] // { arity: 1 }
  2962. Project (#1{messageid}) // { arity: 1 }
  2963. Join on=(#2{tagid} = #3{id}) type=differential // { arity: 8 }
  2964. implementation
  2965. %1:tag[#0{id}]KAe » %0:l1[#2{tagid}]KAe
  2966. Get l1 // { arity: 3 }
  2967. ArrangeBy keys=[[#0{id}]] // { arity: 5 }
  2968. ReadIndex on=materialize.public.tag tag_name=[lookup value=("Thailand_Noriega")] // { arity: 5 }
  2969. cte l7 =
  2970. Project (#0{id}, #1{messageid}, #4{person2id}) // { arity: 3 }
  2971. Join on=(#0{id} = #3{person1id} AND #4{person2id} = #5{id}) type=delta // { arity: 6 }
  2972. implementation
  2973. %0:l6 » %1:l4[#1{person1id}]KA » %2[#0]UKA
  2974. %1:l4 » %2[#0]UKA » %0:l6[#0]K
  2975. %2 » %1:l4[#2{person2id}]KA » %0:l6[#0]K
  2976. ArrangeBy keys=[[#0{id}]] // { arity: 2 }
  2977. Get l6 // { arity: 2 }
  2978. Get l4 // { arity: 3 }
  2979. ArrangeBy keys=[[#0{id}]] // { arity: 1 }
  2980. Distinct project=[#0{id}] // { arity: 1 }
  2981. Project (#0{id}) // { arity: 1 }
  2982. Get l6 // { arity: 2 }
  2983. Return // { arity: 7 }
  2984. Project (#0{id}..=#2{count_person2id}, #0{id}, #4{count_messageid}..=#6) // { arity: 7 }
  2985. Filter (#2{count_person2id} <= 5) AND (#5{count_person2id} <= 5) // { arity: 7 }
  2986. Map ((#1{count_messageid} + #4{count_messageid})) // { arity: 7 }
  2987. Join on=(#0{id} = #3{id}) type=differential // { arity: 6 }
  2988. implementation
  2989. %0[#0{personid}]UKAif » %1[#0]UKAiif
  2990. ArrangeBy keys=[[#0{id}]] // { arity: 3 }
  2991. Reduce group_by=[#0{id}] aggregates=[count(distinct #1{messageid}), count(distinct #2{person2id})] // { arity: 3 }
  2992. Union // { arity: 3 }
  2993. Get l5 // { arity: 3 }
  2994. Map (null) // { arity: 3 }
  2995. Union // { arity: 2 }
  2996. Negate // { arity: 2 }
  2997. Distinct project=[#0{id}, #1{messageid}] // { arity: 2 }
  2998. Project (#0{id}, #1{messageid}) // { arity: 2 }
  2999. Get l5 // { arity: 3 }
  3000. Get l3 // { arity: 2 }
  3001. ArrangeBy keys=[[#0{id}]] // { arity: 3 }
  3002. Reduce group_by=[#0{id}] aggregates=[count(distinct #1{messageid}), count(distinct #2{person2id})] // { arity: 3 }
  3003. Union // { arity: 3 }
  3004. Get l7 // { arity: 3 }
  3005. Map (null) // { arity: 3 }
  3006. Union // { arity: 2 }
  3007. Negate // { arity: 2 }
  3008. Distinct project=[#0{id}, #1{messageid}] // { arity: 2 }
  3009. Project (#0{id}, #1{messageid}) // { arity: 2 }
  3010. Get l7 // { arity: 3 }
  3011. Get l6 // { arity: 2 }
  3012. Used Indexes:
  3013. - materialize.public.tag_name (lookup)
  3014. - materialize.public.person_id (*** full scan ***)
  3015. - materialize.public.person_knows_person_person1id (delta join lookup)
  3016. - materialize.public.person_knows_person_person2id (delta join lookup)
  3017. - materialize.public.message_hastag_tag_tagid (differential join)
  3018. - materialize.public.message_messageid (*** full scan ***)
  3019. Target cluster: quickstart
  3020. EOF
  3021. ######################################################################
  3022. # QUERY 17
  3023. ######################################################################
  3024. # \set tag '\'Cosmic_Egg\''
  3025. # \set delta '12'
  3026. query T multiline
  3027. EXPLAIN OPTIMIZED PLAN WITH(humanized expressions, arity, join implementations) AS VERBOSE TEXT FOR WITH MyMessage as (
  3028. SELECT *
  3029. FROM Message
  3030. -- (tag)<-[:HAS_TAG]-(message)
  3031. WHERE MessageId in (SELECT MessageId FROM Message_hasTag_Tag WHERE TagId IN (SELECT id FROM Tag WHERE Tag.name = 'Cosmic_Egg'))
  3032. )
  3033. -- (message1)-[:HAS_CREATOR]->(person1)
  3034. SELECT Message1.CreatorPersonId AS "person1.id", count(DISTINCT Message2.MessageId) AS messageCount
  3035. FROM MyMessage Message1
  3036. -- (message2 <date filtering>})
  3037. JOIN MyMessage Message2
  3038. ON (Message1.creationDate + (12 || ' hour')::interval) < Message2.creationDate
  3039. JOIN MyMessage Comment
  3040. ON Comment.ParentMessageId = Message2.MessageId
  3041. -- (forum1)-[:Has_MEMBER]->(person2)
  3042. JOIN Forum_hasMember_Person Forum_hasMember_Person2
  3043. ON Forum_hasMember_Person2.ForumId = Message1.ContainerForumId -- forum1
  3044. AND Forum_hasMember_Person2.PersonId = Comment.CreatorPersonId -- person2
  3045. -- (forum1)-[:Has_MEMBER]->(person3)
  3046. JOIN Forum_hasMember_Person Forum_hasMember_Person3
  3047. ON Forum_hasMember_Person3.ForumId = Message1.ContainerForumId -- forum1
  3048. AND Forum_hasMember_Person3.PersonId = Message2.CreatorPersonId -- person3
  3049. WHERE Message1.ContainerForumId <> Message2.ContainerForumId
  3050. -- person2 <> person3
  3051. AND Forum_hasMember_Person2.PersonId <> Forum_hasMember_Person3.PersonId
  3052. -- NOT (forum2)-[:HAS_MEMBER]->(person1)
  3053. AND NOT EXISTS (SELECT 1
  3054. FROM Forum_hasMember_Person Forum_hasMember_Person1
  3055. WHERE Forum_hasMember_Person1.ForumId = Message2.ContainerForumId -- forum2
  3056. AND Forum_hasMember_Person1.PersonId = Message1.CreatorPersonId -- person1
  3057. )
  3058. GROUP BY Message1.CreatorPersonId
  3059. ORDER BY messageCount DESC, Message1.CreatorPersonId ASC
  3060. LIMIT 10
  3061. ----
  3062. Explained Query:
  3063. Finish order_by=[#1{count_messageid} desc nulls_first, #0{creatorpersonid} asc nulls_last] limit=10 output=[#0, #1]
  3064. With
  3065. cte l0 =
  3066. Project (#0{creationdate}, #1{messageid}, #9{creatorpersonid}, #10{containerforumid}, #12{parentmessageid}) // { arity: 5 }
  3067. Join on=(#1{messageid} = #13{messageid}) type=differential // { arity: 14 }
  3068. implementation
  3069. %1[#0]UKA » %0:message[#1{messageid}]KA
  3070. ArrangeBy keys=[[#1{messageid}]] // { arity: 13 }
  3071. ReadIndex on=message message_messageid=[differential join] // { arity: 13 }
  3072. ArrangeBy keys=[[#0{messageid}]] // { arity: 1 }
  3073. Distinct project=[#0{messageid}] // { arity: 1 }
  3074. Project (#1{messageid}) // { arity: 1 }
  3075. Join on=(#2{tagid} = #3{id}) type=differential // { arity: 4 }
  3076. implementation
  3077. %1[#0]UKA » %0:message_hastag_tag[#2{tagid}]KA
  3078. ArrangeBy keys=[[#2{tagid}]] // { arity: 3 }
  3079. ReadIndex on=message_hastag_tag message_hastag_tag_tagid=[differential join] // { arity: 3 }
  3080. ArrangeBy keys=[[#0{id}]] // { arity: 1 }
  3081. Distinct project=[#0{id}] // { arity: 1 }
  3082. Project (#0{id}) // { arity: 1 }
  3083. Filter (#0{id}) IS NOT NULL // { arity: 5 }
  3084. ReadIndex on=materialize.public.tag tag_name=[lookup value=("Cosmic_Egg")] // { arity: 5 }
  3085. cte l1 =
  3086. ArrangeBy keys=[[#1{forumid}], [#2{personid}]] // { arity: 3 }
  3087. ReadIndex on=forum_hasmember_person forum_hasmember_person_forumid=[delta join lookup] forum_hasmember_person_personid=[delta join lookup] // { arity: 3 }
  3088. cte l2 =
  3089. Project (#1{creatorpersonid}, #4{messageid}, #6{containerforumid}) // { arity: 3 }
  3090. Filter (#2{containerforumid} != #6{containerforumid}) AND (#5{creatorpersonid} != #7{creatorpersonid}) AND ((#0{creationdate} + 12:00:00) < #3{creationdate}) // { arity: 15 }
  3091. Join on=(#2{containerforumid} = #10{forumid} = #13{forumid} AND #4{messageid} = #8{parentmessageid} AND #5{creatorpersonid} = #14{personid} AND #7{creatorpersonid} = #11{personid}) type=delta // { arity: 15 }
  3092. implementation
  3093. %0:l0 » %3:l1[#1{forumid}]KA » %4:l1[#1{forumid}]KA » %1:l0[#2{creatorpersonid}]K » %2:l0[#0{creatorpersonid}, #1{parentmessageid}]KK
  3094. %1:l0 » %4:l1[#2{personid}]KA » %3:l1[#1{forumid}]KA » %2:l0[#0{creatorpersonid}, #1{parentmessageid}]KK » %0:l0[#2{containerforumid}]K
  3095. %2:l0 » %3:l1[#2{personid}]KA » %4:l1[#1{forumid}]KA » %1:l0[#1{messageid}, #2{creatorpersonid}]KK » %0:l0[#2{containerforumid}]K
  3096. %3:l1 » %4:l1[#1{forumid}]KA » %0:l0[#2{containerforumid}]K » %1:l0[#2{creatorpersonid}]K » %2:l0[#0{creatorpersonid}, #1{parentmessageid}]KK
  3097. %4:l1 » %3:l1[#1{forumid}]KA » %0:l0[#2{containerforumid}]K » %1:l0[#2{creatorpersonid}]K » %2:l0[#0{creatorpersonid}, #1{parentmessageid}]KK
  3098. ArrangeBy keys=[[#2{containerforumid}]] // { arity: 3 }
  3099. Project (#0{creationdate}, #2{creatorpersonid}, #3{containerforumid}) // { arity: 3 }
  3100. Get l0 // { arity: 5 }
  3101. ArrangeBy keys=[[#1{messageid}, #2{creatorpersonid}], [#2{creatorpersonid}]] // { arity: 4 }
  3102. Project (#0{creationdate}..=#3{containerforumid}) // { arity: 4 }
  3103. Get l0 // { arity: 5 }
  3104. ArrangeBy keys=[[#0{creatorpersonid}, #1{parentmessageid}]] // { arity: 2 }
  3105. Project (#2{creatorpersonid}, #4{parentmessageid}) // { arity: 2 }
  3106. Filter (#4{parentmessageid}) IS NOT NULL // { arity: 5 }
  3107. Get l0 // { arity: 5 }
  3108. Get l1 // { arity: 3 }
  3109. Get l1 // { arity: 3 }
  3110. cte l3 =
  3111. Distinct project=[#0{creatorpersonid}, #1{containerforumid}] // { arity: 2 }
  3112. Project (#0{creatorpersonid}, #2{containerforumid}) // { arity: 2 }
  3113. Get l2 // { arity: 3 }
  3114. Return // { arity: 2 }
  3115. Reduce group_by=[#0{creatorpersonid}] aggregates=[count(distinct #1{messageid})] // { arity: 2 }
  3116. Project (#0{creatorpersonid}, #1{messageid}) // { arity: 2 }
  3117. Join on=(#0{creatorpersonid} = #3{creatorpersonid} AND #2{containerforumid} = #4{containerforumid}) type=differential // { arity: 5 }
  3118. implementation
  3119. %0:l2[#0, #2]KK » %1[#0, #1]KK
  3120. ArrangeBy keys=[[#0{creatorpersonid}, #2{containerforumid}]] // { arity: 3 }
  3121. Get l2 // { arity: 3 }
  3122. ArrangeBy keys=[[#0{creatorpersonid}, #1{containerforumid}]] // { arity: 2 }
  3123. Union // { arity: 2 }
  3124. Negate // { arity: 2 }
  3125. Project (#0{creatorpersonid}, #1{containerforumid}) // { arity: 2 }
  3126. Join on=(#0{creatorpersonid} = #2{personid} AND #1{containerforumid} = #3{forumid}) type=differential // { arity: 4 }
  3127. implementation
  3128. %0:l3[#0, #1]UKKA » %1[#0, #1]UKKA
  3129. ArrangeBy keys=[[#0{creatorpersonid}, #1{containerforumid}]] // { arity: 2 }
  3130. Get l3 // { arity: 2 }
  3131. ArrangeBy keys=[[#0{personid}, #1{forumid}]] // { arity: 2 }
  3132. Distinct project=[#1{personid}, #0{forumid}] // { arity: 2 }
  3133. Project (#1{forumid}, #2{personid}) // { arity: 2 }
  3134. ReadIndex on=forum_hasmember_person forum_hasmember_person_forumid=[*** full scan ***] // { arity: 3 }
  3135. Get l3 // { arity: 2 }
  3136. Used Indexes:
  3137. - materialize.public.tag_name (lookup)
  3138. - materialize.public.forum_hasmember_person_forumid (*** full scan ***, delta join lookup)
  3139. - materialize.public.forum_hasmember_person_personid (delta join lookup)
  3140. - materialize.public.message_hastag_tag_tagid (differential join)
  3141. - materialize.public.message_messageid (differential join)
  3142. Target cluster: quickstart
  3143. EOF
  3144. ######################################################################
  3145. # QUERY 18
  3146. ######################################################################
  3147. # \set tag '\'Fyodor_Dostoyevsky\''
  3148. query T multiline
  3149. EXPLAIN OPTIMIZED PLAN WITH(humanized expressions, arity, join implementations) AS VERBOSE TEXT FOR WITH
  3150. PersonWithInterest AS (
  3151. SELECT pt.PersonId AS PersonId
  3152. FROM Person_hasInterest_Tag pt, Tag t
  3153. WHERE t.name = 'Fyodor_Dostoyevsky' AND pt.TagId = t.id
  3154. ),
  3155. FriendsOfInterested AS (
  3156. SELECT k.Person1Id AS InterestedId, k.Person2Id AS FriendId
  3157. FROM PersonWithInterest p, Person_knows_Person k
  3158. WHERE p.PersonId = k.Person1Id
  3159. )
  3160. SELECT k1.InterestedId AS "person1.id", k2.InterestedId AS "person2.id", count(k1.FriendId) AS mutualFriendCount
  3161. FROM FriendsOfInterested k1
  3162. JOIN FriendsOfInterested k2
  3163. ON k1.FriendId = k2.FriendId -- pattern: mutualFriend
  3164. -- negative edge
  3165. WHERE k1.InterestedId != k2.InterestedId
  3166. AND NOT EXISTS (SELECT 1
  3167. FROM Person_knows_Person k3
  3168. WHERE k3.Person1Id = k2.InterestedId -- pattern: person2
  3169. AND k3.Person2Id = k1.InterestedId -- pattern: person1
  3170. )
  3171. GROUP BY k1.InterestedId, k2.InterestedId
  3172. ORDER BY mutualFriendCount DESC, k1.InterestedId ASC, k2.InterestedId ASC
  3173. LIMIT 20
  3174. ----
  3175. Explained Query:
  3176. Finish order_by=[#2{count} desc nulls_first, #0{personid} asc nulls_last, #1{personid} asc nulls_last] limit=20 output=[#0..=#2]
  3177. With
  3178. cte l0 =
  3179. ArrangeBy keys=[[#1{person2id}]] // { arity: 2 }
  3180. Project (#0{personid}, #9{person2id}) // { arity: 2 }
  3181. Join on=(#0{personid} = #8{person1id} AND #1{tagid} = #2{id}) type=delta // { arity: 10 }
  3182. implementation
  3183. %0:person_hasinterest_tag » %1:tag[#0{id}]KAe » %2:person_knows_person[#1{person1id}]KA
  3184. %1:tag » %0:person_hasinterest_tag[#1{tagid}]KA » %2:person_knows_person[#1{person1id}]KA
  3185. %2:person_knows_person » %0:person_hasinterest_tag[#0{personid}]K » %1:tag[#0{id}]KAe
  3186. ArrangeBy keys=[[#0{personid}], [#1{tagid}]] // { arity: 2 }
  3187. Project (#1{personid}, #2{tagid}) // { arity: 2 }
  3188. ReadIndex on=person_hasinterest_tag person_hasinterest_tag_tagid=[*** full scan ***] // { arity: 3 }
  3189. ArrangeBy keys=[[#0{id}]] // { arity: 5 }
  3190. ReadIndex on=materialize.public.tag tag_name=[lookup value=("Fyodor_Dostoyevsky")] // { arity: 5 }
  3191. ArrangeBy keys=[[#1{person1id}]] // { arity: 3 }
  3192. ReadIndex on=person_knows_person person_knows_person_person1id=[delta join lookup] // { arity: 3 }
  3193. cte l1 =
  3194. Project (#0{personid}, #2{personid}) // { arity: 2 }
  3195. Filter (#0{personid} != #2{personid}) // { arity: 4 }
  3196. Join on=(#1{person2id} = #3{person2id}) type=differential // { arity: 4 }
  3197. implementation
  3198. %0:l0[#1{friendid}]K » %1:l0[#1{friendid}]K
  3199. Get l0 // { arity: 2 }
  3200. Get l0 // { arity: 2 }
  3201. cte l2 =
  3202. Distinct project=[#0{personid}, #1{personid}] // { arity: 2 }
  3203. Get l1 // { arity: 2 }
  3204. Return // { arity: 3 }
  3205. Reduce group_by=[#0{personid}, #1{personid}] aggregates=[count(*)] // { arity: 3 }
  3206. Project (#0{personid}, #1{personid}) // { arity: 2 }
  3207. Join on=(#0{personid} = #2{personid} AND #1{personid} = #3{personid}) type=differential // { arity: 4 }
  3208. implementation
  3209. %0:l1[#0, #1]KK » %1[#0, #1]KK
  3210. ArrangeBy keys=[[#0{personid}, #1{personid}]] // { arity: 2 }
  3211. Get l1 // { arity: 2 }
  3212. ArrangeBy keys=[[#0{personid}, #1{personid}]] // { arity: 2 }
  3213. Union // { arity: 2 }
  3214. Negate // { arity: 2 }
  3215. Project (#0{personid}, #1{personid}) // { arity: 2 }
  3216. Join on=(#0{personid} = #2{person2id} AND #1{personid} = #3{person1id}) type=differential // { arity: 4 }
  3217. implementation
  3218. %0:l2[#0, #1]UKKA » %1[#0, #1]UKKA
  3219. ArrangeBy keys=[[#0{personid}, #1{personid}]] // { arity: 2 }
  3220. Get l2 // { arity: 2 }
  3221. ArrangeBy keys=[[#0{person2id}, #1{person1id}]] // { arity: 2 }
  3222. Distinct project=[#1{person2id}, #0{person1id}] // { arity: 2 }
  3223. Project (#1{person1id}, #2{person2id}) // { arity: 2 }
  3224. ReadIndex on=person_knows_person person_knows_person_person1id=[*** full scan ***] // { arity: 3 }
  3225. Get l2 // { arity: 2 }
  3226. Used Indexes:
  3227. - materialize.public.tag_name (lookup)
  3228. - materialize.public.person_hasinterest_tag_tagid (*** full scan ***)
  3229. - materialize.public.person_knows_person_person1id (*** full scan ***, delta join lookup)
  3230. Target cluster: quickstart
  3231. EOF
  3232. ######################################################################
  3233. # QUERY 19
  3234. ######################################################################
  3235. # \set city1Id 655::bigint
  3236. # \set city2Id 1138::bigint
  3237. query T multiline
  3238. EXPLAIN OPTIMIZED PLAN WITH(humanized expressions, arity, join implementations) AS VERBOSE TEXT FOR CREATE OR REPLACE MATERIALIZED VIEW PathQ19 AS
  3239. WITH
  3240. -- asymmetrize...
  3241. knows_asymmetric AS (
  3242. SELECT person1id, person2id
  3243. FROM Person_knows_person
  3244. WHERE person1id < person2id
  3245. ),
  3246. -- compute interaction scores (no interactions means we ignore that 'knows' relationship)
  3247. weights(src, dst, w) AS (
  3248. SELECT
  3249. person1id AS src,
  3250. person2id AS dst,
  3251. greatest(round(40 - sqrt(count(*)))::bigint, 1) AS w
  3252. FROM Message m1,
  3253. Message m2,
  3254. knows_asymmetric pp
  3255. WHERE pp.person1id = least(m1.creatorpersonid, m2.creatorpersonid)
  3256. AND pp.person2id = greatest(m1.creatorpersonid, m2.creatorpersonid)
  3257. AND m1.parentmessageid = m2.messageid
  3258. AND m1.creatorpersonid <> m2.creatorpersonid
  3259. GROUP BY src, dst
  3260. )
  3261. -- resymmetrize
  3262. SELECT src, dst, w FROM weights
  3263. UNION ALL
  3264. SELECT dst, src, w FROM weights;
  3265. ----
  3266. materialize.public.pathq19:
  3267. With
  3268. cte l0 =
  3269. Project (#0{person1id}, #1{person2id}, #3) // { arity: 3 }
  3270. Map (greatest(f64toi64(roundf64((40 - sqrtf64(bigint_to_double(#2{count}))))), 1)) // { arity: 4 }
  3271. Reduce group_by=[#0{person1id}, #1{person2id}] aggregates=[count(*)] // { arity: 3 }
  3272. Project (#16{person1id}, #17{person2id}) // { arity: 2 }
  3273. Filter (#0{creatorpersonid} != #11{creatorpersonid}) AND (#16{person1id} < #17{person2id}) // { arity: 18 }
  3274. Join on=(#1{parentmessageid} = #3{messageid} AND #16{person1id} = least(#0{creatorpersonid}, #11{creatorpersonid}) AND #17{person2id} = greatest(#0{creatorpersonid}, #11{creatorpersonid})) type=delta // { arity: 18 }
  3275. implementation
  3276. %0:message » %1:message[#1{messageid}]KA » %2:person_knows_person[#1{person1id}, #2{person2id}]KKAf
  3277. %1:message » %0:message[#1{parentmessageid}]KA » %2:person_knows_person[#1{person1id}, #2{person2id}]KKAf
  3278. %2:person_knows_person » %0:message[×] » %1:message[#1{messageid}]KA
  3279. ArrangeBy keys=[[], [#1{parentmessageid}]] // { arity: 2 }
  3280. Project (#9{creatorpersonid}, #12{parentmessageid}) // { arity: 2 }
  3281. Filter (#12{parentmessageid}) IS NOT NULL // { arity: 13 }
  3282. ReadIndex on=message message_messageid=[*** full scan ***] // { arity: 13 }
  3283. ArrangeBy keys=[[#1{messageid}]] // { arity: 13 }
  3284. ReadIndex on=message message_messageid=[delta join lookup] // { arity: 13 }
  3285. ArrangeBy keys=[[#1{person1id}, #2{person2id}]] // { arity: 3 }
  3286. ReadIndex on=person_knows_person person_knows_person_person1id_person2id=[delta join lookup] // { arity: 3 }
  3287. Return // { arity: 3 }
  3288. Union // { arity: 3 }
  3289. Get l0 // { arity: 3 }
  3290. Project (#1{person2id}, #0{person1id}, #2) // { arity: 3 }
  3291. Get l0 // { arity: 3 }
  3292. Used Indexes:
  3293. - materialize.public.person_knows_person_person1id_person2id (delta join lookup)
  3294. - materialize.public.message_messageid (*** full scan ***, delta join lookup)
  3295. Target cluster: quickstart
  3296. EOF
  3297. statement ok
  3298. CREATE OR REPLACE MATERIALIZED VIEW PathQ19 AS
  3299. WITH
  3300. -- asymmetrize...
  3301. knows_asymmetric AS (
  3302. SELECT person1id, person2id
  3303. FROM Person_knows_person
  3304. WHERE person1id < person2id
  3305. ),
  3306. -- compute interaction scores (no interactions means we ignore that 'knows' relationship)
  3307. weights(src, dst, w) AS (
  3308. SELECT
  3309. person1id AS src,
  3310. person2id AS dst,
  3311. greatest(round(40 - sqrt(count(*)))::bigint, 1) AS w
  3312. FROM Message m1,
  3313. Message m2,
  3314. knows_asymmetric pp
  3315. WHERE pp.person1id = least(m1.creatorpersonid, m2.creatorpersonid)
  3316. AND pp.person2id = greatest(m1.creatorpersonid, m2.creatorpersonid)
  3317. AND m1.parentmessageid = m2.messageid
  3318. AND m1.creatorpersonid <> m2.creatorpersonid
  3319. GROUP BY src, dst
  3320. )
  3321. -- resymmetrize
  3322. SELECT src, dst, w FROM weights
  3323. UNION ALL
  3324. SELECT dst, src, w FROM weights;
  3325. statement ok
  3326. CREATE INDEX PathQ19_src ON PathQ19 (src);
  3327. query T multiline
  3328. EXPLAIN OPTIMIZED PLAN WITH(humanized expressions, arity, join implementations) AS VERBOSE TEXT FOR WITH
  3329. srcs AS (SELECT id FROM Person WHERE locationcityid = 655::bigint),
  3330. dsts AS (SELECT id FROM Person WHERE locationcityid = 1138::bigint),
  3331. completed_paths AS (
  3332. WITH MUTUALLY RECURSIVE
  3333. paths (src bigint, dst bigint, w double precision) AS (
  3334. SELECT id AS src,
  3335. id AS dst,
  3336. 0::double precision AS w
  3337. FROM srcs
  3338. UNION
  3339. SELECT paths1.src AS src,
  3340. paths2.dst AS dst,
  3341. paths1.w + paths2.w AS w
  3342. FROM minimal_paths paths1
  3343. JOIN PathQ19 paths2 -- step-transitive closure
  3344. ON paths1.dst = paths2.src
  3345. ),
  3346. minimal_paths (src bigint, dst bigint, w double precision) AS (
  3347. SELECT src, dst, min(w)
  3348. FROM paths
  3349. GROUP BY src, dst
  3350. )
  3351. SELECT src, dst, w
  3352. FROM minimal_paths
  3353. WHERE dst = ANY (SELECT id FROM dsts)
  3354. )
  3355. SELECT src, dst, w
  3356. FROM completed_paths
  3357. WHERE w = (SELECT min(w) FROM completed_paths)
  3358. ----
  3359. Explained Query:
  3360. With Mutually Recursive
  3361. cte l0 =
  3362. Reduce group_by=[#0{id}, #1{id}] aggregates=[min(#2{w})] // { arity: 3 }
  3363. Distinct project=[#0{id}..=#2] // { arity: 3 }
  3364. Union // { arity: 3 }
  3365. Project (#1{id}, #1{id}, #12) // { arity: 3 }
  3366. Map (0) // { arity: 13 }
  3367. ReadIndex on=materialize.public.person person_locationcityid=[lookup value=(655)] // { arity: 12 }
  3368. Project (#0, #4{dst}, #6) // { arity: 3 }
  3369. Map ((#2{w} + bigint_to_double(#5{w}))) // { arity: 7 }
  3370. Join on=(#1{dst} = #3{src}) type=differential // { arity: 6 }
  3371. implementation
  3372. %1:pathq19[#0{src}]KA » %0:l0[#1{dst}]K
  3373. ArrangeBy keys=[[#1{id}]] // { arity: 3 }
  3374. Filter (#1{id}) IS NOT NULL // { arity: 3 }
  3375. Get l0 // { arity: 3 }
  3376. ArrangeBy keys=[[#0{src}]] // { arity: 3 }
  3377. ReadIndex on=pathq19 pathq19_src=[differential join] // { arity: 3 }
  3378. Return // { arity: 3 }
  3379. With
  3380. cte l1 =
  3381. Project (#0{id}..=#2{min}) // { arity: 3 }
  3382. Join on=(#1{id} = #3{id}) type=differential // { arity: 4 }
  3383. implementation
  3384. %1[#0]UKA » %0:l0[#1]K
  3385. ArrangeBy keys=[[#1{id}]] // { arity: 3 }
  3386. Filter (#1{id}) IS NOT NULL // { arity: 3 }
  3387. Get l0 // { arity: 3 }
  3388. ArrangeBy keys=[[#0{id}]] // { arity: 1 }
  3389. Distinct project=[#0{id}] // { arity: 1 }
  3390. Project (#1{id}) // { arity: 1 }
  3391. Filter (#1{id}) IS NOT NULL // { arity: 12 }
  3392. ReadIndex on=materialize.public.person person_locationcityid=[lookup value=(1138)] // { arity: 12 }
  3393. Return // { arity: 3 }
  3394. Project (#0{id}..=#2{min}) // { arity: 3 }
  3395. Join on=(#2{min} = #3{min_min}) type=differential // { arity: 4 }
  3396. implementation
  3397. %1[#0]UK » %0:l1[#2{w}]K
  3398. ArrangeBy keys=[[#2{min}]] // { arity: 3 }
  3399. Get l1 // { arity: 3 }
  3400. ArrangeBy keys=[[#0{min_min}]] // { arity: 1 }
  3401. Reduce aggregates=[min(#0{min})] // { arity: 1 }
  3402. Project (#2{min}) // { arity: 1 }
  3403. Get l1 // { arity: 3 }
  3404. Used Indexes:
  3405. - materialize.public.person_locationcityid (lookup)
  3406. - materialize.public.pathq19_src (differential join)
  3407. Target cluster: quickstart
  3408. EOF
  3409. # q19 (frank's version)
  3410. query T multiline
  3411. EXPLAIN OPTIMIZED PLAN WITH(humanized expressions, arity, join implementations) AS VERBOSE TEXT FOR WITH MUTUALLY RECURSIVE
  3412. -- Source and destination identifiers, which do not evolve recursively.
  3413. srcs (f bigint) AS (SELECT id FROM Person WHERE locationcityid = 655::bigint),
  3414. dsts (t bigint) AS (SELECT id FROM Person WHERE locationcityid = 1138::bigint),
  3415. -- Only work off of nodes not more than half a viable path.
  3416. active_forward(src bigint, dst bigint, w double precision) AS (
  3417. SELECT * FROM forward WHERE coalesce (w < (SELECT w/2 FROM shortest), true)
  3418. ),
  3419. forward (src bigint, dst bigint, w double precision) AS (
  3420. SELECT DISTINCT ON (src, dst) src, dst, w
  3421. FROM (
  3422. SELECT f as src, f as dst, 0.0 as w FROM srcs
  3423. UNION ALL
  3424. SELECT f.src, p.dst, f.w + p.w
  3425. FROM active_forward f, PathQ19 p
  3426. WHERE f.dst = p.src
  3427. )
  3428. ORDER BY src, dst, w
  3429. ),
  3430. -- Only work off of nodes not more than half a viable path.
  3431. active_reverse(src bigint, dst bigint, w double precision) AS (
  3432. SELECT * FROM reverse WHERE coalesce (w < (SELECT w/2 FROM shortest), true)
  3433. ),
  3434. reverse (src bigint, dst bigint, w double precision) AS (
  3435. SELECT DISTINCT ON (src, dst) src, dst, w
  3436. FROM (
  3437. SELECT t as src, t as dst, 0.0 as w FROM dsts
  3438. UNION ALL
  3439. SELECT r.src, p.dst, r.w + p.w
  3440. FROM active_reverse r, PathQ19 p
  3441. WHERE r.dst = p.src
  3442. )
  3443. ORDER BY src, dst, w
  3444. ),
  3445. -- Once we find a path from `f` to `t` it appears here.
  3446. paths (f bigint, t bigint, w double precision) AS (
  3447. SELECT l.src as f, r.src as t, min(l.w + r.w) AS w
  3448. FROM forward l, reverse r
  3449. WHERE l.dst = r.dst
  3450. GROUP BY l.src, r.src
  3451. ),
  3452. shortest (w double precision) AS (
  3453. SELECT min(w) FROM paths
  3454. )
  3455. SELECT *
  3456. FROM paths
  3457. WHERE w = (SELECT MIN(w) FROM paths)
  3458. ----
  3459. Explained Query:
  3460. With
  3461. cte l0 =
  3462. ArrangeBy keys=[[#8{locationcityid}]] // { arity: 11 }
  3463. ReadIndex on=person person_locationcityid=[lookup] // { arity: 11 }
  3464. cte l1 =
  3465. ArrangeBy keys=[[#0{src}]] // { arity: 3 }
  3466. ReadIndex on=pathq19 pathq19_src=[delta join lookup] // { arity: 3 }
  3467. Return // { arity: 3 }
  3468. With Mutually Recursive
  3469. cte l2 =
  3470. ArrangeBy keys=[[]] // { arity: 1 }
  3471. Union // { arity: 1 }
  3472. Project (#1) // { arity: 1 }
  3473. Map ((#0{w} / 2)) // { arity: 2 }
  3474. Get l7 // { arity: 1 }
  3475. Map (null) // { arity: 1 }
  3476. Union // { arity: 0 }
  3477. Negate // { arity: 0 }
  3478. Project () // { arity: 0 }
  3479. Get l7 // { arity: 1 }
  3480. Constant // { arity: 0 }
  3481. - ()
  3482. cte l3 =
  3483. TopK group_by=[#0{id}, #1{id}] order_by=[#2 asc nulls_last] limit=1 // { arity: 3 }
  3484. Union // { arity: 3 }
  3485. Project (#1{id}, #1{id}, #12) // { arity: 3 }
  3486. Map (0) // { arity: 13 }
  3487. ReadIndex on=materialize.public.person person_locationcityid=[lookup value=(655)] // { arity: 12 }
  3488. Project (#0, #5{dst}, #7) // { arity: 3 }
  3489. Filter coalesce((#2{w} < #3), true) // { arity: 8 }
  3490. Map ((#2{w} + bigint_to_double(#6{w}))) // { arity: 8 }
  3491. Join on=(#1{dst} = #4{src}) type=delta // { arity: 7 }
  3492. implementation
  3493. %0:l3 » %2:l1[#0{src}]KA » %1:l2[×]
  3494. %1:l2 » %0:l3[×] » %2:l1[#0{src}]KA
  3495. %2:l1 » %0:l3[#1{dst}]K » %1:l2[×]
  3496. ArrangeBy keys=[[], [#1{id}]] // { arity: 3 }
  3497. Filter (#1{id}) IS NOT NULL // { arity: 3 }
  3498. Get l3 // { arity: 3 }
  3499. Get l2 // { arity: 1 }
  3500. Get l1 // { arity: 3 }
  3501. cte l4 =
  3502. TopK group_by=[#0{id}, #1{id}] order_by=[#2 asc nulls_last] limit=1 // { arity: 3 }
  3503. Union // { arity: 3 }
  3504. Project (#1{id}, #1{id}, #12) // { arity: 3 }
  3505. Map (0) // { arity: 13 }
  3506. ReadIndex on=materialize.public.person person_locationcityid=[lookup value=(1138)] // { arity: 12 }
  3507. Project (#0, #5{dst}, #7) // { arity: 3 }
  3508. Filter coalesce((#2{w} < #3), true) // { arity: 8 }
  3509. Map ((#2{w} + bigint_to_double(#6{w}))) // { arity: 8 }
  3510. Join on=(#1{dst} = #4{src}) type=delta // { arity: 7 }
  3511. implementation
  3512. %0:l4 » %2:l1[#0{src}]KA » %1:l2[×]
  3513. %1:l2 » %0:l4[×] » %2:l1[#0{src}]KA
  3514. %2:l1 » %0:l4[#1{dst}]K » %1:l2[×]
  3515. ArrangeBy keys=[[], [#1{id}]] // { arity: 3 }
  3516. Filter (#1{id}) IS NOT NULL // { arity: 3 }
  3517. Get l4 // { arity: 3 }
  3518. Get l2 // { arity: 1 }
  3519. Get l1 // { arity: 3 }
  3520. cte l5 =
  3521. Reduce group_by=[#0{id}, #2{id}] aggregates=[min((#1{w} + #3{w}))] // { arity: 3 }
  3522. Project (#0{id}, #2, #3{id}, #5) // { arity: 4 }
  3523. Join on=(#1{id} = #4{id}) type=differential // { arity: 6 }
  3524. implementation
  3525. %0:l3[#1{dst}]K » %1:l4[#1{dst}]K
  3526. ArrangeBy keys=[[#1{id}]] // { arity: 3 }
  3527. Filter (#1{id}) IS NOT NULL // { arity: 3 }
  3528. Get l3 // { arity: 3 }
  3529. ArrangeBy keys=[[#1{id}]] // { arity: 3 }
  3530. Filter (#1{id}) IS NOT NULL // { arity: 3 }
  3531. Get l4 // { arity: 3 }
  3532. cte l6 =
  3533. Reduce aggregates=[min(#0{min})] // { arity: 1 }
  3534. Project (#2{min}) // { arity: 1 }
  3535. Get l5 // { arity: 3 }
  3536. cte l7 =
  3537. Union // { arity: 1 }
  3538. Get l6 // { arity: 1 }
  3539. Map (null) // { arity: 1 }
  3540. Union // { arity: 0 }
  3541. Negate // { arity: 0 }
  3542. Project () // { arity: 0 }
  3543. Get l6 // { arity: 1 }
  3544. Constant // { arity: 0 }
  3545. - ()
  3546. Return // { arity: 3 }
  3547. Project (#0{id}..=#2{min}) // { arity: 3 }
  3548. Join on=(#2{min} = #3{min_min}) type=differential // { arity: 4 }
  3549. implementation
  3550. %1[#0]UK » %0:l5[#2{w}]K
  3551. ArrangeBy keys=[[#2{min}]] // { arity: 3 }
  3552. Get l5 // { arity: 3 }
  3553. ArrangeBy keys=[[#0{min_min}]] // { arity: 1 }
  3554. Reduce aggregates=[min(#0{min})] // { arity: 1 }
  3555. Project (#2{min}) // { arity: 1 }
  3556. Get l5 // { arity: 3 }
  3557. Used Indexes:
  3558. - materialize.public.person_locationcityid (lookup)
  3559. - materialize.public.pathq19_src (delta join lookup)
  3560. Target cluster: quickstart
  3561. EOF
  3562. # original query, w/cross joins
  3563. query T multiline
  3564. EXPLAIN OPTIMIZED PLAN WITH(humanized expressions, arity, join implementations) AS VERBOSE TEXT FOR WITH MUTUALLY RECURSIVE
  3565. srcs (f bigint) AS (SELECT id FROM Person WHERE locationcityid = 655::bigint),
  3566. dsts (t bigint) AS (SELECT id FROM Person WHERE locationcityid = 1138::bigint),
  3567. shorts (dir bool, gsrc bigint, dst bigint, w double precision, dead bool, iter bigint) AS (
  3568. (
  3569. SELECT false, f, f, 0::double precision, false, 0 FROM srcs
  3570. UNION ALL
  3571. SELECT true, t, t, 0::double precision, false, 0 FROM dsts
  3572. )
  3573. UNION
  3574. (
  3575. WITH
  3576. ss AS (SELECT * FROM shorts),
  3577. toExplore AS (SELECT * FROM ss WHERE dead = false ORDER BY w LIMIT 1000),
  3578. -- assumes graph is undirected
  3579. newPoints(dir, gsrc, dst, w, dead) AS (
  3580. SELECT e.dir, e.gsrc AS gsrc, p.dst AS dst, e.w + p.w AS w, false AS dead
  3581. FROM PathQ19 p JOIN toExplore e ON (e.dst = p.src)
  3582. UNION
  3583. SELECT dir, gsrc, dst, w, dead OR EXISTS (SELECT * FROM toExplore e WHERE e.dir = o.dir AND e.gsrc = o.gsrc AND e.dst = o.dst) FROM ss o
  3584. ),
  3585. fullTable AS (
  3586. SELECT DISTINCT ON(dir, gsrc, dst) dir, gsrc, dst, w, dead
  3587. FROM newPoints
  3588. ORDER BY dir, gsrc, dst, w, dead DESC
  3589. ),
  3590. found AS (
  3591. SELECT min(l.w + r.w) AS w
  3592. FROM fullTable l, fullTable r
  3593. WHERE l.dir = false AND r.dir = true AND l.dst = r.dst
  3594. )
  3595. SELECT dir,
  3596. gsrc,
  3597. dst,
  3598. w,
  3599. dead or (coalesce(t.w > (SELECT f.w/2 FROM found f), false)),
  3600. e.iter + 1 AS iter
  3601. FROM fullTable t, (SELECT iter FROM toExplore LIMIT 1) e
  3602. )
  3603. ),
  3604. ss (dir bool, gsrc bigint, dst bigint, w double precision, iter bigint) AS (
  3605. SELECT dir, gsrc, dst, w, iter FROM shorts WHERE iter = (SELECT max(iter) FROM shorts)
  3606. ),
  3607. results (f bigint, t bigint, w double precision) AS (
  3608. SELECT l.gsrc, r.gsrc, min(l.w + r.w)
  3609. FROM ss l, ss r
  3610. WHERE l.dir = false AND r.dir = true AND l.dst = r.dst
  3611. GROUP BY l.gsrc, r.gsrc
  3612. )
  3613. SELECT * FROM results WHERE w = (SELECT min(w) FROM results) ORDER BY f, t
  3614. ----
  3615. Explained Query:
  3616. Finish order_by=[#0{id} asc nulls_last, #1{id} asc nulls_last] output=[#0..=#2]
  3617. With
  3618. cte l0 =
  3619. ArrangeBy keys=[[#8{locationcityid}]] // { arity: 11 }
  3620. ReadIndex on=person person_locationcityid=[lookup] // { arity: 11 }
  3621. Return // { arity: 3 }
  3622. With Mutually Recursive
  3623. cte l1 =
  3624. TopK order_by=[#3 asc nulls_last] limit=1000 // { arity: 5 }
  3625. Project (#0..=#3, #5) // { arity: 5 }
  3626. Filter (#4{dead} = false) // { arity: 6 }
  3627. Get l7 // { arity: 6 }
  3628. cte l2 =
  3629. Distinct project=[#0..=#2] // { arity: 3 }
  3630. Project (#0..=#2{id}) // { arity: 3 }
  3631. Get l7 // { arity: 6 }
  3632. cte l3 =
  3633. Project (#0..=#2) // { arity: 3 }
  3634. Join on=(#0 = #3{dir} AND #1 = #4{gsrc} AND #2 = #5{dst}) type=differential // { arity: 6 }
  3635. implementation
  3636. %1[#0..=#2]UKKKA » %0:l2[#0..=#2]UKKK
  3637. ArrangeBy keys=[[#0..=#2]] // { arity: 3 }
  3638. Filter (#1{gsrc}) IS NOT NULL AND (#2{dst}) IS NOT NULL // { arity: 3 }
  3639. Get l2 // { arity: 3 }
  3640. ArrangeBy keys=[[#0{dir}..=#2{dst}]] // { arity: 3 }
  3641. Distinct project=[#0{dir}..=#2{dst}] // { arity: 3 }
  3642. Project (#0..=#2) // { arity: 3 }
  3643. Filter (#1{gsrc}) IS NOT NULL AND (#2{dst}) IS NOT NULL // { arity: 5 }
  3644. Get l1 // { arity: 5 }
  3645. cte l4 =
  3646. TopK group_by=[#0, #1, #2{dst}] order_by=[#3 asc nulls_last, #4 desc nulls_first] limit=1 // { arity: 5 }
  3647. Distinct project=[#0..=#4] // { arity: 5 }
  3648. Union // { arity: 5 }
  3649. Project (#3, #4, #1{dst}, #7, #8) // { arity: 5 }
  3650. Map ((#6{w} + bigint_to_double(#2{w})), false) // { arity: 9 }
  3651. Join on=(#0{src} = #5{dst}) type=differential // { arity: 7 }
  3652. implementation
  3653. %0:pathq19[#0{src}]KA » %1:l1[#2{dst}]K
  3654. ArrangeBy keys=[[#0{src}]] // { arity: 3 }
  3655. ReadIndex on=pathq19 pathq19_src=[differential join] // { arity: 3 }
  3656. ArrangeBy keys=[[#2{dst}]] // { arity: 4 }
  3657. Project (#0..=#3) // { arity: 4 }
  3658. Filter (#2{dst}) IS NOT NULL // { arity: 5 }
  3659. Get l1 // { arity: 5 }
  3660. Project (#0..=#3, #9) // { arity: 5 }
  3661. Map ((#4{dead} OR #8)) // { arity: 10 }
  3662. Join on=(#0 = #5 AND #1 = #6 AND #2 = #7) type=differential // { arity: 9 }
  3663. implementation
  3664. %0:l7[#0..=#2]KKK » %1[#0..=#2]KKK
  3665. ArrangeBy keys=[[#0..=#2]] // { arity: 5 }
  3666. Project (#0..=#4) // { arity: 5 }
  3667. Get l7 // { arity: 6 }
  3668. ArrangeBy keys=[[#0..=#2]] // { arity: 4 }
  3669. Union // { arity: 4 }
  3670. Map (true) // { arity: 4 }
  3671. Get l3 // { arity: 3 }
  3672. Project (#0..=#2, #6) // { arity: 4 }
  3673. Map (false) // { arity: 7 }
  3674. Join on=(#0 = #3 AND #1 = #4 AND #2 = #5) type=differential // { arity: 6 }
  3675. implementation
  3676. %1:l2[#0..=#2]UKKK » %0[#0..=#2]KKK
  3677. ArrangeBy keys=[[#0..=#2]] // { arity: 3 }
  3678. Union // { arity: 3 }
  3679. Negate // { arity: 3 }
  3680. Get l3 // { arity: 3 }
  3681. Get l2 // { arity: 3 }
  3682. ArrangeBy keys=[[#0..=#2]] // { arity: 3 }
  3683. Get l2 // { arity: 3 }
  3684. cte l5 =
  3685. Reduce aggregates=[min((#0{w} + #1{w}))] // { arity: 1 }
  3686. Project (#1, #3) // { arity: 2 }
  3687. Join on=(#0{dst} = #2{dst}) type=differential // { arity: 4 }
  3688. implementation
  3689. %0:l4[#0{dst}]Kef » %1:l4[#0{dst}]Kef
  3690. ArrangeBy keys=[[#0{dst}]] // { arity: 2 }
  3691. Project (#2{dst}, #3) // { arity: 2 }
  3692. Filter (#0{dir} = false) AND (#2{dst}) IS NOT NULL // { arity: 5 }
  3693. Get l4 // { arity: 5 }
  3694. ArrangeBy keys=[[#0{dst}]] // { arity: 2 }
  3695. Project (#2{dst}, #3) // { arity: 2 }
  3696. Filter (#0{dir} = true) AND (#2{dst}) IS NOT NULL // { arity: 5 }
  3697. Get l4 // { arity: 5 }
  3698. cte l6 =
  3699. Project (#1) // { arity: 1 }
  3700. Map ((#0{min} / 2)) // { arity: 2 }
  3701. Union // { arity: 1 }
  3702. Get l5 // { arity: 1 }
  3703. Map (null) // { arity: 1 }
  3704. Union // { arity: 0 }
  3705. Negate // { arity: 0 }
  3706. Project () // { arity: 0 }
  3707. Get l5 // { arity: 1 }
  3708. Constant // { arity: 0 }
  3709. - ()
  3710. cte l7 =
  3711. Distinct project=[#0..=#5] // { arity: 6 }
  3712. Union // { arity: 6 }
  3713. Project (#1, #0{id}, #0{id}, #2..=#4) // { arity: 6 }
  3714. Map (0, false, 0) // { arity: 5 }
  3715. Union // { arity: 2 }
  3716. Project (#1{id}, #12) // { arity: 2 }
  3717. Map (false) // { arity: 13 }
  3718. ReadIndex on=materialize.public.person person_locationcityid=[lookup value=(655)] // { arity: 12 }
  3719. Project (#1{id}, #12) // { arity: 2 }
  3720. Map (true) // { arity: 13 }
  3721. ReadIndex on=materialize.public.person person_locationcityid=[lookup value=(1138)] // { arity: 12 }
  3722. Project (#0..=#3, #7, #8) // { arity: 6 }
  3723. Map ((#4{dead} OR coalesce((#3{w} > #6), false)), (#5{iter} + 1)) // { arity: 9 }
  3724. CrossJoin type=delta // { arity: 7 }
  3725. implementation
  3726. %0:l4 » %1[×]U » %2[×]U
  3727. %1 » %2[×]U » %0:l4[×]
  3728. %2 » %1[×]U » %0:l4[×]
  3729. ArrangeBy keys=[[]] // { arity: 5 }
  3730. Get l4 // { arity: 5 }
  3731. ArrangeBy keys=[[]] // { arity: 1 }
  3732. TopK limit=1 // { arity: 1 }
  3733. Project (#4) // { arity: 1 }
  3734. Get l1 // { arity: 5 }
  3735. ArrangeBy keys=[[]] // { arity: 1 }
  3736. Union // { arity: 1 }
  3737. Get l6 // { arity: 1 }
  3738. Map (null) // { arity: 1 }
  3739. Union // { arity: 0 }
  3740. Negate // { arity: 0 }
  3741. Project () // { arity: 0 }
  3742. Get l6 // { arity: 1 }
  3743. Constant // { arity: 0 }
  3744. - ()
  3745. Return // { arity: 3 }
  3746. With
  3747. cte l8 =
  3748. Project (#0..=#3) // { arity: 4 }
  3749. Join on=(#4{iter} = #5{max}) type=differential // { arity: 6 }
  3750. implementation
  3751. %1[#0]UK » %0:l7[#4{iter}]K
  3752. ArrangeBy keys=[[#4{iter}]] // { arity: 5 }
  3753. Project (#0..=#3, #5) // { arity: 5 }
  3754. Filter (#2{id}) IS NOT NULL // { arity: 6 }
  3755. Get l7 // { arity: 6 }
  3756. ArrangeBy keys=[[#0{max}]] // { arity: 1 }
  3757. Reduce aggregates=[max(#0{iter})] // { arity: 1 }
  3758. Project (#5) // { arity: 1 }
  3759. Get l7 // { arity: 6 }
  3760. cte l9 =
  3761. Reduce group_by=[#0{id}, #2{id}] aggregates=[min((#1{w} + #3{w}))] // { arity: 3 }
  3762. Project (#0{id}, #2, #3{id}, #5) // { arity: 4 }
  3763. Join on=(#1{id} = #4{id}) type=differential // { arity: 6 }
  3764. implementation
  3765. %0:l8[#1{dst}]Kef » %1:l8[#1{dst}]Kef
  3766. ArrangeBy keys=[[#1{id}]] // { arity: 3 }
  3767. Project (#1{id}..=#3) // { arity: 3 }
  3768. Filter (#0{dir} = false) // { arity: 4 }
  3769. Get l8 // { arity: 4 }
  3770. ArrangeBy keys=[[#1{id}]] // { arity: 3 }
  3771. Project (#1{id}..=#3) // { arity: 3 }
  3772. Filter (#0{dir} = true) // { arity: 4 }
  3773. Get l8 // { arity: 4 }
  3774. Return // { arity: 3 }
  3775. Project (#0{id}..=#2{min}) // { arity: 3 }
  3776. Join on=(#2{min} = #3{min_min}) type=differential // { arity: 4 }
  3777. implementation
  3778. %1[#0]UK » %0:l9[#2{w}]K
  3779. ArrangeBy keys=[[#2{min}]] // { arity: 3 }
  3780. Get l9 // { arity: 3 }
  3781. ArrangeBy keys=[[#0{min_min}]] // { arity: 1 }
  3782. Reduce aggregates=[min(#0{min})] // { arity: 1 }
  3783. Project (#2{min}) // { arity: 1 }
  3784. Get l9 // { arity: 3 }
  3785. Used Indexes:
  3786. - materialize.public.person_locationcityid (lookup)
  3787. - materialize.public.pathq19_src (differential join)
  3788. Target cluster: quickstart
  3789. EOF
  3790. ######################################################################
  3791. # QUERY 20
  3792. ######################################################################
  3793. # \set company '\'Balkh_Airlines\''
  3794. # \set person2Id 10995116285979::bigint
  3795. statement ok
  3796. CREATE OR REPLACE MATERIALIZED VIEW PathQ20 AS
  3797. SELECT p1.personid AS src, p2.personid AS dst, min(abs(p1.classYear - p2.classYear)) + 1 AS w
  3798. FROM Person_knows_person pp, Person_studyAt_University p1, Person_studyAt_University p2
  3799. WHERE pp.person1id = p1.personid
  3800. AND pp.person2id = p2.personid
  3801. AND p1.universityid = p2.universityid
  3802. GROUP BY p1.personid, p2.personid;
  3803. statement ok
  3804. CREATE INDEX PathQ20_src ON PathQ20 (src);
  3805. query T multiline
  3806. EXPLAIN OPTIMIZED PLAN WITH(humanized expressions, arity, join implementations) AS VERBOSE TEXT FOR WITH minimal_paths AS (
  3807. WITH MUTUALLY RECURSIVE
  3808. paths (src bigint, dst bigint, w bigint) AS (
  3809. SELECT 10995116285979::bigint AS src, 10995116285979::bigint AS dst, 0 AS w
  3810. UNION
  3811. SELECT paths1.src, paths2.dst, paths1.w + paths2.w
  3812. FROM minimal_paths paths1
  3813. JOIN PathQ20 paths2 -- step-transitive closure
  3814. ON paths1.dst = paths2.src
  3815. ),
  3816. minimal_paths (src bigint, dst bigint, w bigint) AS (
  3817. SELECT src, dst, min(w)
  3818. FROM paths
  3819. GROUP BY src, dst
  3820. )
  3821. SELECT src, dst, w FROM minimal_paths),
  3822. dsts AS (
  3823. SELECT personid
  3824. FROM Person_workat_company pwc, Company c
  3825. WHERE pwc.companyid = c.id AND c.name='Balkh_Airlines'
  3826. ),
  3827. completed_paths AS (
  3828. SELECT dst, w
  3829. FROM minimal_paths
  3830. WHERE dst IN (SELECT * FROM dsts)
  3831. ),
  3832. results AS (
  3833. SELECT dst, w
  3834. FROM completed_paths
  3835. WHERE w IN (SELECT min(w) FROM completed_paths)
  3836. )
  3837. SELECT dst, w FROM results ORDER BY dst LIMIT 20
  3838. ----
  3839. Explained Query:
  3840. Finish order_by=[#0{dst} asc nulls_last] limit=20 output=[#0, #1]
  3841. With Mutually Recursive
  3842. cte l0 =
  3843. Project (#2, #0{dst}, #1{min}) // { arity: 3 }
  3844. Map (10995116285979) // { arity: 3 }
  3845. Reduce group_by=[#0{dst}] aggregates=[min(#1{w})] // { arity: 2 }
  3846. Distinct project=[#0{dst}, #1] // { arity: 2 }
  3847. Union // { arity: 2 }
  3848. Project (#3{dst}, #5) // { arity: 2 }
  3849. Map ((#1{w} + integer_to_bigint(#4{w}))) // { arity: 6 }
  3850. Join on=(#0{dst} = #2{src}) type=differential // { arity: 5 }
  3851. implementation
  3852. %0:l0[#0{dst}]UK » %1:pathq20[#0{src}]KA
  3853. ArrangeBy keys=[[#0{dst}]] // { arity: 2 }
  3854. Project (#1{dst}, #2{min}) // { arity: 2 }
  3855. Get l0 // { arity: 3 }
  3856. ArrangeBy keys=[[#0{src}]] // { arity: 3 }
  3857. ReadIndex on=pathq20 pathq20_src=[differential join] // { arity: 3 }
  3858. Constant // { arity: 2 }
  3859. - (10995116285979, 0)
  3860. Return // { arity: 2 }
  3861. With
  3862. cte l1 =
  3863. Project (#0{dst}, #1{min}) // { arity: 2 }
  3864. Join on=(#0{dst} = #2{personid}) type=differential // { arity: 3 }
  3865. implementation
  3866. %1[#0]UKA » %0:l0[#0]UK
  3867. ArrangeBy keys=[[#0{dst}]] // { arity: 2 }
  3868. Project (#1{dst}, #2{min}) // { arity: 2 }
  3869. Get l0 // { arity: 3 }
  3870. ArrangeBy keys=[[#0{personid}]] // { arity: 1 }
  3871. Distinct project=[#0{personid}] // { arity: 1 }
  3872. Project (#1{personid}) // { arity: 1 }
  3873. Filter (#5{name} = "Balkh_Airlines") // { arity: 8 }
  3874. Join on=(#2{companyid} = #4{id}) type=differential // { arity: 8 }
  3875. implementation
  3876. %1:company[#0{id}]KAef » %0:person_workat_company[#2{companyid}]KAef
  3877. ArrangeBy keys=[[#2{companyid}]] // { arity: 4 }
  3878. ReadIndex on=person_workat_company person_workat_company_companyid=[differential join] // { arity: 4 }
  3879. ArrangeBy keys=[[#0{id}]] // { arity: 4 }
  3880. ReadIndex on=company company_id=[differential join] // { arity: 4 }
  3881. cte l2 =
  3882. Project (#1{min}) // { arity: 1 }
  3883. Get l1 // { arity: 2 }
  3884. Return // { arity: 2 }
  3885. Project (#0{dst}, #1{min}) // { arity: 2 }
  3886. Filter (#1{min} = #3{min_min}) // { arity: 4 }
  3887. Join on=(#1{min} = #2{min}) type=differential // { arity: 4 }
  3888. implementation
  3889. %1[#0]UKAf » %0:l1[#1]Kf
  3890. ArrangeBy keys=[[#1{min}]] // { arity: 2 }
  3891. Get l1 // { arity: 2 }
  3892. ArrangeBy keys=[[#0{min}]] // { arity: 2 }
  3893. Reduce group_by=[#0{min}] aggregates=[min(#1{min})] // { arity: 2 }
  3894. CrossJoin type=differential // { arity: 2 }
  3895. implementation
  3896. %0[×] » %1:l2[×]
  3897. ArrangeBy keys=[[]] // { arity: 1 }
  3898. Distinct project=[#0{min}] // { arity: 1 }
  3899. Get l2 // { arity: 1 }
  3900. ArrangeBy keys=[[]] // { arity: 1 }
  3901. Get l2 // { arity: 1 }
  3902. Used Indexes:
  3903. - materialize.public.person_workat_company_companyid (differential join)
  3904. - materialize.public.company_id (differential join)
  3905. - materialize.public.pathq20_src (differential join)
  3906. Target cluster: quickstart
  3907. EOF
  3908. # without the unused src
  3909. query T multiline
  3910. EXPLAIN OPTIMIZED PLAN WITH(humanized expressions, arity, join implementations) AS VERBOSE TEXT FOR WITH minimal_paths AS (
  3911. WITH MUTUALLY RECURSIVE
  3912. paths (dst bigint, w bigint) AS (
  3913. SELECT 10995116285979::bigint AS dst, 0 AS w
  3914. UNION
  3915. SELECT paths2.dst, paths1.w + paths2.w
  3916. FROM minimal_paths paths1
  3917. JOIN PathQ20 paths2 -- step-transitive closure
  3918. ON paths1.dst = paths2.src
  3919. ),
  3920. minimal_paths (dst bigint, w bigint) AS (
  3921. SELECT dst, min(w)
  3922. FROM paths
  3923. GROUP BY dst
  3924. )
  3925. SELECT dst, w FROM minimal_paths),
  3926. dsts AS (
  3927. SELECT personid
  3928. FROM Person_workat_company pwc, Company c
  3929. WHERE pwc.companyid = c.id AND c.name='Balkh_Airlines'
  3930. ),
  3931. completed_paths AS (
  3932. SELECT dst, w
  3933. FROM minimal_paths
  3934. WHERE dst IN (SELECT * FROM dsts)
  3935. ),
  3936. results AS (
  3937. SELECT dst, w
  3938. FROM completed_paths
  3939. WHERE w IN (SELECT min(w) FROM completed_paths)
  3940. )
  3941. SELECT dst, w FROM results ORDER BY dst LIMIT 20
  3942. ----
  3943. Explained Query:
  3944. Finish order_by=[#0{dst} asc nulls_last] limit=20 output=[#0, #1]
  3945. With Mutually Recursive
  3946. cte l0 =
  3947. Reduce group_by=[#0{dst}] aggregates=[min(#1{w})] // { arity: 2 }
  3948. Distinct project=[#0{dst}, #1] // { arity: 2 }
  3949. Union // { arity: 2 }
  3950. Project (#3{dst}, #5) // { arity: 2 }
  3951. Map ((#1{w} + integer_to_bigint(#4{w}))) // { arity: 6 }
  3952. Join on=(#0{dst} = #2{src}) type=differential // { arity: 5 }
  3953. implementation
  3954. %0:l0[#0{dst}]UK » %1:pathq20[#0{src}]KA
  3955. ArrangeBy keys=[[#0{dst}]] // { arity: 2 }
  3956. Get l0 // { arity: 2 }
  3957. ArrangeBy keys=[[#0{src}]] // { arity: 3 }
  3958. ReadIndex on=pathq20 pathq20_src=[differential join] // { arity: 3 }
  3959. Constant // { arity: 2 }
  3960. - (10995116285979, 0)
  3961. Return // { arity: 2 }
  3962. With
  3963. cte l1 =
  3964. Project (#0{dst}, #1{min}) // { arity: 2 }
  3965. Join on=(#0{dst} = #2{personid}) type=differential // { arity: 3 }
  3966. implementation
  3967. %1[#0]UKA » %0:l0[#0]UK
  3968. ArrangeBy keys=[[#0{dst}]] // { arity: 2 }
  3969. Get l0 // { arity: 2 }
  3970. ArrangeBy keys=[[#0{personid}]] // { arity: 1 }
  3971. Distinct project=[#0{personid}] // { arity: 1 }
  3972. Project (#1{personid}) // { arity: 1 }
  3973. Filter (#5{name} = "Balkh_Airlines") // { arity: 8 }
  3974. Join on=(#2{companyid} = #4{id}) type=differential // { arity: 8 }
  3975. implementation
  3976. %1:company[#0{id}]KAef » %0:person_workat_company[#2{companyid}]KAef
  3977. ArrangeBy keys=[[#2{companyid}]] // { arity: 4 }
  3978. ReadIndex on=person_workat_company person_workat_company_companyid=[differential join] // { arity: 4 }
  3979. ArrangeBy keys=[[#0{id}]] // { arity: 4 }
  3980. ReadIndex on=company company_id=[differential join] // { arity: 4 }
  3981. cte l2 =
  3982. Project (#1{min}) // { arity: 1 }
  3983. Get l1 // { arity: 2 }
  3984. Return // { arity: 2 }
  3985. Project (#0{dst}, #1{min}) // { arity: 2 }
  3986. Filter (#1{min} = #3{min_min}) // { arity: 4 }
  3987. Join on=(#1{min} = #2{min}) type=differential // { arity: 4 }
  3988. implementation
  3989. %1[#0]UKAf » %0:l1[#1]Kf
  3990. ArrangeBy keys=[[#1{min}]] // { arity: 2 }
  3991. Get l1 // { arity: 2 }
  3992. ArrangeBy keys=[[#0{min}]] // { arity: 2 }
  3993. Reduce group_by=[#0{min}] aggregates=[min(#1{min})] // { arity: 2 }
  3994. CrossJoin type=differential // { arity: 2 }
  3995. implementation
  3996. %0[×] » %1:l2[×]
  3997. ArrangeBy keys=[[]] // { arity: 1 }
  3998. Distinct project=[#0{min}] // { arity: 1 }
  3999. Get l2 // { arity: 1 }
  4000. ArrangeBy keys=[[]] // { arity: 1 }
  4001. Get l2 // { arity: 1 }
  4002. Used Indexes:
  4003. - materialize.public.person_workat_company_companyid (differential join)
  4004. - materialize.public.company_id (differential join)
  4005. - materialize.public.pathq20_src (differential join)
  4006. Target cluster: quickstart
  4007. EOF
  4008. # tracking hops
  4009. query T multiline
  4010. EXPLAIN OPTIMIZED PLAN WITH(humanized expressions, arity, join implementations) AS VERBOSE TEXT FOR WITH minimal_paths AS (
  4011. WITH MUTUALLY RECURSIVE
  4012. paths (src bigint, dst bigint, w bigint, hops bigint) AS (
  4013. SELECT 10995116285979::bigint AS src, 10995116285979::bigint AS dst, 0 AS w, 0 AS hops
  4014. UNION
  4015. SELECT paths1.src, paths2.dst, paths1.w + paths2.w, paths1.hops + 1
  4016. FROM minimal_paths paths1
  4017. JOIN PathQ20 paths2 -- step-transitive closure
  4018. ON paths1.dst = paths2.src
  4019. ),
  4020. minimal_weights (src bigint, dst bigint, w bigint, hops bigint) AS (
  4021. SELECT src, dst, min(w), hops
  4022. FROM paths
  4023. GROUP BY src, dst, hops
  4024. ),
  4025. minimal_paths (src bigint, dst bigint, w bigint, hops bigint) AS (
  4026. SELECT src, dst, w, min(hops)
  4027. FROM minimal_weights
  4028. GROUP BY src, dst, w
  4029. )
  4030. SELECT src, dst, w, hops FROM minimal_paths),
  4031. dsts AS (
  4032. SELECT personid
  4033. FROM Person_workat_company pwc, Company c
  4034. WHERE pwc.companyid = c.id AND c.name='Balkh_Airlines'
  4035. ),
  4036. completed_paths AS (
  4037. SELECT dst, w, hops
  4038. FROM minimal_paths
  4039. WHERE dst IN (SELECT * FROM dsts)
  4040. ),
  4041. results AS (
  4042. SELECT dst, w, hops
  4043. FROM completed_paths
  4044. WHERE w IN (SELECT min(w) FROM completed_paths)
  4045. )
  4046. SELECT dst, w, hops FROM results ORDER BY dst LIMIT 20
  4047. ----
  4048. Explained Query:
  4049. Finish order_by=[#0{dst} asc nulls_last] limit=20 output=[#0..=#2]
  4050. With Mutually Recursive
  4051. cte l0 =
  4052. Project (#3, #0{dst}..=#2{min}) // { arity: 4 }
  4053. Map (10995116285979) // { arity: 4 }
  4054. Reduce group_by=[#0{dst}, #2{min}] aggregates=[min(#1{hops})] // { arity: 3 }
  4055. Reduce group_by=[#0{dst}, #2] aggregates=[min(#1{w})] // { arity: 3 }
  4056. Distinct project=[#0{dst}..=#2] // { arity: 3 }
  4057. Union // { arity: 3 }
  4058. Project (#4{dst}, #6, #7) // { arity: 3 }
  4059. Map ((#1{w} + integer_to_bigint(#5{w})), (#2{hops} + 1)) // { arity: 8 }
  4060. Join on=(#0{dst} = #3{src}) type=differential // { arity: 6 }
  4061. implementation
  4062. %1:pathq20[#0{src}]KA » %0:l0[#0{dst}]K
  4063. ArrangeBy keys=[[#0{dst}]] // { arity: 3 }
  4064. Project (#1{dst}..=#3{min}) // { arity: 3 }
  4065. Get l0 // { arity: 4 }
  4066. ArrangeBy keys=[[#0{src}]] // { arity: 3 }
  4067. ReadIndex on=pathq20 pathq20_src=[differential join] // { arity: 3 }
  4068. Constant // { arity: 3 }
  4069. - (10995116285979, 0, 0)
  4070. Return // { arity: 3 }
  4071. With
  4072. cte l1 =
  4073. Project (#0{dst}..=#2{min}) // { arity: 3 }
  4074. Join on=(#0{dst} = #3{personid}) type=differential // { arity: 4 }
  4075. implementation
  4076. %1[#0]UKA » %0:l0[#0]K
  4077. ArrangeBy keys=[[#0{dst}]] // { arity: 3 }
  4078. Project (#1{dst}..=#3{min}) // { arity: 3 }
  4079. Get l0 // { arity: 4 }
  4080. ArrangeBy keys=[[#0{personid}]] // { arity: 1 }
  4081. Distinct project=[#0{personid}] // { arity: 1 }
  4082. Project (#1{personid}) // { arity: 1 }
  4083. Filter (#5{name} = "Balkh_Airlines") // { arity: 8 }
  4084. Join on=(#2{companyid} = #4{id}) type=differential // { arity: 8 }
  4085. implementation
  4086. %1:company[#0{id}]KAef » %0:person_workat_company[#2{companyid}]KAef
  4087. ArrangeBy keys=[[#2{companyid}]] // { arity: 4 }
  4088. ReadIndex on=person_workat_company person_workat_company_companyid=[differential join] // { arity: 4 }
  4089. ArrangeBy keys=[[#0{id}]] // { arity: 4 }
  4090. ReadIndex on=company company_id=[differential join] // { arity: 4 }
  4091. cte l2 =
  4092. Project (#1{min}) // { arity: 1 }
  4093. Get l1 // { arity: 3 }
  4094. Return // { arity: 3 }
  4095. Project (#0{dst}..=#2{min}) // { arity: 3 }
  4096. Filter (#1{min} = #4{min_min}) // { arity: 5 }
  4097. Join on=(#1{min} = #3{min}) type=differential // { arity: 5 }
  4098. implementation
  4099. %1[#0]UKAf » %0:l1[#1]Kf
  4100. ArrangeBy keys=[[#1{min}]] // { arity: 3 }
  4101. Get l1 // { arity: 3 }
  4102. ArrangeBy keys=[[#0{min}]] // { arity: 2 }
  4103. Reduce group_by=[#0{min}] aggregates=[min(#1{min})] // { arity: 2 }
  4104. CrossJoin type=differential // { arity: 2 }
  4105. implementation
  4106. %0[×] » %1:l2[×]
  4107. ArrangeBy keys=[[]] // { arity: 1 }
  4108. Distinct project=[#0{min}] // { arity: 1 }
  4109. Get l2 // { arity: 1 }
  4110. ArrangeBy keys=[[]] // { arity: 1 }
  4111. Get l2 // { arity: 1 }
  4112. Used Indexes:
  4113. - materialize.public.person_workat_company_companyid (differential join)
  4114. - materialize.public.company_id (differential join)
  4115. - materialize.public.pathq20_src (differential join)
  4116. Target cluster: quickstart
  4117. EOF
  4118. # original query, w/extra crossjoins
  4119. query T multiline
  4120. EXPLAIN OPTIMIZED PLAN WITH(humanized expressions, arity, join implementations) AS VERBOSE TEXT FOR WITH MUTUALLY RECURSIVE
  4121. srcs(f bigint) AS (SELECT 10995116285979::bigint),
  4122. dsts(t bigint) AS (
  4123. SELECT personid
  4124. FROM Person_workat_company pwc, Company c
  4125. WHERE pwc.companyid = c.id AND c.name='Balkh_Airlines'
  4126. ),
  4127. -- Try to find any path with a faster two way BFS
  4128. -- visited nodes plus (on each iteration) nodes in PathQ20 we haven't yet seen
  4129. anyPath (pos bigint) AS (
  4130. SELECT f FROM srcs
  4131. UNION
  4132. (
  4133. WITH
  4134. ss AS (SELECT pos FROM anyPath)
  4135. SELECT dst
  4136. FROM ss, PathQ20
  4137. WHERE pos = src AND NOT EXISTS (SELECT 1 FROM ss, dsts WHERE ss.pos = dsts.t)
  4138. )
  4139. ),
  4140. -- are we there yet? at first, no (unless src is a dst)
  4141. pathexists (exists bool) AS (
  4142. SELECT true WHERE EXISTS (SELECT 1 FROM anyPath ss, dsts WHERE ss.pos = dsts.t)
  4143. ),
  4144. shorts (dir bool, gsrc bigint, dst bigint, w bigint, dead bool, iter bigint) AS (
  4145. (
  4146. SELECT false, f, f, 0, false, 0 FROM srcs WHERE EXISTS (SELECT 1 FROM pathexists)
  4147. UNION
  4148. SELECT true, t, t, 0, false, 0 FROM dsts WHERE EXISTS (SELECT 1 FROM pathexists)
  4149. )
  4150. UNION
  4151. (
  4152. WITH ss AS (SELECT * FROM shorts),
  4153. toExplore AS (SELECT * FROM ss WHERE dead = false ORDER BY w limit 1000),
  4154. -- assumes graph is undirected
  4155. newPoints(dir, gsrc, dst, w, dead) AS (
  4156. SELECT e.dir, e.gsrc AS gsrc, p.dst AS dst, e.w + p.w AS w, false AS dead
  4157. FROM PathQ20 p JOIN toExplore e ON (e.dst = p.src)
  4158. UNION ALL
  4159. SELECT dir, gsrc, dst, w, dead OR EXISTS (SELECT * FROM toExplore e WHERE e.dir = o.dir AND e.gsrc = o.gsrc AND e.dst = o.dst) FROM ss o
  4160. ),
  4161. fullTable AS (
  4162. SELECT distinct ON(dir, gsrc, dst) dir, gsrc, dst, w, dead
  4163. FROM newPoints
  4164. ORDER BY dir, gsrc, dst, w, dead DESC
  4165. ),
  4166. found AS (
  4167. SELECT min(l.w + r.w) AS w
  4168. FROM fullTable l, fullTable r
  4169. WHERE l.dir = false AND r.dir = true AND l.dst = r.dst
  4170. )
  4171. SELECT dir,
  4172. gsrc,
  4173. dst,
  4174. w,
  4175. dead or (coalesce(t.w > (SELECT f.w/2 FROM found f), false)),
  4176. e.iter + 1 AS iter
  4177. FROM fullTable t, (SELECT iter FROM toExplore limit 1) e
  4178. )
  4179. ),
  4180. ss (dir bool, gsrc bigint, dst bigint, w bigint, iter bigint) AS (
  4181. SELECT dir, gsrc, dst, w, iter FROM shorts WHERE iter = (SELECT max(iter) FROM shorts)
  4182. ),
  4183. results(f bigint, t bigint, w bigint) AS (
  4184. SELECT l.gsrc, r.gsrc, min(l.w + r.w)
  4185. FROM ss l, ss r
  4186. WHERE l.dir = false AND r.dir = true AND l.dst = r.dst
  4187. GROUP BY l.gsrc, r.gsrc
  4188. )
  4189. SELECT t, w FROM results WHERE w = (SELECT min(w) FROM results) ORDER BY t LIMIT 20
  4190. ----
  4191. Explained Query:
  4192. Finish order_by=[#0{personid} asc nulls_last] limit=20 output=[#0, #1]
  4193. With
  4194. cte l0 =
  4195. Project (#1{personid}) // { arity: 1 }
  4196. Filter (#5{name} = "Balkh_Airlines") // { arity: 8 }
  4197. Join on=(#2{companyid} = #4{id}) type=differential // { arity: 8 }
  4198. implementation
  4199. %1:company[#0{id}]KAef » %0:person_workat_company[#2{companyid}]KAef
  4200. ArrangeBy keys=[[#2{companyid}]] // { arity: 4 }
  4201. ReadIndex on=person_workat_company person_workat_company_companyid=[differential join] // { arity: 4 }
  4202. ArrangeBy keys=[[#0{id}]] // { arity: 4 }
  4203. ReadIndex on=company company_id=[differential join] // { arity: 4 }
  4204. cte l1 =
  4205. ArrangeBy keys=[[#0{src}]] // { arity: 3 }
  4206. ReadIndex on=pathq20 pathq20_src=[differential join, delta join lookup] // { arity: 3 }
  4207. cte l2 =
  4208. ArrangeBy keys=[[#0{personid}]] // { arity: 1 }
  4209. Get l0 // { arity: 1 }
  4210. Return // { arity: 2 }
  4211. With Mutually Recursive
  4212. cte l3 =
  4213. Distinct project=[#0{dst}] // { arity: 1 }
  4214. Union // { arity: 1 }
  4215. Project (#2{dst}) // { arity: 1 }
  4216. Join on=(#0{pos} = #1{src}) type=delta // { arity: 4 }
  4217. implementation
  4218. %0:l3 » %1:l1[#0{src}]KA » %2[×]
  4219. %1:l1 » %0:l3[#0{pos}]UK » %2[×]
  4220. %2 » %0:l3[×] » %1:l1[#0{src}]KA
  4221. ArrangeBy keys=[[], [#0{dst}]] // { arity: 1 }
  4222. Get l3 // { arity: 1 }
  4223. Get l1 // { arity: 3 }
  4224. ArrangeBy keys=[[]] // { arity: 0 }
  4225. Union // { arity: 0 }
  4226. Negate // { arity: 0 }
  4227. Distinct project=[] // { arity: 0 }
  4228. Project () // { arity: 0 }
  4229. Join on=(#0{dst} = #1{personid}) type=differential // { arity: 2 }
  4230. implementation
  4231. %0:l3[#0{pos}]UK » %1:l2[#0{t}]K
  4232. ArrangeBy keys=[[#0{dst}]] // { arity: 1 }
  4233. Get l3 // { arity: 1 }
  4234. Get l2 // { arity: 1 }
  4235. Constant // { arity: 0 }
  4236. - ()
  4237. Constant // { arity: 1 }
  4238. - (10995116285979)
  4239. cte l4 =
  4240. TopK order_by=[#3 asc nulls_last] limit=1000 // { arity: 5 }
  4241. Project (#0..=#3, #5) // { arity: 5 }
  4242. Filter (#4{dead} = false) // { arity: 6 }
  4243. Get l12 // { arity: 6 }
  4244. cte l5 =
  4245. Distinct project=[#0..=#2] // { arity: 3 }
  4246. Project (#0..=#2{personid}) // { arity: 3 }
  4247. Get l12 // { arity: 6 }
  4248. cte l6 =
  4249. ArrangeBy keys=[[#0..=#2]] // { arity: 3 }
  4250. Get l5 // { arity: 3 }
  4251. cte l7 =
  4252. Project (#0..=#2) // { arity: 3 }
  4253. Join on=(#0 = #3{dir} AND #1 = #4{gsrc} AND #2 = #5{dst}) type=differential // { arity: 6 }
  4254. implementation
  4255. %1[#0..=#2]UKKKA » %0:l6[#0..=#2]UKKK
  4256. Get l6 // { arity: 3 }
  4257. ArrangeBy keys=[[#0{dir}..=#2{dst}]] // { arity: 3 }
  4258. Distinct project=[#0{dir}..=#2{dst}] // { arity: 3 }
  4259. Project (#0..=#2) // { arity: 3 }
  4260. Get l4 // { arity: 5 }
  4261. cte l8 =
  4262. TopK group_by=[#0, #1, #2{dst}] order_by=[#3 asc nulls_last, #4 desc nulls_first] limit=1 // { arity: 5 }
  4263. Union // { arity: 5 }
  4264. Project (#3, #4, #1{dst}, #7, #8) // { arity: 5 }
  4265. Map ((#6{w} + integer_to_bigint(#2{w})), false) // { arity: 9 }
  4266. Join on=(#0{src} = #5{dst}) type=differential // { arity: 7 }
  4267. implementation
  4268. %0:l1[#0{src}]KA » %1:l4[#2{dst}]K
  4269. Get l1 // { arity: 3 }
  4270. ArrangeBy keys=[[#2{dst}]] // { arity: 4 }
  4271. Project (#0..=#3) // { arity: 4 }
  4272. Get l4 // { arity: 5 }
  4273. Project (#0..=#3, #9) // { arity: 5 }
  4274. Map ((#4{dead} OR #8)) // { arity: 10 }
  4275. Join on=(#0 = #5 AND #1 = #6 AND #2 = #7) type=differential // { arity: 9 }
  4276. implementation
  4277. %0:l12[#0..=#2]KKK » %1[#0..=#2]KKK
  4278. ArrangeBy keys=[[#0..=#2]] // { arity: 5 }
  4279. Project (#0..=#4) // { arity: 5 }
  4280. Get l12 // { arity: 6 }
  4281. ArrangeBy keys=[[#0..=#2]] // { arity: 4 }
  4282. Union // { arity: 4 }
  4283. Map (true) // { arity: 4 }
  4284. Get l7 // { arity: 3 }
  4285. Project (#0..=#2, #6) // { arity: 4 }
  4286. Map (false) // { arity: 7 }
  4287. Join on=(#0 = #3 AND #1 = #4 AND #2 = #5) type=differential // { arity: 6 }
  4288. implementation
  4289. %1:l6[#0..=#2]UKKK » %0[#0..=#2]KKK
  4290. ArrangeBy keys=[[#0..=#2]] // { arity: 3 }
  4291. Union // { arity: 3 }
  4292. Negate // { arity: 3 }
  4293. Get l7 // { arity: 3 }
  4294. Get l5 // { arity: 3 }
  4295. Get l6 // { arity: 3 }
  4296. cte l9 =
  4297. Reduce aggregates=[min((#0{w} + #1{w}))] // { arity: 1 }
  4298. Project (#1, #3) // { arity: 2 }
  4299. Join on=(#0{dst} = #2{dst}) type=differential // { arity: 4 }
  4300. implementation
  4301. %0:l8[#0{dst}]Kef » %1:l8[#0{dst}]Kef
  4302. ArrangeBy keys=[[#0{dst}]] // { arity: 2 }
  4303. Project (#2{dst}, #3) // { arity: 2 }
  4304. Filter (#0{dir} = false) // { arity: 5 }
  4305. Get l8 // { arity: 5 }
  4306. ArrangeBy keys=[[#0{dst}]] // { arity: 2 }
  4307. Project (#2{dst}, #3) // { arity: 2 }
  4308. Filter (#0{dir} = true) // { arity: 5 }
  4309. Get l8 // { arity: 5 }
  4310. cte l10 =
  4311. Project (#1) // { arity: 1 }
  4312. Map ((#0{min} / 2)) // { arity: 2 }
  4313. Union // { arity: 1 }
  4314. Get l9 // { arity: 1 }
  4315. Map (null) // { arity: 1 }
  4316. Union // { arity: 0 }
  4317. Negate // { arity: 0 }
  4318. Project () // { arity: 0 }
  4319. Get l9 // { arity: 1 }
  4320. Constant // { arity: 0 }
  4321. - ()
  4322. cte l11 =
  4323. Distinct project=[] // { arity: 0 }
  4324. Project () // { arity: 0 }
  4325. Join on=(#0{dst} = #1{personid}) type=differential // { arity: 2 }
  4326. implementation
  4327. %0:l3[#0{pos}]UK » %1:l2[#0{t}]K
  4328. ArrangeBy keys=[[#0{dst}]] // { arity: 1 }
  4329. Get l3 // { arity: 1 }
  4330. Get l2 // { arity: 1 }
  4331. cte l12 =
  4332. Distinct project=[#0..=#5] // { arity: 6 }
  4333. Union // { arity: 6 }
  4334. Project (#0..=#2{personid}, #4, #3, #5) // { arity: 6 }
  4335. Map (false, 0, 0) // { arity: 6 }
  4336. Distinct project=[#0..=#2{personid}] // { arity: 3 }
  4337. Union // { arity: 3 }
  4338. Project (#1, #0, #0) // { arity: 3 }
  4339. Map (10995116285979, false) // { arity: 2 }
  4340. Get l11 // { arity: 0 }
  4341. Project (#1, #0{personid}, #0{personid}) // { arity: 3 }
  4342. Map (true) // { arity: 2 }
  4343. CrossJoin type=differential // { arity: 1 }
  4344. implementation
  4345. %1:l11[×]U » %0:l0[×]
  4346. ArrangeBy keys=[[]] // { arity: 1 }
  4347. Get l0 // { arity: 1 }
  4348. ArrangeBy keys=[[]] // { arity: 0 }
  4349. Get l11 // { arity: 0 }
  4350. Project (#0..=#3, #7, #8) // { arity: 6 }
  4351. Map ((#4{dead} OR coalesce((#3{w} > #6), false)), (#5{iter} + 1)) // { arity: 9 }
  4352. CrossJoin type=delta // { arity: 7 }
  4353. implementation
  4354. %0:l8 » %1[×]U » %2[×]U
  4355. %1 » %2[×]U » %0:l8[×]
  4356. %2 » %1[×]U » %0:l8[×]
  4357. ArrangeBy keys=[[]] // { arity: 5 }
  4358. Get l8 // { arity: 5 }
  4359. ArrangeBy keys=[[]] // { arity: 1 }
  4360. TopK limit=1 // { arity: 1 }
  4361. Project (#4) // { arity: 1 }
  4362. Get l4 // { arity: 5 }
  4363. ArrangeBy keys=[[]] // { arity: 1 }
  4364. Union // { arity: 1 }
  4365. Get l10 // { arity: 1 }
  4366. Map (null) // { arity: 1 }
  4367. Union // { arity: 0 }
  4368. Negate // { arity: 0 }
  4369. Project () // { arity: 0 }
  4370. Get l10 // { arity: 1 }
  4371. Constant // { arity: 0 }
  4372. - ()
  4373. Return // { arity: 2 }
  4374. With
  4375. cte l13 =
  4376. Project (#0..=#3) // { arity: 4 }
  4377. Join on=(#4{iter} = #5{max}) type=differential // { arity: 6 }
  4378. implementation
  4379. %1[#0]UK » %0:l12[#4{iter}]K
  4380. ArrangeBy keys=[[#4{iter}]] // { arity: 5 }
  4381. Project (#0..=#3, #5) // { arity: 5 }
  4382. Get l12 // { arity: 6 }
  4383. ArrangeBy keys=[[#0{max}]] // { arity: 1 }
  4384. Reduce aggregates=[max(#0{iter})] // { arity: 1 }
  4385. Project (#5) // { arity: 1 }
  4386. Get l12 // { arity: 6 }
  4387. cte l14 =
  4388. Project (#1{personid}, #2{min}) // { arity: 2 }
  4389. Reduce group_by=[#0{personid}, #2{personid}] aggregates=[min((#1{w} + #3{w}))] // { arity: 3 }
  4390. Project (#0{personid}, #2, #3{personid}, #5) // { arity: 4 }
  4391. Join on=(#1{personid} = #4{personid}) type=differential // { arity: 6 }
  4392. implementation
  4393. %0:l13[#1{dst}]Kef » %1:l13[#1{dst}]Kef
  4394. ArrangeBy keys=[[#1{personid}]] // { arity: 3 }
  4395. Project (#1{personid}..=#3) // { arity: 3 }
  4396. Filter (#0{dir} = false) // { arity: 4 }
  4397. Get l13 // { arity: 4 }
  4398. ArrangeBy keys=[[#1{personid}]] // { arity: 3 }
  4399. Project (#1{personid}..=#3) // { arity: 3 }
  4400. Filter (#0{dir} = true) // { arity: 4 }
  4401. Get l13 // { arity: 4 }
  4402. Return // { arity: 2 }
  4403. Project (#0{personid}, #1{min}) // { arity: 2 }
  4404. Join on=(#1{min} = #2{min_min}) type=differential // { arity: 3 }
  4405. implementation
  4406. %1[#0]UK » %0:l14[#1{w}]K
  4407. ArrangeBy keys=[[#1{min}]] // { arity: 2 }
  4408. Get l14 // { arity: 2 }
  4409. ArrangeBy keys=[[#0{min_min}]] // { arity: 1 }
  4410. Reduce aggregates=[min(#0{min})] // { arity: 1 }
  4411. Project (#1{min}) // { arity: 1 }
  4412. Get l14 // { arity: 2 }
  4413. Used Indexes:
  4414. - materialize.public.person_workat_company_companyid (differential join)
  4415. - materialize.public.company_id (differential join)
  4416. - materialize.public.pathq20_src (differential join, delta join lookup)
  4417. Target cluster: quickstart
  4418. EOF