BigQuery需要太长时间才能从Android加载数据

时间:2019-06-21 10:17:52

标签: android google-bigquery firebase-analytics

BigQuery查询调用花费太长时间来加载数据。在BigQuery Google Cloud Platform上花费大约7到8秒即可得到结果,而相同结果则需要1秒。

我尝试过与Google Cloud BigQuery库的文档相同。 https://cloud.google.com/bigquery/docs/quickstarts/quickstart-client-libraries

InputStream is = 
mContext.getAssets().open("service_account.json");
BigQuery bigquery = BigQueryOptions.newBuilder()
.setProjectId("uniorder-prod")
.setCredentials(ServiceAccountCredentials.fromStream(is))
.build().getService();

QueryJobConfiguration queryConfig = 
QueryJobConfiguration.newBuilder("standard sql query")
.setUseLegacySql(false)
.build();

JobId jobId = JobId.of(UUID.randomUUID().toString());
Job queryJob = bigquery
                    .create(JobInfo
                            .newBuilder(queryConfig)
                            .setJobId(jobId).build());
queryJob = queryJob.waitFor();

if (queryJob == null) {
throw new RuntimeException("Job no longer exists");
} else if (queryJob.getStatus().getError() != null) {
throw new 
RuntimeException(queryJob.getStatus().getError().toString());
}

QueryResponse response = bigquery.getQueryResults(jobId);
TableResult result = queryJob.getQueryResults();

//Current query execution time is 7-8 second
//Expected query execution time is 1 or less than 1 second

//My SQL BigQuery
SELECT
  EXTRACT(DATE
  FROM
    TIMESTAMP(param2.value.string_value)) AS date,
  SUM(param3.value.double_value) AS total_price
FROM
  `uniorder-prod.analytics_200255431.events_*`,
  UNNEST(event_params) AS param1,
  UNNEST(event_params) AS param2,
  UNNEST(event_params) AS param3
WHERE
  event_name = "total_consumption_res"
  AND param1.key = "user_id"
  AND param1.value.int_value = 118
  AND param2.key = "timestamp"
  AND param3.key = "total_price"
  AND _TABLE_SUFFIX BETWEEN '20190601'
  AND '20190630'
GROUP BY
  date
ORDER BY
  date ASC

1 个答案:

答案 0 :(得分:0)

在BigQuery上运行查询

queryJob = queryJob.waitFor();

与通过网络将查询结果拉回到应用程序中不同

QueryResponse response = bigquery.getQueryResults(jobId);

您首先要运行查询,然后获取结果。

您可以通过删除上述几行来减少查询时间。

您的网络/互联网速度与BigQuery中的查询性能无关。 BigQuery是一个多租户架构,您可以与其他用户共享计算资源。如果您想要低延迟响应,则使用了错误的工具。我会考虑使用CloudSQL或Datastore之类的东西。