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目的: 查询性能测试,比 ...
随机推荐
- 类名.class和getClass()区别
class叫做“类字面量”,因class是关键字, 所以class编译时确定,getclass()运行时根据实际实例确定.String.class 是能对类名的引用取得在内存中该类型class对象的引 ...
- php超时时间说明
一,http请求超时时间 可能出现的场景: 1,curl进程运行了一个世纪还木结束,curl的时候设置了超时时间 --connect-timeout 1000 2,operation timed ou ...
- Android基本功:异步任务(AsyncTask)
一.解决新线程无法更新UI组建问题的方案 为了解决新线程不能更新UI组建的问题,Andorid提供了如下几种解决方案: 使用Handler实现线程之间的通信. Activity.runOnUiThre ...
- mysql 中只能使用 localhost 登录,用ip不能登陆
解决办法 1 首先使用 localhost 登录 mysql 服务器,如果想要所用ip都可以登录本地mysql 服务器,执行以下授权命令 2 grant all privileges on *.* t ...
- 发布Asp.net core到nginx 使用nginx代理
In this guide, we will cover setting up a production-ready ASP.NET environment on an Ubuntu 16.04 Se ...
- 安装Flume的时候出现File Channel transaction capacity cannot be greater than the capacity of the channel capacity -解决方案 摘自网络
部署flume集群时,在启动collector服务器没报错,启动agent服务器报错: File Channel transaction capacity cannot be greater than ...
- windows下bash终端--git-bash总汇
git bash是一个比较好用的终端(win下),但有一些细节需要调整后才好用. 1.比如,登陆后终端的表格,以及个别符号显示为乱码. 解决方法: 1).点击git bash左上角,选择“option ...
- vue-router新手指南
在学习完vue.js以及vuex之后,我们还剩下vue全家桶中的最后一个需要学习的组件,这就是vue-router了,本篇文章我们就来一起认识和入门vue-router.为什么我们只是入门呢?因为在这 ...
- C++ 读写MySQL经典 (转载)
from: http://blog.csdn.net/jemlee2002/article/details/1523164 看过很多C或是C++操作MySQL数据库的文章,大部分太吃力了,甚至有一 ...
- (原创)拨开迷雾见月明-剖析asio中的proactor模式(一)
使用asio之前要先对它的设计思想有所了解,了解设计思想将有助于我们理解和应用asio.asio是基于proactor模式的,asio的proactor模式隐藏于大量的细节当中,要找到它的踪迹,往往有 ...