Meet industry peers, ask questions, collaborate to find answers, and connect with Googlers who are making the products you use every day.<\/p>", "imageupload.max_uploaded_images_per_upload" : 100, "imageupload.max_uploaded_images_per_user" : 10000, "integratedprofile.connect_mode" : "", "tkb.toc_maximum_heading_level" : "2", "tkb.toc_heading_list_style" : "disc", "sharedprofile.show_hovercard_score" : true, "config.search_before_post_scope" : "community", "tkb.toc_heading_indent" : "15", "p13n.cta.recommendations_feed_dismissal_timestamp" : -1, "imageupload.max_file_size" : 10024, "layout.show_batch_checkboxes" : false, "integratedprofile.cta_connect_slim_dismissal_timestamp" : -1 }, "isAnonymous" : true, "policies" : { "image-upload.process-and-remove-exif-metadata" : true }, "registered" : false, "emailRef" : "", "id" : -1, "login" : "Former Community Member" }, "Server" : { "communityPrefix" : "/qsqph94282", "nodeChangeTimeStamp" : 1732279306521, "tapestryPrefix" : "/gc", "deviceMode" : "DESKTOP", "responsiveDeviceMode" : "DESKTOP", "membershipChangeTimeStamp" : "0", "version" : "24.7", "branch" : "24.7-release", "showTextKeys" : false }, "Config" : { "phase" : "prod", "integratedprofile.cta.reprompt.delay" : 30, "profileplus.tracking" : { "profileplus.tracking.enable" : false, "profileplus.tracking.click.enable" : false, "profileplus.tracking.impression.enable" : false }, "app.revision" : "2409051714-s8dac8f1df8-b80", "navigation.manager.community.structure.limit" : "1000" }, "Activity" : { "Results" : [ ] }, "NodeContainer" : { "viewHref" : "https://www.googlecloudcommunity.com/gc/Cloud-Forums/ct-p/cloud-forums", "description" : "Get answers to your questions and share your knowledge about the Google Cloud.", "id" : "cloud-forums", "shortTitle" : "Cloud Forums", "title" : "Cloud Forums", "nodeType" : "category" }, "Page" : { "skins" : [ "googlecloud", "theme_hermes", "responsive_peak" ], "authUrls" : { "loginUrl" : "https://www.googlecloudcommunity.com/gc/user/userloginpage?dest_url=https%3A%2F%2Fwww.googlecloudcommunity.com%2Fgc%2FData-Analytics%2FBigquery-cost-execution%2Fm-p%2F836295", "loginUrlNotRegistered" : "https://www.googlecloudcommunity.com/gc/user/userloginpage?redirectreason=notregistered&dest_url=https%3A%2F%2Fwww.googlecloudcommunity.com%2Fgc%2FData-Analytics%2FBigquery-cost-execution%2Fm-p%2F836295", "loginUrlNotRegisteredDestTpl" : "https://www.googlecloudcommunity.com/gc/user/userloginpage?redirectreason=notregistered&dest_url=%7B%7BdestUrl%7D%7D" }, "name" : "ForumTopicPage", "rtl" : false, "object" : { "viewHref" : "/gc/Data-Analytics/Bigquery-cost-execution/td-p/822690", "subject" : "Bigquery cost execution", "id" : 822690, "page" : "ForumTopicPage", "type" : "Thread" } }, "WebTracking" : { "Activities" : { }, "path" : "Community:Google Cloud Community/Category:Google Cloud/Category:Cloud Forums/Board:Data Analytics/Message:Re: Bigquery cost execution" }, "Feedback" : { "targeted" : { } }, "Seo" : { "markerEscaping" : { "pathElement" : { "prefix" : "@", "match" : "^[0-9][0-9]$" }, "enabled" : false } }, "TopLevelNode" : { "viewHref" : "https://www.googlecloudcommunity.com/gc/Google-Cloud/ct-p/google-cloud", "description" : "Find answers, ask questions, and connect with our community of experts.", "id" : "google-cloud", "shortTitle" : "Google Cloud", "title" : "Google Cloud", "nodeType" : "category" }, "Community" : { "viewHref" : "https://www.googlecloudcommunity.com/", "integratedprofile.lang_code" : "en", "integratedprofile.country_code" : "US", "id" : "qsqph94282", "shortTitle" : "Google Cloud Community", "title" : "Google Cloud Community" }, "CoreNode" : { "conversationStyle" : "forum", "viewHref" : "https://www.googlecloudcommunity.com/gc/Data-Analytics/bd-p/cloud-data-analytics", "settings" : { }, "description" : "Want more from your data? Have questions about products like BigQuery, Dataflow, or Pub/Sub? Join the conversation here.", "id" : "cloud-data-analytics", "shortTitle" : "Data Analytics", "title" : "Data Analytics", "nodeType" : "Board", "ancestors" : [ { "viewHref" : "https://www.googlecloudcommunity.com/gc/Cloud-Forums/ct-p/cloud-forums", "description" : "Get answers to your questions and share your knowledge about the Google Cloud.", "id" : "cloud-forums", "shortTitle" : "Cloud Forums", "title" : "Cloud Forums", "nodeType" : "category" }, { "viewHref" : "https://www.googlecloudcommunity.com/gc/Google-Cloud/ct-p/google-cloud", "description" : "Find answers, ask questions, and connect with our community of experts.", "id" : "google-cloud", "shortTitle" : "Google Cloud", "title" : "Google Cloud", "nodeType" : "category" }, { "viewHref" : "https://www.googlecloudcommunity.com/", "description" : "The official home of Google Cloud and Workspace community forums, learning hub, and community blogs.", "id" : "qsqph94282", "shortTitle" : "Google Cloud Community", "title" : "Google Cloud Community", "nodeType" : "Community" } ] } }; LITHIUM.Components.RENDER_URL = "/gc/util/componentrenderpage/component-id/#{component-id}?render_behavior=raw"; LITHIUM.Components.ORIGINAL_PAGE_NAME = 'forums/v5/ForumTopicPage'; LITHIUM.Components.ORIGINAL_PAGE_ID = 'ForumTopicPage'; LITHIUM.Components.ORIGINAL_PAGE_CONTEXT = 'SkCIuphes0T2pYi5JkHS0T5lmxUcgr74jYgR2dzTxzL7mkUZBq0xnXREoqaZIJM_br8O4U6yRtfJFOSyklPcgI4Uhw7N6JRo4pCRILlL9SmRaJQaA4ZQh4PCOCNQzpb3mswvc6QNAcOICYYXO9TkoLBfFDtxqNtDFqGp2JV753xwFOU2SFBETxG1fTlUfJS359XnKNFOAVu2iINblcV8xu3QyjpoDPqds6ZZE_-aKngtJhfpf26ZSjdQ1og7sii3fesx-I-QEMCGFMnMeKum3xj5mMG4Ks8eVn5kF8aiKNQI0njToUbLWATFKapjFqazm5tGl1m2Ze_azLMS0phOuu35v1uNgqgCjeruoAvLKEXUeU08YBJa5bEr690-ZXlV'; LITHIUM.Css = { "BASE_DEFERRED_IMAGE" : "lia-deferred-image", "BASE_BUTTON" : "lia-button", "BASE_SPOILER_CONTAINER" : "lia-spoiler-container", "BASE_TABS_INACTIVE" : "lia-tabs-inactive", "BASE_TABS_ACTIVE" : "lia-tabs-active", "BASE_AJAX_REMOVE_HIGHLIGHT" : "lia-ajax-remove-highlight", "BASE_FEEDBACK_SCROLL_TO" : "lia-feedback-scroll-to", "BASE_FORM_FIELD_VALIDATING" : "lia-form-field-validating", "BASE_FORM_ERROR_TEXT" : "lia-form-error-text", "BASE_FEEDBACK_INLINE_ALERT" : "lia-panel-feedback-inline-alert", "BASE_BUTTON_OVERLAY" : "lia-button-overlay", "BASE_TABS_STANDARD" : "lia-tabs-standard", "BASE_AJAX_INDETERMINATE_LOADER_BAR" : "lia-ajax-indeterminate-loader-bar", "BASE_AJAX_SUCCESS_HIGHLIGHT" : "lia-ajax-success-highlight", "BASE_CONTENT" : "lia-content", "BASE_JS_HIDDEN" : "lia-js-hidden", "BASE_AJAX_LOADER_CONTENT_OVERLAY" : "lia-ajax-loader-content-overlay", "BASE_FORM_FIELD_SUCCESS" : "lia-form-field-success", "BASE_FORM_WARNING_TEXT" : "lia-form-warning-text", "BASE_FORM_FIELDSET_CONTENT_WRAPPER" : "lia-form-fieldset-content-wrapper", "BASE_AJAX_LOADER_OVERLAY_TYPE" : "lia-ajax-overlay-loader", "BASE_FORM_FIELD_ERROR" : "lia-form-field-error", "BASE_SPOILER_CONTENT" : "lia-spoiler-content", "BASE_FORM_SUBMITTING" : "lia-form-submitting", "BASE_EFFECT_HIGHLIGHT_START" : "lia-effect-highlight-start", "BASE_FORM_FIELD_ERROR_NO_FOCUS" : "lia-form-field-error-no-focus", "BASE_EFFECT_HIGHLIGHT_END" : "lia-effect-highlight-end", "BASE_SPOILER_LINK" : "lia-spoiler-link", "BASE_DISABLED" : "lia-link-disabled", "FACEBOOK_LOGOUT" : "lia-component-users-action-logout", "FACEBOOK_SWITCH_USER" : "lia-component-admin-action-switch-user", "BASE_FORM_FIELD_WARNING" : "lia-form-field-warning", "BASE_AJAX_LOADER_FEEDBACK" : "lia-ajax-loader-feedback", "BASE_AJAX_LOADER_OVERLAY" : "lia-ajax-loader-overlay", "BASE_LAZY_LOAD" : "lia-lazy-load" }; LITHIUM.noConflict = true; LITHIUM.useCheckOnline = false; LITHIUM.RenderedScripts = [ "jquery.lithium-selector-extensions.js", "jquery.position-toggle-1.0.js", "plugin.js", "jquery.tokeninput-1.6.2.js", "plugin.js", "jquery.delayToggle-1.0.js", "AjaxSupport.js", "InlineMessageReplyEditor.js", "jquery.hoverIntent-r6.js", "jquery.autocomplete.js", "jquery.ui.resizable.js", "KeepSessionAlive.js", "plugin.js", "jquery.iframe-transport.js", "Tooltip.js", "PartialRenderProxy.js", "LazyLoadComponent.js", "ElementQueries.js", "en.js", "plugin.js", "Placeholder.js", "jquery.ui.mouse.js", "ForceLithiumJQuery.js", "Video.js", "plugin.js", "plugin.js", "plugin.js", "jquery.tmpl-1.1.1.js", "LiModernizr.js", "UserListActual.js", "DropDownMenuVisibilityHandler.js", "OoyalaPlayer.js", "AjaxFeedback.js", "PolyfillsAll.js", "SearchAutoCompleteToggle.js", "AutoComplete.js", "DeferredImages.js", "en.js", "HelpIcon.js", "jquery.effects.core.js", "plugin.js", "plugin.js", "DropDownMenu.js", "CustomEvent.js", "Auth.js", "ResizeSensor.js", "json2.js", "jquery.function-utils-1.0.js", "plugin.js", "plugin.js", "Events.js", "jquery.appear-1.1.1.js", "aws-sdk.js", "jquery.clone-position-1.0.js", "jquery.ui.position.js", "jquery.tools.tooltip-1.2.6.js", "MessageViewDisplay.js", "en.js", "jquery.json-2.6.0.js", "en.js", "en.js", "SearchForm.js", "Components.js", "jquery.ajax-cache-response-1.0.js", "EarlyEventCapture.js", "jquery.ui.draggable.js", "theme.js", "jquery.lithium-toastmessage.js", "Text.js", "plugin.js", "NoConflict.js", "jquery.placeholder-2.0.7.js", "plugin.js", "InputEditForm.js", "jquery.ui.widget.js", "en.js", "BlockEvents.js", "Lithium.js", "FieldSet.js", "jquery.iframe-shim-1.0.js", "Dialog.js", "plugin.js", "TinyMceEditor.js", "en.js", "plugin.js", "TokenInputAutoComplete.js", "Namespace.js", "jquery.blockui.js", "Link.js", "en.js", "Forms.js", "jquery.ui.dialog.js", "plugin.js", "InlineMessageReplyContainer.js", "en.js", "Sandbox.js", "InformationBox.js", "Loader.js", "DataHandler.js", "CookieBannerAlert.js", "jquery.scrollTo.js", "ElementMethods.js", "plugin.js", "ProductsField.js", "brightcove_uploader.js", "DynamicPager.js", "ThreadedDetailMessageList.js", "plugin.js", "tinymce-patched.js", "plugin.js", "ReCaptchaV3.js", "api.js", "plugin.js", "en.js", "jquery.js", "ActiveCast3.js", "jquery.viewport-1.0.js", "jquery.ui.core.js", "prism.js", "plugin.js", "jquery.fileupload.js", "jquery.css-data-1.0.js", "Throttle.js", "MessageEditor.js", "InlineMessageEditor.js", "jquery.effects.slide.js", "SpoilerToggle.js", "plugin.js", "Globals.js", "Cache.js", "MessageBodyDisplay.js", "plugin.js" ];(function(){LITHIUM.AngularSupport=function(){function g(a,c){a=a||{};for(var b in c)"[object object]"===Object.prototype.toString.call(c[b])?a[b]=g(a[b],c[b]):a[b]=c[b];return a}var d,f,b={coreModule:"li.community",coreModuleDeps:[],noConflict:!0,bootstrapElementSelector:".lia-page .min-width .lia-content",bootstrapApp:!0,debugEnabled:!1,useCsp:!0,useNg2:!1},k=function(){var a;return function(b){a||(a=document.createElement("a"));a.href=b;return a.href}}();LITHIUM.Angular={};return{preventGlobals:LITHIUM.Globals.preventGlobals, restoreGlobals:LITHIUM.Globals.restoreGlobals,init:function(){var a=[],c=document.querySelector(b.bootstrapElementSelector);a.push(b.coreModule);b.customerModules&&0

