-- 创建 schema create schema lucifer; -- 创建表 CREATE TABLE lucifer.products ( id INTEGERNOT NULL, name CHAR(16) NOT NULL, type CHAR(30), code CHAR(30), address CHAR(200) );
1 2 3 4 5 6
-- 创建 unique 索引 createunique index idx_products_id on lucifer.products (id); -- 创建 b-tree 索引 create index idx_products_name on lucifer.products using btree(name); -- 创建 表达式索引 create index idx_products_code on lucifer.products (substr(code,1,4));
2.设置索引1不可用,修改索引2的表空间,重命名索引3
1 2 3 4 5
alter index lucifer.idx_products_id unusable; CREATE TABLESPACE example0 RELATIVE LOCATION 'tablespace1/tablespace_0'; alter index lucifer.idx_products_name set tablespace example0; alter index lucifer.idx_products_code rename to idx_products_code1; select*from pg_indexes where tablename ='products';
3.重建索引2和products的所有索引
1 2 3
alter index lucifer.idx_products_name rebuild; reindex index lucifer.idx_products_name; reindex table lucifer.products;
4.使用\d+和系统视图pg_indexes查看索引信息
1 2
\d+ lucifer.products select*from pg_indexes where tablename ='products';
5.删除索引、表和表空间
1 2 3 4 5 6
drop index lucifer.idx_products_id; drop index lucifer.idx_products_name; drop index lucifer.idx_products_code1; droptable lucifer.products; drop schema lucifer; drop tablespace example0;