256
bytes to 4500
bytes. 1024
. 256
to 20480
. 10240
bytes (10 KB). 0
to 20
. 5
. 200
to provide more data points. 5
. In the Google Cloud console, go to the Cloud SQL Instances page.
To enable query insights for a Cloud SQL instance by using gcloud
, run gcloud sql instances patch
with the --insights-config-query-insights-enabled
flag as follows after replacing INSTANCE_ID with the ID of the instance.
gcloudsqlinstancespatchINSTANCE_ID\--insights-config-query-insights-enabled
Also, use one or more of the following optional flags:
--insights-config-query-string-length
Sets the default query length limit. Higher query lengths are more useful for analytical queries, but they also require more memory. Changing the query length requires you to restart the instance. For Cloud SQL Enterprise edition, you can specify a value in bytes from 256
to 4500
. The default query length is 1024
bytes. For Cloud SQL Enterprise Plus edition, you can specify a limit in bytes from 256
to 20480
. The default value is 10240
bytes (10 KB).
--query_plans_per_minute
By default, a maximum of 5 executed query plan samples are captured per minute across all databases on the instance. Increasing the sampling rate is likely to give you more data points but might add a performance overhead. To disable sampling, set this value to 0
. For Cloud SQL Enterprise edition, you can change the value from 0 to 20. For Cloud SQL Enterprise Plus edition, you can increase the maximum up to 200 to provide more data points.
Replace the following:
gcloudsqlinstancespatchINSTANCE_ID\ --insights-config-query-insights-enabled\ --insights-config-query-string-length=INSIGHTS_CONFIG_QUERY_STRING_LENGTH\ --query_plans_per_minute=QUERY_PLANS_PER_MINUTE\ --tier=API_TIER_STRING\ --region=REGION
To enable query insights for a Cloud SQL instance by using the REST API, call the instances.patch
method with insightsConfig
settings.
Before using any of the request data, make the following replacements:
HTTP method and URL:
PATCH https://sqladmin.googleapis.com/v1/projects/PROJECT_ID/instances/INSTANCE_ID
Request JSON body:
{ "settings" : { "insightsConfig" : { "queryInsightsEnabled" : true, "recordClientAddress" : true, "queryStringLength" : 1024, "queryPlansPerMinute" : 20, } } }
To send your request, expand one of these options:
You should receive a JSON response similar to the following:
{ "kind": "sql#operation", "targetLink": "https://sqladmin.googleapis.com/v1/projects/PROJECT_ID/instances/INSTANCE_ID", "status": "PENDING", "user": "user@example.com", "insertTime": "2025-03-28T22:43:40.009Z", "operationType": "UPDATE", "name": "OPERATION_ID", "targetId": "INSTANCE_ID", "selfLink": "https://sqladmin.googleapis.com/v1/projects/PROJECT_ID/operations/OPERATION_ID", "targetProject": "PROJECT_ID" }
To use Terraform to enable query insights for a Cloud SQL instance, set the query_insights_enabled
flag to true
.
Also, you can use one or more of the following optional flags:
query_string_length
: for Cloud SQL Enterprise edition, you can specify a value in bytes from 256
to 4500
. The default query length is 1024
bytes. For Cloud SQL Enterprise Plus edition, you can specify a limit in bytes from 256
to 20480
. The default value is 10240
bytes (10 KB). record_client_address
: set the value to true
if you want to record the client IP address. The default is false
.query_plans_per_minute
: for Cloud SQL Enterprise edition, you can set the value from 0
to 20
. The default is 5
. For Cloud SQL Enterprise Plus edition, you can increase the maximum up to 200
to provide more data points. Here's an example:
To apply your Terraform configuration in a Google Cloud project, complete the steps in the following sections.
Set the default Google Cloud project where you want to apply your Terraform configurations.
You only need to run this command once per project, and you can run it in any directory.
export GOOGLE_CLOUD_PROJECT=PROJECT_ID
Environment variables are overridden if you set explicit values in the Terraform configuration file.
Each Terraform configuration file must have its own directory (also called a root module).
.tf
extension—for example main.tf
. In this tutorial, the file is referred to as main.tf
. mkdir DIRECTORY && cd DIRECTORY && touch main.tf
If you are following a tutorial, you can copy the sample code in each section or step.
Copy the sample code into the newly created main.tf
.
Optionally, copy the code from GitHub. This is recommended when the Terraform snippet is part of an end-to-end solution.
terraform init
Optionally, to use the latest Google provider version, include the -upgrade
option:
terraform init -upgrade
terraform plan
Make corrections to the configuration as necessary.
yes
at the prompt: terraform apply
Wait until Terraform displays the "Apply complete!" message.
Metrics are expected to be available in query insights within minutes of query completion.
The Query insights dashboard shows the query load based on factors that you select. Query load is a measurement of the total work for all the queries in the instance in the selected time range. The dashboard provides a series of filters that help you view query load.
To open the Query insights dashboard, follow these steps:
The Query insights dashboard opens. Depending on whether you're using query insights for Cloud SQL Enterprise edition or query insights for Cloud SQL Enterprise Plus edition, the Query insights dashboard shows the following information about your instance:
Database query load is a measure of the work (in CPU seconds) that the executed queries in your selected database perform over time. Each running query is either using or waiting for CPU resources, IO resources, or lock resources. Database query load is the ratio of the amount of time taken by all the queries that are completed in a given time window to the wall-clock time.
The top-level query insights dashboard shows the Database load by execution time graph. Drop-down menus on the dashboard let you filter the graph for all databases or a specific database.
Colored lines in the graph show per database load by execution time. Review the graph and use the filtering options to explore these questions:
All queries
Databases
To customize the database load chart, select a dimension from the Database Load by execution time drop-down.
To view the top contributors to the database load, you can use the Top dimensions by database load table. The Top dimensions by database load table displays the top contributors for the time period and dimension that you select in the Database Load by execution time chart drop-down. You can modify the time period or dimension to view the top contributors for a different dimension or time period.
In the Top dimensions by data load table, you can select the following tabs.
Tab | Description |
---|---|
Queries | The table displays the top normalized queries by total execution time. For each query, the data shown in columns are listed as follows:
|
Databases | The table shows the list of top databases that contributed to the load during the chosen time period across all executed queries.
|
The Top queries table provides an overview of the queries that cause the most query load. The table shows all the normalized queries for the time range and options selected on the Query insights dashboard. It sorts queries by the total execution time during the time range that you selected.
To sort the table, select a column heading.
The table shows the following properties:
Query insights stores and displays only normalized queries.
A query plan takes a sample of your query and breaks it down into individual operations. It explains and analyzes each operation in the query.
The Query plan samples graph shows all the query plans running at particular times and the amount of time each plan took to run. You can change the rate at which query plan samples are captured per minute. See Enable Query Insights.
By default, the panel on the right shows the details for the sample query plan that takes the longest time, as visible on the Query plan samples graph. To see the details for another sample query plan, click the relevant circle on the graph. Expanded details show a model of all the operations in the query plan.
Each operation shows the rows returned and, if available, the cost of the operation. Use the full screen view or the
Try to narrow down the problem by looking into the following questions:
Query details can provide further details about a specific query, including average execution time, the number of times called, and the average number of rows returned. It can also offer recommendations, such as indexes, to help improve query performance.
Query details also provide the following metrics:
You can use I/O metrics to help you correlate slower than usual queries with more intensive I/O usage or changes in I/O patterns.
For example, if the number of logical reads increased, then that could be an indication that the query started reading more data than usual. If the number of physical reads increased, then that could indicate that Cloud SQL started reading more data from disk rather than from the buffer cache. For example, perhaps there wasn't enough memory to cache all data or the instance started reading too much data. If the number of logical writes increased, then that could indicate that the query started writing more data to the data disk.
This is one example of how this metric could help measure the impact of the query on instance I/O.
This metric can help you detect changes in memory consumption related to the query, including the following examples:
tempdb
database, which is slower than using memory.To disable query insights for a Cloud SQL instance by using the Google Cloud console, follow these steps:
In the Google Cloud console, go to the Cloud SQL Instances page.
To disable query insights for a Cloud SQL instance by using gcloud
, run gcloud sql instances patch
with the --no-insights-config-query-insights-enabled
flag as follows, after replacing INSTANCE_ID with the ID of the instance.
gcloudsqlinstancespatchINSTANCE_ID\--no-insights-config-query-insights-enabled
To disable query insights for a Cloud SQL instance by using the REST API, call the instances.patch
method with queryInsightsEnabled
set to false
as follows.
Before using any of the request data, make the following replacements:
HTTP method and URL:
PATCH https://sqladmin.googleapis.com/sql/v1beta4/projects/project-id/instances/instance-id
Request JSON body:
{ "settings" : { "insightsConfig" : { "queryInsightsEnabled" : false } } }
To send your request, expand one of these options:
You should receive a JSON response similar to the following:
{ "kind": "sql#operation", "targetLink": "https://sqladmin.googleapis.com/sql/v1beta4/projects/project-id/instances/instance-id", "status": "PENDING", "user": "user@example.com", "insertTime": "2021-01-28T22:43:40.009Z", "operationType": "UPDATE", "name": "operation-id", "targetId": "instance-id", "selfLink": "https://sqladmin.googleapis.com/sql/v1beta4/projects/project-id/operations/operation-id", "targetProject": "project-id" }
To disable query insights for Cloud SQL Enterprise Plus edition, do the following:
In the Google Cloud console, go to the Cloud SQL Instances page.
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2025-04-28 UTC.