๐ 1. ์๋ผ์คํฑ์์น(Elasticsearch)๋ฅผ ์ฌ์ฉํ๊ฒ ๋ ๊ณ๊ธฐ
ํ๋ก์ ํธ๋ฅผ ์งํํ๋ฉด์ ์๋์ ์ผ๋ก ํฌ๊ธฐ๊ฐ ํฐ ๋๋ฌผ ๋ฐ์ดํฐ(130๋ง ๊ฑด์ ๋ฐ์ดํฐ)๋ฅผ ์ฌ์ฉํ ์ผ์ด ์์๋ค.
ํ์ํ ์์ ์ '๋๋ฌผ์ ๊ณ์ธต์ ๋ถ๋ฅ๋ฅผ ๋ํ๋ด๋ ๊ฐ์์ ํน์ ๊ณ์ธต์ด ์๋ ๋๋ฌผ์ ์กฐํํ๋ ๊ฒ'์ด์๋ค. ์ด๋ฌํ ๊ณผ์ ์์ ์ด๋ป๊ฒ ํ๋ฉด ์กฐํ ์๊ฐ์ ๋จ์ถ์ํฌ ์ ์์๊น ๊ณ ๋ฏผํ์๊ณ ๊ทธ ๊ฒฐ๊ณผ ์๋ผ์คํฑ์์น(Elasticsearch)๋ฅผ ์ฌ์ฉํ๊ฒ ๋์๋ค.
์์ ์ ๋ํด์ ์กฐ๊ธ ๋ ์์ธํ๊ฒ ์ดํด๋ณด๋๋ก ํ์. ์ํ๋ ๋์์ '๋๋ฌผ์ ๊ณ์ธต์ ๋ถ๋ฅ(higherClassification)์์ ํน์ ๊ณ์ธต(ํฌ์ ๋ฅ, ์์๋ฅ, ์กฐ๋ฅ,...)์ด ์กด์ฌํ๋ ๋๋ฌผ๋ค์ ๊ฐ์ ธ์ค๋ ๊ฒ'์ด๋ค.
๋ฐ์ดํฐ๋ ์๋์ ๊ฐ์ด ๊ตฌ์ฑ๋์ด ์๋ค.
{
...
"higherClassification": "Animalia; Chordata; Vertebrata; Mammalia; Proboscidea; Elephantidae"
...
},
{
...
"higherClassification": "Animalia; Chordata; Vertebrata; Mammalia; Rodentia; Mymorpha"
...
}
๋ง์ฝ Mammalia(ํฌ์ ๋ฅ)๋ผ๋ ๊ณ์ธต์ ํฌํจํ๊ณ ์๋ ๋๋ฌผ์ ๊ฐ์ ธ์จ๋ค๊ณ ํ๋ฉด ์๋ 2๊ฐ์ ๋ฐ์ดํฐ๋ฅผ ๋ชจ๋ ๊ฐ์ ธ์ฌ ๊ฒ์ด๋ค.
{
...
"higherClassification": "Animalia; Chordata; Vertebrata; Mammalia; Proboscidea; Elephantidae"
...
},
{
...
"higherClassification": "Animalia; Chordata; Vertebrata; Mammalia; Rodentia; Mymorpha"
...
}
ํ์ง๋ง, Elephantidae(์ฝ๋ผ๋ฆฌ๊ณผ)๋ผ๋ ๊ณ์ธต์ ํฌํจํ๊ณ ์๋ ๋๋ฌผ์ ๊ฐ์ ธ์จ๋ค๊ณ ํ๋ฉด, 2๊ฐ๊ฐ ์๋ 1๊ฐ์ ๋ฐ์ดํฐ๋ง ๊ฐ์ ธ์ฌ ๊ฒ์ด๋ค.
{
...
"higherClassification": "Animalia; Chordata; Vertebrata; Mammalia; Proboscidea; Elephantidae"
...
},
{
...
"higherClassification": "Animalia; Chordata; Vertebrata; Mammalia; Rodentia; Mymorpha"
...
}
์ฆ, 130๋ง ๊ฑด์ ๋ฐ์ดํฐ์์ ํน์ ์์ฑ(๋๋ฌผ์ ๊ณ์ธต์ ๋ถ๋ฅ, higherClassification)์ ๊ฐ์ ๋ํ๋ด๋ ๋ฌธ์์ด์ ํน์ ๋ฌธ์์ด(ํฌ์ ๋ฅ, ์์๋ฅ, ์กฐ๋ฅ,...)์ด ์๋์ง ํ์ธํ๊ณ ๊ทธ ๊ฐ์ ๊ฐ์ ธ์์ผ ํ๋ค.
๋ฐ์ดํฐ์์ ์ํ๋ ๊ฐ์ ๋น ๋ฅด๊ฒ ๊ฐ์ ธ์ค๊ธฐ ์ํด ์๋ 3๊ฐ์ง ๊ธฐ์ ์ ๊ณ ๋ คํด ๋ณด์๋ค.
๐ 1. MySQL

