Monitor active queries

This page describes how to monitor and troubleshoot the queries that are active in your database. Monitoring these queries can help identify causes of system latency and high CPU usage.

On the Query Insights dashboard, you can view a summary of active transactions and a list of transactions with associated query and statistics. The transactions are displayed by the transaction start time. If there is a large number of queries running, the results may be limited to a subset of total queries.

Before you begin

To view your active queries, you must do the following:

  1. Set up Gemini in Databases.
  2. Create a custom role and add the following permissions:
    Tasks Roles Permissions
    View recommendations databaseinsights.viewer databaseinsights.activeQueries.fetch
    databaseinsights.activitySummary.fetch
    Terminate recommendations databaseinsights.operationsAdmin N/A

View active queries

To view your active queries, do the following:

  1. Add this role for each user that is viewing active queries.

  2. In the Google Cloud console, go to the Cloud SQL Instances page.

    Go to Cloud SQL Instances

  3. To open the Overview page of an instance, click the instance name.

  4. Do one of the following to display the Query insights dashboard:

    • Select the Query insights tab.
    • Click Go to Query insights for more in-depth info on queries and performance. The Query insights dashboard opens. It shows details about the instance at the top.
  5. Click the Active query view tab.

    From here, you can modify the query information that appears in the Longest running transactions table:

    • Database: filters query load on a specific database or all databases.
    • User: filters query load from a specific user account.
    • Summary scorecard of all active queries: provides an overview of all the active queries by displaying the total connections based on the following parameters:
      1. Distribution of active connections categorized by connection state.
      2. Distribution of active transactions based on transaction state.
      3. Distribution of query durations.
      4. Longest running transactions: provides an overview of the top 50 running queries (in the active state) based on descending execution time. You can filter and sort active queries in the table.

View normalized active queries

You can view a list of top long-running transactions with normalized active queries on the Query insights dashboard. A normalized active query removes sensitive data and returns a digest. The digest is the same for different values used in the following example:

  • Regular queries

    • select * from test_table where id=1;
    • select * from test_table_where id=2;
  • Digest or normalized query

    • select * from test_table where id=?;

A query that runs in different sessions is displayed as different entries on the dashboard.

View top longest running transactions

The Longest running transactions table on the Query insights dashboard contains the following columns:

Column name Description
Process ID Unique identifier of the connection.
Query SQL query text.
Transaction status The current execution state of the transaction. Permitted values include: RUNNING, LOCK WAIT, ROLLING BACK, and COMMITTING.
Thread state Thread state (or query state) indicates the current state of the active thread.
Transaction start time The timestamp when the transaction started its execution.
Transaction duration The duration of current active transaction in seconds.
Transaction wait duration The duration of wait in seconds on current active transaction.
Thread state duration Time elapsed in the query.
Transaction rows locked Number of rows locked by the current active transaction.
Transaction rows modified Number of rows modified by the current active transaction.
Database Database name on which this connection is running.
User name Name of the user connected to the database.
Client address The IP address of client from where a user connects to the database.
Action Contains a link for terminating a transaction.

The display is refreshed every 60 seconds automatically.

Terminate a connection

To terminate a query or a transaction, you must have the databaseinsights.operationsAdmin role. For more information, see IAM basic and predefined roles reference.

To terminate a query or transaction, complete the following steps:

  1. In the Longest running transaction table, select a query.
  2. In the Action column, click Terminate connection.
  3. In the Terminate connection window, click Confirm.

A database request returns immediately while the termination happens in the background.