PostgreSQL 语法特性
批量插入
方式一
1
INSERT INTO table_name SELECT...FROM source_table
方式二
1
INSERT INTO VALUES (),(),…()
比一条SQL插入一条数据的方式能减少和数据库的交互,减少数据库WAL(Write-Ahead Logging)日志的生成,提升插入效率
- 方式三
1
copy aa from '/home/digoal/aa.csv' with (delimiter U&'\0009');
U&’\0009’ TAB作为分隔符
RETURNING关键字
UPDATE|INSERT|DELETE 支持RETURNING返回更改过的数据
UPSERT语法
1 | insert into table_name values(...) on conflict (conflict_target) |
conflict_target字段必须为主键或者唯一键
数据抽样
- BERNOULLI抽样方式
更好的随机性,但比SYSTEM抽样方式性能低. - SYSTEM抽样
随机抽取表上数据块的数据.聚合函数
- string_agg(expression, delimiter)
函数返回的类型和输入参数类型一致 - array_agg(expression)
返回数组类型窗口函数
| Function | Return Type | Description | Function |
|---|---|---|---|
| row_number() | bigint | number of the current row within its partition, counting from 1 | row_number() |
| rank() | bigint | rank of the current row with gaps; same as row_number of its first peer | rank() |
| dense_rank() | bigint | rank of the current row without gaps; this function counts peer groups | dense_rank() |
| percent_rank() | double precision | relative rank of the current row: (rank - 1) / (total rows - 1) | percent_rank() |
| cume_dist() | double precision | relative rank of the current row: (number of rows preceding or peer with current row) / (total rows) | cume_dist() |
| ntile(num_buckets integer) | integer | integer ranging from 1 to the argument value, dividing the partition as equally as possible | ntile(num_buckets integer) |
| lag(value any [, offsetinteger [, default any ]]) | same type as value | returns value evaluated at the row that is offset rows before the current row within the partition; if there is no such row, instead return default. Bothoffset and default are evaluated with respect to the current row. If omitted, offset defaults to 1 and default to null | lag(value any [, offsetinteger [, default any ]]) |
| lead(value any [, offsetinteger [, default any ]]) | same type as value | returns value evaluated at the row that is offset rows after the current row within the partition; if there is no such row, instead return default. Bothoffset and default are evaluated with respect to the current row. If omitted, offset defaults to 1 and default to null | lead(value any [, offsetinteger [, default any ]]) |
| first_value(value any) | same type as value | returns value evaluated at the row that is the first row of the window frame | first_value(value any) |
| last_value(value any) | same type as value | returns value evaluated at the row that is the last row of the window frame | last_value(value any) |
| nth_value(value any, nthinteger) | same type as value | returns value evaluated at the row that is the nth row of the window frame (counting from 1); null if no such row | nth_value(value any, nthinteger) |
PostgreSQL 递归查询
PostgreSQL 递归查询
示例数据
| id | name | fatherid |
|---|---|---|
| 1 | 中国 | 0 |
| 2 | 辽宁 | 1 |
| 3 | 山东 | 1 |
| 4 | 沈阳 | 2 |
| 5 | 大连 | 2 |
| 6 | 济南 | 3 |
| 7 | 和平区 | 4 |
| 8 | 沈河区 | 4 |
查询规则
当给定一个id时能得到它完整的地名,例如当id=7时,地名是:中国辽宁沈阳和平区,当id=5时,地名是:中国辽宁大连,这是一个典型的层次数据递归应用场景。
PostgreSQL实现
创建示例数据
1 | CREATE TABLE test_area(id int4,name varchar(32),fatherid int4); |
查询
1 | with RECURSIVE t as |
结果
中国辽宁沈阳沈河区
RECURSIVE:引用自己的输出
string_agg: PG聚合函数,类比Oracle的listagg
Oracle实现
创建示例数据
1 | CREATE TABLE test_area(id int,name varchar(32),fatherid int); |
查询
1 | select listagg(name, '') within |
结果
中国辽宁沈阳沈河区