materialized_view.slt 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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. statement ok
  10. CREATE TABLE accounts(id int, balance int);
  11. statement ok
  12. CREATE OR REPLACE MATERIALIZED VIEW mv AS
  13. SELECT * FROM accounts WHERE balance = 100;
  14. mode cockroach
  15. # baseline explain (no index used)
  16. query T multiline
  17. EXPLAIN OPTIMIZED PLAN WITH (humanized expressions) AS VERBOSE TEXT FOR MATERIALIZED VIEW mv;
  18. ----
  19. materialize.public.mv:
  20. Filter (#1{balance} = 100)
  21. ReadStorage materialize.public.accounts
  22. Source materialize.public.accounts
  23. filter=((#1{balance} = 100))
  24. Target cluster: quickstart
  25. EOF
  26. statement ok
  27. CREATE INDEX accounts_balance_idx ON accounts(balance);
  28. # ensure that the index is still not used
  29. query T multiline
  30. EXPLAIN OPTIMIZED PLAN WITH (humanized expressions) AS VERBOSE TEXT FOR MATERIALIZED VIEW mv;
  31. ----
  32. materialize.public.mv:
  33. Filter (#1{balance} = 100)
  34. ReadStorage materialize.public.accounts
  35. Source materialize.public.accounts
  36. filter=((#1{balance} = 100))
  37. Target cluster: quickstart
  38. EOF
  39. query T multiline
  40. EXPLAIN PHYSICAL PLAN AS VERBOSE TEXT FOR MATERIALIZED VIEW mv;
  41. ----
  42. materialize.public.mv:
  43. Get::Collection materialize.public.accounts
  44. raw=true
  45. Source materialize.public.accounts
  46. filter=((#1{balance} = 100))
  47. Target cluster: quickstart
  48. EOF
  49. # re-create the view so it can pick up the index
  50. statement ok
  51. CREATE OR REPLACE MATERIALIZED VIEW mv AS
  52. SELECT * FROM accounts WHERE balance = 100;
  53. # ensure that the index is now used by the view
  54. query T multiline
  55. EXPLAIN OPTIMIZED PLAN WITH (humanized expressions) AS VERBOSE TEXT FOR MATERIALIZED VIEW mv;
  56. ----
  57. materialize.public.mv:
  58. Project (#0{id}, #1{balance})
  59. ReadIndex on=materialize.public.accounts accounts_balance_idx=[lookup value=(100)]
  60. Used Indexes:
  61. - materialize.public.accounts_balance_idx (lookup)
  62. Target cluster: quickstart
  63. EOF
  64. query T multiline
  65. EXPLAIN PHYSICAL PLAN AS VERBOSE TEXT FOR MATERIALIZED VIEW mv;
  66. ----
  67. materialize.public.mv:
  68. Join::Linear
  69. linear_stage[0]
  70. closure
  71. project=(#1, #0)
  72. lookup={ relation=0, key=[#1{balance}] }
  73. stream={ key=[#0], thinning=() }
  74. source={ relation=1, key=[#0] }
  75. Get::PassArrangements materialize.public.accounts
  76. raw=false
  77. arrangements[0]={ key=[#1{balance}], permutation={#0: #1, #1: #0}, thinning=(#0) }
  78. types=[integer?, integer?]
  79. ArrangeBy
  80. raw=true
  81. arrangements[0]={ key=[#0], permutation=id, thinning=() }
  82. types=[integer]
  83. Constant
  84. - (100)
  85. Used Indexes:
  86. - materialize.public.accounts_balance_idx (lookup)
  87. Target cluster: quickstart
  88. EOF
  89. # rename the index
  90. statement ok
  91. ALTER INDEX accounts_balance_idx RENAME TO accounts_balance_index;
  92. # ensure that the index is still used by the view
  93. query T multiline
  94. EXPLAIN OPTIMIZED PLAN WITH (humanized expressions) AS VERBOSE TEXT FOR MATERIALIZED VIEW mv;
  95. ----
  96. materialize.public.mv:
  97. Project (#0{id}, #1{balance})
  98. ReadIndex on=materialize.public.accounts accounts_balance_index=[lookup value=(100)]
  99. Used Indexes:
  100. - materialize.public.accounts_balance_index (lookup)
  101. Target cluster: quickstart
  102. EOF
  103. query T multiline
  104. EXPLAIN PHYSICAL PLAN AS VERBOSE TEXT FOR MATERIALIZED VIEW mv;
  105. ----
  106. materialize.public.mv:
  107. Join::Linear
  108. linear_stage[0]
  109. closure
  110. project=(#1, #0)
  111. lookup={ relation=0, key=[#1{balance}] }
  112. stream={ key=[#0], thinning=() }
  113. source={ relation=1, key=[#0] }
  114. Get::PassArrangements materialize.public.accounts
  115. raw=false
  116. arrangements[0]={ key=[#1{balance}], permutation={#0: #1, #1: #0}, thinning=(#0) }
  117. types=[integer?, integer?]
  118. ArrangeBy
  119. raw=true
  120. arrangements[0]={ key=[#0], permutation=id, thinning=() }
  121. types=[integer]
  122. Constant
  123. - (100)
  124. Used Indexes:
  125. - materialize.public.accounts_balance_index (lookup)
  126. Target cluster: quickstart
  127. EOF
  128. # drop the index
  129. statement ok
  130. DROP INDEX accounts_balance_index;
  131. # The index is still used by the view
  132. query T multiline
  133. EXPLAIN OPTIMIZED PLAN WITH (humanized expressions) AS VERBOSE TEXT FOR MATERIALIZED VIEW mv;
  134. ----
  135. materialize.public.mv:
  136. Project (#0{id}, #1{balance})
  137. ReadIndex on=materialize.public.accounts [DELETED INDEX]=[lookup value=(100)]
  138. Used Indexes:
  139. - [DELETED INDEX] (lookup)
  140. Target cluster: quickstart
  141. EOF
  142. query T multiline
  143. EXPLAIN PHYSICAL PLAN AS VERBOSE TEXT FOR MATERIALIZED VIEW mv;
  144. ----
  145. materialize.public.mv:
  146. Join::Linear
  147. linear_stage[0]
  148. closure
  149. project=(#1, #0)
  150. lookup={ relation=0, key=[#1{balance}] }
  151. stream={ key=[#0], thinning=() }
  152. source={ relation=1, key=[#0] }
  153. Get::PassArrangements materialize.public.accounts
  154. raw=false
  155. arrangements[0]={ key=[#1{balance}], permutation={#0: #1, #1: #0}, thinning=(#0) }
  156. types=[integer?, integer?]
  157. ArrangeBy
  158. raw=true
  159. arrangements[0]={ key=[#0], permutation=id, thinning=() }
  160. types=[integer]
  161. Constant
  162. - (100)
  163. Used Indexes:
  164. - [DELETED INDEX] (lookup)
  165. Target cluster: quickstart
  166. EOF