在BigQuery中,你可以运行一个查询来查询另一个查询的结果。

huangapple go评论73阅读模式
英文:

BigQuery - can I run a query on a query?

问题

背景

我正在开发一个管理系统的 Web 应用程序。

在其中一个页面上,客户端显示了一些聚合值的报告。
客户端报告具有分页、排序和过滤功能。

服务器端使用 Go 编写,数据存储在 BigQuery 中的一个大型数据集中(每个表对应一天)。服务器代码使用库"google.golang.org/api/bigquery/v2"与 BQ 进行通信。

实现

由于主查询需要很长时间,我使用Query API运行查询,然后缓存JobID以供后续调用使用。

query := &bigquery.QueryRequest{
	DefaultDataset: "myDataSet",
	Kind:           "json",
	Query:          queryStr,
	UseQueryCache:  true,
}
qr, err := service.Jobs.Query(project, query).Do()

// 缓存 job id
key := getMD5Hash(queryStr)
item := &memcache.Item{
	Key:        key,
	Value:      []byte(qr.JobReference.JobId),
	Expiration: time.Hour * 24,
}

err := memcache.Set(c.ctx, item)

然后,我使用缓存的 jobID,并使用getQueryResults获取数据的分页。

qrc := service.Jobs.GetQueryResults(project, jobId)
if maxResults > 0 {
	qrc.MaxResults(int64(maxResults))
}
qrc.StartIndex(uint64(startIndex))
qrslice, err := qrc.Do()

问题

我想对数据进行_过滤_和_排序_,但不重复进行底层(繁重的)查询。是否有办法在原始查询创建的临时表上运行另一个查询?

也就是说,如果我的原始表是 A,并在其上运行了一个查询,生成了一个临时表 TEMP_JOB;是否可以在 TEMP_JOB 上执行 SQL 查询?

英文:

Background

I am developing a management system web app.

On one of the pages, the client shows a report of some aggregated values.
The client report has pagination, sorting and filtering.

The server side is written in Go, and the data is stored in a large dataset in BigQuery (each table is for one day). The server code communicates with BQ using the library "google.golang.org/api/bigquery/v2".

Implementation

Since the main query takes a lot of time, I use the Query API to run the query and then cache the JobID for subsequent calls.

query := &bigquery.QueryRequest{
		DefaultDataset: "myDataSet",
		Kind:           "json",
		Query:          queryStr,
		UseQueryCache:  true,
	}
qr, err := service.Jobs.Query(project, query).Do()

// cache the job id
key := getMD5Hash(queryStr)
item := &memcache.Item{
	Key:        key,
	Value:      []byte(qr.JobReference.JobId),
	Expiration: time.Hour * 24,
}

err := memcache.Set(c.ctx, item)

Then I use the cached jobID and then use getQueryResults to get pages of the data.

qrc := service.Jobs.GetQueryResults(project, jobId)
if maxResults > 0 {
	qrc.MaxResults(int64(maxResults))
}
qrc.StartIndex(uint64(startIndex))
qrslice, err := qrc.Do()

Question

I want to filter and sort the data, but without repeating the underlying (heavy) query. Is the an option to run another query on the temporary table that was created by the original query?

Meaning that if my original table is A and I ran a query on it, resulting in a temporary table TEMP_JOB; is it possible to execute an SQL query on TEMP_JOB?

答案1

得分: 2

你可以设置destinationTable属性,甚至可以将其放置在一个单独的数据集中,为整个数据集设置一个默认的过期时间,例如1小时、2天或其他时间。这样,无论在该数据集下创建了哪个表,它都会自动过期。

通过这种方式,你可以控制所创建表的名称,并且它们会自动过期,你不需要编写脚本来删除它们,也不会产生额外的费用。

英文:

You can set the destinationTable property and even place in a separate dataset where you set a default expiration time eg 1 hour, 2days or whatever for the entire dataset. This way whatever table you create under that dataset automatically expires.

This way you control the name of the name of the table you created, and this way it will automatically expire and you don't need to build a script to delete it and won't incure costs.

huangapple
  • 本文由 发表于 2016年10月27日 16:40:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/40279985.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定