Google bigquery 如何优化由于自定义维度而大得惊人的BigQuery查询?

Google bigquery 如何优化由于自定义维度而大得惊人的BigQuery查询?,google-bigquery,Google Bigquery,我有一个查询,它精确地提取了我所需要的内容,但是非常大。当我把它拉到Tableau中(正如我需要做的那样),它大部分时间都超时了。有没有办法优化此查询,使其不会太慢/太小 SELECT date, 'iOS' as app_source, (SELECT value from UNNEST(h.customDimensions) WHERE index=4) as user_id, h.eventinfo.eventlabel, COUNT(*) as events FROM

我有一个查询,它精确地提取了我所需要的内容,但是非常大。当我把它拉到Tableau中(正如我需要做的那样),它大部分时间都超时了。有没有办法优化此查询,使其不会太慢/太小

SELECT
  date,
  'iOS' as app_source,
  (SELECT value from UNNEST(h.customDimensions) WHERE index=4) as user_id,
  h.eventinfo.eventlabel,
  COUNT(*) as events
FROM
   `ga_ios_table`,
  UNNEST(hits) h
WHERE
  h.type='EVENT'
  and (
  (h.eventInfo.eventCategory = 'Live' and h.eventInfo.eventAction = 'Chat' and h.eventInfo.eventLabel = 'Team Chat')
  or (h.eventInfo.eventCategory = 'Live' and h.eventInfo.eventAction = 'Photo' and h.eventInfo.eventLabel = 'Team Chat')
  or (h.eventInfo.eventCategory = 'Messages' and h.eventInfo.eventAction = 'Chat' and h.eventInfo.eventLabel = 'Direct Message')
  or (h.eventInfo.eventCategory = 'Messages' and h.eventInfo.eventAction = 'Chat' and h.eventInfo.eventLabel = 'Group Message')
  or (h.eventInfo.eventCategory = 'Messages' and h.eventInfo.eventAction = 'Chat' and h.eventInfo.eventLabel = 'Team Message')
  or (h.eventInfo.eventCategory = 'Messages' and h.eventInfo.eventAction = 'Chat' and h.eventInfo.eventLabel = 'Team Message Includes Photo')
  or (h.eventInfo.eventCategory = 'Messages' and h.eventInfo.eventAction = 'Chat' and h.eventInfo.eventLabel = 'Group Message Includes Photo')
  or (h.eventInfo.eventCategory = 'Messages' and h.eventInfo.eventAction = 'Chat' and h.eventInfo.eventLabel = 'Direct Message Includes Photo')
  )
  and date >= "20180101"
GROUP BY
  1,2,3,4

UNION ALL

SELECT
  date,
  'Android' as app_source,
  (SELECT value from UNNEST(h.customDimensions) WHERE index=4) as user_id,
  h.eventinfo.eventlabel,
  COUNT(*) as events
FROM
   `ga_android_table`,
  UNNEST(hits) h
WHERE
  h.type='EVENT'
  and (
  (h.eventInfo.eventCategory = 'Live' and h.eventInfo.eventAction = 'New Chat' and h.eventInfo.eventLabel = 'Team Chat')
  or (h.eventInfo.eventCategory = 'Live' and h.eventInfo.eventAction = 'New Photo' and h.eventInfo.eventLabel = 'Team Chat')
  or (h.eventInfo.eventCategory = 'Messages' and h.eventInfo.eventAction = 'Chat' and h.eventInfo.eventLabel = 'Direct Message')
  or (h.eventInfo.eventCategory = 'Messages' and h.eventInfo.eventAction = 'Chat' and h.eventInfo.eventLabel = 'Group Message')
  or (h.eventInfo.eventCategory = 'Messages' and h.eventInfo.eventAction = 'Chat' and h.eventInfo.eventLabel = 'Direct Message Includes Photos')
  or (h.eventInfo.eventCategory = 'Messages' and h.eventInfo.eventAction = 'Chat' and h.eventInfo.eventLabel = 'Group Message Includes Photos')
  or (h.eventInfo.eventCategory = 'Messages' and h.eventInfo.eventAction = 'Chat' and h.eventInfo.eventLabel = 'Team Message')
  )
  and date >= "20180101"
GROUP BY
   1,2,3,4

实际上看不到有太多改进的余地-而不是像下面的示例中那样使查询的某些部分不那么冗长

#standardSQL
SELECT
  DATE,
  'iOS' AS app_source,
  (SELECT value FROM UNNEST(h.customDimensions) WHERE index=4) AS user_id,
  h.eventinfo.eventlabel,
  COUNT(*) AS events
FROM
   `ga_ios_table`,
  UNNEST(hits) h
WHERE
  h.type='EVENT' 
  AND (
    (h.eventInfo.eventCategory, h.eventInfo.eventAction, h.eventInfo.eventLabel) IN (
      ('Live', 'Chat', 'Team Chat'),
      ('Live', 'Photo', 'Team Chat'),
      ('Messages', 'Chat', 'Direct Message'),
      ('Messages', 'Chat', 'Group Message'),
      ('Messages', 'Chat', 'Team Message'),
      ('Messages', 'Chat', 'Team Message Includes Photo'),
      ('Messages', 'Chat', 'Group Message Includes Photo'),
      ('Messages', 'Chat', 'Direct Message Includes Photo')
    )
  )
  AND DATE >= "20180101"
GROUP BY
  1,2,3,4

UNION ALL

SELECT
  DATE,
  'Android' AS app_source,
  (SELECT value FROM UNNEST(h.customDimensions) WHERE index=4) AS user_id,
  h.eventinfo.eventlabel,
  COUNT(*) AS events
FROM
   `ga_android_table`,
  UNNEST(hits) h
WHERE
  h.type='EVENT' 
  AND (
    (h.eventInfo.eventCategory, h.eventInfo.eventAction, h.eventInfo.eventLabel) IN (
      ('Live', 'New Chat', 'Team Chat'),
      ('Live', 'New Photo', 'Team Chat'),
      ('Messages', 'Chat', 'Direct Message'),
      ('Messages', 'Chat', 'Group Message'),
      ('Messages', 'Chat', 'Direct Message Includes Photos'),
      ('Messages', 'Chat', 'Group Message Includes Photos'),
      ('Messages', 'Chat', 'Team Message')
    )
  )
  AND DATE >= "20180101"
GROUP BY
   1,2,3,4  

我没有办法测试上面的内容是否能改善您的情况——但有时候,更简洁的语法会有所帮助

如果在BigQuery中发生超时(例如,Tableau等待BigQuery完成作业的时间很长),或者Tableau传输BigQuery提供的数据结果的时间很长(例如,BQ可能在几秒钟内完成作业),则必须对其进行界定。您可以从BQ UI检查BQ花费的时间,并搜索相关的查询/作业。