views.sql 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. -- static entity views (Umbra's create-static-materialized-views.sql)
  2. CREATE OR REPLACE VIEW Country AS
  3. SELECT id, name, url, PartOfPlaceId AS PartOfContinentId
  4. FROM Place
  5. WHERE type = 'Country'
  6. ;
  7. CREATE INDEX Country_id ON Country (id);
  8. CREATE OR REPLACE VIEW City AS
  9. SELECT id, name, url, PartOfPlaceId AS PartOfCountryId
  10. FROM Place
  11. WHERE type = 'City'
  12. ;
  13. CREATE INDEX City_id ON City (id);
  14. CREATE INDEX City_PartOfCountryId ON City (PartOfCountryId);
  15. CREATE OR REPLACE VIEW Company AS
  16. SELECT id, name, url, LocationPlaceId AS LocatedInCountryId
  17. FROM Organisation
  18. WHERE type = 'Company'
  19. ;
  20. CREATE INDEX Company_id ON Company (id);
  21. CREATE OR REPLACE VIEW University AS
  22. SELECT id, name, url, LocationPlaceId AS LocatedInCityId
  23. FROM Organisation
  24. WHERE type = 'University'
  25. ;
  26. CREATE INDEX University_id ON University (id);
  27. -- Umbra manually materializes this using load_mht (queries.py)
  28. CREATE OR REPLACE VIEW Message_hasTag_Tag AS
  29. (SELECT creationDate, CommentId as MessageId, TagId FROM Comment_hasTag_Tag)
  30. UNION
  31. (SELECT creationDate, PostId as MessageId, TagId FROM Post_hasTag_Tag);
  32. CREATE INDEX Message_hasTag_Tag_MessageId ON Message_hasTag_Tag (MessageId);
  33. CREATE INDEX Message_hasTag_Tag_TagId ON Message_hasTag_Tag (TagId);
  34. -- Umbra manually materializes this using load_plm (queries.py)
  35. CREATE OR REPLACE VIEW Person_likes_Message AS
  36. (SELECT creationDate, PersonId, CommentId as MessageId FROM Person_likes_Comment)
  37. UNION
  38. (SELECT creationDate, PersonId, PostId as MessageId FROM Person_likes_Post);
  39. CREATE INDEX Person_likes_Message_PersonId ON Person_likes_Message (PersonId);
  40. CREATE INDEX Person_likes_Message_MessageId ON Person_likes_Message (MessageId);
  41. -- A recursive materialized view containing the root Post of each Message (for Posts, themselves, for Comments, traversing up the Message thread to the root Post of the tree)
  42. CREATE OR REPLACE VIEW Message AS
  43. WITH MUTUALLY RECURSIVE
  44. -- compute the transitive closure (with root information) using minimnal info
  45. roots (MessageId bigint, RootPostId bigint, RootPostLanguage text, ContainerForumId bigint, ParentMessageId bigint) AS
  46. ( SELECT id AS MessageId, id AS RootPostId, language AS RootPostLanguage, ContainerForumId, NULL::bigint AS ParentMessageId FROM Post
  47. UNION SELECT
  48. Comment.id AS MessageId,
  49. ParentPostId AS RootPostId,
  50. language AS RootPostLanguage,
  51. Post.ContainerForumId AS ContainerForumId,
  52. ParentPostId AS ParentMessageId
  53. FROM Comment
  54. JOIN Post
  55. ON Comment.ParentPostId = Post.id),
  56. ms (MessageId bigint, RootPostId bigint, RootPostLanguage text, ContainerForumId bigint, ParentMessageId bigint) AS
  57. ( SELECT *
  58. FROM roots
  59. UNION SELECT
  60. Comment.id AS MessageId,
  61. ms.RootPostId AS RootPostId,
  62. ms.RootPostLanguage AS RootPostLanguage,
  63. ms.ContainerForumId AS ContainerForumId,
  64. ParentCommentId AS ParentMessageId
  65. FROM Comment
  66. JOIN ms
  67. ON ParentCommentId = ms.MessageId)
  68. -- now do the late materialization
  69. ( SELECT
  70. creationDate,
  71. id AS MessageId,
  72. id AS RootPostId,
  73. language AS RootPostLanguage,
  74. content,
  75. imageFile,
  76. locationIP,
  77. browserUsed,
  78. length,
  79. CreatorPersonId,
  80. ContainerForumId,
  81. LocationCountryId,
  82. NULL::bigint AS ParentMessageId
  83. FROM Post
  84. UNION (SELECT
  85. Comment.creationDate AS creationDate,
  86. Comment.id AS MessageId,
  87. ms.RootPostId AS RootPostId,
  88. ms.RootPostLanguage AS RootPostLanguage,
  89. Comment.content AS content,
  90. NULL::text AS imageFile,
  91. Comment.locationIP AS locationIP,
  92. Comment.browserUsed AS browserUsed,
  93. Comment.length AS length,
  94. Comment.CreatorPersonId AS CreatorPersonId,
  95. ms.ContainerForumId AS ContainerForumId,
  96. Comment.LocationCountryId AS LocationCityId,
  97. ms.ParentMessageId AS ParentMessageId
  98. FROM Comment
  99. JOIN ms
  100. ON Comment.id = ms.MessageId));
  101. -- every message accounts for exactly one post or comment:
  102. --
  103. -- materialize=> select count(*) from Message;
  104. -- count
  105. -- ---------
  106. -- 2860664
  107. -- (1 row)
  108. --
  109. -- Time: 1005301.819 ms (16:45.302)
  110. -- materialize=> select (select count(*) as num_posts from Post) + (select count(*) as num_comments from Comment);
  111. -- ?column?
  112. -- ----------
  113. -- 2860664
  114. -- (1 row)
  115. --
  116. -- Time: 18313.558 ms (00:18.314)
  117. CREATE INDEX Message_MessageId ON Message (MessageId);
  118. CREATE INDEX Message_ContainerForumId ON Message (ContainerForumId);
  119. CREATE INDEX Message_ParentMessageId ON Message (ParentMessageId);
  120. CREATE INDEX Message_CreatorPersonId ON Message (CreatorPersonId);
  121. CREATE INDEX Message_RootPostLanguage ON Message (RootPostLanguage);
  122. -- views
  123. CREATE OR REPLACE VIEW Comment_View AS
  124. SELECT creationDate, MessageId AS id, locationIP, browserUsed, content, length, CreatorPersonId, LocationCountryId, ParentMessageId
  125. FROM Message
  126. WHERE ParentMessageId IS NOT NULL;
  127. CREATE OR REPLACE VIEW Post_View AS
  128. SELECT creationDate, MessageId AS id, imageFile, locationIP, browserUsed, RootPostLanguage, content, length, CreatorPersonId, ContainerForumId, LocationCountryId
  129. FROM Message
  130. WHERE ParentMessageId IS NULL;