限制视图以在两个日期分区之间进行选择

时间:2017-02-24 10:55:41

标签: google-analytics google-bigquery

我希望为Google Analytics登录页面定义一个视图。我试图通过将以下查询保存为视图来设置它:

SELECT
  date,
  fullVisitorId AS fv,
  visitID AS v,
  h.page.pagePath AS landing_page
FROM
  `project-id.dataset.ga_sessions_*`, UNNEST(hits) AS h
WHERE
  hitNumber = 1

在加入此视图的查询中,我计划将它们限制在两个日期分区之间,如下所示:

SELECT
  sessions.date,
  fullVisitorId AS fv,
  visitId AS v,
  landing_page
FROM `project-id.dataset.ga_sessions_*` AS sessions, UNNEST(hits) AS h
JOIN `project-id.dataset.landing_pages` AS landing_pages
ON landing_pages.fv = sessions.fullVisitorId
AND landing_pages.date = sessions.date
AND landing_pages.v = sessions.visitId
WHERE
    _TABLE_SUFFIX BETWEEN '20170108' AND '20170108'

这似乎仍然可以选择大量的数据~5GB,而不是大约60MB的数据。

如何重新编写视图,以便它只选择消费查询定义的相关日期分区?

1 个答案:

答案 0 :(得分:1)

确保在视图定义中包含_TABLE_SUFFIX,以便您可以在视图中的查询中引用它。以下是将_TABLE_SUFFIX转换为日期的示例:

SELECT
  date,
  fullVisitorId AS fv,
  visitID AS v,
  h.page.pagePath AS landing_page,
  PARSE_DATE('%Y%m%d', _TABLE_SUFFIX) AS sessions_date
FROM
  `project-id.dataset.ga_sessions_*`, UNNEST(hits) AS h
WHERE
  hitNumber = 1;

现在尝试查看视图:

SELECT
  COUNT(DISTINCT fullVisitorId) AS total_visitors
FROM `dataset.view_name`
WHERE sessions_date = '2017-01-08';