Skip to content

Instantly share code, notes, and snippets.

@chochkov
Last active April 30, 2016 11:47
Show Gist options
  • Save chochkov/4cea09a04314b29a7bf2a1b06614b70f to your computer and use it in GitHub Desktop.
Save chochkov/4cea09a04314b29a7bf2a1b06614b70f to your computer and use it in GitHub Desktop.
DB_NAME='hstore_jsonb_test'
MAX_RECORDS=1e7
DICTIONARY="'ivan', 'dobri', 'pesho', 'genadi', 'smotlyo', 'metodi', 'dimitar', 'stamen', 'petyo', 'gosho'"
psql -U postgres -c "SELECT pg_terminate_backend(pid) FROM pg_stat_activity WHERE datname = '$DB_NAME'"
psql -U postgres -c "DROP DATABASE IF EXISTS $DB_NAME"
psql -U postgres -c "CREATE DATABASE $DB_NAME"
psql $DB_NAME -U postgres -c "
CREATE EXTENSION hstore;
-- create gin index on hstore and equally distributed dataset from $DICTIONARY
CREATE TABLE hstore_table (id integer, settings hstore);
CREATE INDEX hstore_index ON hstore_table USING gin(settings);
INSERT INTO hstore_table SELECT id, hstore(str, str) FROM (
SELECT generate_series(1, $MAX_RECORDS) id, unnest(array[$DICTIONARY]) str
) t;
-- copy the hstore to the jsonb table
CREATE TABLE jsonb_table (id integer, settings jsonb);
INSERT INTO jsonb_table SELECT id, settings::jsonb FROM hstore_table;
CREATE INDEX jsonb_index ON jsonb_table USING gin(settings);
"
psql $DB_NAME -U postgres -c 'vacuum analyze'
psql $DB_NAME -U postgres -c "EXPLAIN ANALYZE SELECT * FROM hstore_table WHERE settings ? 'ivan'"
psql $DB_NAME -U postgres -c "EXPLAIN ANALYZE SELECT * FROM jsonb_table WHERE settings ? 'ivan'"
@chochkov
Copy link
Author

nikola@[local] hstore_jsonb_test # select pg_size_pretty(pg_indexes_size('hstore_table'));
 pg_size_pretty
----------------
 441 MB
(1 row)

Time: 2.407 ms
nikola@[local] hstore_jsonb_test # select pg_size_pretty(pg_indexes_size('jsonb_table'));
 pg_size_pretty
----------------
 22 MB
(1 row)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment