@@ -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 use unique constraints.
14
15
* How to implement full text search with PostgreSQL.
@@ -544,6 +545,34 @@ When building a model with a foreign key that will reference this UUID, treat
544
545
$ rails generate model Case device_id:uuid
545
546
```
546
547
548
+ Indexing
549
+ --------
550
+
551
+ * [ index creation] ( https://www.postgresql.org/docs/current/sql-createindex.html )
552
+
553
+ PostgreSQL includes a variety of index options. The following options are
554
+ supported by the PostgreSQL adapter in addition to the
555
+ [ common index options] ( https://api.rubyonrails.org/classes/ActiveRecord/ConnectionAdapters/SchemaStatements.html#method-i-add_index )
556
+
557
+ ### Include
558
+
559
+ When creating a new index, non-key columns can be included with the ` :include ` option.
560
+ These keys are not used in index scans for searching, but can be read during an index
561
+ only scan without having to visit the associated table.
562
+
563
+ ``` ruby
564
+ # db/migrate/20131220144913_add_index_users_on_email_include_id.rb
565
+
566
+ add_index :users , :email , include: :id
567
+ ```
568
+
569
+ Multiple columns are supported:
570
+
571
+ ``` ruby
572
+ # db/migrate/20131220144913_add_index_users_on_email_include_id_and_created_at.rb
573
+
574
+ add_index :users , :email , include: [:id , :created_at ]
575
+ ```
547
576
548
577
Generated Columns
549
578
-----------------
0 commit comments