---
title: Getting Started with Database Monitoring
description: >-
  Monitor database health and performance, troubleshoot slow queries, and create
  dashboards for query analysis and optimization.
breadcrumbs: Docs > Getting Started > Getting Started with Database Monitoring
---

# Getting Started with Database Monitoring

## Overview{% #overview %}

Datadog Database Monitoring helps you to better understand the health and performance of your databases and to determine the root cause of any problems.

In one place, you can view:

- Host-level metrics
- Explain plans
- Historical query performance metrics

Work through this guide to set up Datadog Database Monitoring on an example PostgreSQL database. Next, identify an expensive query, troubleshoot a slow query, and create a dashboard to view changes in query volume.

## Setup{% #setup %}

### Prerequisites{% #prerequisites %}

Before getting started, you need a [Datadog account](https://www.datadoghq.com/free-datadog-trial/).

To run the example application, you need a machine with [GNU Make](https://www.gnu.org/software/make/) and [Docker Desktop](https://www.docker.com/products/docker-desktop/). Have your Datadog [API key](https://app.datadoghq.com/organization-settings/api-keys) available.

### Install the example application{% #install-the-example-application %}

The example application runs three containers: a PostgreSQL or MySQL database, the Datadog Agent, and a Go orders app that continuously generates realistic database traffic. While the app runs, the Agent sends the following signals to Datadog:

| Signal                | Description                                                                                            |
| --------------------- | ------------------------------------------------------------------------------------------------------ |
| Query metrics         | Per-query latency, execution count, and rows examined.                                                 |
| Explain plans         | Execution plans sampled at runtime, surfacing costly operations.                                       |
| Full table scan       | The `SELECT * FROM orders WHERE status = ?` query has no index on `status`, causing a sequential scan. |
| Lock contention       | The app uses `SELECT ... FOR UPDATE` to simulate concurrent row locks.                                 |
| APM ↔ DBM correlation | Traces from the orders service are linked to their database queries, enabling end-to-end visibility.   |

You can view this data in Datadog Database Monitoring.

Follow these instructions to install the example application on macOS or Linux.

1. Clone the [repository](https://github.com/DataDog/dd-database-monitoring-example) containing the example application:

   ```
   git clone https://github.com/DataDog/dd-database-monitoring-example
   ```

1. Change to the `dd-database-monitoring-example` directory:

   ```
   cd dd-database-monitoring-example
   ```

1. Set the environment variable `DD_API_KEY` to your Datadog API key:

   ```gdscript3
   export DD_API_KEY=<API_KEY>
   ```

1. Start the application. Choose the database you want to monitor:

   - **PostgreSQL:**
     ```
     make postgres
     ```
   - **MySQL:**
     ```
     make mysql
     ```

If your Datadog account is not on the US1 site, also export `DD_SITE` before running `make`:

   ```gdscript3
   export DD_SITE=datadoghq.eu   # example: EU site
   ```

The command continues to run until you stop it by pressing Ctrl + C.

## Identify an expensive query{% #identify-an-expensive-query %}

Which query consumes the most database time? To find out, use the Query Metrics view.

1. On the [Database Monitoring](https://app.datadoghq.com/databases) page, click the Query metrics tab in the UI.

1. Sort the Normalized Query table by Percent time to see the query that the database spends the most time executing.

The query that consumes the most database time appears on the first line. Look for the query scanning the `orders` table by `status`—for example, `SELECT * FROM orders WHERE status = ?`. This query is expensive because there is no index on the `status` column, so the database performs a full sequential scan of the table on every execution.

   {% image
      source="https://docs.dd-static.net/images/database_monitoring/dbm_qm_sort_time.2b4d553100d73a23c7d112483bcbaae2.png?auto=format&fit=max&w=850 1x, https://docs.dd-static.net/images/database_monitoring/dbm_qm_sort_time.2b4d553100d73a23c7d112483bcbaae2.png?auto=format&fit=max&w=850&dpr=2 2x"
      alt="Normalized queries sorted by percent time" /%}

## Troubleshoot a slow query{% #troubleshoot-a-slow-query %}

In addition to identifying slow queries, Datadog Database Monitoring can help you diagnose them. A query's Explain Plan describes the steps that the database takes to resolve the query. View an Explain Plan by clicking on a sample in the Query Samples view.

1. Navigate to the Query Samples view within [Database Monitoring](https://app.datadoghq.com/databases) by selecting the Samples tab.

1. In the In dropdown, select Explain Plans.

1. Sort the Normalized Query table by Duration.

   {% image
      source="https://docs.dd-static.net/images/database_monitoring/dbm_qs_explain_plan_duration.88b9cc717cf04733c9ded7c4af49521f.png?auto=format&fit=max&w=850 1x, https://docs.dd-static.net/images/database_monitoring/dbm_qs_explain_plan_duration.88b9cc717cf04733c9ded7c4af49521f.png?auto=format&fit=max&w=850&dpr=2 2x"
      alt="Normalized query samples sorted by duration" /%}

1. Find a query in the table with data in the Explain Plan column and click on it to open the Sample Details page.

1. Under Explain Plan, click List View. The list view breaks down each step the database takes to execute the query—the operation type, the table it runs against, and cost estimates—giving you a starting point to understand why a query is slow.

   {% image
      source="https://docs.dd-static.net/images/database_monitoring/dbm_qs_explain_plan_list_view.5c489946ea9cdca7d5efb5dc1d70babe.png?auto=format&fit=max&w=850 1x, https://docs.dd-static.net/images/database_monitoring/dbm_qs_explain_plan_list_view.5c489946ea9cdca7d5efb5dc1d70babe.png?auto=format&fit=max&w=850&dpr=2 2x"
      alt="Query explain plan showing Seq Scan on orders table" /%}

## Correlate traces and database queries{% #correlate-traces-and-database-queries %}

The orders app emits APM traces that are automatically linked to the database queries they generate. Use this correlation to move seamlessly between a slow trace and the exact query responsible.

1. Navigate to [APM > Traces](https://app.datadoghq.com/apm/traces) and find a trace from the orders service.

1. On the trace flame graph, click a database span to open its details panel.

1. In the details panel, click View in DBM to jump directly to that query in Database Monitoring.

Conversely, in a Query Sample in Database Monitoring, click View Trace to open the originating APM trace.

{% image
   source="?auto=format&fit=max&w=850 1x, ?auto=format&fit=max&w=850&dpr=2 2x"
   alt="APM trace linked to a DBM query sample" /%}

## Visualize database health and performance{% #visualize-database-health-and-performance %}

To understand the health and performance of your databases at a glance, add Datadog Database Monitoring metrics to a dashboard.

### View changes in query volume{% #view-changes-in-query-volume %}

For example, you can see the absolute change in query volume in the past hour by adding a Change widget to track a query count metric.

1. Select Dashboards > New Dashboard in the UI.

1. Enter a name for your dashboard. Click the New Dashboard button to go to your new dashboard.

1. To add content to your dashboard, click Add Widgets.

1. In the widget carousel, select the Change widget.

1. Select `postgresql.queries.count` in the Metric dropdown. This metric counts the number of queries sent to a PostgreSQL database. If you are monitoring MySQL, use `mysql.queries` instead.

1. Select `host` in the Break it down by dropdown so that the widget aggregates queries by host.

   {% image
      source="https://docs.dd-static.net/images/database_monitoring/dashboard_change_postgres.7bfcf73bf3929c803a5ba1a810a2192e.png?auto=format&fit=max&w=850 1x, https://docs.dd-static.net/images/database_monitoring/dashboard_change_postgres.7bfcf73bf3929c803a5ba1a810a2192e.png?auto=format&fit=max&w=850&dpr=2 2x"
      alt="Configure change widget for postgres queries metric" /%}

1. Click the Save button. The dashboard shows your new widget.

   {% image
      source="https://docs.dd-static.net/images/database_monitoring/dashboard_change_widget.bb48d1cc5d6ed75fdd308b07d5c63a08.png?auto=format&fit=max&w=850 1x, https://docs.dd-static.net/images/database_monitoring/dashboard_change_widget.bb48d1cc5d6ed75fdd308b07d5c63a08.png?auto=format&fit=max&w=850&dpr=2 2x"
      alt="Change widget showing query count" /%}

### View out-of-the-box dashboards{% #view-out-of-the-box-dashboards %}

Observe current database activity, resource utilization, and more on out-of-the-box dashboards provided by Datadog Database Monitoring.

To access the dashboards, from the [Database Monitoring](https://app.datadoghq.com/databases) page, select the Dashboards tab and choose the dashboard that you want to see.

You can clone and modify out-of-the-box dashboards to suit your needs.

## Further Reading{% #further-reading %}

- [Database Monitoring](https://docs.datadoghq.com/database_monitoring.md)
- [Troubleshooting](https://docs.datadoghq.com/database_monitoring/troubleshooting.md)
- [Database performance monitoring with Datadog](https://www.datadoghq.com/blog/database-performance-monitoring-datadog/)
- [Join an interactive session to level up your Database Monitoring](https://dtdg.co/fe)
- [Monitoring a Postgres Database with Datadog DBM](https://learn.datadoghq.com/courses/database-monitoring)
