Advertisement
Guest User

Untitled

a guest
Feb 6th, 2016
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.38 KB | None | 0 0
  1. -- clean up database from previous test
  2. DROP SCHEMA public CASCADE;
  3. CREATE SCHEMA public;
  4.  
  5. -- create EAV tables
  6. CREATE TABLE entity (
  7. id SERIAL PRIMARY KEY,
  8. name TEXT,
  9. description TEXT
  10. );
  11. CREATE TABLE entity_attribute (
  12. id SERIAL PRIMARY KEY,
  13. name TEXT,
  14. description TEXT
  15. );
  16. CREATE TABLE entity_attribute_value (
  17. id SERIAL PRIMARY KEY,
  18. entity_id INT REFERENCES entity(id),
  19. entity_attribute_id INT REFERENCES entity_attribute(id),
  20. value TEXT
  21. );
  22.  
  23. -- create entities
  24. INSERT INTO entity(name, description)
  25. SELECT 'entity_' || i, 'Test entity no. ' || i
  26. FROM generate_series(1, 1000000) AS i;
  27.  
  28. -- create attributes
  29. INSERT INTO entity_attribute(name, description)
  30. VALUES
  31. ('color', 'The color of the entity') -- id = 1
  32. , ('lenght', 'The lenght of the entity') -- id = 2
  33. , ('width', 'The width of the entity') -- id = 3
  34. , ('hassomething', 'A bool expressing if the entity has something') -- id = 4
  35. , ('country', 'The home country of the entity'); -- id = 5
  36.  
  37. -- insert default values for entities and attributes
  38. INSERT INTO entity_attribute_value(entity_id, entity_attribute_id, value)
  39. SELECT i, 1, 'red' FROM generate_series(1, 1000000) AS i;
  40. INSERT INTO entity_attribute_value(entity_id, entity_attribute_id, value)
  41. SELECT i, 2, '120' FROM generate_series(1, 1000000) AS i;
  42. INSERT INTO entity_attribute_value(entity_id, entity_attribute_id, value)
  43. SELECT i, 3, '3.1882420' FROM generate_series(1, 1000000) AS i;
  44. INSERT INTO entity_attribute_value(entity_id, entity_attribute_id, value)
  45. SELECT i, 4, 'true' FROM generate_series(1, 1000000) AS i;
  46. INSERT INTO entity_attribute_value(entity_id, entity_attribute_id, value)
  47. SELECT i, 5, 'Belgium' FROM generate_series(1, 1000000) AS i;
  48.  
  49. -- create JSONB table
  50. CREATE TABLE entity_jsonb (
  51. id SERIAL PRIMARY KEY,
  52. name TEXT,
  53. description TEXT,
  54. properties JSONB
  55. );
  56.  
  57. -- insert the same data
  58. INSERT INTO entity_jsonb (name, description, properties)
  59. SELECT 'entity_' || i, 'Test entity no. ' || i,
  60. json_build_object( 'color','red',
  61. 'lenght', 120,
  62. 'width', 3.1882420,
  63. 'hassomething', true,
  64. 'country', 'Belgium'
  65. )
  66. FROM generate_series(1, 1000000) AS i;
  67.  
  68. -- VACUUM ANALYZE;
  69. VACUUM ANALYZE entity;
  70. VACUUM ANALYZE entity_attribute;
  71. VACUUM ANALYZE entity_attribute_value;
  72. VACUUM ANALYZE entity_jsonb;
  73.  
  74. SELECT 'Update some records' AS NextTest;
  75.  
  76. EXPLAIN ANALYSE UPDATE entity_jsonb SET properties = jsonb_set(properties, '{"color"}', '"blue"') WHERE id = 120;
  77. EXPLAIN ANALYSE UPDATE entity_jsonb SET properties = jsonb_set(properties, '{"hassomething"}', 'false') WHERE id = 1200;
  78. EXPLAIN ANALYSE UPDATE entity_jsonb SET properties = jsonb_set(properties, '{"lenght"}', '256') WHERE id = 20;
  79. EXPLAIN ANALYSE UPDATE entity_jsonb SET properties = jsonb_set(properties, '{"width"}', '1.20') WHERE id = 5012;
  80.  
  81.  
  82. EXPLAIN ANALYSE UPDATE entity_attribute_value SET value = 'blue' WHERE entity_attribute_id = 1 AND entity_id = 120;
  83. EXPLAIN ANALYSE UPDATE entity_attribute_value SET value = 'false' WHERE entity_attribute_id = 4 AND entity_id = 1200;
  84. EXPLAIN ANALYSE UPDATE entity_attribute_value SET value = '256' WHERE entity_attribute_id = 2 AND entity_id = 20;
  85. EXPLAIN ANALYSE UPDATE entity_attribute_value SET value = '1.20' WHERE entity_attribute_id = 3 AND entity_id = 5012;
  86.  
  87. SELECT 'Select all entity_names that have a certain property' AS NextTest;
  88.  
  89. EXPLAIN ANALYSE SELECT name FROM entity_jsonb WHERE properties ->> 'color' = 'blue';
  90. EXPLAIN ANALYSE SELECT name FROM entity_jsonb WHERE properties @> '{"color": "blue"}';
  91. EXPLAIN ANALYSE SELECT name FROM entity_jsonb WHERE properties ->> 'hassomething' = 'false';
  92.  
  93. EXPLAIN ANALYSE SELECT e.name FROM entity e
  94. INNER JOIN entity_attribute_value eav ON e.id = eav.entity_id
  95. INNER JOIN entity_attribute ea ON eav.entity_attribute_id = ea.id
  96. WHERE ea.name = 'color' AND eav.value = 'blue';
  97. EXPLAIN ANALYSE SELECT e.name FROM entity e
  98. INNER JOIN entity_attribute_value eav ON e.id = eav.entity_id
  99. INNER JOIN entity_attribute ea ON eav.entity_attribute_id = ea.id
  100. WHERE ea.name = 'hassomething' AND eav.value = 'false';
  101.  
  102. -- REPEAT WITH INDEXES
  103. CREATE INDEX ON entity_attribute_value (entity_id);
  104. CREATE INDEX ON entity_attribute_value (entity_attribute_id);
  105.  
  106. CREATE INDEX ON entity_jsonb USING GIN (properties);
  107.  
  108. SELECT 'Update some records (with indexes)' AS NextTest;
  109.  
  110. EXPLAIN ANALYSE UPDATE entity_jsonb SET properties = jsonb_set(properties, '{"color"}', '"blue"') WHERE id = 121;
  111. EXPLAIN ANALYSE UPDATE entity_jsonb SET properties = jsonb_set(properties, '{"hassomething"}', 'false') WHERE id = 1201;
  112. EXPLAIN ANALYSE UPDATE entity_jsonb SET properties = jsonb_set(properties, '{"lenght"}', '256') WHERE id = 21;
  113. EXPLAIN ANALYSE UPDATE entity_jsonb SET properties = jsonb_set(properties, '{"width"}', '1.20') WHERE id = 5013;
  114.  
  115.  
  116. EXPLAIN ANALYSE UPDATE entity_attribute_value SET value = 'blue' WHERE entity_attribute_id = 1 AND entity_id = 121;
  117. EXPLAIN ANALYSE UPDATE entity_attribute_value SET value = 'false' WHERE entity_attribute_id = 4 AND entity_id = 1201;
  118. EXPLAIN ANALYSE UPDATE entity_attribute_value SET value = '256' WHERE entity_attribute_id = 2 AND entity_id = 21;
  119. EXPLAIN ANALYSE UPDATE entity_attribute_value SET value = '1.20' WHERE entity_attribute_id = 3 AND entity_id = 5013;
  120.  
  121. SELECT 'Select all entity_names that have a certain property (with indexes)' AS NextTest;
  122.  
  123. EXPLAIN ANALYSE SELECT name FROM entity_jsonb WHERE properties ->> 'color' = 'blue';
  124. EXPLAIN ANALYSE SELECT name FROM entity_jsonb WHERE properties @> '{"color": "blue"}';
  125. EXPLAIN ANALYSE SELECT name FROM entity_jsonb WHERE properties ->> 'hassomething' = 'false';
  126.  
  127. EXPLAIN ANALYSE SELECT e.name FROM entity e
  128. INNER JOIN entity_attribute_value eav ON e.id = eav.entity_id
  129. INNER JOIN entity_attribute ea ON eav.entity_attribute_id = ea.id
  130. WHERE ea.name = 'color' AND eav.value = 'blue';
  131. EXPLAIN ANALYSE SELECT e.name FROM entity e
  132. INNER JOIN entity_attribute_value eav ON e.id = eav.entity_id
  133. INNER JOIN entity_attribute ea ON eav.entity_attribute_id = ea.id
  134. WHERE ea.name = 'hassomething' AND eav.value = 'false';
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement