title: BIGQUERY / my queries are very slow and expensive
body: <p>I have a database in Firestore, send the information to Bigquery with a extension 'Stream Collections to BigQuery'.</p>
  <p>But I have the tables with all the data in the same column called 'data'. Also, the data shows all the records/logs, and I just want to see the latest status.</p>
  <p>To be able to see the data separated by columns and see the last value, create this query and them save as view.</p>
  <pre><code>SELECT
    document_name,
    document_id,
    timestamp,
    event_id,
    operation,
    created_at, 
    status_old, 
    status_new
   
  
  FROM
    (
      SELECT
        document_name,
        document_id,
        FIRST_VALUE(timestamp) OVER(
          PARTITION BY document_name
          ORDER BY
            timestamp DESC
        ) AS timestamp,
        FIRST_VALUE(event_id) OVER(
          PARTITION BY document_name
          ORDER BY
            timestamp DESC
        ) AS event_id,
        FIRST_VALUE(operation) OVER(
          PARTITION BY document_name
          ORDER BY
            timestamp DESC
        ) AS operation,
     FIRST_VALUE(`proyect_name.DatasetID.firestoreTimestamp`(JSON_EXTRACT(data, '$.timestamp'))) OVER(
          PARTITION BY document_name
          ORDER BY
            timestamp DESC
        ) AS created_at,
           FIRST_VALUE(JSON_EXTRACT_SCALAR(data, '$.metadata.old')) OVER(
          PARTITION BY document_name
          ORDER BY
            timestamp DESC
        ) AS status_old,
                FIRST_VALUE(JSON_EXTRACT_SCALAR(data, '$.metadata.new')) OVER(
          PARTITION BY document_name
          ORDER BY
            timestamp DESC
        ) AS status_new
  
      FROM
        table1
      ORDER BY
        document_name,
        timestamp DESC
    )
  GROUP BY
    document_name,
    document_id,
    timestamp,
    event_id,
    operation,
    created_at, 
    status_old, 
    status_new
  
  </code></pre>
  <p>(I have several fields to add in addition to these, but it is just an example).</p>
  <p>For all my querys, I use to use this 'view'.</p>
  <p>The problem I have is that this query /view, takes a long time and cost:</p>
  <ul>
  <li>Bytes processed: 555.78 MB</li>
  <li>Bytes billed: 556 MB</li>
  <li>Elapsed time: 43 sec</li>
  <li>Slot time consumed: 4 min 29 sec</li>
  <li>Bytes shuffled: 836.62 MB</li>
  </ul>
  <p>Is there a way to make this more optimal?</p>