Postgresql에서 MS SQL의 Cluster Index 효과 내기 0 0 2,425

by 지현명 [PostgreSQL 노하우/팁/자료] [2016.11.15 18:26:35]



--pg공식 메뉴얼의 주의 사항은 ..

Keep in mind that the CLUSTER operation takes an ACCESS EXCLUSIVE
lock on the table, which typically means that it does not allow any other
operation on the table while CLUSTER is running.

ACCESS EXCLUSIVE : 해당 테이블에 대해 암것도 못하네요...

https://www.postgresql.org/docs/current/static/explicit-locking.html 

"Table 13-2. Conflicting Lock Modes" 참고

성능은 적용하기 전과 40%정도 좋아 진다고 하는데...근데 lock이...파티셔닝해서 하면 될듯한데..

CREATE TABLE table_cluster(
name varchar(2),
id int
);

 

CREATE INDEX table_cluster_idx ON table_cluster(name);
CREATE INDEX table_cluster_idx2 ON table_cluster(id);

 

insert into table_cluster values ('aa', 1);
insert into table_cluster values ('ba', 2);
insert into table_cluster values ('cc', 3);
insert into table_cluster values ('ab', 4);
 

--SELECT * FROM heap_page_items(get_raw_page('table_cluster', 0));
SELECT * FROM heap_page_item_attrs(get_raw_page('table_cluster', 0), 'table_cluster'::regclass);

 

'ab', 4가 맨 아래에 저장된 것을 확인

 

table_cluster_idx  인덱스를 기준으로 Cluster하는 방법은 2가지

#1. 아래처럼 테이블과 인덱스를 지정
CLUSTER table_cluster USING table_cluster_idx; --필요없는 row삭제됨.

 

#2. Cluster할 인덱스를 테이블에 alter해 놓고 CLUSTER 사용
--테이블에 하나만 생성 할 수 있다.
--아래와 같이 정의 되어 있다고 해서 데이터가 정렬되서 입력되지 않는다.
--CLUSTER [Table] 을 해야 한다.
ALTER TABLE table_cluster CLUSTER ON table_cluster_idx;
--ALTER TABLE table_cluster set without CLUSTEr; --클러스터 설정 삭제
CLUSTER table_cluster; -- 이 명령어를 실행 해야 한다.

 

--SELECT * FROM heap_page_items(get_raw_page('table_cluster', 0));
SELECT * FROM heap_page_item_attrs(get_raw_page('table_cluster', 0), 'table_cluster'::regclass);

 

 

클러스터를 실행 한 후 데이터의 순서가 바뀌었다. t_ctid까지 변경됨.

 

간단히 정리하면....

table_cluster_idx  인덱스로 Cluster하기 전

table_cluster_idx  인덱스로 Cluster 적용 후

 

 

p.s

인덱스 페이지도 Cluster하기 전과 다르게 바뀝니다.

SELECT * FROM bt_page_items('table_cluster_idx', 1);

table_cluster_idx 인덱스로 Cluster 하기 전

적용 후

댓글등록
SQL문을 포맷에 맞게(깔끔하게) 등록하려면 code() 버튼을 클릭하여 작성 하시면 됩니다.
로그인 사용자만 댓글을 작성 할 수 있습니다. 로그인, 회원가입