Skip to content

Instantly share code, notes, and snippets.

@mr-pascal
Last active March 21, 2021 08:34
Show Gist options
  • Save mr-pascal/15375bea5b2a5568ff48c98f24dfa0f6 to your computer and use it in GitHub Desktop.
Save mr-pascal/15375bea5b2a5568ff48c98f24dfa0f6 to your computer and use it in GitHub Desktop.
/**
* Run query on BigQuery and print query statistics
* @param {string} query The query to run
* @param {string} location The location of the dataset
* @returns {Promise<void>}
*/
const runQueryWithStatistics = async (query, location) => {
const options = {
query,
// Location must match that of the dataset(s) referenced in the query.
location,
// Normally this should be kept to 'true' but we set it to false
// to be able to measure everything correctly
useQueryCache: false,
};
// Run the query as a job
let [job] = await bigquery.createQueryJob(options);
// Wait for the query to finish
await job.getQueryResults();
// Getting fresh job data and print them
[job] = await job.get();
printJobStatistics(job);
}
/**
* Takes a BigQuery job and print certain statistical values to the console
* @param {Job} job The BigQuery job to analyze
*/
const printJobStatistics = (job) => {
const statistics = job.metadata.statistics;
const timeTaken = statistics.endTime - statistics.startTime;
const totalBytesProcessed = parseInt(statistics.totalBytesProcessed);
const totalBytesBilled = parseInt(statistics.query.totalBytesBilled);
const totalPartitionsProcessed = parseInt(statistics.query.totalPartitionsProcessed);
const rowsRead = parseInt(statistics.query.queryPlan[0].recordsRead);
console.log(`--- Job Statistics ---`);
console.table([
{ Description: 'Cache hit', Value: statistics.query.cacheHit, Unit: 'Bool' },
{ Description: 'Time taken', Value: timeTaken, Unit: 'ms' },
{ Description: 'Partitions processed', Value: totalPartitionsProcessed, Unit: 'Count' },
{ Description: 'Rows read', Value: rowsRead, Unit: 'Count' },
{ Description: 'Bytes processed', Value: totalBytesProcessed, Unit: 'Bytes' },
{ Description: 'Bytes billed', Value: totalBytesBilled, Unit: 'Bytes' },
]);
console.log(`-------------------------------\n`);
}
const main = async () => {
// TODO: Developer, make sure to add your GCP project ID here!
const projectId = 'my_project';
// TODO: Developer, make sure to add your dataset ID here!
const datasetId = 'my_dataset';
const location = 'US';
const createQuery = (tableId) => `
SELECT *
FROM \`${projectId}.${datasetId}.${tableId}\`
WHERE date = Date("2021-01-03")
LIMIT 10
`;
console.log(`--- Without Partition ---`);
await runQueryWithStatistics(
createQuery('non_partitioned_table'),
location
);
console.log(`--- With Partition ---`);
await runQueryWithStatistics(
createQuery('partitioned_table'),
location
);
}
main();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment