mirror of
https://github.com/jbranchaud/til
synced 2026-01-07 09:08:01 +00:00
Add Difference Between Explain And Explain Analyze as a postgres til
This commit is contained in:
@@ -9,7 +9,7 @@ and pairing with smart people at Hashrocket.
|
|||||||
|
|
||||||
For a steady stream of TILs, [sign up for my newsletter](https://tinyletter.com/jbranchaud).
|
For a steady stream of TILs, [sign up for my newsletter](https://tinyletter.com/jbranchaud).
|
||||||
|
|
||||||
_861 TILs and counting..._
|
_862 TILs and counting..._
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
@@ -415,6 +415,7 @@ _861 TILs and counting..._
|
|||||||
- [Default Schema](postgres/default-schema.md)
|
- [Default Schema](postgres/default-schema.md)
|
||||||
- [Defining Arrays](postgres/defining-arrays.md)
|
- [Defining Arrays](postgres/defining-arrays.md)
|
||||||
- [Determining The Age Of Things](postgres/determining-the-age-of-things.md)
|
- [Determining The Age Of Things](postgres/determining-the-age-of-things.md)
|
||||||
|
- [Difference Between Explain And Explain Analyze](postgres/difference-between-explain-and-explain-analyze.md)
|
||||||
- [Dump And Restore A Database](postgres/dump-and-restore-a-database.md)
|
- [Dump And Restore A Database](postgres/dump-and-restore-a-database.md)
|
||||||
- [Edit Existing Functions](postgres/edit-existing-functions.md)
|
- [Edit Existing Functions](postgres/edit-existing-functions.md)
|
||||||
- [Escaping A Quote In A String](postgres/escaping-a-quote-in-a-string.md)
|
- [Escaping A Quote In A String](postgres/escaping-a-quote-in-a-string.md)
|
||||||
|
|||||||
44
postgres/difference-between-explain-and-explain-analyze.md
Normal file
44
postgres/difference-between-explain-and-explain-analyze.md
Normal file
@@ -0,0 +1,44 @@
|
|||||||
|
# Difference Between Explain And Explain Analyze
|
||||||
|
|
||||||
|
The `explain` statement allows you to gain some insight into the performance of
|
||||||
|
a query. You may hear `explain` and `explain analyze` referred to
|
||||||
|
interchangeably in conversation. Though they can both be used to explore how a
|
||||||
|
query will perform, it's important to know a key difference. `explain analyze`
|
||||||
|
executes the query, `explain` does not.
|
||||||
|
|
||||||
|
For `select` queries, the distinction may not feel that important. For
|
||||||
|
`insert`s, `update`s, and `delete`s, you'll want to be clear about which one
|
||||||
|
you are using.
|
||||||
|
|
||||||
|
```sql
|
||||||
|
> explain insert into books (title, author) values ('Fledgling', 'Octavia Butler');
|
||||||
|
QUERY PLAN
|
||||||
|
----------------------------------------------------
|
||||||
|
Insert on books (cost=0.00..0.01 rows=1 width=76)
|
||||||
|
-> Result (cost=0.00..0.01 rows=1 width=76)
|
||||||
|
|
||||||
|
> select count(*) from books;
|
||||||
|
count
|
||||||
|
-------
|
||||||
|
0
|
||||||
|
```
|
||||||
|
|
||||||
|
With `explain`, you get cost estimates of the `insert` statement.
|
||||||
|
|
||||||
|
```sql
|
||||||
|
> explain analyze insert into books (title, author) values ('Fledgling', 'Octavia Butler');
|
||||||
|
QUERY PLAN
|
||||||
|
----------------------------------------------------------------------------------------------
|
||||||
|
Insert on books (cost=0.00..0.01 rows=1 width=76) (actual time=0.285..0.285 rows=0 loops=1)
|
||||||
|
-> Result (cost=0.00..0.01 rows=1 width=76) (actual time=0.012..0.012 rows=1 loops=1)
|
||||||
|
Planning time: 0.021 ms
|
||||||
|
Execution time: 0.309 ms
|
||||||
|
|
||||||
|
> select count(*) from books;
|
||||||
|
count
|
||||||
|
-------
|
||||||
|
1
|
||||||
|
```
|
||||||
|
|
||||||
|
With `explain analyze`, you get estimates and actual numbers. You also get a
|
||||||
|
row inserted in the `books` table.
|
||||||
Reference in New Issue
Block a user