Guest User

Untitled

a guest
Aug 2nd, 2021
312
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. BEGIN;
  2. -- A table of measured values with an index on identifier
  3. DROP TABLE IF EXISTS measured CASCADE;
  4. CREATE TABLE
  5. measured
  6. (
  7.   id int,
  8.   ts timestamp,
  9.   identifier text,
  10.   measured_value int
  11. )
  12. ;
  13. CREATE INDEX ON measured(identifier);
  14.  
  15. -- A table of estimated values with an index on identifier
  16. DROP TABLE IF EXISTS estimated CASCADE;
  17. CREATE TEMPORARY TABLE
  18. estimated
  19. (
  20.   id int,
  21.   ts timestamp,
  22.   identifier character varying,
  23.   estimated_value int
  24. )
  25. ;
  26. CREATE INDEX ON estimated(identifier);
  27.  
  28. -- Delete any existing test data
  29. TRUNCATE TABLE measured;
  30. TRUNCATE TABLE estimated;
  31.  
  32. -- Generate lots of random data
  33. INSERT INTO measured
  34. SELECT
  35. generate_series(1, 1000000),
  36. to_timestamp((random() * 100000)::int),
  37. left(md5(random()::text), 2),
  38. random() * 10
  39. ;
  40. INSERT INTO estimated
  41. SELECT
  42. generate_series(1, 1000000),
  43. to_timestamp((random() * 100000)::int),
  44. left(md5(random()::text), 2),
  45. random() * 10
  46. ;
  47.  
  48. -- A view to look at for estimated and measured values
  49. -- for the same timestamp and identifier
  50. DROP VIEW IF EXISTS combined;
  51. CREATE TEMPORARY VIEW
  52. combined
  53. AS
  54. SELECT
  55. COALESCE(measured.ts, estimated.ts) AS ts,
  56. COALESCE(measured.identifier, estimated.identifier) AS identifier,
  57. measured_value,
  58. estimated_value
  59. FROM
  60. measured
  61. FULL OUTER JOIN
  62. estimated
  63. ON
  64. measured.identifier = estimated.identifier
  65. AND
  66. measured.ts = estimated.ts
  67. ;
  68.  
  69. EXPLAIN ANALYZE
  70. SELECT
  71. *
  72. FROM
  73. combined
  74. WHERE
  75. identifier = 'ab'
  76. ;
  77.  
  78. ROLLBACK;
  79.  
Advertisement
Add Comment
Please, Sign In to add comment