index-advice.td 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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. > CREATE CLUSTER c1 REPLICAS (r1 (SIZE '1'));
  10. > CREATE CLUSTER c2 REPLICAS (r1 (SIZE '1'));
  11. > CREATE MATERIALIZED VIEW v11 IN CLUSTER c1 AS (SELECT 1);
  12. > CREATE VIEW v21 AS (SELECT * FROM v11);
  13. > CREATE DEFAULT INDEX IN CLUSTER c1 ON v21;
  14. > CREATE VIEW v31 AS (SELECT * FROM v21);
  15. > CREATE VIEW v32 AS (SELECT * FROM v21);
  16. > CREATE DEFAULT INDEX IN CLUSTER c1 ON v32;
  17. > CREATE VIEW v41 AS (SELECT * FROM v31);
  18. > CREATE DEFAULT INDEX IN CLUSTER c2 ON v41;
  19. > CREATE DEFAULT INDEX IN CLUSTER c1 ON v41;
  20. > CREATE MATERIALIZED VIEW v42 IN CLUSTER c1 AS (SELECT * FROM v31 UNION ALL SELECT * FROM v32);
  21. > CREATE DEFAULT INDEX IN CLUSTER c2 ON v42;
  22. # dependency graph of the above objects
  23. #
  24. # ┌────────────────────────┐
  25. # │ v11 │
  26. # ┌──────────►│ materialized view (c1) │
  27. # │ └────────────────────────┘
  28. # │
  29. # │
  30. # │
  31. # ┌─────────┴─────────┐
  32. # │ v21 │
  33. # ┌──────────►│ index (c1) │◄──────────┐
  34. # │ └───────────────────┘ │
  35. # │ │
  36. # │ │
  37. # │ │
  38. # ┌─────────┴─────────┐ ┌─────────┴─────────┐
  39. # │ v31 │ │ v32 │
  40. # ┌───────────►│ view │◄──────────┬──────────►│ index (c1) │
  41. # │ └───────────────────┘ │ └───────────────────┘
  42. # │ │
  43. # │ │
  44. # │ │
  45. # ┌─────────┴─────────┐ ┌───────────┴────────────┐
  46. # │ v41 │ │ v42 │
  47. # │ index (c1, c2) │ │ materialized view (c1) │
  48. # └───────────────────┘ │ index (c2) │
  49. # └────────────────────────┘
  50. #
  51. # With these dependencies, the index on v32 and the materialized view on v11 should be removed
  52. # because these objects only have a single maintained dependency. But an index should be added
  53. # on v31 because the work can be reused by v41 and v42. Moreover, v41 should be turned into a
  54. # materialized view because the result is currently recomputed by the index on the remote
  55. # cluster c2.
  56. > SELECT o.name, a.hint, a.details FROM mz_internal.mz_index_advice AS a JOIN mz_objects AS o ON (a.object_id = o.id) ORDER BY name;
  57. name hint details
  58. --------------------------------------------------------------------------------------------------------------------------------
  59. v11 "convert to a view" "no dependencies from sinks nor from objects on different clusters"
  60. v21_primary_idx "keep" "multiple downstream dependencies: {v31,v42}"
  61. v31 "add index" "multiple downstream dependencies: {v41,v42}"
  62. v32_primary_idx "drop unless queried directly" "fewer than two downstream dependencies: {v42}"
  63. v41 "convert to materialized view" "dependencies on multiple clusters: {v41_primary_idx,v41_primary_idx1}"
  64. v41_primary_idx "drop unless queried directly" "associated object does not have any dependencies (maintained or not maintained)"
  65. v41_primary_idx1 "drop unless queried directly" "associated object does not have any dependencies (maintained or not maintained)"
  66. v42 "keep" "dependencies from sinks or objects on different clusters: {v42_primary_idx}"
  67. v42_primary_idx "drop unless queried directly" "associated object does not have any dependencies (maintained or not maintained)"
  68. # verify that all indexes and materialized views have a recommendation
  69. > SELECT id FROM mz_objects WHERE id LIKE 'u%' AND type IN ('index', 'materialized-view')
  70. EXCEPT ALL
  71. SELECT o.id FROM mz_internal.mz_index_advice AS a INNER JOIN mz_objects AS o ON (a.object_id = o.id) WHERE o.type IN ('index', 'materialized-view');
  72. # cleanup test
  73. > DROP MATERIALIZED VIEW v11 CASCADE;
  74. > CREATE MATERIALIZED VIEW v11 IN CLUSTER c1 AS (SELECT 1);
  75. > CREATE DEFAULT INDEX IN CLUSTER c1 ON v11;
  76. > SELECT o.name, a.hint, a.details FROM mz_internal.mz_index_advice AS a JOIN mz_objects AS o ON (a.object_id = o.id) ORDER BY name;
  77. name hint details
  78. ---------------------------------------------------------------------------------------------------------------------------------
  79. v11 "convert to a view with an index" "no dependencies from sinks nor from objects on different clusters, but maintained dependencies on the same cluster: {v11_primary_idx}"
  80. v11_primary_idx "drop unless queried directly" "associated object does not have any dependencies (maintained or not maintained)"
  81. # verify that all indexes and materialized views have a recommendation
  82. > SELECT id FROM mz_objects WHERE id LIKE 'u%' AND type IN ('index', 'materialized-view')
  83. EXCEPT ALL
  84. SELECT o.id FROM mz_internal.mz_index_advice AS a INNER JOIN mz_objects AS o ON (a.object_id = o.id) WHERE o.type IN ('index', 'materialized-view');
  85. # cleanup test
  86. > DROP MATERIALIZED VIEW v11 CASCADE;
  87. > CREATE VIEW v11 AS (SELECT 1);
  88. > CREATE DEFAULT INDEX IN CLUSTER c1 ON v11;
  89. > SELECT o.name, a.hint, a.details FROM mz_internal.mz_index_advice AS a JOIN mz_objects AS o ON (a.object_id = o.id) ORDER BY name;
  90. name hint details
  91. ---------------------------------------------------------------------------------------------------------------------------------
  92. v11_primary_idx "drop unless queried directly" "associated object does not have any dependencies (maintained or not maintained)"
  93. # verify that all indexes and materialized views have a recommendation
  94. > SELECT id FROM mz_objects WHERE id LIKE 'u%' AND type IN ('index', 'materialized-view')
  95. EXCEPT ALL
  96. SELECT o.id FROM mz_internal.mz_index_advice AS a INNER JOIN mz_objects AS o ON (a.object_id = o.id) WHERE o.type IN ('index', 'materialized-view');
  97. # cleanup test
  98. > DROP VIEW v11 CASCADE;
  99. > CREATE SOURCE s1
  100. IN CLUSTER c1
  101. FROM LOAD GENERATOR COUNTER (UP TO 100);
  102. > CREATE DEFAULT INDEX IN CLUSTER c1 ON s1;
  103. > SELECT o.name, a.hint, a.details FROM mz_internal.mz_index_advice AS a JOIN mz_objects AS o ON (a.object_id = o.id) ORDER BY name;
  104. name hint details
  105. ---------------------------------------------------------------------------------------------------------------------------------
  106. s1_primary_idx "drop unless queried directly" "sources do not transform data and can expose data directly"
  107. # verify that all indexes and materialized views have a recommendation
  108. > SELECT id FROM mz_objects WHERE id LIKE 'u%' AND type IN ('index', 'materialized-view')
  109. EXCEPT ALL
  110. SELECT o.id FROM mz_internal.mz_index_advice AS a INNER JOIN mz_objects AS o ON (a.object_id = o.id) WHERE o.type IN ('index', 'materialized-view');
  111. > DROP SOURCE s1 CASCADE;
  112. > CREATE SOURCE s1
  113. IN CLUSTER c1
  114. FROM LOAD GENERATOR COUNTER (UP TO 100);
  115. > CREATE VIEW v21 AS (SELECT * FROM s1);
  116. > CREATE DEFAULT INDEX IN CLUSTER c1 ON v21;
  117. > CREATE VIEW v22 AS (SELECT * FROM s1);
  118. > CREATE DEFAULT INDEX IN CLUSTER c1 ON v22;
  119. > SELECT o.name, a.hint, a.details FROM mz_internal.mz_index_advice AS a JOIN mz_objects AS o ON (a.object_id = o.id) ORDER BY name;
  120. name hint details
  121. ---------------------------------------------------------------------------------------------------------------------------------
  122. v21_primary_idx "drop unless queried directly" "associated object does not have any dependencies (maintained or not maintained)"
  123. v22_primary_idx "drop unless queried directly" "associated object does not have any dependencies (maintained or not maintained)"
  124. # verify that all indexes and materialized views have a recommendation
  125. > SELECT id FROM mz_objects WHERE id LIKE 'u%' AND type IN ('index', 'materialized-view')
  126. EXCEPT ALL
  127. SELECT o.id FROM mz_internal.mz_index_advice AS a INNER JOIN mz_objects AS o ON (a.object_id = o.id) WHERE o.type IN ('index', 'materialized-view');
  128. > DROP SOURCE s1 CASCADE;
  129. > CREATE SOURCE s1
  130. IN CLUSTER c1
  131. FROM LOAD GENERATOR COUNTER (UP TO 100);
  132. > CREATE DEFAULT INDEX IN CLUSTER c1 ON s1;
  133. > CREATE VIEW v21 AS (SELECT * FROM s1);
  134. > CREATE DEFAULT INDEX IN CLUSTER c1 ON v21;
  135. > CREATE VIEW v22 AS (SELECT * FROM s1);
  136. > CREATE DEFAULT INDEX IN CLUSTER c1 ON v22;
  137. > SELECT o.name, a.hint, a.details FROM mz_internal.mz_index_advice AS a JOIN mz_objects AS o ON (a.object_id = o.id) ORDER BY name;
  138. name hint details
  139. ---------------------------------------------------------------------------------------------------------------------------------
  140. s1_primary_idx "drop unless queried directly" "sources do not transform data and can expose data directly"
  141. v21_primary_idx "drop unless queried directly" "associated object does not have any dependencies (maintained or not maintained)"
  142. v22_primary_idx "drop unless queried directly" "associated object does not have any dependencies (maintained or not maintained)"
  143. # verify that all indexes and materialized views have a recommendation
  144. > SELECT id FROM mz_objects WHERE id LIKE 'u%' AND type IN ('index', 'materialized-view')
  145. EXCEPT ALL
  146. SELECT o.id FROM mz_internal.mz_index_advice AS a INNER JOIN mz_objects AS o ON (a.object_id = o.id) WHERE o.type IN ('index', 'materialized-view');
  147. > DROP SOURCE s1 CASCADE;