postgresql索引

1、B-tree索引

create index idx_contacts_name on contacts(name);

2、数组索引#

create index idx_contacts_phone on contacts using gin(phone);

注:phone在contacts表中是一个数组类型

3、降序索引

create index idx_contacts_name on contacts(name desc);

4、指定存储参数

create index idx_contacts_name on contacts(name) with(fillfactor=50);

注:fillfactor是常用的存储参数

5、指定空值排在前面

create index idx_contacts_name on contacts(name desc nulls first);

6、避免创建索引的长时间阻塞,可以在index关键字后面增加concurrently关键字,可以减少索引的阻塞时间

create index concurrently idx_contacts_name on contacts(name desc);

注意,重建索引时不支持concurrently ,可以新建一个索引,然后删除旧索引,另外并发索引被强制取消,可能会留下无效索引,这个索引将会导致更新变慢,如果是唯一索引,还会导致插入重复值失败。

7、修改索引

索引重命名:alter index name rename to new_name;

设置表空间:alter index name set tablespace tablespace_name;

设置存储参数:alter index name set(storage_parameter=value[,...])

重设存储参数:alter index name reset(storeage_parameter[,...])

8、删除索引

drop index if exists idx_contacts_name_old;

8、cascade会把索引和依赖索引的对象全部删除

PostgreSQL中的索引是一种数据结构,用于加快数据的检索速度并提高查询性能。它可以通过B-树(B-Tree)或哈希(Hash)等算法实现。在PostgreSQL中,可以使用`CREATE INDEX`语句创建索引,使用`DROP INDEX`语句删除索引索引的创建和维护需要权衡数据的特点、查询模式和性能需求。对于联合索引,可以使用覆盖索引来仅访问索引列就返回所有数据。索引的存储成本取决于索引列占行数据的占比,而索引对于查询效率的提升与要查找的数据量有关。此外,表的统计信息对于执行计划也非常重要。在PostgreSQL中,可以使用多个单列索引来实现联合索引的效果。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [PostgresSQL中的索引](https://blog.csdn.net/Xia13225498444/article/details/131065663)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 50%"] - *3* [数据库系列-PostgreSQL索引](https://blog.csdn.net/Evan_L/article/details/118652358)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值