[Snow-ball]프로그래밍(컴퓨터)/DATABASE
[PSQL] DataBase 에 index 를 사용하면 과연 더 빠를까? 직접 테스트를 진행해보자
Snow-ball
2023. 4. 27. 21:37
반응형
PSQL을 사용하다가 Inex를 사용할려고 한다. 그러다가 정말 index를 설정한다면 이론적으로와 동일하게 더 빠르게 동작을 할까? 라는 의문이 생겨서 테스트를 시작하게 되었다.
테스트를 위한 코드이다 보니 매우 간결하게 테이블을 만들기로 했다.
1
2
3
4
5
6
7
8
9
|
model Users {
@@map(name: "users")
id String @id
accountId String
nickname String
password String
}
|
cs |
4가지의 컬럼만 사용할 예정이다.
사용하는 S/W, H/W의 스펙
OS: macOS Ventura 13.3.1
memory: 32GB
chip: Apple M1 Pro
postgres(PostgresSQL): 14.6
Test Start!!
1) psql에 더미 데이터 100000만개를 넣고 시작했다.
1
2
3
4
5
|
insert into users select md5(random()::text) as id,
md5(random()::text) as "accountId",
md5(random()::text) as nickname,
md5(random()::text) as password
from generate_series(1, 100000) as gs;
|
cs |
2) insert 를 해주고 count(*)로 확인을 해보자.
3) users(Table Column)의 id 기준으로 맨 아래있는 데이터를 찾아오자
1
|
select * from users order by id desc limit 1;
|
cs |
그럼 밑에 사진처럼 검색이 된다.
4) nickname을 index 할 것이기 때문에 nickname 복사해서 테스트를 진행해보자.
4-1) 일단 위의 모델(psql schema)를 유지한채로 쿼리를 사용해보자.
1
|
explain analyze select * from users where nickname = '7562472d5ca0f766b99e61ecaf2e34d6'
|
cs |
actual time: 21
Excution Time: 22.900ms
4-2) 모델에 @@index([nickname])을 추가했다.
1
2
3
4
5
6
7
8
9
|
model Users {
@@map(name: "users")
@@index([nickname])
id String @id
accountId String
nickname String
password String
}
|
cs |
그리고 나서 migrate를 진행해주고 나서 query를 사용한다.
1
|
explain analyze select * from users where nickname = '7562472d5ca0f766b99e61ecaf2e34d6'
|
cs |
actual time = 0.2
Execution Time: 0.252ms
5) 두개의 값을 비교해보자
반응형