MySQL๋ก ์์ ์์ ์ ํ๊ฒ ๋๋ค๋ฉด Full-Scan์ ํ๋ค๋ ๋ฌธ์ ์ ์ด ์์๋ค.
๋ํ, ๋ฌธ์์ด์์ ํน์ ๋ฌธ์๊ฐ ์กด์ฌํ๋์ง ์๊ธฐ ์ํด LIKE ์ฌ์ฉํ๊ฒ ๋๋ค. LIKE๊ฐ ๋์ํ๊ฒ ๋๋ฉด Turbo Boyer-Moore ์๊ณ ๋ฆฌ์ฆ์ด ์๋ํ๊ฒ ๋๋ค.
๊ณต์๋ฌธ์๋ฅผ ์ดํด๋ณด๋ฉด, LIKE๋ฅผ ์ฌ์ฉํ๋ ๊ฒฝ์ฐ ์ผ๋ฐ์ ์ผ๋ก ์ธ๋ฑ์ค๊ฐ ๊ฑธ๋ ค์๋ ๊ฒฝ์ฐ B-Tree๋ฅผ ์ด์ฉํ๊ณ , ๊ทธ๋ ์ง ์์ ๊ฒฝ์ฐ์๋ Turbo Boyer-์๊ณ ๋ฆฌ์ฆ์ ์ฌ์ฉํ๋ค๊ณ ๋์ด์๋ค.
MySQL :: MySQL 8.0 Reference Manual :: 8.3.9 Comparison of B-Tree and Hash Indexes
8.3.9 Comparison of B-Tree and Hash Indexes Understanding the B-tree and hash data structures can help predict how different queries perform on different storage engines that use these data structures in their indexes, particularly for the MEMORY storage
dev.mysql.com
Turbo Boyer-Moore์ ๋ํ ๋ด์ฉ์ ์๋์ ๊ธ์ ์ฐธ๊ณ ํ์.
Turbo-BM algorithm
The Turbo-BM algorithm is an amelioration of the Boyer-Moore algorithm. It needs no extra preprocessing and requires only a constant extra space with respect to the original Boyer-Moore algorithm. It consists in remembering the factor of the text that matc
www-igm.univ-mlv.fr
์ด๋ฌํ ์ฌ์ค๊ณผ ํจ๊ป ์๊ฐ๋ณต์ก๋๋ฅผ ๊ณ์ฐํด ๋ณด๋ฉด O(๋ฐ์ดํฐ์ ํฌ๊ธฐ_130๋ง ๊ฑด) x O(๋ฌธ์์ด์ ๊ธธ์ด)๊ฐ ๋๊ฒ ๋๋ค.
๐ 2. Apatch Hive

Apatch Hive๋ ๊ตฌ์กฐํ๋ ๋น ๋ฐ์ดํฐ๋ฅผ ์ฒ๋ฆฌํ๋ ๋ฐ ์ฌ์ฉ๋๋ฉด HiveQL์ ์ด์ฉํ์ฌ ํธ๋ฆฌํ๊ฒ ํ๋ก(๋ถ์ฐ ํ์ผ ์์คํ )์ ์ด์ฉํ ์ ์๊ฒ ํ๋ ๊ฒ์ด๋ค.
HiveQL์ ํตํด์ ์กฐํ๋ฅผ ์คํํ๋ฉด ๋ด๋ถ์ ์ผ๋ก ๋งต-๋ฆฌ๋์ค ์ธ์ด๋ก ๋ณํํ๋ ์์ ์ ๊ฑฐ์น๊ธฐ ๋๋ฌธ์, ๋งต๊ณผ ๋ฆฌ๋์ค ๊ฐ์ ์ ํ๋ง ์์ ์ผ๋ก ์ธํ์ฌ ์๋๊ฐ ๋๋ฆฌ๋ค๋ ๋จ์ ์ด ์๋ค.
๐ 3. Elasticsearch

