(query, db_name="default")
return sum(count(db_) for db_ in db_name)
Estimate query row count

    Source:
    https://www.citusdata.com/blog/2016/10/12/count-performance/#dup_counts_estimated_filtered

    :param query: A queryset or two-tuple `(<SQL string>, <bind params>)`.
    :param db_name: A database name (str) or sequence of database names
    to query. The sum of counts from all databases will be returned.
Estimate query row count
Estimate query row count
def estimate_row_count(query, db_name="default"):
    """Estimate query row count

    Source:
    https://www.citusdata.com/blog/2016/10/12/count-performance/#dup_counts_estimated_filtered

    :param query: A queryset or two-tuple `(<SQL string>, <bind params>)`.
    :param db_name: A database name (str) or sequence of database names
    to query. The sum of counts from all databases will be returned.
    """
    def count(db_name):
        with connections[db_name].cursor() as cursor:
            cursor.execute(sql, params)
            lines = [line for line, in cursor.fetchall()]
            for line in lines:
                match = rows_expr.search(line)
                if match:
                    return int(match.group(1))
            return 0

    rows_expr = re.compile(r" rows=(\d+) ")
    if hasattr(query, "query"):
        sql, params = query.query.sql_with_params()
    else:
        sql, params = query
    sql = f"EXPLAIN {sql}"
    if isinstance(db_name, str):
        return count(db_name)
    return sum(count(db_) for db_ in db_name)
def estimate_row_count ( query , db_name = "default" ) : def count ( db_name ) : with connections [ db_name ] . cursor ( ) as cursor : cursor . execute ( sql , params ) lines = [ line for line , in cursor . fetchall ( ) ] for line in lines : match = rows_expr . search ( line ) if match : return int ( match . group ( 1 ) ) return 0 rows_expr = re . compile ( r" rows=(\d+) " ) if hasattr ( query , "query" ) : sql , params = query . query . sql_with_params ( ) else : sql , params = query sql = f"EXPLAIN {sql}" if isinstance ( db_name , str ) : return count ( db_name ) return sum ( count ( db_ ) for db_ in db_name )