Get hands-on experience with 20+ free Google Cloud products and $300 in free credit for new customers.

Bigquery cost execution

How can i get the real time alert if the user executes a SQL in BIG QUERY and that SQL execution is taking a lot of time?

0 7 174
7 REPLIES 7

Hi @shubham21feb,

Welcome to Google Cloud Community!

BigQuery cost refers to the charges incurred when running queries. Usage is usually reflected several hours later to billing, so it is impossible to get a real-time cost alert on your queries. A workaround is to cap your project’s quotas usage to monitor your real-time cost control. You can also set your Budget Alert to Forecasted cost as it notifies users earlier based on cost estimates.

I hope the above information is helpful.

If you access to Gemini in the BigQuery console, try asking it something like "how do i create an alert if a bigquery query runs longer than 2 minutes". It should give you a good description with sample script.

Else, go to Cloud Monitoring and click on the "Alerting" tab. From there, create a new policy. Default option is to use Builder which is an easy way to get started. Once you are familiar with how it works, you should use scripts especially for production so you can have code reviews, re-use, manage versions, etc... Using Builder, select the metrics Global-->Query-->Query execution times. There are a few steps to complete but they're intuitive and you can always click on "Learn more" for docs. You do need to configure a notification channel; recommend at least 2 and of different types (e.g. 1 email, 1 SMS or some other mix, don't do 2 emails else you fly blind if mail service is asleep). Once done, run some queries that you know will exceed whatever threshold you set in the alert and you will receive the notifications in the channels you configured. 

