Advertisement
rockdrilla

pgsql 10 hash index (large test)

Oct 10th, 2017
432
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. testdb=# CREATE TABLE table1 (
  2. testdb(#   "id"     SERIAL NOT NULL,
  3. testdb(#   "value"  TEXT NOT NULL
  4. testdb(# );
  5. CREATE TABLE
  6.  
  7. testdb=# ALTER TABLE ONLY "table1"
  8. testdb-#   ADD CONSTRAINT "table1_PK_id" PRIMARY KEY ("id"),
  9. testdb-#   ADD CONSTRAINT "table1_UN_value" UNIQUE ("value");
  10. ALTER TABLE
  11.  
  12. testdb=# CREATE TABLE table2 (
  13. testdb(#   "id"     SERIAL UNIQUE NOT NULL,
  14. testdb(#   "value"  TEXT UNIQUE NOT NULL
  15. testdb(# );
  16. CREATE TABLE
  17.  
  18. testdb=# ALTER TABLE ONLY "table2"
  19. testdb-#   ADD CONSTRAINT "table2_PK_id" PRIMARY KEY ("id"),
  20. testdb-#   ADD CONSTRAINT "table2_UN_value" UNIQUE ("value");
  21. ALTER TABLE
  22.  
  23. testdb=# CREATE INDEX table2_hash_value ON "table2" USING hash ("value");
  24. CREATE INDEX
  25.  
  26. testdb=# CREATE FUNCTION fill_data()
  27. testdb-# RETURNS VOID
  28. testdb-# LANGUAGE plpgsql
  29. testdb-#   AS $$
  30. testdb$#   DECLARE
  31. testdb$#     i INTEGER;
  32. testdb$#     p TEXT;
  33. testdb$#   BEGIN
  34. testdb$#     FOR i IN 1..800000 LOOP
  35. testdb$#       p := 'zzz'||log(i)||'xxx';
  36. testdb$#       INSERT INTO "table1" ("value") VALUES (p);
  37. testdb$#       INSERT INTO "table2" ("value") VALUES (p);
  38. testdb$#     END LOOP;
  39. testdb$#   END;
  40. testdb$#   $$;
  41. CREATE FUNCTION
  42.  
  43. testdb=# explain analyze verbose select fill_data();
  44.                                          QUERY PLAN
  45. --------------------------------------------------------------------------------------------
  46.  Result  (cost=0.00..0.26 rows=1 width=4) (actual time=72509.791..72509.792 rows=1 loops=1)
  47.    Output: fill_data()
  48.  Planning time: 0.045 ms
  49.  Execution time: 72509.869 ms
  50. (4 rows)
  51.  
  52. testdb=# REINDEX DATABASE testdb;
  53. REINDEX
  54.  
  55. testdb=# \d+
  56.                             List of relations
  57.  Schema |     Name      |   Type   |  Owner   |    Size    | Description
  58. --------+---------------+----------+----------+------------+-------------
  59.  public | table1        | table    | postgres | 46 MB      |
  60.  public | table1_id_seq | sequence | postgres | 8192 bytes |
  61.  public | table2        | table    | postgres | 46 MB      |
  62.  public | table2_id_seq | sequence | postgres | 8192 bytes |
  63. (4 rows)
  64.  
  65. testdb=# \di+
  66.                               List of relations
  67.  Schema |       Name        | Type  |  Owner   | Table  | Size  | Description
  68. --------+-------------------+-------+----------+--------+-------+-------------
  69.  public | table1_PK_id      | index | postgres | table1 | 17 MB |
  70.  public | table1_UN_value   | index | postgres | table1 | 31 MB |
  71.  public | table2_PK_id      | index | postgres | table2 | 17 MB |
  72.  public | table2_UN_value   | index | postgres | table2 | 31 MB |
  73.  public | table2_hash_value | index | postgres | table2 | 26 MB |
  74.  public | table2_id_key     | index | postgres | table2 | 17 MB |
  75.  public | table2_value_key  | index | postgres | table2 | 31 MB |
  76. (7 rows)
  77.  
  78. testdb=# explain analyze verbose select id from table1 where value = 'zzz5xxx';
  79.                                                            QUERY PLAN
  80. ---------------------------------------------------------------------------------------------------------------------------------
  81.  Index Scan using "table1_UN_value" on public.table1  (cost=0.42..8.44 rows=1 width=4) (actual time=0.055..0.057 rows=1 loops=1)
  82.    Output: id
  83.    Index Cond: (table1.value = 'zzz5xxx'::text)
  84.  Planning time: 0.433 ms
  85.  Execution time: 0.089 ms
  86. (5 rows)
  87.  
  88. testdb=# explain analyze verbose select id from table2 where value = 'zzz5xxx';
  89.                                                            QUERY PLAN
  90. ---------------------------------------------------------------------------------------------------------------------------------
  91.  Index Scan using table2_hash_value on public.table2  (cost=0.00..8.02 rows=1 width=4) (actual time=0.028..0.029 rows=1 loops=1)
  92.    Output: id
  93.    Index Cond: (table2.value = 'zzz5xxx'::text)
  94.  Planning time: 0.255 ms
  95.  Execution time: 0.051 ms
  96. (5 rows)
  97.  
  98. testdb=# explain analyze verbose select id from table1 where value = 'wtfomg';
  99.                                                            QUERY PLAN
  100. ---------------------------------------------------------------------------------------------------------------------------------
  101.  Index Scan using "table1_UN_value" on public.table1  (cost=0.42..8.44 rows=1 width=4) (actual time=0.034..0.034 rows=0 loops=1)
  102.    Output: id
  103.    Index Cond: (table1.value = 'wtfomg'::text)
  104.  Planning time: 0.061 ms
  105.  Execution time: 0.055 ms
  106. (5 rows)
  107.  
  108. testdb=# explain analyze verbose select id from table2 where value = 'wtfomg';
  109.                                                            QUERY PLAN
  110. ---------------------------------------------------------------------------------------------------------------------------------
  111.  Index Scan using table2_hash_value on public.table2  (cost=0.00..8.02 rows=1 width=4) (actual time=0.006..0.006 rows=0 loops=1)
  112.    Output: id
  113.    Index Cond: (table2.value = 'wtfomg'::text)
  114.  Planning time: 0.061 ms
  115.  Execution time: 0.019 ms
  116. (5 rows)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement