본문 바로가기

BackEnd/DB(SQL)

[DB_MySQL] 이것이 MySQL이다 ch11 전체 텍스트 검색과 파티션 2 (파티션)

728x90
반응형

1. 파티션 개요와 실습
◎ 파티션
   ▷ 대량의 테이블을 물리적으로 여러 개의 테이블로 쪼개기
   ▷ 데이터의 분포 특성, 자주 사용되는 쿼리문이 무엇인지에 따라서 효율에 차이 있음

 

 파티션 구성
   ▷ 파티션 테이블에는 Primary Key 지정 하면 안됨 
   ▷ 데이터 입력 : 입력됨과 동시에 파티션 키에 의해서 데이터가 각 파티션으로 나뉘어짐

 

1. parttbl 테이블 생성 및 값 입력
-- 파티션으로 테이블 구현 : 대용량의 테이블을 물리적으로 분할하여 사용
-- sqldb.sql을 이용하여 sqldb 초기화

create database if not exists partdb;
use partdb;

drop table if exists parttbl;

create table parttbl
(
	userID char(8) not null,
    name varchar(10) not null,
    birthYear int not null,
    addr char(2) not null)
partition by range(birthYear)(
	partition part1 values less than(1971),
	partition part2 values less than(1979),
	partition part3 values less than maxvalue
);

insert into parttbl
	select userID, name, birthYear, addr from sqldb.usertbl;

select * from parttbl;​


2. 파티션 확인
-- 파티션 확인
select table_schema, table_name, partition_name, partition_ordinal_position, table_rows
	from information_schema.partitions
    where table_name = 'parttbl';

-- 범위를 이용한 조회 1965년 이전 출생자
select * from parttbl where birthYear <= 1965

-- explain : 어느 파티션에서 조회를 했는지 확인(쿼리문 앞에 추가)
explain select * from parttbl where birthYear <= 1965;


현재 파티션을 확인하는 코드입니다.



3. 파티션 관리 
-- 파티션 관리 : 분할
-- 현재 파티션 3 -> 1979 ~ 1986 미만 (파티션 3), 1986 이상 (파티션 4)
-- ALTE TABLE ...... REORANIZE PARTITION 문
alter table parttbl
	reorganize partition part3 into(
    partition part3 values less than (1986),
    partition part4 values less than maxvalue
    );

optimize table parttbl;

-- 파티션 확인
select table_schema, table_name, partition_name, partition_ordinal_position, table_rows
	from information_schema.partitions
    where table_name = 'parttbl';​


파티션으로 범위에 따라 추가적으로 분할합니다.

 

파티션을 통해 대량의 테이블을 물리적으로 여러 개의 테이블로 쪼개보았습니다.

분해해도 데이터의 분포 특성과 자주 사용되는 쿼리문이 무엇인지에 따라서 효율에 차이 있을 수 있네요!

 

많은 분들의 피드백은 언제나 환영합니다!

 

많은 댓글 부탁드려요~~

 

728x90
반응형