PostgreSQL LIKE 查询效率提升实验<转>
一、未做索引的查询效率
作为对比,先对未索引的查询做测试
EXPLAIN ANALYZE select * from gallery_map where author = '曹志耘';
QUERY PLAN
-----------------------------------------------------------------------------------------------------------------
Seq Scan on gallery_map (cost=0.00..7002.32 rows=1025 width=621) (actual time=0.011..39.753 rows=1031 loops=1)
Filter: ((author)::text = '曹志耘'::text)
Rows Removed by Filter: 71315
Planning time: 0.194 ms
Execution time: 39.879 ms
(5 rows)
Time: 40.599 ms
EXPLAIN ANALYZE select * from gallery_map where author like '曹志耘';
QUERY PLAN
-----------------------------------------------------------------------------------------------------------------
Seq Scan on gallery_map (cost=0.00..7002.32 rows=1025 width=621) (actual time=0.017..41.513 rows=1031 loops=1)
Filter: ((author)::text ~~ '曹志耘'::text)
Rows Removed by Filter: 71315
Planning time: 0.188 ms
Execution time: 41.669 ms
(5 rows)
Time: 42.457 ms
EXPLAIN ANALYZE select * from gallery_map where author like '曹志耘%';
QUERY PLAN
-----------------------------------------------------------------------------------------------------------------
Seq Scan on gallery_map (cost=0.00..7002.32 rows=1028 width=621) (actual time=0.017..41.492 rows=1031 loops=1)
Filter: ((author)::text ~~ '曹志耘%'::text)
Rows Removed by Filter: 71315
Planning time: 0.307 ms
Execution time: 41.633 ms
(5 rows)
Time: 42.676 ms
很显然都会做全表扫描
二、创建btree索引
PostgreSQL默认索引是btree
CREATE INDEX ix_gallery_map_author ON gallery_map (author);
EXPLAIN ANALYZE select * from gallery_map where author = '曹志耘';
QUERY PLAN
-------------------------------------------------------------------------------------------------------------------------------------
Bitmap Heap Scan on gallery_map (cost=36.36..2715.37 rows=1025 width=621) (actual time=0.457..1.312 rows=1031 loops=1)
Recheck Cond: ((author)::text = '曹志耘'::text)
Heap Blocks: exact=438
-> Bitmap Index Scan on ix_gallery_map_author (cost=0.00..36.10 rows=1025 width=0) (actual time=0.358..0.358 rows=1031 loops=1)
Index Cond: ((author)::text = '曹志耘'::text)
Planning time: 0.416 ms
Execution time: 1.422 ms
(7 rows)
Time: 2.462 ms
EXPLAIN ANALYZE select * from gallery_map where author like '曹志耘';
QUERY PLAN
-------------------------------------------------------------------------------------------------------------------------------------
Bitmap Heap Scan on gallery_map (cost=36.36..2715.37 rows=1025 width=621) (actual time=0.752..2.119 rows=1031 loops=1)
Filter: ((author)::text ~~ '曹志耘'::text)
Heap Blocks: exact=438
-> Bitmap Index Scan on ix_gallery_map_author (cost=0.00..36.10 rows=1025 width=0) (actual time=0.560..0.560 rows=1031 loops=1)
Index Cond: ((author)::text = '曹志耘'::text)
Planning time: 0.270 ms
Execution time: 2.295 ms
(7 rows)
Time: 3.444 ms
EXPLAIN ANALYZE select * from gallery_map where author like '曹志耘%';
QUERY PLAN
-----------------------------------------------------------------------------------------------------------------
Seq Scan on gallery_map (cost=0.00..7002.32 rows=1028 width=621) (actual time=0.015..41.389 rows=1031 loops=1)
Filter: ((author)::text ~~ '曹志耘%'::text)
Rows Removed by Filter: 71315
Planning time: 0.260 ms
Execution time: 41.518 ms
(5 rows)
Time: 42.430 ms
EXPLAIN ANALYZE select * from gallery_map where author like '%研究室';
QUERY PLAN
-----------------------------------------------------------------------------------------------------------------
Seq Scan on gallery_map (cost=0.00..7002.32 rows=2282 width=621) (actual time=0.064..52.824 rows=2152 loops=1)
Filter: ((author)::text ~~ '%研究室'::text)
Rows Removed by Filter: 70194
Planning time: 0.254 ms
Execution time: 53.064 ms
(5 rows)
Time: 53.954 ms
可以看到,等于、like的全匹配是用到索引的,like的模糊查询还是全表扫描
三、创建gin索引
CREATE EXTENSION pg_trgm;
CREATE INDEX ix_gallery_map_author ON gallery_map USING gin (author gin_trgm_ops);
EXPLAIN ANALYZE select * from gallery_map where author like '曹%';
QUERY PLAN
-------------------------------------------------------------------------------------------------------------------------------------
Bitmap Heap Scan on gallery_map (cost=19.96..2705.69 rows=1028 width=621) (actual time=0.419..1.771 rows=1031 loops=1)
Recheck Cond: ((author)::text ~~ '曹%'::text)
Heap Blocks: exact=438
-> Bitmap Index Scan on ix_gallery_map_author (cost=0.00..19.71 rows=1028 width=0) (actual time=0.312..0.312 rows=1031 loops=1)
Index Cond: ((author)::text ~~ '曹%'::text)
Planning time: 0.358 ms
Execution time: 1.916 ms
(7 rows)
Time: 2.843 ms
EXPLAIN ANALYZE select * from gallery_map where author like '%耘%';
QUERY PLAN
-----------------------------------------------------------------------------------------------------------------
Seq Scan on gallery_map (cost=0.00..7002.32 rows=1028 width=621) (actual time=0.015..51.641 rows=1031 loops=1)
Filter: ((author)::text ~~ '%耘%'::text)
Rows Removed by Filter: 71315
Planning time: 0.268 ms
Execution time: 51.957 ms
(5 rows)
Time: 52.899 ms
EXPLAIN ANALYZE select * from gallery_map where author like '%研究室%';
QUERY PLAN
-------------------------------------------------------------------------------------------------------------------------------------
Bitmap Heap Scan on gallery_map (cost=31.83..4788.42 rows=2559 width=621) (actual time=0.914..4.195 rows=2402 loops=1)
Recheck Cond: ((author)::text ~~ '%研究室%'::text)
Heap Blocks: exact=868
-> Bitmap Index Scan on ix_gallery_map_author (cost=0.00..31.19 rows=2559 width=0) (actual time=0.694..0.694 rows=2402 loops=1)
Index Cond: ((author)::text ~~ '%研究室%'::text)
Planning time: 0.306 ms
Execution time: 4.403 ms
(7 rows)
Time: 5.227 ms
gin_trgm索引的效果好多了
由于pg_trgm的索引是把字符串切成多个3元组,然后使用这些3元组做匹配,所以gin_trgm索引对于少于3个字符(包括汉字)的查询,只有前缀匹配会走索引
另外,还测试了btree_gin,效果和btree一样
注意:
gin_trgm要求数据库必须使用UTF-8编码
demo_v1 # \l demo_v1
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
---------+-----------+----------+-------------+-------------+-------------------
demo_v1 | wmpp_user | UTF8 | en_US.UTF-8 | en_US.UTF-8 |
参考:
http://www.postgres.cn/docs/9.4/pgtrgm.html
http://dba.stackexchange.com/questions/10694/pattern-matching-with-like-similar-to-or-regular-expressions-in-postgresql/10696
本站原创,转载时保留以下信息:
本文转自:深度开源(open-open.com)
原文地址:http://www.open-open.com/lib/view/open1463100004089.html
PostgreSQL LIKE 查询效率提升实验<转>的更多相关文章
- 查询效率提升10倍!3种优化方案,帮你解决MySQL深分页问题
开发经常遇到分页查询的需求,但是当翻页过多的时候,就会产生深分页,导致查询效率急剧下降. 有没有什么办法,能解决深分页的问题呢? 本文总结了三种优化方案,查询效率直接提升10倍,一起学习一下. 1. ...
- Hive SQL查询效率提升之Analyze方案的实施
0.简介 Analyze,分析表(也称为计算统计信息)是一种内置的Hive操作,可以执行该操作来收集表上的元数据信息.这可以极大的改善表上的查询时间,因为它收集构成表中数据的行计数,文件计数和文件大小 ...
- 实验:ignite查询效率探究
前面的文章讲到ignite支持扫描查询和sql查询,其sql查询是ignite产品的一个亮点,那么哪一种的查询更适合我们的产品使用呢,往下看: 先分别贴一下扫描查询和sql查询两种查询方式的代码,供参 ...
- WFS: postgresql(postgis)和shp文件查询效率对比
对GeoServer上的WFS的各种数据源查询效率感兴趣,做个测试.本次测试了Postgresql.geopackage.shp文件三种数据源的查询效率,无论是本机还是服务器环境,pg存储查询效率都比 ...
- 见招拆招-PostgreSQL中文全文索引效率优化
* { color: #3e3e3e } body { font-family: "Helvetica Neue", Helvetica, "Hiragino Sans ...
- SQL 提高查询效率
1.关于SQL查询效率,100w数据,查询只要1秒,与您分享: 机器情况p4: 2.4内存: 1 Gos: windows 2003数据库: ms sql server 2000目的: 查询性能测试, ...
- 疑难杂症——EF+Automapper引发的查询效率问题解析
前言:前面总结了一些WebApi里面常见问题的解决方案,本来打算来分享下oData+WebApi的使用方式的,奈何被工作所困,只能将此往后推了.今天先来看看EF和AutoMapper联合使用的一个问题 ...
- 【MySQL】过滤后的结果集较大,用LIMIT查询分页记录,查询效率不理想
> 参考的优秀文章 优化LIMIT分页--<高性能MySQL>(电子工业出版社) > 场景描述 遇到一个场景:查询排序后的结果集较大,我们采用分页显示,每页显示20条记录,但是 ...
- 关于SQL查询效率,100w数据,查询只要1秒
1.关于SQL查询效率,100w数据,查询只要1秒,与您分享:机器情况p4: 2.4内存: 1 Gos: windows 2003数据库: ms sql server 2000目的: 查询性能测试,比 ...
随机推荐
- C++11 中值得关注的几大变化(详解)
源文章来自前C++标准委员会的 Danny Kalev 的 The Biggest Changes in C++11 (and Why You Should Care),赖勇浩做了一个中文翻译在这里. ...
- SIMULINK的模块库介绍
SIMILINK模块库按功能进行分为以下8类子库:Continuous(连续模块)Discrete(离散模块)Function&Tables(函数和平台模块)Math(数学模块)Nonline ...
- 编写 T4 文本模板
文本模板由以下部件组成: 1)指令 - 控制模板处理方式的元素. 2)文本块 - 直接复制到输出的内容. 3)控制块 - 向文本插入可变值并控制文本的条件或重复部件的程序代码. 指令: 指令是控制模板 ...
- Google Guava中的前置条件
前置条件:让方法调用的前置条件判断更简单. Guava在Preconditions类中提供了若干前置条件判断的实用方法,我们建议[在Eclipse中静态导入这些方法]每个方法都有三个变种: check ...
- 启动supervisord 生成openvpn用于android的证书
service supervisord start netstat -anp | grep 你的端口号 生成证书: 进入到openvpn的证书生成文件夹,通常在/etc/openvpn/easy-r ...
- php分享二十八:mysql运行中的问题排查
一:杀掉mysql连接的方法: kill thread_id: 杀掉当前进程,断开连接 kill query thread_id: 只杀掉某连接当前的SQL,而不断开连接. 批量杀死MySQL连接的 ...
- JDK1.6新特性,基础类库篇,Jar与Zip增强
1. API改变 增加了两个实现类 java.util.zip.DeflaterInputStream: 此类为解压缩 "deflate" 压缩格式的数据实现流过滤器.它还用作其他 ...
- Eclipse中如何安装和使用GrepCode插件 (转)
GrepCode(GC)Eclipse插件允许Eclipse用户在Eclipse IDE中搜索由GrepCode提供的工厂类.本教程介绍如何安装和使用插件.使用Eclipse3.5(Galileo)的 ...
- ASP.NET CORE下取IP地址
先记下来,以后用上了直接来这复制 string ip1 = HttpContext.Request.Headers["X-Real-IP"]; //取IP,NGINX中的配置里要写 ...
- Mac OS X 10.8.5 安装编译glib
过程比较坎坷,就此记录一番一遍后人 1. 下载pkg-config然后解压安装: ./configure make -j 24 sudo make install 2.下载libiconv解压安装 . ...