解决方案:使用分页方式分批请求数据,降低响应时间。代码示例(Python):
def get_data_with_paging(table_id, start_index, page_size):
    client = bigquery.Client()
    table_ref = client.dataset(dataset_id).table(table_id)
    table = client.get_table(table_ref)
    schema = table.schema
    # Define paging parameters
    total_rows = table.num_rows
    total_pages = (total_rows // page_size) + 1
    current_page = start_index // page_size + 1
    start_index = (current_page - 1) * page_size
    end_index = start_index + page_size
    # Query data using paging parameters
    query = """
        SELECT *
        FROM `{table}`
        LIMIT {start}, {end}
    """.format(table=table.full_table_id, start=start_index, end=end_index)
    results = []
    while current_page <= total_pages:
        query_job = client.query(query)
        rows = query_job.result()
        # Append rows to results list
        for row in rows:
            results.append(row)
        # Update paging parameters for the next iteration
        start_index += page_size
        end_index = start_index + page_size
        current_page += 1
    return results
下一篇:BigQueryAPI延迟问题