daangn-daangn / daangn-server

🥕당근 서버 리포지토리🥕
4 stars 2 forks source link

mongodb index 적용 #113

Closed cotchan closed 2 years ago

cotchan commented 2 years ago

작업 내용

목차

  1. Participant
    • Participant에 chat_room_id, user_id에 대한 복합인덱스 적용
    • Participant에 user_id에 대한 단일인덱스 적용
    • findByChatRoomIdAndUserId(String chatRoomId, Long userId) 쿼리 검증
    • findAllByUserIdAndOutIsFalseOrderByUpdatedAtDesc(Long userId, Pageable pageable) 쿼리 검증
  2. ChatRoom
    • ChatRoom에 product_id, identifier에 대한 복합인덱스 적용
    • findByProductIdAndIdentifier(Long productId, String identifier) 쿼리 검증
    • countAllByProductId(Long productId) 쿼리 검증
    • findAllByChatRoomIds(List chatRoomIds, Sort sort) 쿼리 검증

Participant

Participant에 chat_room_id, user_id에 대한 복합인덱스 적용

db.participants.createIndex({chat_room_id:1, user_id:1});
Optional<Participant> findByChatRoomIdAndUserId(String chatRoomId, Long userId);
boolean existsByChatRoomIdAndUserId(String chatRoomId, Long userId);

Participant에 user_id에 대한 단일인덱스 적용

db.participants.createIndex({user_id:1});
List<Participant> findAllByUserIdAndOutIsFalseOrderByUpdatedAtDesc(Long userId, Pageable pageable);
> db.participants.getIndexes()
[
    {
        "v" : 2,
        "key" : {
            "_id" : 1
        },
        "name" : "_id_"
    },
    {
        "v" : 2,
        "key" : {
            "chat_room_id" : 1,
            "user_id" : 1
        },
        "name" : "chat_room_id_1_user_id_1"
    },
    {
        "v" : 2,
        "key" : {
            "user_id" : 1
        },
        "name" : "user_id_1"
    }
]

findByChatRoomIdAndUserId(String chatRoomId, Long userId) 쿼리 검증

db.participants.explain().find({ $and: [{"chat_room_id": "12345"}, {"user_id": 1} ] });

결과

"executionStats" : {
    "executionSuccess" : true,
    "nReturned" : 0,
    "executionTimeMillis" : 0,
    "totalKeysExamined" : 0,
    "totalDocsExamined" : 0,
    "executionStages" : {
        "stage" : "FETCH",
        "nReturned" : 0,
        "docsExamined" : 0,
        "inputStage" : {
            "stage" : "IXSCAN",
            "nReturned" : 0,
            "indexName" : "chat_room_id_1_user_id_1",

findAllByUserIdAndOutIsFalseOrderByUpdatedAtDesc(Long userId, Pageable pageable) 쿼리 검증

db.participants.explain("executionStats").find({ $and: [{"user_id": 1},{"out": false} ]}).sort({"updated_at":-1});

결과

"executionStats" : {
    "executionSuccess" : true,
    "nReturned" : 1,
    "executionTimeMillis" : 0,
    "totalKeysExamined" : 1,
    "totalDocsExamined" : 1,
    "executionStages" : {
        "stage" : "SORT",
        "nReturned" : 1,
        "sortPattern" : {
            "updated_at" : -1
        },
        "totalDataSizeSorted" : 249,
        "inputStage" : {
            "stage" : "FETCH",
            "filter" : {
                "out" : {
                    "$eq" : false
                }
            },
            "nReturned" : 1,
            "executionTimeMillisEstimate" : 0,
            "inputStage" : {
                "stage" : "IXSCAN",
                "nReturned" : 1,
                "keyPattern" : {
                    "user_id" : 1
                },
                "indexName" : "user_id_1",

ChatRoom

ChatRoom에 product_id, identifier에 대한 복합인덱스 적용

db.chat_rooms.createIndex({product_id:1, identifier:1});
Optional<ChatRoom> findByProductIdAndIdentifier(@Param("productId") Long productId,
                        @Param("identifier") String identifier);

Long countAllByProductId(Long productId);

boolean existsByProductIdAndIdentifier(Long productId, String identifier);                      
> db.chat_rooms.getIndexes()
[
    {
        "v" : 2,
        "key" : {
            "_id" : 1
        },
        "name" : "_id_"
    },
    {
        "v" : 2,
        "key" : {
            "product_id" : 1,
            "identifier" : 1
        },
        "name" : "product_id_1_identifier_1"
    }
]

findByProductIdAndIdentifier(Long productId, String identifier) 쿼리 검증

db.chat_rooms.explain("executionStats").find({ $and: [{"product_id": 12345}, {"identifier": "12345"} ] });

결과

    "executionStats" : {
        "executionSuccess" : true,
        "nReturned" : 0,
        "executionTimeMillis" : 0,
        "totalKeysExamined" : 0,
        "totalDocsExamined" : 0,
        "executionStages" : {
            "stage" : "FETCH",
            "nReturned" : 0,
            "inputStage" : {
                "stage" : "IXSCAN",
                "nReturned" : 0,
                "indexName" : "product_id_1_identifier_1",

countAllByProductId(Long productId) 쿼리 검증

db.chat_rooms.explain("executionStats").find({"product_id":100}).count();

결과

"executionStats" : {
    "executionSuccess" : true,
    "nReturned" : 0,
    "executionTimeMillis" : 0,
    "totalKeysExamined" : 3,
    "totalDocsExamined" : 0,
    "executionStages" : {
        "stage" : "COUNT",
        "nReturned" : 0,
        "inputStage" : {
            "stage" : "COUNT_SCAN",
            "nReturned" : 2,
            "indexName" : "product_id_1_identifier_1",

findAllByChatRoomIds(List chatRoomIds, Sort sort) 쿼리 검증

db.chat_rooms.explain("executionStats").find({ _id: { $in: ['1234','234','3241']} }).sort({"updated_at":-1});

결과

"executionStats" : {
    "executionSuccess" : true,
    "nReturned" : 0,
    "executionTimeMillis" : 0,
    "totalKeysExamined" : 1,
    "totalDocsExamined" : 0,
    "executionStages" : {
        "stage" : "SORT",
        "nReturned" : 0,
        "sortPattern" : {
            "updated_at" : -1
        },
        "inputStage" : {
            "stage" : "FETCH",
            "nReturned" : 0,
            "inputStage" : {
                "stage" : "IXSCAN",
                "nReturned" : 0,
                "keyPattern" : {
                    "_id" : 1
                },
                "indexName" : "_id_",