暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

postgresql 常用语句

原创 Oracle 2022-12-21
429

常用语句
查询语句标记类型: 两种
int ‘’
‘’::varchar(32)
查询数组: select * from xxx where phone @> array[‘1234’::varchar(32)]; @>包含的意思 可在phone字段创建GIN索引
pg是大小写敏感的数据库,查询时如果想大小写不敏感就用lower()函数,模糊查询时用ILIKE 不区分大小写 like 区分大小写
create database
create database zjk_db1 with encoding=‘UTF8’ template=‘template1’ owner=‘zjk_test1’; 注意如果字符集不是utf8 要用template0,不然会导致复制错误
eg: create database zjk;
set time zone ‘PRC’; 有些时候查询时间用的
索引
查看索引信息: \d+ 索引名
创建索引: pg有多种索引,B-tree索引(mysql类似),Hash索引(处理简单的等值查询不会记WAL日志,很少用),GiST, SP-GiST, GIN(反转索引,包含多个键的值,如数组)
create index idx_xxx on table_name(column) : 创建Btree
create index idx_xx on table_name usinng gin(column) : GIN索引
创建索引是锁写的,只读,不是online DDL,要用online需要使用concurrently,并行,创建索引的执行时间也会变长
create index concurrently idx_xxx on table_name(column)
函数索引: create index idx_xxx on table_name (lower(column_name)),为lower函数在column_name列上创建函数索引,不创建索引因为使用了函数所以走不到column_name上的索引,创建了函数索引后执行where lower(note)=‘a’ 时就能走到索引了
部分索引: create index idx_xxx on table_name (column_name) where not (column_name > xx and column_name < xx),按照条件筛选的部分内容创建上索引
重建索引: 重建索引不支持从currently,但是支持同一个字段建立多个索引,可以先用concurrently创建一个新的索引,再删除老的索引
alter index idx_xxx rename to new_xxx : 改索引名
alter index idx_xxx set tablespace tablespace_name :更改到其他白哦空间
alter index idx_xxx set (storage_parameter=value) :修改索引的填充因子
alter index idx_xxx reset (storage_parameter[,…]) :重指填充因子
删除索引
drop index idx_xxx
drop index if exists idx_xxx :存在删除,不存在也不报错
默认是restrict的,也就是有外部依赖索引时删除索引会失败,cascade表示强制把外部依赖也删除,比如外键约束
drop index idx_xxx cascade

原文链接:https://blog.csdn.net/zhangjikuan/article/details/88670871

「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论