让Greenplum 支持反转索引

GP的反转索引可以通过函数reverse来实现,但是这个函数在GP的版本中没有,所以需要port过来。
可以在9.5的代码中找到
src/backend/utils/adt/varlena.c

vi reverse.c

#include <string.h>
#include "postgres.h"
#include "fmgr.h"

PG_MODULE_MAGIC;

PG_FUNCTION_INFO_V1(text_reverse);

/*
 * Return reversed string
 */
Datum
text_reverse(PG_FUNCTION_ARGS)
{
        text       *str = PG_GETARG_TEXT_PP(0);
        const char *p = VARDATA_ANY(str);
        int                     len = VARSIZE_ANY_EXHDR(str);
        const char *endp = p + len;
        text       *result;
        char       *dst;

        result = palloc(len + VARHDRSZ);
        dst = (char *) VARDATA(result) + len;
        SET_VARSIZE(result, len + VARHDRSZ);

        if (pg_database_encoding_max_length() > 1)
        {
                /* multibyte version */
                while (p < endp)
                {
                        int                     sz;

                        sz = pg_mblen(p);
                        dst -= sz;
                        memcpy(dst, p, sz);
                        p += sz;
                }
        }
        else
        {
                /* single byte version */
                while (p < endp)
                        *(--dst) = *p++;
        }

        PG_RETURN_TEXT_P(result);
}

编译

gcc -O3 -Wall -Wextra -Werror -I /home/digoal/gpsrc/src/include -g -fPIC -c ./reverse.c -o reverse.o
gcc -O3 -Wall -Wextra -Werror -I /home/digoal/gpsrc/src/include -g -shared reverse.o -o libreverse.so

cp libreverse.so /home/digoal/gphome/lib

拷贝到所有节点

gpscp -f ./host /home/digoal/gphome/lib/libreverse.so =:/home/digoal/gphome/lib/

创建函数并测试可用性

postgres=# create or replace function reverse(text) returns text as '/home/digoal/gphome/lib/libreverse.so', 'text_reverse' language C STRICT immutable;
CREATE FUNCTION
postgres=# select reverse('abc');
 reverse
---------
 cba
(1 row)

postgres=# select reverse('a f d12');
 reverse
---------
 21d f a
(1 row)

postgres=# select reverse(null);
 reverse
---------

(1 row)

创建反转索引测试

postgres=# create table t(id int, info text);
NOTICE:  Table doesn't have 'DISTRIBUTED BY' clause -- Using column named 'id' as the Greenplum Database data distribution key for this table.
HINT:  The 'DISTRIBUTED BY' clause determines the distribution of data. Make sure column(s) chosen are the optimal data distribution key to minimize skew.
CREATE TABLE
postgres=# create index idx on t(reverse(info));
CREATE INDEX
postgres=# insert into t select id,md5(random()::text) from generate_series(1,1000000) t(id);
INSERT 0 1000000

postgres=# select id,info,reverse(info) from t limit 10;
  id  |               info               |             reverse
------+----------------------------------+----------------------------------
  197 | fefb23cb1705e6faa74601e6cd0dc8d7 | 7d8cd0dc6e10647aaf6e5071bc32bfef
  314 | 64c3d79458fc0ba2413e5493582830dd | dd0382853945e3142ab0cf85497d3c46
  426 | e0486de86c2c6bd72912fbc33d059de8 | 8ed950d33cbf21927db6c2c68ed6840e
  715 | c5087148a8086e2201a63b158268adbb | bbda862851b36a1022e6808a8417805c
  768 | 50ebdba7ff260d5adc11495313817221 | 12271831359411cda5d062ff7abdbe05
  944 | 8da13db138858ec1f78193f5c16cc310 | 013cc61c5f39187f1ce858831bd31ad8
 1057 | cf029096c2c66714861d0adba9ab49d8 | 8d94ba9abda0d16841766c2c690920fc
 1233 | ae6eb1bdf32b15c73c1a04adf54b9881 | 1889b45fda40a1c37c51b23fdb1be6ea
 1286 | 9943f89159055e0450765ab0968a1ad8 | 8da1a8690ba5670540e55095198f3499
 1575 | b8f0337315d238070984b9883a965c57 | 75c569a3889b489070832d5137330f8b
(10 rows)

postgres=# select * from t where reverse(info)>='7d8cd0dc6e10647aaf6e507' and reverse(info)<'7d8cd0dc6e10647aaf6e508';
 id  |               info
-----+----------------------------------
 197 | fefb23cb1705e6faa74601e6cd0dc8d7
(1 row)

postgres=# explain analyze select * from t where reverse(info)>='7d8cd0dc6e10647aaf6e507' and reverse(info)<'7d8cd0dc6e10647aaf6e508';
                                                           QUERY PLAN
--------------------------------------------------------------------------------------------------------------------------------
 Gather Motion 240:1  (slice1; segments: 240)  (cost=11012.90..13972.90 rows=40001 width=37)
   Rows out:  1 rows at destination with 9.113 ms to first row, 43 ms to end, start offset by 1.317 ms.
   ->  Bitmap Heap Scan on t  (cost=11012.90..13972.90 rows=167 width=37)
         Recheck Cond: reverse(info) >= '7d8cd0dc6e10647aaf6e507'::text AND reverse(info) < '7d8cd0dc6e10647aaf6e508'::text
         Rows out:  1 rows (seg46) with 0.156 ms to first row, 0.178 ms to end, start offset by 9.850 ms.
         ->  Bitmap Index Scan on idx  (cost=0.00..11002.90 rows=167 width=0)
               Index Cond: reverse(info) >= '7d8cd0dc6e10647aaf6e507'::text AND reverse(info) < '7d8cd0dc6e10647aaf6e508'::text
               Bitmaps out:  Avg 1.0 x 240 workers.  Max 1 (seg0) with 0.021 ms to end, start offset by 8.845 ms.
               Work_mem used:  9K bytes.
 Slice statistics:
   (slice0)    Executor memory: 475K bytes.
   (slice1)    Executor memory: 321K bytes avg x 240 workers, 329K bytes max (seg46).  Work_mem: 9K bytes max.
 Statement statistics:
   Memory used: 128000K bytes
 Total runtime: 71.958 ms
