Files
supabase/apps/reference/docs/guides/integrations/pgmustard.mdx
2022-08-13 22:24:54 +08:00

67 lines
2.8 KiB
Plaintext
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
---
id: pgmustard
title: 'pgMustard'
description: 'Troubleshoot slow queries on Supabase with pgMustard, a visualization tool that also gives advice.'
---
This guide explains how to troubleshoot slow queries on Supabase using `explain` and pgMustard.
[pgMustard](https://pgmustard.com/) is a visualization tool for [`explain analyze`](https://www.postgresql.org/docs/current/using-explain.html#USING-EXPLAIN-ANALYZE) that also gives performance tips.
## Step 1: Get the query plan from Supabase
Use `explain analyze` to get a query plan from Postgres. This will run the query behind the scenes, so be careful with data modification queries.
pgMustard requires plans to be in json format, and the buffers, verbose, and settings parameters allow it to give better tips.
So a good prefix for your query would be:
```jsx
explain (analyze, format json, buffers, verbose, settings)
```
Run the query, and copy the output.
![01-supabase-run-query](/img/guides/integrations/pgmustard/01-supabase-run-query.png)
If youre using the Supabase SQL Editor, this is easily copied from the cell titled `QUERY PLAN`, as seen above.
If you have any trouble, check out pgMustards guide for [getting a query plan](https://www.pgmustard.com/getting-a-query-plan).
## Step 2: Paste the query plan into pgMustard
Paste the json output into pgMustard and press Submit.
![02-paste-plan-pgmustard](/img/guides/integrations/pgmustard/02-paste-plan-pgmustard.png)
## Step 3: Look through the top tips and slowest operations
Review the top tips in pgMustard. These are scored on a scale of 0 to 5 stars, based on how much time-saving potential they have (5 stars meaning lots of potential).
![03-review-tips-pgmustard](/img/guides/integrations/pgmustard/03-review-tips-pgmustard.png)
Click one of the tips, or one of the operations, to see more information.
![04-click-tip-pgmustard](/img/guides/integrations/pgmustard/04-click-tip-pgmustard.png)
## Step 4: Consider your options
If you get some promising suggestions, you may wish to explore them.
If you dont get any tips, your query might be quite fast for the amount of work its doing.
For the example we saw in Step 3, let's try adding an index on the `customer_name` field in Supabase.
![05-create-index-supabase](/img/guides/integrations/pgmustard/05-create-index-supabase.png)
Going through Steps 1-3 again, we now get an efficient index scan, that will scale nicely as our data grows.
![06-check-pgmustard](/img/guides/integrations/pgmustard/06-check-pgmustard.png)
We could look into why Postgres isnt choosing to do an index-only scan here, but pgMustard is letting us know that it doesnt think well gain much by doing so, by scoring the tip 0.3 out of 5.
## Resources
- [pgMustard](https://www.pgmustard.com) official website.
- [pgMustard explain glossary](https://www.pgmustard.com/docs/explain).