In case you're tempted to setup functions to automatically terminate these queries, recommend you don't, at least for the first few months. Some queries may legitimately take a longer time and you do want them to complete. Also, users who don't know you're doing this might build "unintelligent" retries that creates other problems.

Thank you @gojoe  for the detailed answer.

I was trying to set up the alert based on the above explanation and trying to select the notification channel as Pub/Sub to get the real time alert and from there user who is execution a query should get an email but it is not working for me when i am executing the query as per the setup threshold but i am not getting any email.

And What is the cost implication of this set up?

shubham21feb_0-1730077797876.pngshubham21feb_1-1730077848140.png

 

Suggest you start with a very narrow time window to make sure your policy and notification channel are working first then adjust the window to meet your requirements.

If you haven't already seen it, please check out the latency and sample frequency documented here. Note the bottom section with guidance related to alerts.

Hi @gojoe , I tried to implement the above solution but that is not giving the information of user who is executing the query with bytes billed metric. What other metrics or user defined metrics is available that can gives us user email address we can drop an email alert to the user if it crosses the threshold.

Don't know if/how we can directly notify the user that submitted the query that exceeded an alert threshold from Alerts. A common approach is to configure the alert to take action by notifying a pub/sub channel. You can deploy a Cloud Function that responds to a Pub/Sub trigger. That Cloud Function can do pretty much anything you want. Other channels can take action automatically also (e.g. slack, webhook, email, etc..), each with their own additional setup.

