ldbc_bi_eager.slt 223 KB

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