def _query_limited(table, query, limit=None):
    """ Helper to limit query results without the full table scan executed by LIMIT """

    with table.bind.connect() as conn:
        if limit is None:
            return list(conn.execute(query).fetchall())

        cursor = conn.execute(query)
        results = []

        try:
            for _ in range(limit):
                results.append(next(cursor))
        except StopIteration:
            pass

        return results