To get the userID, you can write a script as part of the Cloud Function to retrieve data relevant to the notification you want to send to the User. A few options on how to get the user_email, total_bytes_billed, etc... are documented here. INFORMATION_SCHEMAJOBS_by_USER is one option. You can then add more details to help the user take the appropriate action.

One thing to consider is whether the user who submitted the query is the right person to notify and if so, can that person actually do anything meaningful. E.g. if the query came from some BI tool or dashboard, it might be using a proxy user or service account to access BQ so the notification goes nowhere. Even if it gets to the actual user (some tools pass-on the actual user's credentials), if that's an analyst, customer service agent, etc... they might not be able to do anything either. 

I'd start with logging violations and notify users if they do it repeatedly. If the numbers are high, maybe publish them to a dashboard (e.g. using Looker) and ping stakeholders and offenders daily with a link to the dashboard.

Hi @gojoe , I created the eventarc trigger as below and use that trigger in the cloud function to send email to the user that option worked.

gcloud eventarc triggers update bq-audit-trigger-function-5 \

    --location=us-central1 \

    --destination-run-service=function-5 \

--event-filters="type=google.cloud.audit.log.v1.written" \

  --event-filters="serviceName=bigquery.googleapis.com" \

  --event-filters="methodName=jobservice.jobcompleted" \

    --service-account=service_account