Follow me on LinkedIn - AI, GA4, BigQuery

You can generate 10 different versions of the same query, and they can all produce identical results.

Each version is just a different execution strategy around the same logical question: “Given GA4’s event table, return metric X and dimension Y over time, correctly deduped.”

They differ in performance characteristics, scan volume, and readability.


For example:

Consider the following base query:

SELECT
  geo.continent,
  geo.sub_continent,
  geo.country,
  geo.region,
  geo.city,
  COUNT(DISTINCT user_pseudo_id) AS total_users
FROM
  `<Enter your table id here>`
WHERE
  _TABLE_SUFFIX BETWEEN '20251001' AND '20251031'
  AND user_pseudo_id IS NOT NULL
GROUP BY
  geo.continent, geo.sub_continent, geo.country, geo.region, geo.city
ORDER BY
  total_users DESC;

Note: Use your table ID.

Now let's generate 10 different SQL versions of this query using different optimisation strategies. All must produce identical results.

Version 1 - Standard Baseline (Clean and Direct).

SELECT
  geo.continent,
  geo.sub_continent,
  geo.country,
  geo.region,
  geo.city,
  COUNT(DISTINCT user_pseudo_id) AS total_users
FROM
  `dbrt-ga4.analytics_207472454.events_*`
WHERE
  _TABLE_SUFFIX BETWEEN '20251001' AND '20251031'
  AND user_pseudo_id IS NOT NULL
GROUP BY
  geo.continent, geo.sub_continent, geo.country, geo.region, geo.city
ORDER BY
  total_users DESC;

Use: Baseline reference for all versions.

Optimization Level: Medium (simple, readable, but full scan).

Version 2 - APPROX_COUNT_DISTINCT (Speed-Optimized).

SELECT
  geo.continent,
  geo.sub_continent,
  geo.country,
  geo.region,
  geo.city,
  APPROX_COUNT_DISTINCT(user_pseudo_id) AS total_users
FROM
  `dbrt-ga4.analytics_207472454.events_*`
WHERE
  _TABLE_SUFFIX BETWEEN '20251001' AND '20251031'
GROUP BY
  geo.continent, geo.sub_continent, geo.country, geo.region, geo.city
ORDER BY
  total_users DESC;

Use: Fast exploration/dashboards (minor accuracy loss <0.5%).

Optimization Level: High (fastest aggregation).

Version 3 - Pre-Deduplication (CTE with DISTINCT first).