> ## Documentation Index
> Fetch the complete documentation index at: https://docs.costory.io/llms.txt
> Use this file to discover all available pages before exploring further.

# Allocate the cost of a shared Cloud SQL database to the teams using it

> Use Costory's virtual dimensions and usage metrics to split a shared Cloud SQL database cost across consuming teams, based on actual storage or compute usage.

## Prerequisites

1. Identify what drives your database cost increases:
   * **Storage size**: choose this if the last resource increase was due to growing storage.
   * **Compute resources**: choose this if the last resource increase was due to CPU or memory pressure.

2. Know where the usage metric is available (e.g., Datadog, Google Cloud Monitoring).

## Output

* Gain visibility into each team's cost contribution.
* Schedule recurring reports to share cost allocation with stakeholders.
* Set alerts if a team's share grows unexpectedly.

## Steps

<Steps>
  <Step title="Choose the correct metric">
    Select the metric that matches your cost driver. Below are example queries for Datadog and Google Cloud SQL.

    **Storage size**

    <Tabs>
      <Tab title="Datadog">
        ```
        avg:postgresql.database_size{<yourWhereClause>} by {db}.rollup(avg, daily, 'UTC')
        ```
      </Tab>

      <Tab title="Google Cloud SQL">
        Not available. Use a Datadog-sourced metric instead.
      </Tab>
    </Tabs>

    **Compute resources**

    <Tabs>
      <Tab title="Datadog">
        ```
        sum:postgresql.queries.time{<yourWhereClause>} by {user}.rollup(sum, daily, 'UTC')
        ```
      </Tab>

      <Tab title="Google Cloud SQL">
        ```
        sum by ("user")(
          rate({
            "__name__"="cloudsql.googleapis.com/database/postgresql/insights/aggregate/execution_time",
            "monitored_resource"="cloudsql_instance_database"
          }[${__interval}])
        )
        ```
      </Tab>
    </Tabs>
  </Step>

  <Step title="Import the metric in Costory">
    Go to [Costory Metrics](https://app.costory.io/datasources/metrics) and import the metric.

    <img src="https://mintcdn.com/costory/dHz2ISqs3xGAuo-u/use-cases/allocate_shared_cloud_sql_database/0.png?fit=max&auto=format&n=dHz2ISqs3xGAuo-u&q=85&s=d6bf4cdf96b718eceadd0dcff6add332" alt="Import metric, data source selection screen" width="2060" height="1392" data-path="use-cases/allocate_shared_cloud_sql_database/0.png" />

    <img src="https://mintcdn.com/costory/dHz2ISqs3xGAuo-u/use-cases/allocate_shared_cloud_sql_database/1.png?fit=max&auto=format&n=dHz2ISqs3xGAuo-u&q=85&s=6434497bc774ef67049e158441648c24" alt="Import metric, configuration screen" width="2060" height="1392" data-path="use-cases/allocate_shared_cloud_sql_database/1.png" />

    For both compute resources and storage size, we recommend using **SUM** when aggregating the metric at weekly or monthly levels. Using a maximum aggregation might over-allocate cost to the team responsible for a single-day spike.

    Click **Import**. The first import may take a few minutes to complete.
  </Step>

  <Step title="Preview the result using Explorer">
    Go to [Explorer](https://app.costory.io/explorer) and query the metric you just imported. Verify that the per-team breakdown looks reasonable before creating a virtual dimension.
  </Step>

  <Step title="Create a virtual dimension to allocate the cost">
    1. Go to [Dimensions](https://app.costory.io/dimensions) and create a new virtual dimension.

    2. Rely on a filter on `resource_id` or resource name to scope the database cost correctly.

    3. Instead of relying on dimension value mapping, use the **Dynamic allocation based on usage metrics** option:

           <img src="https://mintcdn.com/costory/dHz2ISqs3xGAuo-u/use-cases/allocate_shared_cloud_sql_database/2.png?fit=max&auto=format&n=dHz2ISqs3xGAuo-u&q=85&s=e95b054cb47debbd5f4abfcf6979ec80" alt="Virtual dimension, dynamic allocation option" width="2410" height="836" data-path="use-cases/allocate_shared_cloud_sql_database/2.png" />

    4. Map the group-by dimensions to the correct team or owner. You have three options:

       * **Identity mapping**: your database name equals your team name.
       * **Regex mapping**: your database name contains your team name (extract it with a regex).
       * **Manual mapping**: your database name has no relation to the team name; map each name manually.

           <img src="https://mintcdn.com/costory/dHz2ISqs3xGAuo-u/use-cases/allocate_shared_cloud_sql_database/3.png?fit=max&auto=format&n=dHz2ISqs3xGAuo-u&q=85&s=0163b84b55768e10733ebaffa9e1dc5d" alt="Virtual dimension, mapping options" width="1474" height="1500" data-path="use-cases/allocate_shared_cloud_sql_database/3.png" />

    <Warning>
      If you choose manual mapping and a new database appears later, you will need to edit the virtual dimension to allocate the new database to the correct team.
    </Warning>

    5. Save the virtual dimension.
  </Step>

  <Step title="Create a report to visualize the cost allocation">
    Go to [Explorer](https://app.costory.io/explorer) and create a new report using your virtual dimension as a group-by. You can also schedule the report to be sent to Slack on a weekly or monthly basis.
  </Step>
</Steps>

## Related

* Need to allocate a shared API instead of a database? See [Split Shared API Costs Across Teams](/use-cases/reallocate_api_costs) for a step-by-step guide using an external usage metric like API call counts.
