pgsql中的索引不能重名,重名的創(chuàng)建失敗。
創(chuàng)新互聯(lián)公司服務(wù)項(xiàng)目包括薩迦網(wǎng)站建設(shè)、薩迦網(wǎng)站制作、薩迦網(wǎng)頁(yè)制作以及薩迦網(wǎng)絡(luò)營(yíng)銷(xiāo)策劃等。多年來(lái),我們專(zhuān)注于互聯(lián)網(wǎng)行業(yè),利用自身積累的技術(shù)優(yōu)勢(shì)、行業(yè)經(jīng)驗(yàn)、深度合作伙伴關(guān)系等,向廣大中小型企業(yè)、政府機(jī)構(gòu)等提供互聯(lián)網(wǎng)行業(yè)的解決方案,薩迦網(wǎng)站推廣取得了明顯的社會(huì)效益與經(jīng)濟(jì)效益。目前,我們服務(wù)的客戶以成都為中心已經(jīng)輻射到薩迦省份的部分城市,未來(lái)相信會(huì)繼續(xù)擴(kuò)大服務(wù)區(qū)域并繼續(xù)獲得客戶的支持與信任!
?
創(chuàng)建二級(jí)索引的命令:create index CONCURRENTLYidx_abc on tb1(a,b);
?
注意:reindex重建索引的過(guò)程是阻塞的,一般大表不建議使用這個(gè)命令,可以重建一個(gè)索引,然后刪除老的索引。
?
下面看一個(gè)《PostgreSQL實(shí)戰(zhàn)》書(shū)上page202的例子:
由于PG的MVCC機(jī)制,當(dāng)運(yùn)行大量的更新操作后,會(huì)有索引膨脹的現(xiàn)象。這時(shí)候 可以通過(guò) create index concurrently不阻塞查詢(xún)和更新的情況下,在線重新重建索引,創(chuàng)建好索引之后,再刪除原先的索引,減少索引的尺寸,提高查詢(xún)速度。對(duì)于主鍵也可以使用這種方式進(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)
?
?
這樣就完成了主鍵索引的重建,對(duì)于大規(guī)模的數(shù)據(jù)庫(kù)集群,可以通過(guò) pg_repack工具進(jìn)行定時(shí)的索引重建。
?
當(dāng)前文章:PG創(chuàng)建or修改索引
鏈接地址:http://chinadenli.net/article2/jeisic.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供全網(wǎng)營(yíng)銷(xiāo)推廣、營(yíng)銷(xiāo)型網(wǎng)站建設(shè)、搜索引擎優(yōu)化、商城網(wǎng)站、定制網(wǎng)站、網(wǎng)站排名
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)