Skip to content

Commit 77d8c13

Browse files
committed
[ci skip] Add info on how to use the buffers option in Postgres
The buffer option was added as part of the Active Record Explain as part of this PR: rails#47043 However perhaps they missed updating the guides on how to use this option as part of ActiveRecord Explain specifically in the context of the PostgreSQL DB
1 parent 281a00a commit 77d8c13

File tree

2 files changed

+74
-0
lines changed

2 files changed

+74
-0
lines changed

guides/source/active_record_postgresql.md

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ After reading this guide, you will know:
1515
* How to implement exclusion constraints.
1616
* How to implement full text search with PostgreSQL.
1717
* How to back your Active Record models with database views.
18+
* How to use Explain options to gain insights that could potentially further
19+
improve PostgreSQL's query performance
1820

1921
--------------------------------------------------------------------------------
2022

@@ -807,3 +809,40 @@ For example, to exclude comments from your structure dump, add this to an initia
807809
```ruby
808810
ActiveRecord::Tasks::DatabaseTasks.structure_dump_flags = ["--no-comments"]
809811
```
812+
813+
Explain options to give deeper insight into Query Performance
814+
--------------
815+
816+
### Buffer
817+
818+
The Buffer option that comes with PostgreSQL's `Explain` command gives one
819+
insights with regard to the data it is reading or writing when performing a
820+
query operation and which of that data comes from cache or another source like
821+
disk.
822+
823+
The buffer option is intended to give you one or more hints into what
824+
specifically within the query your executing could be a potential cause of
825+
slowness and thereby has room for further improvement.
826+
827+
828+
An example Active Record query that uses `explain` with the `buffers` option:
829+
830+
```ruby
831+
Company.where(id: owning_companies_ids).explain(:analyze, :buffers)
832+
```
833+
834+
Output of above query:
835+
836+
```sql
837+
=> EXPLAIN (ANALYZE, BUFFERS) SELECT "companies".* FROM "companies"
838+
WHERE "companies"."id" IN ($1, $2, $3) [["id", 365], ["id", 364], ["id", 360]]
839+
QUERY PLAN
840+
-------------------------------------------------------------------------------
841+
Seq Scan on companies (cost=0.00..2.21 rows=3 width=64)
842+
(actual time=0.009..0.012 rows=3 loops=1)
843+
Filter: (id = ANY ('{365,364,360}'::bigint[]))
844+
Rows Removed by Filter: 10
845+
Buffers: shared hit=1
846+
Planning Time: 0.023 ms
847+
Execution Time: 0.011 ms
848+
```

wrap_text.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
How to use Explain options to gain insights that could potentially further
2+
improve PostgreSQL's query performance
3+
4+
Explain options to give deeper insight into Query Performance
5+
--------------
6+
7+
8+
### Buffer
9+
10+
The Buffer option that comes with PostgreSQL's `Explain` command gives one
11+
insights with regard to the data it is reading or writing when performing a
12+
query operation and which of that data comes from cache or another source like
13+
disk.
14+
15+
The buffer option is intended to give you one or more hints into what
16+
specifically within the query your executing could be a potential cause of
17+
slowness and thereby has room for further improvement.
18+
19+
20+
An example Active Record query that uses `explain` with the `buffers` option:
21+
22+
```ruby Company.where(id: owning_companies_ids).explain(:analyze, :buffers) ```
23+
24+
Output of above query:
25+
26+
```sql
27+
=> EXPLAIN (ANALYZE, BUFFERS) SELECT "companies".* FROM "companies"
28+
WHERE "companies"."id" IN ($1, $2, $3) [["id", 365], ["id", 364], ["id", 360]]
29+
QUERY PLAN
30+
-------------------------------------------------------------------------------
31+
Seq Scan on companies (cost=0.00..2.21 rows=3 width=64)
32+
(actual time=0.009..0.012 rows=3 loops=1) Filter: (id = ANY ('
33+
{365,364,360}'::bigint[])) Rows Removed by Filter: 10 Buffers: shared hit=1
34+
Planning Time: 0.049 ms Execution Time: 0.023 ms(6 rows)
35+
```

0 commit comments

Comments
 (0)