@@ -9,6 +9,7 @@ After reading this guide, you will know:
9
9
10
10
* How to use PostgreSQL's datatypes.
11
11
* How to use UUID primary keys.
12
+ * How to include non-key columns in indexes.
12
13
* How to use deferrable foreign keys.
13
14
* How to implement full text search with PostgreSQL.
14
15
* 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
543
544
$ rails generate model Case device_id:uuid
544
545
```
545
546
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
+ ```
546
575
547
576
Generated Columns
548
577
-----------------
0 commit comments