David542
David542

Reputation: 110083

BigQuery limit to X rows for a query

I have an application and I want to add a restriction if someone hasn't paid for an account, where they can sample data queries, but cannot view all data. Let's say there is an index with 100M rows and they want to do the following query:

SELECT product, SUM(revenue)
FROM bq_table
GROUP BY product

How would I limit this to the first X rows without writing a query that costs just as much as the normal query by doing a full table scan. For example, something like the following:

# x = 10000

SELECT product, SUM(revenue) FROM (
    SELECT * FROM bq_table LIMIT 10000
) bq_table_limited GROUP BY product

Note that I do not know all the columns beforehand so it's not that easy to write a where clause in the subselect.

Upvotes: 0

Views: 9048

Answers (1)

Felipe Hoffa
Felipe Hoffa

Reputation: 59175

The best you can do here: Create a separate table/dataset for those users.

Materialize your inner query (SELECT * FROM bq_table LIMIT 10000) and let people work with that sample.

(it would be interesting to know more of the context and how you are planning to give users access to BigQuery)

Upvotes: 3

Related Questions