how to list ALL table sizes in a project

前端 未结 1 1347
深忆病人
深忆病人 2021-01-22 20:19

Is there a way to list all the table size in BigQuery?

I know a command like this:

select 
  table_id,
  sum(size_bytes)/pow(10,9) as size
from
  certain         


        
1条回答
  •  闹比i
    闹比i (楼主)
    2021-01-22 20:27

    At the moment there's no possible way to do that in a single query, but you can do it with a script, here is my python script that prints out the list:

    from google.cloud import bigquery
    
    client = bigquery.Client()
    
    datasets = list(client.list_datasets())
    project = client.project
    
    if datasets:
        print('Datasets in project {}:'.format(project))
        for dataset in datasets:  # API request(s)
            print('Dataset: {}'.format(dataset.dataset_id))
    
            query_job = client.query("select table_id, sum(size_bytes)/pow(10,9) as size from `"+dataset.dataset_id+"`.__TABLES__ group by 1")
    
            results = query_job.result()
            for row in results:
                print("\tTable: {} : {}".format(row.table_id, row.size))
    
    else:
        print('{} project does not contain any datasets.'.format(project))
    

    0 讨论(0)
提交回复
热议问题