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

openGauss每日一练第17天 | 索引管理

原创 不了峰 2022-12-10
352

1 创建索引

zyfdb=> create table test(id serial primary key,testnum serial); NOTICE: CREATE TABLE will create implicit sequence "test_id_seq" for serial column "test.id" NOTICE: CREATE TABLE will create implicit sequence "test_testnum_seq" for serial column "test.testnum" NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index "test_pkey" for table "test" CREATE TABLE zyfdb=> zyfdb=> create index idx_test_testnum on test(testnum); CREATE INDEX zyfdb=> 查看索引 zyfdb=> \di List of relations Schema | Name | Type | Owner | Table | Storage --------+------------------+-------+-------+-------+--------- public | idx_test_testnum | index | user5 | test | public | test_pkey | index | user5 | test | (2 rows)

2 通过hint使用索引

CREATE TABLE customer ( ca_address_sk integer NOT NULL , ca_address_id character(16), ca_street_number character(10) , ca_street_name character varying(60) , ca_street_type character(15) , ca_suite_number character(10) , ca_city character varying(60) , ca_county character varying(30) , ca_state character(2) , ca_zip character(10) , ca_country character varying(20) , ca_gmt_offset numeric(5,2) , ca_location_type character(20) ); insert into customer values (1, 'AAAAAAAABAAAAAAA', '18', 'Jackson', 'Parkway', 'Suite 280', 'Fairfield', 'Maricopa County', 'AZ', '86192' ,'United States', -7.00, 'condo'), (2, 'AAAAAAAACAAAAAAA', '362', 'Washington 6th', 'RD', 'Suite 80', 'Fairview', 'Taos County', 'NM', '85709', 'United States', -7.00, 'condo'), (3, 'AAAAAAAADAAAAAAA', '585', 'Dogwood Washington', 'Circle', 'Suite Q', 'Pleasant Valley', 'York County', 'PA', '12477', 'United States', -5.00, 'single family'); EXPLAIN SELECT * FROM customer WHERE ca_address_sk<100; zyfdb=> EXPLAIN SELECT * FROM customer WHERE ca_address_sk<100; QUERY PLAN ------------------------------------------------------------ Seq Scan on customer (cost=0.00..11.25 rows=33 width=788) Filter: (ca_address_sk < 100) (2 rows) create index customer_idx on customer(ca_address_sk); EXPLAIN SELECT /*+ indexscan(customer customer_idx ) */ * FROM customer WHERE ca_address_sk<100; zyfdb=> EXPLAIN SELECT /*+ indexscan(customer customer_idx ) */ zyfdb-> * FROM customer WHERE ca_address_sk<100; QUERY PLAN ------------------------------------------------------------------------------- [Bypass] Index Scan using customer_idx on customer (cost=0.00..8.27 rows=1 width=788) Index Cond: (ca_address_sk < 100) (3 rows)

3 rename索引

zyfdb=> zyfdb=> ALTER INDEX idx_test_testnum RENAME TO idx_test_testnum_new; ALTER INDEX zyfdb=> zyfdb=> \di List of relations Schema | Name | Type | Owner | Table | Storage --------+----------------------+-------+-------+----------+--------- public | customer_idx | index | user5 | customer | public | idx_test_testnum_new | index | user5 | test | public | test_pkey | index | user5 | test | (3 rows)

4 重建索引

--重建一个单独索引 ALTER INDEX idx_test_testnum_new REBUILD; REINDEX INDEX idx_test_testnum_new; --重建所有索引 reindex table test;

5 移动索引到其他表空间

--查看索引所在的表空间 select * from pg_indexes where tablename = 'test'; zyfdb=> select schemaname ,tablename ,indexname , tablespace from pg_indexes where tablename = 'test'; schemaname | tablename | indexname | tablespace ------------+-----------+----------------------+------------ public | test | test_pkey | public | test | idx_test_testnum_new | (2 rows) --将索引idx_test_testnum_new移动到表空间myindex_ts: ALTER INDEX idx_test_testnum_new SET TABLESPACE enmtbs; --或 select schemaname ,tablename ,indexname , tablespace from pg_indexes where indexname = 'idx_test_testnum_new'; schemaname | tablename | indexname | tablespace ------------+-----------+----------------------+------------ public | test | idx_test_testnum_new | enmtbs (1 row)

6 删除索引

zyfdb=> \di List of relations Schema | Name | Type | Owner | Table | Storage --------+----------------------+-------+-------+----------+--------- public | customer_idx | index | user5 | customer | public | idx_test_testnum_new | index | user5 | test | public | test_pkey | index | user5 | test | (3 rows) zyfdb=> drop index idx_test_testnum_new; DROP INDEX zyfdb=> \di List of relations Schema | Name | Type | Owner | Table | Storage --------+--------------+-------+-------+----------+--------- public | customer_idx | index | user5 | customer | public | test_pkey | index | user5 | test | (2 rows)
「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论