Skip to content

Commit 95f4d75

Browse files
Steven AbramsSteven Abrams
authored andcommitted
Add section on INCLUDE option to postgres doc
- Add a section to the ActiveRecord PostgreSQL doc describing use of the INCLUDE index option. - Related to rails#44803
1 parent f2327e8 commit 95f4d75

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

guides/source/active_record_postgresql.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ After reading this guide, you will know:
99

1010
* How to use PostgreSQL's datatypes.
1111
* How to use UUID primary keys.
12+
* How to include non-key columns in indexes.
1213
* How to use deferrable foreign keys.
1314
* How to implement full text search with PostgreSQL.
1415
* How to back your Active Record models with database views.
@@ -543,6 +544,34 @@ When building a model with a foreign key that will reference this UUID, treat
543544
$ rails generate model Case device_id:uuid
544545
```
545546

547+
Indexing
548+
--------
549+
550+
* [index creation](https://www.postgresql.org/docs/current/sql-createindex.html)
551+
552+
PostgreSQL includes a variety of index options. The following options are
553+
supported by the PostgreSQL adapter in addition to the
554+
[common index options](https://api.rubyonrails.org/classes/ActiveRecord/ConnectionAdapters/SchemaStatements.html#method-i-add_index)
555+
556+
### Include
557+
558+
When creating a new index, non-key columns can be included with the `:include` option.
559+
These keys are not used in index scans for searching, but can be read during an index
560+
only scan without having to visit the associated table.
561+
562+
```ruby
563+
# db/migrate/20131220144913_add_index_users_on_email_include_id.rb
564+
565+
add_index :users, :email, include: :id
566+
```
567+
568+
Multiple columns are supported:
569+
570+
```ruby
571+
# db/migrate/20131220144913_add_index_users_on_email_include_id_and_created_at.rb
572+
573+
add_index :users, :email, include: [:id, :created_at]
574+
```
546575

547576
Generated Columns
548577
-----------------

0 commit comments

Comments
 (0)