Elasticsearch(์ดํ ES)๋ ์ญ์ธ๋ฑ์ค(Inverted Index, ์ญ์์ธ)๋ฅผ ์ด์ฉํ๋ค. (ES์ ๋ํ ์์ธํ ์ค๋ช ์ ์๋์ ๋์ต๋๋ค.)
์ญ์ธ๋ฑ์ค๋ฅผ ํ์ฉํ์ฌ ํค์๋๋ฅผ ํตํด ๋ฌธ์๋ฅผ ์ฐพ๋๋ค. ๊ทธ๋์ ์ฐ๋ฆฌ๊ฐ ์ํ๋ ์กฐํ ์์ ์ด ๋งค์ฐ ๋น ๋ฅด๋ค๋ ์ฅ์ ์ด ์๋ค.
3๊ฐ์ง ๊ธฐ์ ์ ๋น๊ต๋ถ์ํ ๊ฒฐ๊ณผ, ES๊ฐ '์๋์ ์ผ๋ก ํฐ ๋ฐ์ดํฐ์์ ํน์ ๋ฌธ์์ด์ ๊ฐ์ง ๋ฐ์ดํฐ๋ฅผ ์ฐพ๋ ์์ '์ ๊ฐ์ฅ ์ ์ ํ๋ค๊ณ ์๊ฐํ์ฌ ES๋ฅผ ์ฐ๊ธฐ๋ก ํ์๋ค.
๐ 2. ES ๊ฐ๋ & ํน์ง
๐ 1. ๊ฐ๋
ES๋ Apache Lucene ๊ธฐ๋ฐ์ ์คํ์์ค ๋ถ์ฐ ๊ฒ์ ์์ง์ด๋ค. (Apache Lucene์ ๋ชจ๋ ๊ฒ์์์ง์ ์์ด๋ผ๊ณ ํ ์ ์๋ค.)
๊ธฐ๋ณธ์ ์ผ๋ก ๋ฐ์ดํฐ๋ฅผ ์ธ๋ฑ์ฑํ์ฌ ์ ์ฅํ๊ณ , ๊ฒ์, ์ง๊ณ ๋ฑ์ ๊ธฐ๋ฅ์ ์ํํ๋ค.
๐ 2. ํน์ง
ES์ ๊ฐ์ฅ ํฐ ํน์ง์ ์ฐ๋ฆฌ๊ฐ ํํ ์๋ ์ธ๋ฑ์ค(forward index)๊ฐ ์๋ ์ญ์ธ๋ฑ์ค(inverted index)๋ฅผ ์ฌ์ฉํ๋ค๋ ๊ฒ์ด๋ค.
์ธ๋ฑ์ค๋ฅผ ์ฌ์ฉํ๋ค๋ฉด ์๋์ ๊ฐ์ด ๋ฐ์ดํฐ๊ฐ ์ ์ฅ๋ ๊ฒ์ด๋ค.
Document | Words |
๋ฌธ์1 | ์ฌ๊ณผ, ๋ฐ๋๋, ๊ทค |
๋ฌธ์2 | ์ฌ๊ณผ, ์ฅ๋ฏธ |
๋ฌธ์3 | ์ฌ๊ณผ, ๊ณต, ๊ทค |
๋ฐ๋ฉด, ์ญ์ธ๋ฑ์ค๋ฅผ ์ด์ฉํ๋ค๋ฉด ์๋์ ๊ฐ์ด ๋ฐ์ดํฐ๊ฐ ์ ์ฅ๋๋ค.
Word | Document |
์ฌ๊ณผ | ๋ฌธ์1, ๋ฌธ์2, ๋ฌธ์3 |
๊ทค | ๋ฌธ์1, ๋ฌธ์3 |
๋ฐ๋๋ | ๋ฌธ์1 |
์ฅ๋ฏธ | ๋ฌธ์2 |
๊ณต | ๋ฌธ์3 |
๊ทค์ ๊ฐ์ง๊ณ ์๋ ๋ฐ์ดํฐ๋ฅผ ๊ฐ์ ธ์จ๋ค๊ณ ํด๋ณด์. ์ธ๋ฑ์ค๋ฅผ ์ด์ฉํ์ฌ ์กฐํํ๋ฉด ๋ค์๊ณผ ๊ฐ์ด ํ ์คํธ๋ฅผ ํ๋ํ๋ ์ฝ์ผ๋ฉด์ ๊ฒ์์ ํ ๊ฒ์ด๋ค.
Document | Words | ๊ฒ์(๋น๊ตํด๋ณด๋ ๋์) | ์กฐํ(๊ฐ์ ธ์ค๋ ๋์) |
๋ฌธ์1 | ์ฌ๊ณผ, ๋ฐ๋๋, ๊ทค | O | O |
๋ฌธ์2 | ์ฌ๊ณผ, ์ฅ๋ฏธ | O | X |
๋ฌธ์3 | ์ฌ๊ณผ, ๊ณต, ๊ทค | O | O |
ํ์ง๋ง, ์ญ์ธ๋ฑ์ค๋ฅผ ์ด์ฉํ์ฌ ์กฐํํ๋ฉด ํ ์คํธ๋ฅผ ํ๋ํ๋ ์ฝ์ง ์๊ณ ๋ ํค์๋์ ํด๋นํ๋ ๊ฐ์ ๋ฐ๋ก ์ป์ ์ ์๋ค.
Word | Document | ๊ฒ์(๋น๊ตํด๋ณด๋ ๋์) | ์กฐํ(๊ฐ์ ธ์ค๋ ๋์) |
์ฌ๊ณผ | ๋ฌธ์1, ๋ฌธ์2, ๋ฌธ์3 | X | X |
๊ทค | ๋ฌธ์1, ๋ฌธ์3 | O | O |
๋ฐ๋๋ | ๋ฌธ์1 | X | X |
์ฅ๋ฏธ | ๋ฌธ์2 | X | X |
๊ณต | ๋ฌธ์3 | X | X |
์ด๋ฌํ ์ด์ ๋ก ES๋ฅผ ์ด์ฉํ๋ฉด ๋น ๋ฅธ ์๋๋ก ์กฐํ๋ฅผ ํ ์ ์๋ค.
๐ 3. ์ ์ฉ
์คํ๋ง๋ถํธ ํ๊ฒฝ์์ ES๋ฅผ ์ ์ฉํ์๋ค. 'ES ์ฌ์ฉ์ ์ํ ํ๊ฒฝ ์ธํ , ๋ฐ์ดํฐ ์ฝ์ , ๋ฐ์ดํฐ ์กฐํ'๋ผ๋ 3๊ฐ์ง ๋จ๊ณ๋ฅผ ์ด์ฉํ์ฌ ์ดํด๋ณด์.
๐ 1. ES ์ฌ์ฉ์ ์ํ ํ๊ฒฝ ์ธํ
1. dependency ์ถ๊ฐ
dependencies {
implementation 'org.springframework.data:spring-data-elasticsearch:4.2.2'
}
2. config ์ค์
@Configuration
@EnableElasticsearchRepositories(basePackageClasses = {Animal.class})
public class ElasticSearchConfig extends AbstractElasticsearchConfiguration {
@Value("${elasticsearch.host}")
private String host;
@Value("${elasticsearch.port}")
private int port;
@Override
public RestHighLevelClient elasticsearchClient() {
ClientConfiguration clientConfiguration = ClientConfiguration.builder()
.connectedTo(host + ":" + port)
.build();
return RestClients.create(clientConfiguration).rest();
}
}
3. ๊ฐ์ฒด ์์ฑ
@NoArgsConstructor
@Getter
public class Animal {
@Id
private Long id;
private String continent;
private String country;
private String decimalLatitude;
private String decimalLongitude;
private String scientificName;
private String family;
private String genus;
private String higherClassification;
@Builder
private Animal(Long id, String continent, String country,
String decimalLatitude, String decimalLongitude, String scientificName,
String family, String genus, String higherClassification) {
this.id = id;
this.continent = continent;
this.country = country;
this.decimalLatitude = decimalLatitude;
this.decimalLongitude = decimalLongitude;
this.scientificName = scientificName;
this.family = family;
this.genus = genus;
this.higherClassification = higherClassification;
}
}
4. Repository ์์ฑ
JpaRepository์ ์ ์ฌํ ๋ฐฉ๋ฒ์ผ๋ก ElasticRepository๋ฅผ ์ฌ์ฉํ์ฌ Repository๋ฅผ ๋ง๋ค๋ฉด ๋๋ค.
public interface AnimalRepository extends ElasticsearchRepository<Animal, Long>,
CrudRepository<Animal, Long> {
List<Animal> findByHigherClassificationContains(String country);
}
๐ 2. ๋ฐ์ดํฐ ์ฝ์
๊ธฐ์กด ๋ฐ์ดํฐ๋ฒ ์ด์ค์ ์๋ ๊ฐ์ ์ด์ฉํ์ฌ ES์ ๊ฐ์ ๋ฃ์ด์ฃผ์๋ค.
public void addAnimalAtES(List<Animal> animals) {
animalRepository.saveAll(animals);
}
๐ 3. ๋ฐ์ดํฐ ์กฐํ
๋ค์๊ณผ ๊ฐ์ด ํน์ ๊ณ์ธต ๊ฐ์ ์ด์ฉํ์ฌ ์กฐํํ๋ค.
public List<Animal> getAnimalsBy(String higherClass) {
return AnimalRepository.findByHigherClassificationContains(higherClass);
}
๐ 4. ๊ฒฐ๊ณผ
๋ณ๊ฒฝ ์ ๊ฑธ๋ฆฐ ์๊ฐ(MySQL์ ์ฌ์ฉํ๋ ๊ฒฝ์ฐ)๊ณผ ๋ณ๊ฒฝ ํ ๊ฑธ๋ฆฐ ์๊ฐ(ES๋ฅผ ์ฌ์ฉํ๋ ๊ฒฝ์ฐ)์ ๋ค์๊ณผ ๊ฐ๋ค.
๋ณ๊ฒฝ ์ (ms) | ๋ณ๊ฒฝ ํ(ms) | |
ํฌ์ ๋ฅ | 1332.6 | 75.8 |
ํ์ถฉ๋ฅ | 1318.0 | 64.3 |
์กฐ๋ฅ | 1393.8 | 87.2 |
์์๋ฅ | 1353.3 | 87.7 |
๊ฐ๊ฐ๋ฅ | 1325.2 | 89.8 |
๋ณ๊ฒฝ ์ (ms) | ๋ณ๊ฒฝ ํ(ms) | |
์ ์ฒด | 1346.3 | 81.9 |
ES๋ฅผ ํ์ฉํ์ฌ ๊ธฐ์กด ๋๋น(MySQL) ์ฝ 16๋ฐฐ์ ๊ฐ์ ๋ ์ฑ๋ฅ์ ํ์ธํ ์ ์์๋ค. (5๊ฐ์ ์ํฉ์ ๋ํด์ ๊ฐ๊ฐ 20๋ฒ์ฉ, ์ด 100๋ฒ์ ๋น๊ต๋ฅผ ์งํํ ๊ฒฐ๊ณผ์ ๋๋ค.)
ํด๋น ๊ธ์
Yoon Sung๋์ 'Mysql Like ๋์ ๋ฐฉ์',
MySQL ๊ณต์๋ฌธ์ '8.3.9 Comparision of B-Tree and Hash Indexes',
'Turbo-BM algorithm',
yeomyaloo๋์ '์ผํ๋ชฐ ๋ง๋ค๊ธฐ ํ๋ก์ ํธ - ์๋ผ์คํฑ์์น(elasticsearch)์ ์คํ๋ง๋ถํธ ์ฐ๋ํด๋ณด์',
์๋๋์ '[spring boot] ์คํ๋ง๋ถํธ์์ elasticsearch ์์ํ๊ธฐ',
msjeong97๋์ 'Elasticsearch๋',
justdoIT0703๋์ '[Elastic Search] ์๋ผ์คํฑ์์น๋? 2. ์ญ ์์ธ(Inverted Index)',
์ ์ด์จ(Jayon)๋์ '[๋ฐ์ดํฐ๋ฒ ์ด์ค] ์ญ์์ธ์ด๋?',
์ ์ฐธ๊ณ ํ์์ต๋๋ค.
'๐ ํ๊ธฐ > 2023๋ ' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
ํ ์คํธ ์ปค๋ฒ๋ฆฌ์ง 100% ๋ฌ์ฑ ํ๊ธฐ (2) | 2023.12.07 |
---|---|
ํ์ดํ๋ก๊ทธ๋๋ฐ(Pair Programming) ํ๊ธฐ (0) | 2023.05.28 |