pgsql中的索引不能重名,重名的創(chuàng)建失敗。
創(chuàng)新互聯(lián)公司服務(wù)項目包括薩迦網(wǎng)站建設(shè)、薩迦網(wǎng)站制作、薩迦網(wǎng)頁制作以及薩迦網(wǎng)絡(luò)營銷策劃等。多年來,我們專注于互聯(lián)網(wǎng)行業(yè),利用自身積累的技術(shù)優(yōu)勢、行業(yè)經(jīng)驗、深度合作伙伴關(guān)系等,向廣大中小型企業(yè)、政府機構(gòu)等提供互聯(lián)網(wǎng)行業(yè)的解決方案,薩迦網(wǎng)站推廣取得了明顯的社會效益與經(jīng)濟(jì)效益。目前,我們服務(wù)的客戶以成都為中心已經(jīng)輻射到薩迦省份的部分城市,未來相信會繼續(xù)擴大服務(wù)區(qū)域并繼續(xù)獲得客戶的支持與信任!
?
創(chuàng)建二級索引的命令:create index CONCURRENTLYidx_abc on tb1(a,b);
?
注意:reindex重建索引的過程是阻塞的,一般大表不建議使用這個命令,可以重建一個索引,然后刪除老的索引。
?
下面看一個《PostgreSQL實戰(zhàn)》書上page202的例子:
由于PG的MVCC機制,當(dāng)運行大量的更新操作后,會有索引膨脹的現(xiàn)象。這時候 可以通過 create index concurrently不阻塞查詢和更新的情況下,在線重新重建索引,創(chuàng)建好索引之后,再刪除原先的索引,減少索引的尺寸,提高查詢速度。對于主鍵也可以使用這種方式進(jìn)行,例如:
db1=# \d testdata
???????????????? Table "public.testdata"
? Column? |???? Type???? | Collation | Nullable | Default
----------+--------------+-----------+----------+---------
?id?????? | integer????? |?????????? | not null |
?course?? | integer????? |?????????? |????????? |
?grade??? | numeric(4,2) |?????????? |????????? |
?testtime | date???????? |?????????? |????????? |
Indexes:
??? "testdata_pkey" PRIMARY KEY, btree (id)
?
db1=# create unique index concurrently on testdata using btree(id);
?
db1=#select
schemaname,
relname,
indexrelname,
pg_relation_size (indexrelid) as index_size,
idx_scan,
idx_tup_read,
idx_tup_fetch
from pg_stat_user_indexes where
indexrelname in (select indexname from pg_indexes where schemaname ='public' and tablename='testdata');
?schemaname | relname? |? indexrelname?? | index_size | idx_scan | idx_tup_read | idx_tup_fetch
------------+----------+-----------------+------------+----------+--------------+---------------
?public???? | testdata | testdata_pkey?? |????? 16384 |?????? ?2 |????? 5999998 |?????? 5999998
?public???? | testdata |testdata_id_idx |????? 16384 |??????? 0 |??????????? 0 |???????????? 0
(2 rows)
?
db1=# begin;
BEGIN
?
db1=# alter table testdata drop constraint testdata_pkey;
ALTER TABLE
?
db1=# alter table testdata add constraint testdata_id_idx primary key using index testdata_id_idx ;
ALTER TABLE
?
db1=# end;
COMMIT
?
db1=# \d testdata
???????????????? Table "public.testdata"
? Column? |???? Type???? | Collation | Nullable | Default
----------+--------------+-----------+----------+---------
?id?????? | integer????? |?????????? | not null |
?course?? | integer????? |?????????? |????????? |
?grade??? | numeric(4,2) |?????????? |????????? |
?testtime | date???????? |?????????? |????????? |
Indexes:
??? "testdata_id_idx" PRIMARY KEY, btree (id)
?
db1=#select
schemaname,
relname,
indexrelname,
pg_relation_size (indexrelid) as index_size,
idx_scan,
idx_tup_read,
idx_tup_fetch
from pg_stat_user_indexes where
indexrelname in (select indexname from pg_indexes where schemaname ='public' and tablename='testdata');
schemaname | relname? |? indexrelname?? | index_size | idx_scan | idx_tup_read | idx_tup_fetch
------------+----------+-----------------+------------+----------+--------------+---------------
?public???? | testdata |testdata_id_idx |????? 16384 |?????? ?0 |??????????? 0 |???????????? 0
(1 row)
?
?
這樣就完成了主鍵索引的重建,對于大規(guī)模的數(shù)據(jù)庫集群,可以通過 pg_repack工具進(jìn)行定時的索引重建。
?
當(dāng)前文章:PG創(chuàng)建or修改索引
鏈接地址:http://aaarwkj.com/article2/jeisic.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供全網(wǎng)營銷推廣、營銷型網(wǎng)站建設(shè)、搜索引擎優(yōu)化、商城網(wǎng)站、定制網(wǎng)站、網(wǎng)站排名
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)