quickstart.td 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. # This test verifies the Quickstart page works: https://materialize.com/docs/get-started/quickstart/
  10. # Uses shared compute+storage cluster
  11. $ set-sql-timeout duration=60s
  12. $ postgres-execute connection=postgres://mz_system:materialize@${testdrive.materialize-internal-sql-addr}
  13. ALTER SYSTEM SET enable_create_table_from_source = true
  14. > CREATE CLUSTER quickstart_tutorial REPLICAS (r1 (SIZE '4'));
  15. > SET CLUSTER = quickstart_tutorial
  16. > CREATE SOURCE auction_house
  17. IN CLUSTER quickstart_tutorial
  18. FROM LOAD GENERATOR AUCTION
  19. (TICK INTERVAL '0.05s', UP TO 1000)
  20. > CREATE TABLE accounts FROM SOURCE auction_house (REFERENCE accounts);
  21. > CREATE TABLE auctions FROM SOURCE auction_house (REFERENCE auctions);
  22. > CREATE TABLE bids FROM SOURCE auction_house (REFERENCE bids);
  23. > CREATE TABLE organizations FROM SOURCE auction_house (REFERENCE organizations);
  24. > CREATE TABLE users FROM SOURCE auction_house (REFERENCE users);
  25. > SHOW SOURCES
  26. name type cluster comment
  27. --------------------------------------------------------------------------
  28. auction_house load-generator quickstart_tutorial ""
  29. auction_house_progress progress <null> ""
  30. > SHOW TABLES
  31. name comment
  32. --------------------------------
  33. accounts ""
  34. bids ""
  35. auctions ""
  36. organizations ""
  37. users ""
  38. > SELECT id, seller, item FROM auctions WHERE id = 1
  39. 1 1824 "Best Pizza in Town"
  40. > SELECT id, buyer, auction_id, amount FROM bids WHERE id = 10
  41. 10 3844 1 59
  42. > CREATE VIEW winning_bids AS
  43. SELECT DISTINCT ON (auctions.id) bids.*, auctions.item, auctions.seller
  44. FROM auctions, bids
  45. WHERE auctions.id = bids.auction_id
  46. AND bids.bid_time < auctions.end_time
  47. AND mz_now() >= auctions.end_time
  48. ORDER BY auctions.id,
  49. bids.bid_time DESC,
  50. bids.amount,
  51. bids.buyer
  52. > SELECT id, buyer, auction_id, amount, item, seller FROM winning_bids WHERE id = 18
  53. 18 1036 1 86 "Best Pizza in Town" 1824
  54. > CREATE INDEX wins_by_item ON winning_bids (item)
  55. > CREATE INDEX wins_by_bidder ON winning_bids (buyer)
  56. > CREATE INDEX wins_by_seller ON winning_bids (seller)
  57. ? EXPLAIN OPTIMIZED PLAN AS VERBOSE TEXT FOR SELECT * FROM winning_bids WHERE item = 'Best Pizza in Town' ORDER BY bid_time DESC
  58. Explained Query (fast path):
  59. Finish order_by=[#4 desc nulls_first] output=[#0..=#6]
  60. Project (#1..=#5, #0, #6)
  61. ReadIndex on=materialize.public.winning_bids wins_by_item=[lookup value=("Best Pizza in Town")]
  62. Used Indexes:
  63. - materialize.public.wins_by_item (lookup)
  64. Target cluster: quickstart_tutorial
  65. > SELECT id, buyer, auction_id, amount, item, seller FROM winning_bids WHERE item = 'Best Pizza in Town' AND id < 100 ORDER BY bid_time DESC
  66. 62 3016 6 77 "Best Pizza in Town" 2500
  67. 46 2530 4 49 "Best Pizza in Town" 2822
  68. 18 1036 1 86 "Best Pizza in Town" 1824
  69. > CREATE VIEW fraud_activity AS
  70. SELECT w2.seller,
  71. w2.item AS seller_item,
  72. w2.amount AS seller_amount,
  73. w1.item buyer_item,
  74. w1.amount buyer_amount
  75. FROM winning_bids w1,
  76. winning_bids w2
  77. WHERE w1.buyer = w2.seller
  78. AND w2.amount > w1.amount
  79. > SELECT * FROM fraud_activity where seller_item = 'Gift Basket' and seller = 3408
  80. 3408 "Gift Basket" 69 "Gift Basket" 58
  81. > CREATE TABLE fraud_accounts (id bigint)
  82. $ set-regex match=\d{13,20} replacement=<TIMESTAMP>
  83. > BEGIN
  84. > DECLARE c CURSOR FOR SUBSCRIBE TO (
  85. SELECT buyer
  86. FROM winning_bids
  87. WHERE buyer NOT IN (SELECT id FROM fraud_accounts) AND buyer = 12
  88. GROUP BY buyer
  89. ORDER BY 1 ASC LIMIT 5
  90. )
  91. > FETCH 1 c WITH (timeout='30s')
  92. <TIMESTAMP> 1 12
  93. $ postgres-execute connection=postgres://materialize:materialize@${testdrive.materialize-sql-addr}
  94. INSERT INTO fraud_accounts VALUES (12)
  95. > FETCH 1 c WITH (timeout='30s')
  96. <TIMESTAMP> -1 12
  97. > COMMIT
  98. > CREATE VIEW funds_movement AS
  99. SELECT id, SUM(credits) as credits, SUM(debits) as debits
  100. FROM (
  101. SELECT seller as id, amount as credits, 0 as debits
  102. FROM winning_bids
  103. UNION ALL
  104. SELECT buyer as id, 0 as credits, amount as debits
  105. FROM winning_bids
  106. )
  107. GROUP BY id
  108. # > SELECT SUM(credits), SUM(debits) FROM funds_movement
  109. # 37529 37529
  110. #
  111. # > BEGIN
  112. # > DECLARE c CURSOR FOR SUBSCRIBE TO (
  113. # SELECT SUM(credits), SUM(debits) FROM funds_movement
  114. # )
  115. # > FETCH 1 c WITH (timeout='1s')