280만개 데이터를 이용한 테스트(MySQL에서 제공하는 테스트 데이터 이용)

select * from employees.salaries order by rand() limit 15;
select count(*) from employees.salaries;  # 2,844,047

https://s3-us-west-2.amazonaws.com/secure.notion-static.com/6bdb15dc-39dd-444c-9a94-33b852cdaa5e/Untitled.png

# default
create table sal
select *
from employees.salaries
order by rand();

# clustered index
create table sal_c
select *
from employees.salaries
order by rand();

# 클러스터 인덱스(pk) 지정
alter table sal_c
    add primary key (emp_no, from_date);

# secondary index
create table sal_se
select *
from employees.salaries
order by rand();

# 보조 인덱스 지정
alter table sal_se
    add index idx_emp_no (emp_no, from_date);

show index from sal;
show index from sal_c;
show index from sal_se;

clustered index

clustered index

secondary index

secondary index

show table status;

https://s3-us-west-2.amazonaws.com/secure.notion-static.com/bc2dd087-9498-4656-80ae-46289422dc9e/Untitled.png

DOC : SHOW TABLE STATUS;

Data_length: 데이터 파일의 크기

→ clustered index를 위해 할당된 크기(bytes) = innoDB page size(16KB) x clustered index size

Index_length: index 파일의 크기

→ innoDB page size x non_clustered index size

→ clustered index(sal_c)는 리프 페이지가 곧 데이터 페이지이기 때문에 index_length는 0으로 출력된다.

Data_free: 할당은 되었으나 사용하지 않는 공간(여분의 공간)

TEST - INDEX 속도 측정

select * from sal where emp_no = 430609;  # non-index
select * from sal_c where emp_no = 430609;  # clustered index
select * from sal_se where emp_no = 430609;  # secondary index

non index, clustered, secondary index 순서대로 1s 357ms, 51ms, 64ms 소요

non index, clustered, secondary index 순서대로 1s 357ms, 51ms, 64ms 소요

TEST - INDEX 사용 시 읽은 페이지 수 확인

쿼리를 실행할 때 몇 개의 페이지를 읽는지 확인