> For the complete documentation index, see [llms.txt](https://docs.cennso.com/cennso/cennso-db/latest/llms.txt) (all documentation: [llms.txt](https://docs.cennso.com/llms.txt)). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.cennso.com/cennso/cennso-db/latest/troubleshooting/db-instance-memory-monitoring.md).

# MySQL memory monitoring

The Cennso Database extension provides rich capabilities for monitoring the database instances deployed and managed by it. The database metrics exported by the [Prometheus mysqld exporter](https://github.com/percona/mysqld_exporter) might be insufficient to troubleshoot problems with high memory consumption. In this case, you can add new metrics based on [custom SQL queries](https://docs.cennso.com/cennso/cennso-db/latest/deployment-guides/cennso-database-observability.md#metrics-based-on-custom-queries).

One of the most useful source to understand what part of a MySQL instance consumes a lot of memory, can be the [memory_summary_global_by_event_name](https://dev.mysql.com/doc/refman/8.4/en/performance-schema-memory-summary-tables.html) table from the [performance_schema](https://dev.mysql.com/doc/refman/8.4/en/performance-schema.html) database.

> [!NOTE]
> To have access to this table, the `performance_schema` must be enabled in your database instance. To check if it is enabled, use the following SQL statement:
>
> ```SQL
> SELECT @@GLOBAL.performance_schema;
> ```
>
> The result must look like this:
>
> ```
> +-----------------------------+
> | @@GLOBAL.performance_schema |
> +-----------------------------+
> |                           1 |
> +-----------------------------+
> ```
>
> If it is disabled (the value is `0`), you can enable it by adding `performance_schema=ON` configuration to the **spec.mysql.additionalConfiguration**.

You can build a custom metric `mysql_memory_by_event_current_bytes` using the following configuration of the `mysqld-exporter`:

```yaml
spec:
  metricsExporter:
    enabled: true
    customQueries: |
      mysql_memory_by_event:
        query: |
          SELECT EVENT_NAME,
                 CURRENT_NUMBER_OF_BYTES_USED AS current_bytes,
                 HIGH_NUMBER_OF_BYTES_USED    AS high_bytes
          FROM performance_schema.memory_summary_global_by_event_name
          WHERE CURRENT_NUMBER_OF_BYTES_USED > 1048576
        metrics:
          - EVENT_NAME:
              usage: "LABEL"
          - current_bytes:
              usage: "GAUGE"
              description: "Current bytes allocated, by performance_schema memory instrument"
          - high_bytes:
              usage: "GAUGE"
              description: "Peak bytes allocated since reset, by memory instrument"
```

After this metric is enabled in your Cennso Database extension deployment, you can add the following [promql](https://prometheus.io/docs/prometheus/latest/querying/basics/) query to the [official dashboard](https://docs.cennso.com/cennso/cennso-db/latest/observability/dashboard.md) of the Cennso Database extension:

```promql
topk(5, mysql_memory_by_event_current_bytes{
    k8s_cluster_name="$CLUSTER_NAME",
    namespace="$MYSQL_NAMESPACE",
    mysql_instance=~"$MYSQL_INSTANCE",
    instance=~"$MYSQL_INSTANCE",
    EVENT_NAME!="memory/innodb/buf_buf_pool"
  })
```
