repl_set:PRIMARY> db.test_multikey_index.getIndexes()
[
{
"v" : 2,
"key" : {
"_id" : 1
},
"name" : "_id_",
"ns" : "test_db.test_multikey_index"
}
]
repl_set:PRIMARY> db.test_multikey_index.find({topic:'server-3'}).explain()
{
"queryPlanner" : {
"plannerVersion" : 1,
"namespace" : "test_db.test_multikey_index",
"indexFilterSet" : false,
"parsedQuery" : {
"topic" : {
"$eq" : "server-3"
}
},
"queryHash" : "28760B72",
"planCacheKey" : "28760B72",
"winningPlan" : {
"stage" : "COLLSCAN",
"filter" : {
"topic" : {
"$eq" : "server-3"
}
},
"direction" : "forward"
},
"rejectedPlans" : [ ]
},
"serverInfo" : {
"host" : "localhost.localdomain",
"port" : 27017,
"version" : "4.2.8",
"gitVersion" : "43d25964249164d76d5e04dd6cf38f6111e21f5f"
},
"ok" : 1,
"$clusterTime" : {
"clusterTime" : Timestamp(1596622293, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
},
"operationTime" : Timestamp(1596622293, 1)
}
repl_set:PRIMARY> db.test_multikey_index.getIndexes()
[
{
"v" : 2,
"key" : {
"_id" : 1
},
"name" : "_id_",
"ns" : "test_db.test_multikey_index"
},
{
"v" : 2,
"key" : {
"topic" : 1
},
"name" : "topic_1",
"ns" : "test_db.test_multikey_index"
}
]
repl_set:PRIMARY> db.test_multikey_index.find({topic:'server-3'}).explain()
{
"queryPlanner" : {
"plannerVersion" : 1,
"namespace" : "test_db.test_multikey_index",
"indexFilterSet" : false,
"parsedQuery" : {
"topic" : {
"$eq" : "server-3"
}
},
"queryHash" : "28760B72",
"planCacheKey" : "9C0A2855",
"winningPlan" : {
"stage" : "FETCH",
"inputStage" : {
"stage" : "IXSCAN",
"keyPattern" : {
"topic" : 1
},
"indexName" : "topic_1",
"isMultiKey" : true,
"multiKeyPaths" : {
"topic" : [
"topic"
]
},
"isUnique" : false,
"isSparse" : false,
"isPartial" : false,
"indexVersion" : 2,
"direction" : "forward",
"indexBounds" : {
"topic" : [
"[\"server-3\", \"server-3\"]"
]
}
}
},
"rejectedPlans" : [ ]
},
"serverInfo" : {
"host" : "localhost.localdomain",
"port" : 27017,
"version" : "4.2.8",
"gitVersion" : "43d25964249164d76d5e04dd6cf38f6111e21f5f"
},
"ok" : 1,
"$clusterTime" : {
"clusterTime" : Timestamp(1596622053, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
},
"operationTime" : Timestamp(1596622053, 1)
}
package com.biz.mongodb;
import com.mongodb.BasicDBObject;
import com.mongodb.Block;
import com.mongodb.MongoClientSettings;
import com.mongodb.ServerAddress;
import com.mongodb.client.MongoClient;
import com.mongodb.client.MongoClients;
import com.mongodb.client.MongoDatabase;
import com.mongodb.connection.ClusterSettings;
import lombok.extern.slf4j.Slf4j;
import org.bson.Document;
import java.util.*;
/**
* 멀티키 Index 테스트
*
* @author 엄승하
*/
@Slf4j
public class FindMultiKeyIndexSample {
private static final String dbAddr = "192.168.56.1"; //로컬 vm에 mongodb 4.2를 설치함
private static final String dbName = "test_db"; //db명
private static String colNm = "test_multikey_index";
private static String indexField = "topic"; //인덱스 필드
private static final int testDataSize = 100_000; //테스트 데이터 생성 갯수
private static MongoDatabase mongoDB;
public static void main(String[] args) {
List<ServerAddress> serverList = Arrays.asList(new ServerAddress(dbAddr, 27017));
Block<ClusterSettings.Builder> clusterSettings = builder -> builder.hosts(serverList);
MongoClient mongoClient = MongoClients.create(MongoClientSettings.builder().applyToClusterSettings(clusterSettings).build());
mongoDB = mongoClient.getDatabase(dbName);
//컬렉션 추가
mongoDB.createCollection(colNm);
mongoDB.getCollection(colNm).createIndex(new BasicDBObject(indexField, 1)); //인덱스 추가
saveBulkData(colNm, testDataSize); //테스트 데이터 저장
mongoClient.close(); //DB커넥션 반환
System.out.println("\n\n === 프로그램 종료 ===");
}
/**
* bulk로 데이터 저장
*
* @param colNm 저장할 컬렉션명
* @param dataSize 저장할 데이터 갯수
*/
public static void saveBulkData(String colNm, int dataSize) {
Date now = new Date();
List<Document> docs = new ArrayList<>(dataSize);
List<String> case1 = new ArrayList<>();
case1.add("server-1");
case1.add("server-2");
List<String> case2 = new ArrayList<>();
case2.add("server-2");
case2.add("server-3");
for (int i = 1; i <= dataSize; i++) {
Document doc = new Document();
doc.append("i", i);
doc.append("uuid", UUID.randomUUID().toString());
if (i % 2 == 0) { // 2가지의 경우로 데이터 저장
doc.append("topic", case1);
} else {
doc.append("topic", case2);
}
doc.append("doc_make_date", now);
docs.add(doc);
}
mongoDB.getCollection(colNm).insertMany(docs); //N개 insert
//Thread.sleep(10); //부하분산 목적
}
}