HypoPG is a PostgreSQL extension adding support for hypothetical indexes.
An hypothetical -- or virtual -- index is an index that doesn't really exists, andthus doesn't cost CPU, disk or any resource to create. They're useful to knowif specific indexes can increase performance for problematic queries, sinceyou can know if PostgreSQL will use these indexes or not without having tospend resources to create them.
For more thorough informations, please consult the officialdocumentation.
For other general information, you can also consult this blogpost.
sudo make install
CREATE EXTENSION hypopg;
Note that hypopg doesn't provide extension upgrade scripts, as there's nodata saved in any of the objects created. Therefore, you need to first dropthe extension then create it again to get the new version.
NOTE: The hypothetical indexes are contained in a single backend. Therefore,if you add multiple hypothetical indexes, concurrent connections doingEXPLAIN
won't be bothered by your hypothetical indexes.
Assuming a simple test case:
rjuju=# CREATE TABLE hypo AS SELECT id, 'line ' || id AS val FROM generate_series(1,10000) id;
rjuju=# EXPLAIN SELECT * FROM hypo WHERE id = 1;
QUERY PLAN
-------------------------------------------------------
Seq Scan on hypo (cost=0.00..180.00 rows=1 width=13)
Filter: (id = 1)
(2 rows)
The easiest way to create an hypothetical index is to use thehypopg_create_index
functions with a regular CREATE INDEX
statement as arg.
For instance:
rjuju=# SELECT * FROM hypopg_create_index('CREATE INDEX ON hypo (id)');
NOTE: Some information from the CREATE INDEX
statement will be ignored, such asthe index name if provided. Some of the ignored information will be handled ina future release.
You can check the available hypothetical indexes in your own backend:
rjuju=# SELECT * FROM hypopg_list_indexes();
indexrelid | indexname | nspname | relname | amname
-----------+-------------------------------------------+---------+---------+--------
205101 | <41072>btree_hypo_id | public | hypo | btree
If you need more technical information on the hypothetical indexes, thehypopg()
function will return the hypothetical indexes in a similar way aspg_index
system catalog.
And now, let's see if your previous EXPLAIN
statement would use such an index:
rjuju=# EXPLAIN SELECT * FROM hypo WHERE id = 1;
QUERY PLAN
------------------------------------------------------------------------------------
Index Scan using <41072>hypo_btree_hypo_id on hypo (cost=0.29..8.30 rows=1 width=13)
Index Cond: (id = 1)
(2 rows)
Of course, only EXPLAIN
without ANALYZE
will use hypothetical indexes:
rjuju=# EXPLAIN ANALYZE SELECT * FROM hypo WHERE id = 1;
QUERY PLAN
-------------------------------------------------------------------------------------------------
Seq Scan on hypo (cost=0.00..180.00 rows=1 width=13) (actual time=0.036..6.072 rows=1 loops=1)
Filter: (id = 1)
Rows Removed by Filter: 9999
Planning time: 0.109 ms
Execution time: 6.113 ms
(5 rows)
To remove your backend's hypothetical indexes, you can use the functionhypopg_drop_index(indexrelid)
with the OID that the hypopg_list_indexes()
function returns and call hypopg_reset()
to remove all at once, or just closeyour current connection.
安装与测试Hypopg(适用于pg9.0版本以上) 下载,安装hypopg插件(pg数据库的插件,虚拟索引): 官网: 官网 github(建议): GitHub 源下找对对应的版本及系统:源 如果在github下下载的包,则解压文件,进入文件价下,执行命令: (注意:这里是默认安装在默认数据库下,如需要安装在指定版本的pg下,看下面的一些步骤) make sudo make install
在oracle中,很早就有virtual index的实现,可以很好判断索引是否可以被使用。今天介绍一款PG虚拟索引的插件hypopg,可以达到同样的效果。 虚拟索引是指实际上并不存在的索引,不需要花费CPU、磁盘等任何资源来创建索引。它可以帮助我们了解特定的索引是否可以使用索引提高查询性能,而不必花费资源来创建它们。 git地址如下: https://github.com/HypoPG/hypo
作者:徐田原 HypoPG概述 在PG数据库中,如果检查某个索引是否有助于一个或多个查询,HypoPG就能起到关键作用,它是 postgresql 的一个 extension,允许创建虚拟索引,观察优化器是否使用。因此,可以提供需要优化哪些查询,以及想要尝试哪些索引的方法。那么对于一般的使用者,如何更好的判断加索引是否有效呢? 虚拟索引是一个很有用的东西,没有副作用,只是虚拟的索引,建立虚拟索引后
查看并添加postgresql插件 查看 \dx 添加 CREATE EXTENSION hypopg; 查看虚拟索引 命令失效 SELECT * FROM hypopg_list_indexes(); 改为 select * from hypopg_list_indexes; 构建虚拟索引 SELECT * FROM hypopg_create_index('CREATE INDEX O
进入pg源码文件夹下cd /opt/module/postgresql-12.2/contrib/一定要在这装!!! 下载hypopggit clone git://github.com/HypoPG/hypopg 如果没有git要先执行执行命令: wget http://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm