天天看点

数据库选型思索 - 致 架构师、开发者

postgresql , 数据库选型

数据库选型是一件很大的事情,也是一件很头疼的事情。

很多企业并没有数据库的选型标准,或者并不了解业务需要什么样的数据库。

很多企业的数据库是开发说了算,熟悉什么就用什么,很多选型失误,导致后期非常尴尬的局面。

那么数据库选型要注意什么呢?

列举一些例子,取自如下文档

<a href="https://github.com/digoal/blog/blob/master/201701/20170125_01_pdf_002.pdf">《数据库选型的关键指标》</a>

1. 递归查询, connect by, 树形查询

postgresql 通过(with 或 tablefunc支持)支持例子

<a href="https://yq.aliyun.com/articles/240">https://yq.aliyun.com/articles/240</a>

<a href="http://www.postgresql.org/docs/9.5/static/tablefunc.html">http://www.postgresql.org/docs/9.5/static/tablefunc.html</a>

2. 窗口查询, window over

postgresql 支持例子

<a href="http://blog.163.com/digoal@126/blog/static/16387704020137154137930">http://blog.163.com/digoal@126/blog/static/16387704020137154137930</a>

<a href="http://blog.163.com/digoal@126/blog/static/16387704020121024102312302/">http://blog.163.com/digoal@126/blog/static/16387704020121024102312302/</a>

<a href="http://blog.163.com/digoal@126/blog/static/16387704020124239390354/">http://blog.163.com/digoal@126/blog/static/16387704020124239390354/</a>

<a href="http://www.postgresql.org/docs/9.5/static/functions-window.html">http://www.postgresql.org/docs/9.5/static/functions-window.html</a>

3. rollup, grouping sets, cube

<a href="http://www.postgresql.org/docs/9.5/static/queries-table-expressions.html#queries-grouping-sets">http://www.postgresql.org/docs/9.5/static/queries-table-expressions.html#queries-grouping-sets</a>

<a href="http://blog.163.com/digoal@126/blog/static/16387704020154269591874/">http://blog.163.com/digoal@126/blog/static/16387704020154269591874/</a>

4. 高级聚合(json,数组,相关性,标准差(采样,全局),截距,斜率,方差(采样,全局),mode,percentile_cont,distc,rank,dense_rank,percent_rank,cume_dist,grouping)

<a href="http://www.postgresql.org/docs/9.5/static/functions-aggregate.html">http://www.postgresql.org/docs/9.5/static/functions-aggregate.html</a>

<a href="http://blog.163.com/digoal@126/blog/static/1638770402015224124337/">http://blog.163.com/digoal@126/blog/static/1638770402015224124337/</a>

<a href="http://blog.163.com/digoal@126/blog/static/1638770402015379286873/">http://blog.163.com/digoal@126/blog/static/1638770402015379286873/</a>

<a href="http://blog.163.com/digoal@126/blog/static/16387704020153713222764">http://blog.163.com/digoal@126/blog/static/16387704020153713222764</a>

4. hash join, merge join, nestloop join

<a href="http://www.postgresql.org/docs/9.5/static/xoper-optimization.html">http://www.postgresql.org/docs/9.5/static/xoper-optimization.html</a>

<a href="http://www.postgresql.org/docs/9.5/static/planner-optimizer.html">http://www.postgresql.org/docs/9.5/static/planner-optimizer.html</a>

5. 哈希聚合

<a href="http://www.postgresql.org/docs/9.5/static/runtime-config-query.html#runtime-config-query-enable">http://www.postgresql.org/docs/9.5/static/runtime-config-query.html#runtime-config-query-enable</a>

6. 事务间共享事务快照

<a href="http://www.postgresql.org/docs/9.5/static/functions-admin.html#functions-snapshot-synchronization">http://www.postgresql.org/docs/9.5/static/functions-admin.html#functions-snapshot-synchronization</a>

<a href="http://www.postgresql.org/docs/9.5/static/sql-set-transaction.html">http://www.postgresql.org/docs/9.5/static/sql-set-transaction.html</a>

<a href="http://www.postgresql.org/docs/9.5/static/app-pgdump.html">http://www.postgresql.org/docs/9.5/static/app-pgdump.html</a>

<a href="http://blog.163.com/digoal@126/blog/static/163877040201326829943/">http://blog.163.com/digoal@126/blog/static/163877040201326829943/</a>

<a href="http://blog.163.com/digoal@126/blog/static/163877040201241134721101/">http://blog.163.com/digoal@126/blog/static/163877040201241134721101/</a>

7. 展开式索引(支持多列任意组合查询)

<a href="https://github.com/digoal/blog/blob/master/201702/20170205_01.md">《宝剑赠英雄 - 任意组合字段等效查询, 探探postgresql多列展开式b树》</a>

1. foreign key

<a href="http://www.postgresql.org/docs/9.5/static/ddl-constraints.html">http://www.postgresql.org/docs/9.5/static/ddl-constraints.html</a>

2. for no key update, for key share 粒度锁

<a href="http://www.postgresql.org/docs/9.5/static/explicit-locking.html#locking-rows">http://www.postgresql.org/docs/9.5/static/explicit-locking.html#locking-rows</a>

<a href="http://blog.163.com/digoal@126/blog/static/16387704020130249109133/">http://blog.163.com/digoal@126/blog/static/16387704020130249109133/</a>

<a href="http://blog.163.com/digoal@126/blog/static/16387704020130305109687/">http://blog.163.com/digoal@126/blog/static/16387704020130305109687/</a>

3. check 约束

4. exclusion 约束

1. 表空间

2. alter 列值转表达式

(alter table alter column c1 type newtype using (expression(...)) )

<a href="http://www.postgresql.org/docs/9.5/static/sql-altertable.html">http://www.postgresql.org/docs/9.5/static/sql-altertable.html</a>

<a href="https://yq.aliyun.com/articles/30470">https://yq.aliyun.com/articles/30470</a>

3. alter table 需要重组表的操作

postgresql少量操作需要重组

vacuum full, cluster, 修改字段数据类型, (修改长度不需要重组表)

4. 分区表

<a href="https://github.com/digoal/blog/blob/master/201610/20161024_01.md">《postgresql 9.5+ 高效分区表实现 - pg_pathman》</a>

<a href="https://github.com/digoal/blog/blob/master/201612/20161215_01.md">《postgresql 10.0 内置分区表》</a>

5. 物化视图

<a href="http://www.postgresql.org/docs/9.5/static/sql-creatematerializedview.html">http://www.postgresql.org/docs/9.5/static/sql-creatematerializedview.html</a>

6. 物化视图增量刷新

<a href="http://www.postgresql.org/docs/9.5/static/sql-refreshmaterializedview.html">http://www.postgresql.org/docs/9.5/static/sql-refreshmaterializedview.html</a>

7. 表继承关系

<a href="http://www.postgresql.org/docs/9.5/static/tutorial-inheritance.html">http://www.postgresql.org/docs/9.5/static/tutorial-inheritance.html</a>

8. 使用 like 建结构类似的表

<a href="http://www.postgresql.org/docs/9.5/static/sql-createtable.html">http://www.postgresql.org/docs/9.5/static/sql-createtable.html</a>

1. 客户端开发语言支持

c, java, python, ...

2. 函数

返回 void, 单行,srf,事件触发器(mysql 不支持),触发器

例子

<a href="http://blog.163.com/digoal@126/blog/static/16387704020132131361949/">http://blog.163.com/digoal@126/blog/static/16387704020132131361949/</a>

<a href="http://www.postgresql.org/docs/9.5/static/event-triggers.html">http://www.postgresql.org/docs/9.5/static/event-triggers.html</a>

3. 2pc

4. 服务端绑定变量

<a href="http://www.postgresql.org/docs/9.5/static/sql-prepare.html">http://www.postgresql.org/docs/9.5/static/sql-prepare.html</a>

5. savepoint

6. 异步消息

<a href="http://www.postgresql.org/docs/9.5/static/sql-notify.html">http://www.postgresql.org/docs/9.5/static/sql-notify.html</a>

<a href="http://www.postgresql.org/docs/9.5/static/sql-listen.html">http://www.postgresql.org/docs/9.5/static/sql-listen.html</a>

7. 游标

数组for循环,query for循环,游标for循环

postgresql 全面支持例子

<a href="http://www.postgresql.org/docs/9.5/static/plpgsql-control-structures.html">http://www.postgresql.org/docs/9.5/static/plpgsql-control-structures.html</a>

<a href="http://www.postgresql.org/docs/9.5/static/plpgsql-cursors.html">http://www.postgresql.org/docs/9.5/static/plpgsql-cursors.html</a>

(mysql 暂不支持数组)

1. 数据类型

1.1 postgresql

高精度numeric, 浮点, 自增序列,货币,字节流,时间,日期,时间戳,布尔,枚举,平面几何,立体几何,多维几何,地球,postgis,网络,比特流,全文检索,uuid,xml,json,数组,复合类型,域类型,范围,树类型,化学类型,基因序列,fdw, 大对象, 图像

1.2 mysql

数字,时间,字符串,简单的gis,json

2. 支持索引的数据类型

2.1 postgresql

高精度numeric, 浮点, 自增序列,货币,字节流,时间,日期,时间戳,布尔,枚举,平面几何,立体几何,多维几何,地球,postgis,网络,比特流,全文检索,uuid,xml,json,数组,复合类型,域类型,范围,树类型,化学,基因序列

2.2 mysql

数字,字符串,比特流,时间,全文检索,gis

1. 索引方法

postgresql 支持 btree, hash, gist, sp-gist, gin, brin , bloom , rum 索引

mysql 支持 btree, gis类型索引

2. 规则表达式、前后模糊查询,支持索引检索

postgresql 支持

3. 数组支持索引

4. 全文检索支持索引

5. 索引扩展功能

5.1 postgresql 支持 表达式索引,部分索引,联合索引