(15 rows)

适用场景:

  1. 带后缀的检索。
时间: 2024-09-16 07:53:22

让Greenplum 支持反转索引的相关文章

Greenplum 空间(GIS)数据检索 B-Tree &amp; GiST 索引实践 - 阿里云HybridDB for PostgreSQL最佳实践

标签 PostgreSQL , GIS , PostGIS , Greenplum , 空间检索 , GiST , B-Tree , geohash 背景 气象数据.地震数据.室内定位.室外定位.手机.车联网.还有我们最喜欢的"左划不喜欢.右划喜欢",越来越多的位置属性的数据.将来会越来越多. 基于GIS的数据分析.OLTP业务也越来越受到决策者的青睐,例如商场的选址决策,O2O的广告营销等.有很多基于多边形.时间.用户对象属性过滤的需求. 阿里云HybridDB for Postgr

Greenplum 模糊查询 实践

标签 PostgreSQL , Greenplum , orafunc , 阿里云HybridDB for PostgreSQL , reverse , like , 模糊查询 背景 文本搜索的需求分为: 1.全匹配,如: select * from table where column = 'xxxx'; 2.后模糊,如: select * from table where column like 'xxxx%'; 3.前模糊,如: select * from table where colu

Greenplum 类型一致性使用规范 - 索引条件、JOIN的类型一致性限制

标签 PostgreSQL , Greenplum , 类型一致 , 索引 , inner转换 , join 背景 在查询时,有很多用户会犯浑,发现建立了索引,但是查询偏偏不走索引. 怎么不走索引啊? 这里做容易混淆的是类型的差异,例如字段类型为字符串,但是输入的是INT类型,这样就可能不走索引.(除非创建了自动的CAST,自动进行类型转换) 查询的输入类型与索引的类型一致是非常有必要的. 例子 1.建表(使用变长.定长字符串类型),写入测试数据 create table tbl3(id int

《Greenplum企业应用实战》一3.5 索引

3.5 索引 Greenplum支持B-tree.bitmap.函数索引等,在这里我们简单介绍一下B-tree索引: testDB=# create table test_index_1 as select * from test_distribute_1; SELECT 5000000 testDB=# select id,flag from test_index_1 where id=100; id | flag -----+------ 100 | 0 (1 row) Time: 2606

自动建立正确索引(btree,hash,gin,gist,sp-gist,brin,bitmap...)的方法

标签 PostgreSQL , 索引接口 , 自动创建索引 , 自动选择索引接口 , (btree,hash,bitmap,gin,gist,sp-gist,brin,rum,bloom,zoomdb) 背景 PostgreSQL的索引接口是开放的,支持btree,hash,bitmap,gin,gist,sp-gist,brin,rum,bloom,zoomdb等索引接口.因此,不同的数据类型,有不同的索引结构可以选择. 由于索引接口众多(应对不同的场景),一些用户可能无法判断应该选择使用哪种

PostgreSQL 9种索引的原理和应用场景

标签 PostgreSQL , btree , hash , gin , gist , sp-gist , brin , bloom , rum , zombodb , bitmap 背景 PostgreSQL 拥有众多开放特性,例如 1.开放的数据类型接口,使得PG支持超级丰富的数据类型,除了传统数据库支持的类型,还支持GIS,JSON,RANGE,IP,ISBN,图像特征值,化学,DNA等等扩展的类型,用户还可以根据实际业务扩展更多的类型. 2.开放的操作符接口,使得PG不仅仅支持常见的类型

mysql/Java服务端对emoji的支持 专题

关于utf8不支持emoji是因为emoji是用4个字节存储的字符,而mysql的utf8只能存储1-3个字节的字符.那就存不了呗 需要更改的地方:(1)Mysql服务器client,mysql,mysqld中需要显式指定字符集为utf8mb4(2)在(1)的服务器上创建的db,需要为utf8mb4字符集,COLLATE为utf8mb4_unicode_ci 或 utf8mb4_general_ci(3) 在(2)的db中创建table和存放emoji字段的字符集为utf8mb4,collate

开源大数据引擎:Greenplum 数据库架构分析

Greenplum 数据库是最先进的分布式开源数据库技术,主要用来处理大规模的数据分析任务,包括数据仓库.商务智能(OLAP)和数据挖掘等.自2015年10月正式开源以来,受到国内外业内人士的广泛关注.本文就社区关心的Greenplum数据库技术架构进行介绍. 一. Greenplum数据库简介 大数据是个炙手可热的词,各行各业都在谈.一谈到大数据,好多人认为就是hadoop.实际上Hadoop只是大数据若干处理方案中的一个.现在的SQL.NoSQL.NewSQL.Hadoop等等,都能在不同层

Greenplum深度优化技巧

以下内容根据演讲PPT以及现场分享整理而成. 本次的分享将主要对云上的Greenplum进行介绍.有的同学往往有一些疑问就是Greenplum产品有什么优势?为什么要使用Greenplum?Greenplum能解决什么问题?在今天的分享中也将探讨这几个问题.最后还会提到一些阿里云Greenplum小组所做的工作. 本次的分享主要分为三个方面: 一.ApsaraDB for GP的定位 二.ApsaraDB for GP内核定制 三.未来规划 一.ApsaraDB for GP的定位 首先提出两个