Optimizing Bulk Deletion of Nested Documents in MongoDB

I have two MongoDB documents:

Customer Document:

@Document(collection = "customers")
@Setter
@Getter
public class Customer {
    @Id
    private String id;

    @Field("purchaseIds ")
    @Indexed
    private List<String> purchaseIds = new ArrayList<>();

    //other fields
}

Purchase Document:

@Document(collection = "purchases")
@Setter
@Getter
public class Purchase {
    @Id
    private String id;


    //other fields
}

Each customer can have many purchases. In the database, there are more than a hundred million records for both customers and their purchases. I need to delete a set of purchases based on certain criteria, and this process can be very performance-intensive.

I am using a batch approach to delete purchases. Here is my current implementation:

private static Criteria getCustomersPurchaseDeleteCriteria() {
    return Criteria.where("isProcessed").is(true);
}

private void removeNonProcessedElevenTwelveLogs(Criteria logDeleteCriteria) {
    int batchSize = chunkSize; // Define the batch size for pagination
    ObjectId lastId = null;
    boolean hasMoreResults = true;

    // Bulk operations to remove all matching purchases
    BulkOperations bulkOperations = mongoTemplate.bulkOps(BulkOperations.BulkMode.UNORDERED, Purchase.class);

    while (hasMoreResults) {
        // Query to first find matching Customers whose purchases are about to be deleted
        Query query = new Query(getCustomersPurchaseDeleteCriteria())
                .limit(batchSize)
                .with(Sort.by(Sort.Direction.ASC, "_id"));

        if (Objects.nonNull(lastId)) {
            query.addCriteria(Criteria.where("_id").gt(lastId));
        }

        // Apply projection to only fetch the purchaseIds field
        query.fields().include("purchaseIds");

        double start = System.currentTimeMillis();

        List<String> deletingPurchaseIds = new ArrayList<>();

        // Fetch documents with specified criteria
        List<Customer> matchingCustomers = mongoTemplate.find(query, Customer.class);

        for (Customer customer : matchingCustomers) {
            deletingPurchaseIds.addAll(customer.getPurchaseIds());
            // Keep track of the last processed ID
            lastId = new ObjectId(customer.getId());
        }

        log.info("time for finding and extracting ids from matching customers: {} sec", (System.currentTimeMillis() - start) / 1000);

        if (deletingPurchaseIds.isEmpty()) {
            hasMoreResults = false;
            continue;
        }

        // Criteria for Purchases related to the matching Customers
        Criteria deleteCriteria = Criteria.where("_id").in(deletingPurchaseIds);

        // Create a query with the deleteCriteria
        Query purchaseDeleteQuery = new Query(deleteCriteria);

        start = System.currentTimeMillis();

        // Bulk operations to remove all matching Purchases
        bulkOperations.remove(purchaseDeleteQuery);
        BulkWriteResult execute = bulkOperations.execute();

        log.info("time for deleting batch of purchases: {} sec and deleted size: {}", (System.currentTimeMillis() - start) / 1000, execute.getDeletedCount());

        // Check if there are more results to fetch
        hasMoreResults = deletingPurchaseIds.size() == batchSize;
    }

    log.info("finished purchases removal for matched customers.");
}

Problem: Even with a batch size of 1000, it takes about 3 hours to delete a test case of 5 million customers and 5 million purchases. I need to optimize this process.

Questions:

  1. Is there a more efficient way to perform bulk deletions in MongoDB?

  2. Are there any improvements I can make to my current approach to speed up the deletion process?

Any help or guidance on this would be greatly appreciated. Thanks in advance!

What I Tried: I implemented a batch deletion approach where I retrieve matching customers in chunks, extract their purchaseIds, and then perform bulk deletions on the purchases. Specifically, I use a batch size of 1000 and track the last processed ID to paginate through the results.

What I Expected: I expected this method to efficiently delete millions of records within a reasonable time frame, significantly improving performance compared to a non-batch approach.

What Actually Happened: Despite using batching and bulk operations, the deletion process takes approximately 3 hours for a test case involving 5 million customers and 5 million purchases. The performance is not meeting the application’s requirements, and the process is too slow for production use.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật