分区当SQL Query包含子查询时,Google BigQuery中的工作不正常

问题描述 投票:1回答:1

我在Big-query中有以下表结构

**query_all_partition**
property_unique_date    DATE    REQUIRED    
page_url    STRING  REQUIRED    
click   INTEGER REQUIRED    
impression  INTEGER REQUIRED    
position    FLOAT   REQUIRED    

在这里,我已经在property_unique_date上指定了分区

**property_data**

fetch_date  DATE    REQUIRED    
property_url    STRING  REQUIRED    
property_unique_date    DATE    REQUIRED

Let me give you a brief background

我想捕获不同网站的Google搜索分析数据(例如点击次数,针对特定网站发生的展示次数或根据某些关键字重定向到这些网站等)

之前我有一个包含以下字段的表,并且分区在fetch_date上

  • fetch_date
  • property_url
  • PAGE_URL
  • 点击
  • 印象
  • 位置

因此,当我在两个日期之间基于fetch_dates i..e进行查询时,查询处理仅发生在所需数据上,这是为了降低成本。但是,如果我们只存储一个网站或属性网址的数据,这种方法很好。当我开始存储不同属性的数据时,甚至查询其中一个属性和特定的获取日期范围,它正在处理指定日期范围内所有属性的数据,导致大量数据处理和成本核算,因为无法对日期/时间戳以外的字段。

所以,我想出了我创建两个表的方法

  • query_all_partition
  • property_data

所以,我开始存储property_url和fetch_date组合的日期。就像我将1971-01-01到1980-12-31的范围用于存储说属性P1的数据一样。因此,假设我将从2018年1月起为P1的每个数据存储数据,它将是

fetch_date  property_url   property_unique_date
2018-01-01   P1               1971-01-01
2018-01-02   P1               1971-01-02
2018-01-01   P2               1981-01-01
2018-01-02   P2               1981-01-02

通过这种方法,我可以为每个属性存储至少10年的数据。在query_partition_all下,我开始存储property_unique_date而不是fetch_date和property_url

现在,为了测试,我已经为两个属性存储了1个月的数据。 P1是非常大的属性,P2是非常小的属性。存储2018年7月的数据,其中属性为1971-01-01至1971-01-31分配给P1的属性唯一日期为7月为31天,P2为1981-01-01至1981-01-31。

运行以下查询并附加相同的快照

两个属性 - P1(大型物业)(1971-01-01至1971-01-31) - P2(小型物业)(1981-01-01至1981-01-31)

我运行了以下查询

select page_url, sum(click) as click,sum(impression) as impression from `searchanalytics.query_all_partition` where property_unique_date BETWEEN ('1971-01-01') and ('1971-01-31') group by page_url

Image with property_unique_dates hardcoded for property P1. Please see the data being processed.

select page_url, sum(click) as click,sum(impression) as impression from `searchanalytics.query_all_partition` where property_unique_date BETWEEN ('1981-01-01') and ('1981-01-31') group by page_url

Image with property_unique_dates hardcoded for property P2. Please see the data being processed.Its small so till here everything is fine

问题出现了,而不是硬编码属性唯一日期,我开始从子查询中获取它(从propertydata表查询)。请参阅作为第3和第4张图像的一部分处理的查询和数据。以下是查询。处理的数据是完整的数据

select page_url, sum(click) as click,sum(impression) as impression from `searchanalytics.query_all_partition` where property_unique_date BETWEEN (select property_unique_date from `searchanalytics.property_data` where fetch_date='2018-01-01' and property_url='P1') and (select property_unique_date from `searchanalytics.property_data` where fetch_date='2018-01-31' and property_url='P1') group by page_url

select page_url, sum(click) as click,sum(impression) as impression from `searchanalytics.query_all_partition` where property_unique_date BETWEEN (select property_unique_date from `searchanalytics.property_data` where fetch_date='2018-01-01' and property_url='P2') and (select property_unique_date from `searchanalytics.property_data` where fetch_date='2018-01-31' and property_url='P2') group by page_url

Data for property P1 with property unique dates not hardcoded

Data for property P2 with property unique dates not hardcoded

在第3和第4中,它处理表的完整数据而不是子集。为什么会如此。谁能解释一下,如何解决这个问题?

非常感谢您的详细回复。

sql google-bigquery partitioning
1个回答
0
投票

来自the documentation

需要评估查询的多个阶段以便解析谓词(例如内部查询或子查询)的复杂查询将不会从查询中删除分区。

所以不,你不能使用基于子查询的过滤器,并期望它只将扫描限制到匹配的分区。

© www.soinside.com 2019 - 2024. All rights reserved.