[Solved] BigQuery UNNEST No Results
Looking to automatically optimize YOUR SQL query? Start for free.

EverSQL Database Performance Knowledge Base

BigQuery UNNEST No Results

I'm relatively new to the concept of nested data and trying to get my head around the right way to flatten some GA data in BigQuery (https://support.google.com/analytics/answer/3437719?hl=en).

Now to give some context, for each visitor session I'm trying to capture the list of product SKUs that were looked at (detailed view) and if there was a transaction, the transaction id. By my reckoning, and after doing a bit of research, the simplest way to do this looks like this, using LEFT JOINS to bring back everything:

SELECT fullVisitorId as uId, visitId as vId, h.transaction.transactionId as 
trId, STRING_AGG(p.productSKU, "|") as skus
FROM
`test-bigquery.12345678.ga_sessions_*` t
  LEFT JOIN UNNEST(hits) h
  LEFT JOIN UNNEST(h.product) p
WHERE 
_TABLE_SUFFIX = '20170709'
AND h.eCommerceAction.action_type = '2'
GROUP BY uId, vId, trId

However, this appears to return zero results where trId is not null....

I then had a go at separating the above into two queries and joining. This seems to work and returns a seemingly sensible number of rows (~1000) where trId is not null.

WITH skus AS
(SELECT fullVisitorId as uId, visitId as vId, STRING_AGG(p.productSKU, "|") as skus
    FROM
    `test-bigquery.12345678.ga_sessions_*` t
      LEFT JOIN UNNEST(hits) h
      LEFT JOIN UNNEST(h.product) p
    WHERE 
    _TABLE_SUFFIX = '20170709'
    AND h.eCommerceAction.action_type = '2'
    GROUP BY uId, vId),
transactions AS
  (SELECT fullVisitorId as uId_trans, visitId as vId_trans,  h.transaction.transactionId as trId
    FROM
    `test-bigquery.12345678.ga_sessions_*` t
      LEFT JOIN UNNEST(hits) h
    WHERE 
      _TABLE_SUFFIX = '20170709'
      AND h.transaction.transactionId IS NOT NULL
      GROUP BY uId_trans, vId_trans, trId)
SELECT skus.uId, skus.vId, transactions.trId, skus.skus
FROM skus
LEFT JOIN transactions ON transactions.vId_trans = skus.vId AND transactions.uId_trans = skus.uId

It would be awesome if someone could explain why the two don't give the same answer and hopefully equip me to get involved with all manner of nested fun in the future.... Thanks!

How to optimize this SQL query?

The following recommendations will help you in your SQL tuning process.
You'll find 3 sections below:

  1. Description of the steps you can take to speed up the query.
  2. The optimal indexes for this query, which you can copy and create in your database.
  3. An automatically re-written query you can copy and execute in your database.
The optimization process and recommendations:
  1. Explicitly ORDER BY After GROUP BY (modified query below): By default, the database sorts all 'GROUP BY col1, col2, ...' queries as if you specified 'ORDER BY col1, col2, ...' in the query as well. If a query includes a GROUP BY clause but you want to avoid the overhead of sorting the result, you can suppress sorting by specifying 'ORDER BY NULL'.
  2. Use Numeric Column Types For Numeric Values (query line: 14): Referencing a numeric value (e.g. 20170709) as a string in a WHERE clause might result in poor performance. Possible impacts of storing numbers as varchars: more space will be used, you won't be able to perform arithmetic operations, the data won't be self-validated, aggregation functions like SUM won't work, the output may sort incorrectly and more. If the column is numeric, remove the quotes from the constant value, to make sure a numeric comparison is done.
  3. Use Numeric Column Types For Numeric Values (query line: 15): Referencing a numeric value (e.g. 2) as a string in a WHERE clause might result in poor performance. Possible impacts of storing numbers as varchars: more space will be used, you won't be able to perform arithmetic operations, the data won't be self-validated, aggregation functions like SUM won't work, the output may sort incorrectly and more. If the column is numeric, remove the quotes from the constant value, to make sure a numeric comparison is done.
The optimized query:
SELECT
        fullVisitorId AS uId,
        visitId AS vId,
        h.transaction.transactionId AS trId,
        STRING_AGG(p.productSKU,
        "|") AS skus 
    FROM
        `test-bigquery.12345678.ga_sessions_*` t 
    LEFT JOIN
        UNNEST(hits) h 
    LEFT JOIN
        UNNEST(h.product) p 
    WHERE
        _TABLE_SUFFIX = '20170709' 
        AND h.eCommerceAction.action_type = '2' 
    GROUP BY
        uId,
        vId,
        trId 
    ORDER BY
        NULL

Related Articles



* original question posted on StackOverflow here.