哈希测试
来自 PostgreSQL Wiki
跳转到导航跳转到搜索本页介绍测试步骤的详细说明。
测试前
创建表
postgres=# CREATE TABLE dict (word varchar(100)); postgres=# COPY dict FROM '/tmp/words';
清除缓存 - 关闭数据库并清除内核缓存
$ sudo echo 3 | sudo tee /proc/sys/vm/drop_caches
创建一个表来收集 IO 统计数据
CREATE TABLE hash_stat( heap_blks_read integer, heap_blks_hit integer, idx_blks_read integer, idx_blks_hit integer );
测试哈希索引
创建索引并获取构建时间
postgres=# \timing postgres=# CREATE INDEX wordhash ON dict USING hash (word); 504650.100 ms
获取查询前的统计信息
postgres=# INSERT INTO hash_stat (heap_blks_read, heap_blks_hit, idx_blks_read, idx_blks_hit) SELECT heap_blks_read, heap_blks_hit, idx_blks_read, idx_blks_hit FROM pg_statio_all_tables WHERE relname = 'dict';
再次清除缓存并运行 pgbench。 通过 1000/tps 获取毫秒级的查询时间。
$ sudo echo 3 | sudo tee /proc/sys/vm/drop_caches $ pgbench -U postgres -n -f /tmp/query.sql dict transaction type: Custom query scaling factor: 1 query mode: simple number of clients: 1 number of transactions per client: 10 number of transactions actually processed: 10/10 tps = 0.192666 (including connections establishing) tps = 0.192684 (excluding connections establishing)
获取查询后的统计信息
postgres=# INSERT INTO ...
测试 B 树索引
创建 B 树索引,获取构建时间和查询前的统计信息
postgres=# DROP INDEX wordhash; postgres=# \timing postgres=# CREATE INDEX wordbtree ON dict USING btree (word); 844495.867 ms
之后的测试与哈希索引测试相同。 对于 pgbench 测试,我们得到
$ pgbench -U postgres -n -f /tmp/query.sql dict transaction type: Custom query scaling factor: 1 query mode: simple number of clients: 1 number of transactions per client: 10 number of transactions actually processed: 10/10 tps = 0.176786 (including connections establishing) tps = 0.176802 (excluding connections establishing)
IO 统计结果
状态 | heap_blks_read | heap_blks_hit | idx_blks_read | idx_blks_hit |
哈希 - 查询前 | 394317 | 39916824 | 0 | 0 |
哈希 - 查询后 | 414394 | 39916947 | 19724 | 20276 |
B 树 - 查询前 | 610065 | 39916947 | 0 | 0 |
B 树 - 查询后 | 629956 | 39917056 | 21736 | 58425 |
我们可以通过 (heap_blks_read + idx_blks_read) 获取查询期间的 blocks_read。 您可以在 这里 查看结果。