BigQuery SQL Optimization 1: Filter as Early as Possible

Martin Weitzmann
Towards Data Science
6 min readSep 7, 2022

--

Sometimes I see queries that prepare a column in an early stage only to use that column for filtering in a later stage. Or something gets filtered with HAVING that could have been filtered with WHERE earlier. This slows down queries and increases processing costs. Let’s have a look at what we can do about this!

Here is an exemplary scenario where the same output is created — but one time the query is optimized by filtering earlier:

--

--