π 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 |