title: BigQuery - Query time becomes extremely long
body: <p>Recently all my query takes too long time but basically all of them consume no data.</p>
  
  <p>For example, for a really simple query</p>
  
  <pre><code>Start Time: Jan 14, 2016, 12:35:13 PM
  End Time: Jan 14, 2016, 12:35:15 PM
  Bytes Processed: 0 B
  Bytes Billed: 0 B
  Billing Tier: 1
  Destination Table: ****************.******************
  Write Preference: Append to table
  Allow Large Results: true
  Flatten Results: true
  </code></pre>
  
  <p>this is the information I got from the BQ console, which tells me that this query doesn't consume any data(it's true) and only takes two seconds.</p>
  
  <p>But it actually takes 27 seconds when I run this query again in the console by click <code>Run Query</code> in the query history. And after that, the <code>Query History</code> in the console shows this query takes 2 seconds again.</p>
  
  <p>Basically all the query in this dataset have this issue.</p>
  
  <p>I have over 40000 tables in this dataset. </p>
  
  <p>So my guess is that before the BQ actually run the query, it first locates the table that is gonna be used. Then it starts to execute the query, which here is the <code>start time</code> in the query history.</p>
  
  <p>If that is the case, how should I solve it and why does it take so long?</p>
  
  <p>Here is the query I mentioned(have made some changes):</p>
  
  <pre><code>select "some_id", '2015-12-01', if (count(user_id) == 0, NULL, sum(users_in_today_again) / count(user_id)) as retention
  from
  (
  select
    users_in_last_day.user_id as user_id,
    if(users_in_today.user_id is null, 0, 1) as users_in_today_again
  FROM
  (
  select user_id
  from
    table_date_range(ds.sessions_some_id_, date_add(timestamp('2015-12-01'), -1, "DAY"), date_add(timestamp('2015-12-01'), -1, "DAY"))
  group by user_id
  ) as users_in_last_day
  left join
  (
  select user_id
  from table_date_range(ds.sessions_some_id_, timestamp('2015-12-01'), timestamp('2015-12-01'))
  group by user_id
  ) as users_in_today
  on users_in_last_day.user_id = users_in_today.user_id
  )
  </code></pre>
  
  <p>Thanks in advance!</p>