5.2 postgresql 支持图像相似度、文本相似度搜索

5.3 自定义索引访问方法(bloom, rum, ......)

postgresql支持

1. 多引擎支持

内置heap, 通过插件实现内存表, 列存储, 压缩存储, 流式存储, 非关系存储等。

myisam, innodb, ...

2. 外部表

postgresql支持任意外部数据源, (例如jdbc, file, odbc, oracle, mysql, db2, redis, mongo, es, hadoop......)

<a href="https://wiki.postgresql.org/wiki/fdw">https://wiki.postgresql.org/wiki/fdw</a>

3. dblink

4. 内存表

postgresql 通过外部表支持,或者通过插件支持

5. es(弹性搜索引擎) 引擎

1. 数据加密

postgresql 支持加密数据类型,可选gpg加密算法

2. 认证方法

postgresql 支持 密码、ldap、ad、gssapi、sspi、ident、peer、radius、pam、签名认证

3. 数据传输加密

4. 行安全策略

5. 数据库内部支持libselinux接口, (美国国家安全局制定的安全加强标准)

<a href="http://www.postgresql.org/docs/9.5/static/runtime-config-query.html">http://www.postgresql.org/docs/9.5/static/runtime-config-query.html</a>

1. gpu 并行计算支持

2. 遗传优化器算法

postgresql 支持cbo、cro、遗传算法

3. hint plan

4. cpu 并行计算

postgresql 支持 (线性性能提升)

5. 自定义成本因子

1. 是否支持采样查询

2. 是否支持扩展采样算法

3. 自定义数据类型

4. 自定义索引方法

5. 字符集自动转换, c扩展接口

6. 自定义聚合

7. 自定义窗口

1. 类型扩展,操作符扩展,函数扩展,索引方法扩展,索引扩展,

2. c触发器函数, c事件触发器函数

3. 函数语言扩展

postgresql 支持扩展函数支持接口, erlang, ....

4. 机器学习库

5. 流式计算

<a href="https://github.com/digoal/blog/blob/master/201701/20170125_01.md">《数据库界的华山论剑 tpc.org》</a>

<a href="https://github.com/digoal/blog/blob/master/201609/20160911_01.md">《facebook linkbench 测试postgresql社交关系图谱场景性能》</a>

<a href="https://github.com/digoal/blog/blob/master/201609/20160927_01.md">《postgresql 主机性能测试方法 - 单机多实例》</a>

<a href="https://github.com/digoal/blog/blob/master/201609/20160927_02.md">《postgresql 主机性能测试方法 - 单机单实例》</a>

<a href="https://github.com/digoal/blog/blob/master/201611/20161129_01.md">《postgresql 源码性能诊断(perf profiling)指南》</a>

<a href="https://github.com/digoal/blog/blob/master/201512/20151215_01.md">《"物联网"流式处理应用 - 用postgresql实时处理(万亿每天)》</a>

<a href="https://github.com/digoal/blog/blob/master/201601/20160119_01.md">《postgresql 百亿地理位置数据 近邻查询性能》</a>

<a href="https://github.com/digoal/blog/blob/master/201603/20160302_01.md">《postgresql 百亿数据 秒级响应 正则及模糊查询》</a>

<a href="https://github.com/digoal/blog/blob/master/201603/20160307_01.md">《postgresql 1000亿数据量 正则匹配 速度与激情》</a>

<a href="https://github.com/digoal/blog/blob/master/201612/20161225_01.md">《恭迎万亿级营销(圈人)潇洒的迈入毫秒时代 - 万亿user_tags级实时推荐系统数据库设计》</a>

<a href="https://github.com/digoal/blog/blob/master/201612/20161231_01.md">《从难缠的模糊查询聊开 - postgresql独门绝招之一 gin , gist , sp-gist , rum 索引原理与技术背景》</a>

<a href="https://github.com/digoal/blog/blob/master/201612/20161216_01.md">《分析加速引擎黑科技 - llvm、列存、多核并行、算子复用 大联姻 - 一起来开启postgresql的百宝箱》</a>

1. 函数(过程)语言

postgresql支持几乎所有的地球编程语言,你可以用他们在数据库中编写udf

plpgsql, sql, c, c++, java, javascript, r, python, perl, php, tcl, ruby, lua, ...

1. postgresql完全支持以下下推功能

支持聚合算子下推

支持where条件下推

支持join下推

支持sort下推

支持select 子句下推

支持跨数据源join

postgresql

资源管控能力强。

单实例可以充分发挥hpc的性能,有多少资源就能使用多少资源。

pirt,任意时间点恢复;fpw;

物理、逻辑级流式复制;金融级多副本;

社区状态

单一开源分支,社区力量较集中。全球有1000名以上的内核研发人员。

社区研发由核心组员和committer组成,持续5年以上对社区版本有贡献内核研发人员超过50位。

社区核心人员分别来自数据库厂商,数据库支持和服务公司,数据库最终用户的公司,形成了一个非常好的力量均衡。

社区活跃度

非常活跃(see git.postgresql.org, github.com/postgres)