Advertisement
rockdrilla

pgsql 10 hash index

Oct 10th, 2017
769
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..50000 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=# select fill_data();
  44.  fill_data
  45. -----------
  46.  
  47. (1 row)
  48.  
  49. testdb=# REINDEX DATABASE testdb;
  50. REINDEX
  51.  
  52. testdb=# \d+
  53.                             List of relations
  54.  Schema |     Name      |   Type   |  Owner   |    Size    | Description
  55. --------+---------------+----------+----------+------------+-------------
  56.  public | table1        | table    | postgres | 2976 kB    |
  57.  public | table1_id_seq | sequence | postgres | 8192 bytes |
  58.  public | table2        | table    | postgres | 2976 kB    |
  59.  public | table2_id_seq | sequence | postgres | 8192 bytes |
  60. (4 rows)
  61.  
  62. testdb=# \di+
  63.                                List of relations
  64.  Schema |       Name        | Type  |  Owner   | Table  |  Size   | Description
  65. --------+-------------------+-------+----------+--------+---------+-------------
  66.  public | table1_PK_id      | index | postgres | table1 | 1112 kB |
  67.  public | table1_UN_value   | index | postgres | table1 | 2008 kB |
  68.  public | table2_PK_id      | index | postgres | table2 | 1112 kB |
  69.  public | table2_UN_value   | index | postgres | table2 | 2008 kB |
  70.  public | table2_hash_value | index | postgres | table2 | 2064 kB |
  71.  public | table2_id_key     | index | postgres | table2 | 1112 kB |
  72.  public | table2_value_key  | index | postgres | table2 | 2008 kB |
  73. (7 rows)
  74.  
  75. testdb=# explain analyze verbose select id from table1 where value = 'zzz3xxx';
  76.                                                            QUERY PLAN
  77. ---------------------------------------------------------------------------------------------------------------------------------
  78.  Index Scan using "table1_UN_value" on public.table1  (cost=0.41..8.43 rows=1 width=4) (actual time=0.036..0.037 rows=1 loops=1)
  79.    Output: id
  80.    Index Cond: (table1.value = 'zzz3xxx'::text)
  81.  Planning time: 0.101 ms
  82.  Execution time: 0.063 ms
  83. (5 rows)
  84.  
  85. testdb=# explain analyze verbose select id from table2 where value = 'zzz3xxx';
  86.                                                            QUERY PLAN
  87. ---------------------------------------------------------------------------------------------------------------------------------
  88.  Index Scan using table2_hash_value on public.table2  (cost=0.00..8.02 rows=1 width=4) (actual time=0.015..0.016 rows=1 loops=1)
  89.    Output: id
  90.    Index Cond: (table2.value = 'zzz3xxx'::text)
  91.  Planning time: 0.100 ms
  92.  Execution time: 0.040 ms
  93. (5 rows)
  94.  
  95. testdb=# explain analyze verbose select id from table1 where value = 'wtfomg';
  96.                                                            QUERY PLAN
  97. ---------------------------------------------------------------------------------------------------------------------------------
  98.  Index Scan using "table1_UN_value" on public.table1  (cost=0.41..8.43 rows=1 width=4) (actual time=0.038..0.038 rows=0 loops=1)
  99.    Output: id
  100.    Index Cond: (table1.value = 'wtfomg'::text)
  101.  Planning time: 0.124 ms
  102.  Execution time: 0.078 ms
  103. (5 rows)
  104.  
  105. testdb=# explain analyze verbose select id from table2 where value = 'wtfomg';
  106.                                                            QUERY PLAN
  107. ---------------------------------------------------------------------------------------------------------------------------------
  108.  Index Scan using table2_hash_value on public.table2  (cost=0.00..8.02 rows=1 width=4) (actual time=0.012..0.012 rows=0 loops=1)
  109.    Output: id
  110.    Index Cond: (table2.value = 'wtfomg'::text)
  111.  Planning time: 0.140 ms
  112.  Execution time: 0.039 ms
  113. (5 rows)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement