def METHOD_NAME(domain, doc_type, startdate, enddate):
    hundred_thousand = 100000
    case_count = get_es_case_counts(domain, doc_type, startdate, enddate)
    if case_count < hundred_thousand:
        # small enough domain, so lookup diffs in one go
        return _get_diffs(
            get_primary_db_case_ids(domain, doc_type, startdate, enddate),
            get_es_case_ids(domain, doc_type, startdate, enddate)
        )
    # large domain, so break up by month
    startdate, enddate = get_es_case_range(domain)
    primary_count, es_count, primary_ids, es_ids = 0, 0, set(), set()
    while startdate <= enddate:
        batch_enddate = (startdate + relativedelta(months=1))
        pc1, esc1, p1, es1 = _get_diffs(
            get_primary_db_case_ids(domain, doc_type, startdate, batch_enddate),
            get_es_case_ids(domain, doc_type, startdate, batch_enddate)
        )
        primary_count = primary_count + pc1
        es_count = es_count + esc1
        primary_ids = primary_ids.union(p1)
        es_ids = es_ids.union(es1)
        startdate = batch_enddate
    return primary_count, es_count, primary_ids, es_ids