들어가기
위치기반 서비스 데이터베이스 선택 [MySQL]
들어가기현재 진행중인 프로젝트인 'AmorGakCo(아모르각코)'는 사용자들이 모각코 모임을 개설하고 사용자의 위치를 기반으로 모각코 모임을 조회하는 서비스입니다.서비스 기능의 핵심에는 사용
curiosity-s.tistory.com
저번 포스팅에선 MySQL의 공간 검색 쿼리를 통해 DB Query, API Latency를 측정해봤습니다.
이번 포스팅에선 같은 데이터 셋(서울 지역 한식가게 약 4만6천 건)으로 PostgreSQL GIS 쿼리를 알아보고 MySQL과 비교해보겠습니다.
Hibernate JPQL
Hibernate ORM User Guide
Starting in 6.0, Hibernate allows to configure the default semantics of List without @OrderColumn via the hibernate.mapping.default_list_semantics setting. To switch to the more natural LIST semantics with an implicit order-column, set the setting to LIST.
docs.jboss.org
데이터베이스 선택에 많은 영향이 있지는 않지만 MySQL은 JPQL지원이 PostGIS 보다 떨어집니다.
St_dwithin, St_distance
PostGIS 공식 문서엔 ST_DWithin을 사용하라고 권하고 있습니다.
ST_Distance는 인덱스를 사용하지 않기 때문에 쿼리 성능이 잘 나오지 않습니다.
주의 사항
Is it Lon/Lat or Lat/Lon?
In web mapping APIs like Google Maps, spatial coordinates are often in order of latitude then longitude. In spatial databases like PostGIS and SQL Server, spatial coordinates are in longitude and then latitude. Mixing the two up can lead to questions like
postgis.net
PostgreSQL의 ST_DWithin()에 넘길 좌표값은 경도,위도 순서로 넣어야합니다.
Alter Table로 순서를 직접 뒤집어준다면 위도,경도로 INSERT를 할 수는 있습니다.
테스트 환경
- Postman
- 서울 지역 한식 가게 46,482 건
- 기준 위치 신사역 37.5162149 127.0195806
- 반경 기준 1Km, 3Km, 5Km, 10Km
- 기준 DB PostgreSQL(PostGIS) : ST_DWithin + GiST Index
- 비교 DB MySQL : ST_Within + Spatial Index
- 10회 평균 API Latency 측정
@Query(value = "select kf from KoreanFood kf " +
"where st_dwithin(kf.location, :point, :distance,false) = true")
List<KoreanFood> findByKoreanFoodWithSTDwithin(
@Param("point") Point point,
@Param("distance") double distance);
st_dwithin의 4번째 파라미터는 더 빠른 계산을 위한 flag값 입니다. (정확성은 떨어집니다.)
결과
MySQL의 쿼리를 Spatial Index를 타게 만들어도 PostGIS의 R-Tree기반 GiST인덱스보다 성능이 뒤떨어집니다.
RealMySQL8.0 책에서는 MySQL의 GIS는 도입된지는 꽤 시간이 흘렀지만 앞서 말한 지리좌표계의 도입은 8.0이기 때문에
기능의 정확성이나 성능에 주의가 필요하다고 합니다.
성능 측면에서 PostgreSQL이 상당히 앞서고 있고 Native 쿼리가 아닌 JPQL도 지원하기에 PostgreSQL을 선택하지 않을 이유는 없다고 생각합니다.
관련 코드는 아래 레포지토리를 참고해주세요.
GitHub - songhaechan/amorgakco-gis-performance-test: 아모르각코 GIS 반경 검색 성능 비교
아모르각코 GIS 반경 검색 성능 비교. Contribute to songhaechan/amorgakco-gis-performance-test development by creating an account on GitHub.
github.com
'Spring' 카테고리의 다른 글
Google S2 (3) | 2024.09.11 |
---|---|
Google S2를 이용한 위치 검색 개선 (0) | 2024.09.09 |
위치기반 서비스 데이터베이스 선택 [MySQL] (0) | 2024.07.12 |
게시글과 이미지 등록 API 분리로 API Latency 개선기 (3) (0) | 2024.07.06 |
적정 스레드 풀 크기를 고민해보자 (0) | 2024.06.19 |