虚拟索引是不占用空间的,可以用来方便的进行sql 查询的性能评估,帮助我们了解查询优化的效果
hypopg 是一个pg 扩展,提供了比较方便的函数,我们可以用来评估查询,以下是pgspider 集成hypopg
的一个学习
环境准备
- dockerfile
使用docker 运行,参考我的相关连接
FROM dalongrong/pgspider:base as build
workdir /app
RUN apt-get update && apt-get install -y automake autoconf libtool pkg-config libssl-dev
RUN git clone https://github.com/HypoPG/hypopg.git /app/postgresql-11.6/contrib/hypopg
RUN git clone https://github.com/powa-team/pg_qualstats.git /app/postgresql-11.6/contrib/pg_qualstats
RUN cd /app/postgresql-11.6/contrib/hypopg && make && make install
RUN cd /app/postgresql-11.6/contrib/pg_qualstats && make && make install
FROM debian:stretch-slim
ENV GOSU_VERSION 1.11
RUN apt-get update && apt-get install -y wget libreadline-dev
# explicitly set user/group IDs
RUN set -eux; \
groupadd -r postgres --gid=999; \
# https://salsa.debian.org/postgresql/postgresql-common/blob/997d842ee744687d99a2b2d95c1083a2615c79e8/debian/postgresql-common.postinst#L32-35
useradd -r -g postgres --uid=999 --home-dir=/var/lib/postgresql --shell=/bin/bash postgres; \
# also create the postgres user's home directory with appropriate permissions
# see https://github.com/docker-library/postgres/issues/274
mkdir -p /var/lib/postgresql; \
chown -R postgres:postgres /var/lib/postgresql
RUN wget -O /usr/local/bin/gosu "https://github.com/tianon/gosu/releases/download/$GOSU_VERSION/gosu-$(dpkg --print-architecture)" \
&& chmod +x /usr/local/bin/gosu \
&& gosu nobody true
# make the "en_US.UTF-8" locale so postgres will be utf-8 enabled by default
RUN set -eux; \
if [ -f /etc/dpkg/dpkg.cfg.d/docker ]; then \
# if this file exists, we're likely in "debian:xxx-slim", and locales are thus being excluded so we need to remove that exclusion (since we need locales)
grep -q '/usr/share/locale' /etc/dpkg/dpkg.cfg.d/docker; \
sed -ri '/\/usr\/share\/locale/d' /etc/dpkg/dpkg.cfg.d/docker; \
! grep -q '/usr/share/locale' /etc/dpkg/dpkg.cfg.d/docker; \
fi; \
apt-get update; apt-get install -y locales; rm -rf /var/lib/apt/lists/*; \
localedef -i en_US -c -f UTF-8 -A /usr/share/locale/locale.alias en_US.UTF-8
ENV LANG en_US.utf8
# install "nss_wrapper" in case we need to fake "/etc/passwd" and "/etc/group" (especially for OpenShift)
# https://github.com/docker-library/postgres/issues/359
# https://cwrap.org/nss_wrapper.html
RUN set -eux; \
apt-get update; \
apt-get install -y --no-install-recommends libnss-wrapper; \
rm -rf /var/lib/apt/lists/*
RUN mkdir /docker-entrypoint-initdb.d
copY --from=build /usr/local/pgspider /usr/local/pgspider
RUN sed -ri "s!^#?(listen_addresses)\s*=\s*\S+.*!\1 = '*'!" /usr/local/pgspider/share/postgresql/postgresql.conf.sample; \
grep -F "listen_addresses = '*'" /usr/local/pgspider/share/postgresql/postgresql.conf.sample
RUN echo "shared_preload_libraries = 'pg_qualstats'" >> /usr/local/pgspider/share/postgresql/postgresql.conf.sample
RUN mkdir -p /var/run/postgresql && chown -R postgres:postgres /var/run/postgresql && chmod 2777 /var/run/postgresql
ENV PATH $PATH:/usr/local/pgspider/bin
ENV PGDATA /var/lib/postgresql/data
RUN mkdir -p "$PGDATA" && chown -R postgres:postgres "$PGDATA" && chmod 777 "$PGDATA"
VOLUME /var/lib/postgresql/data
copY docker-entrypoint.sh /usr/local/bin/
RUN ln -s usr/local/bin/docker-entrypoint.sh / # backwards compat
ENTRYPOINT ["docker-entrypoint.sh"]
EXPOSE 5432
CMD ["postgres"]
- docker-compose 文件
构建好的镜像已经push docker hub 了,所以直接使用docker-compose 运行
version: "3"
services:
pg:
image: dalongrong/pgspider:hypopg-pg_qualstats
ports:
- "5432:5432"
environment:
- "POSTGRES_PASSWORD=dalong"
- 启动
docker-compose up -d
试用
- 创建扩展
CREATE EXTENSION hypopg;
- 创建测试数据
CREATE TABLE hypo AS SELECT id, 'line ' || id AS val FROM generate_series(1,10000) id;
- 查询计划查看
EXPLAIN SELECT * FROM hypo WHERE id = 1;
SELECT * FROM hypopg_create_index('CREATE INDEX ON hypo (id)');
- 查询已经创建的虚拟索引
SELECT * FROM hypopg_list_indexes();
- 查询计划分析
EXPLAIN SELECT * FROM hypo WHERE id = 1;
- 查看查询计划并执行
EXPLAIN ANALYZE SELECT * FROM hypo WHERE id = 1;
说明
使用hypopg 扩展你的好处是我们不需要花费空间以及多大的时间,就可以通过慢慢试错同时不影响环境的情况下,进行
sql 的优化调整,是一个很不错的扩展工具
参考资料
https://github.com/HypoPG/hypopg
https://rjuju.github.io/postgresql/2015/07/02/how-about-hypothetical-indexes.html
https://hypopg.readthedocs.io/en/latest/
https://github.com/rongfengliang/pgspider-docker/blob/master/Dockerfile-hypopg-pg_qualstats
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。