2020-10-05 11:08:56 -04:00
---
2022-05-31 11:09:02 -04:00
stage: Data Stores
2020-10-05 11:08:56 -04:00
group: Database
2020-11-26 01:09:20 -05:00
info: To determine the technical writer assigned to the Stage/Group associated with this page, see https://about.gitlab.com/handbook/engineering/ux/technical-writing/#assignments
2020-10-05 11:08:56 -04:00
---
2016-08-11 08:22:21 -04:00
# Adding Database Indexes
Indexes can be used to speed up database queries, but when should you add a new
index? Traditionally the answer to this question has been to add an index for
every column used for filtering or joining data. For example, consider the
following query:
```sql
SELECT *
FROM projects
WHERE user_id = 2;
```
Here we are filtering by the `user_id` column and as such a developer may decide
to index this column.
2022-06-20 20:08:43 -04:00
While in certain cases indexing columns using the above approach may make sense,
it can actually have a negative impact. Whenever you write data to a table, any
existing indexes must also be updated. The more indexes there are, the slower this
can potentially become. Indexes can also take up significant disk space, depending
2016-08-11 08:22:21 -04:00
on the amount of data indexed and the index type. For example, PostgreSQL offers
2022-06-20 20:08:43 -04:00
`GIN` indexes which can be used to index certain data types that cannot be
indexed by regular B-tree indexes. These indexes, however, generally take up more
2020-06-09 23:08:17 -04:00
data and are slower to update compared to B-tree indexes.
2016-08-11 08:22:21 -04:00
2022-06-20 20:08:43 -04:00
Because of all this, it's important make the following considerations
when adding a new index:
2016-08-11 08:22:21 -04:00
2022-06-20 20:08:43 -04:00
1. Do the new queries re-use as many existing indexes as possible?
1. Is there enough data that using an index is faster than iterating over
rows in the table?
2018-11-12 19:39:21 -05:00
1. Is the overhead of maintaining the index worth the reduction in query
2016-08-11 08:22:21 -04:00
timings?
## Re-using Queries
The first step is to make sure your query re-uses as many existing indexes as
possible. For example, consider the following query:
```sql
SELECT *
FROM todos
WHERE user_id = 123
AND state = 'open';
```
Now imagine we already have an index on the `user_id` column but not on the
2022-06-17 17:09:20 -04:00
`state` column. One may think this query performs badly due to `state` being
2016-08-11 08:22:21 -04:00
unindexed. In reality the query may perform just fine given the index on
`user_id` can filter out enough rows.
The best way to determine if indexes are re-used is to run your query using
2022-06-20 20:08:43 -04:00
`EXPLAIN ANALYZE` . Depending on the joined tables and the columns being used for filtering,
you may find an extra index doesn't make much, if any, difference.
2016-08-11 08:22:21 -04:00
In short:
1. Try to write your query in such a way that it re-uses as many existing
indexes as possible.
2018-11-12 19:39:21 -05:00
1. Run the query using `EXPLAIN ANALYZE` and study the output to find the most
2016-08-11 08:22:21 -04:00
ideal query.
## Data Size
2022-06-20 20:08:43 -04:00
A database may not use an index even when a regular sequence scan
(iterating over all rows) is faster, especially for small tables.
2016-08-11 08:22:21 -04:00
2022-06-20 20:08:43 -04:00
Consider adding an index if a table is expected to grow, and your query has to filter a lot of rows.
You may _not_ want to add an index if the table size is small (< `1,000` records),
or if existing indexes already filter out enough rows.
2016-08-11 08:22:21 -04:00
## Maintenance Overhead
2022-06-20 20:08:43 -04:00
Indexes have to be updated on every table write. In the case of PostgreSQL, _all_
2022-06-17 17:09:20 -04:00
existing indexes are updated whenever data is written to a table. As a
2022-06-20 20:08:43 -04:00
result, having many indexes on the same table slows down writes. It's therefore important
to balance query performance with the overhead of maintaining an extra index.
2016-08-11 08:22:21 -04:00
2022-06-20 20:08:43 -04:00
Let's say that adding an index reduces SELECT timings by 5 milliseconds but increases
INSERT/UPDATE/DELETE timings by 10 milliseconds. In this case, the new index may not be worth
it. A new index is more valuable when SELECT timings are reduced and INSERT/UPDATE/DELETE
timings are unaffected.
2016-08-11 08:22:21 -04:00
## Finding Unused Indexes
To see which indexes are unused you can run the following query:
```sql
SELECT relname as table_name, indexrelname as index_name, idx_scan, idx_tup_read, idx_tup_fetch, pg_size_pretty(pg_relation_size(indexrelname::regclass))
FROM pg_stat_all_indexes
WHERE schemaname = 'public'
AND idx_scan = 0
AND idx_tup_read = 0
AND idx_tup_fetch = 0
ORDER BY pg_relation_size(indexrelname::regclass) desc;
```
This query outputs a list containing all indexes that are never used and sorts
2022-06-20 20:08:43 -04:00
them by indexes sizes in descending order. This query helps in
determining whether existing indexes are still required. More information on
2016-08-11 08:22:21 -04:00
the meaning of the various columns can be found at
2019-09-27 08:06:07 -04:00
< https: / / www . postgresql . org / docs / current / monitoring-stats . html > .
2016-08-11 08:22:21 -04:00
2022-06-20 20:08:43 -04:00
Because the query output relies on the actual usage of your database, it
may be affected by factors such as:
2016-08-11 08:22:21 -04:00
2018-11-13 01:07:16 -05:00
- Certain queries never being executed, thus not being able to use certain
2016-08-11 08:22:21 -04:00
indexes.
2018-11-13 01:07:16 -05:00
- Certain tables having little data, resulting in PostgreSQL using sequence
2016-08-11 08:22:21 -04:00
scans instead of index scans.
2022-06-20 20:08:43 -04:00
This data is only reliable for a frequently used database with
plenty of data, and using as many GitLab features as possible.
2020-09-16 17:09:52 -04:00
## Requirements for naming indexes
2022-06-20 20:08:43 -04:00
Indexes with complex definitions must be explicitly named rather than
2020-09-16 17:09:52 -04:00
relying on the implicit naming behavior of migration methods. In short,
that means you **must** provide an explicit name argument for an index
created with one or more of the following options:
- `where`
- `using`
- `order`
- `length`
- `type`
- `opclass`
### Considerations for index names
Index names don't have any significance in the database, so they should
attempt to communicate intent to others. The most important rule to
remember is that generic names are more likely to conflict or be duplicated,
and should not be used. Some other points to consider:
- For general indexes, use a template, like: `index_{table}_{column}_{options}` .
- For indexes added to solve a very specific problem, it may make sense
for the name to reflect their use.
- Identifiers in PostgreSQL have a maximum length of 63 bytes.
- Check `db/structure.sql` for conflicts and ideas.
### Why explicit names are required
As Rails is database agnostic, it generates an index name only
2022-05-13 11:07:43 -04:00
from the required options of all indexes: table name and column names.
2020-09-16 17:09:52 -04:00
For example, imagine the following two indexes are created in a migration:
```ruby
def up
add_index :my_table, :my_column
add_index :my_table, :my_column, where: 'my_column IS NOT NULL'
end
```
Creation of the second index would fail, because Rails would generate
the same name for both indexes.
2022-06-20 20:08:43 -04:00
This naming issue is further complicated by the behavior of the `index_exists?` method.
2022-05-13 11:07:43 -04:00
It considers only the table name, column names, and uniqueness specification
2020-09-16 17:09:52 -04:00
of the index when making a comparison. Consider:
```ruby
def up
unless index_exists?(:my_table, :my_column, where: 'my_column IS NOT NULL')
add_index :my_table, :my_column, where: 'my_column IS NOT NULL'
end
end
```
2022-06-17 17:09:20 -04:00
The call to `index_exists?` returns true if **any** index exists on
`:my_table` and `:my_column` , and index creation is bypassed.
2020-09-16 17:09:52 -04:00
The `add_concurrent_index` helper is a requirement for creating indexes
2022-06-20 20:08:43 -04:00
on populated tables. Because it cannot be used inside a transactional
2020-09-16 17:09:52 -04:00
migration, it has a built-in check that detects if the index already
exists. In the event a match is found, index creation is skipped.
Without an explicit name argument, Rails can return a false positive
for `index_exists?` , causing a required index to not be created
properly. By always requiring a name for certain types of indexes, the
chance of error is greatly reduced.
2021-01-04 13:10:11 -05:00
## Temporary indexes
There may be times when an index is only needed temporarily.
For example, in a migration, a column of a table might be conditionally
2022-06-20 20:08:43 -04:00
updated. To query which columns must be updated in the
[query performance guidelines ](query_performance.md ), an index is needed
that would otherwise not be used.
2021-01-04 13:10:11 -05:00
2022-06-20 20:08:43 -04:00
In these cases, consider a temporary index. To specify a
2021-01-04 13:10:11 -05:00
temporary index:
2022-06-20 20:08:43 -04:00
1. Prefix the index name with `tmp_` and follow the [naming conventions ](database/constraint_naming_convention.md )
and [requirements for naming indexes ](#requirements-for-naming-indexes ) for the rest of the name.
2021-01-04 13:10:11 -05:00
1. Create a follow-up issue to remove the index in the next (or future) milestone.
1. Add a comment in the migration mentioning the removal issue.
A temporary migration would look like:
```ruby
INDEX_NAME = 'tmp_index_projects_on_owner_where_emails_disabled'
def up
# Temporary index to be removed in 13.9 https://gitlab.com/gitlab-org/gitlab/-/issues/1234
add_concurrent_index :projects, :creator_id, where: 'emails_disabled = false', name: INDEX_NAME
end
def down
remove_concurrent_index_by_name :projects, INDEX_NAME
end
```
2021-08-03 05:15:56 -04:00
## Create indexes asynchronously
For very large tables, index creation can be a challenge to manage.
While `add_concurrent_index` creates indexes in a way that does not block
normal traffic, it can still be problematic when index creation runs for
many hours. Necessary database operations like `autovacuum` cannot run, and
on GitLab.com, the deployment process is blocked waiting for index
creation to finish.
To limit impact on GitLab.com, a process exists to create indexes
2022-06-20 20:08:43 -04:00
asynchronously during weekend hours. Due to generally lower traffic and fewer deployments,
index creation can proceed at a lower level of risk.
### Schedule index creation for a low-impact time
2021-08-03 05:15:56 -04:00
1. [Schedule the index to be created ](#schedule-the-index-to-be-created ).
1. [Verify the MR was deployed and the index exists in production ](#verify-the-mr-was-deployed-and-the-index-exists-in-production ).
1. [Add a migration to create the index synchronously ](#add-a-migration-to-create-the-index-synchronously ).
### Schedule the index to be created
Create an MR with a post-deployment migration which prepares the index
for asynchronous creation. An example of creating an index using
the asynchronous index helpers can be seen in the block below. This migration
enters the index name and definition into the `postgres_async_indexes`
table. The process that runs on weekends pulls indexes from this
table and attempt to create them.
```ruby
# in db/post_migrate/
INDEX_NAME = 'index_ci_builds_on_some_column'
def up
prepare_async_index :ci_builds, :some_column, name: INDEX_NAME
end
def down
unprepare_async_index :ci_builds, :some_column, name: INDEX_NAME
end
```
### Verify the MR was deployed and the index exists in production
You can verify if the MR was deployed to GitLab.com by executing
`/chatops run auto_deploy status <merge_sha>` . To verify existence of
the index, you can:
2022-03-03 07:14:02 -05:00
- Use a meta-command in #database -lab, such as: `\d <index_name>` .
- Ensure that the index is not [`invalid` ](https://www.postgresql.org/docs/12/sql-createindex.html#:~:text=The%20psql%20%5Cd%20command%20will%20report%20such%20an%20index%20as%20INVALID ).
- Ask someone in #database to check if the index exists.
2021-08-03 05:15:56 -04:00
- With proper access, you can also verify directly on production or in a
2022-03-03 07:14:02 -05:00
production clone.
2021-08-03 05:15:56 -04:00
### Add a migration to create the index synchronously
After the index is verified to exist on the production database, create a second
2022-05-13 11:07:43 -04:00
merge request that adds the index synchronously. The schema changes must be
2022-05-20 08:08:50 -04:00
updated and committed to `structure.sql` in this second merge request.
2022-05-13 11:07:43 -04:00
The synchronous migration results in a no-op on GitLab.com, but you should still add the
2021-08-03 05:15:56 -04:00
migration as expected for other installations. The below block
demonstrates how to create the second migration for the previous
asynchronous example.
2022-06-20 20:08:43 -04:00
**WARNING:**
Verify that the index exists in production before merging a second migration with `add_concurrent_index` .
If the second migration is deployed before the index has been created,
the index is created synchronously when the second migration executes.
2021-08-03 05:15:56 -04:00
```ruby
# in db/post_migrate/
INDEX_NAME = 'index_ci_builds_on_some_column'
disable_ddl_transaction!
def up
add_concurrent_index :ci_builds, :some_column, name: INDEX_NAME
end
def down
remove_concurrent_index_by_name :ci_builds, INDEX_NAME
end
```
2021-10-31 20:09:31 -04:00
## Test database index changes locally
You must test the database index changes locally before creating a merge request.
2021-12-03 10:10:36 -05:00
### Verify indexes created asynchronously
2021-10-31 20:09:31 -04:00
Use the asynchronous index helpers on your local environment to test changes for creating an index:
1. Enable the feature flags by running `Feature.enable(:database_async_index_creation)` and `Feature.enable(:database_reindexing)` in the Rails console.
1. Run `bundle exec rails db:migrate` so that it creates an entry in the `postgres_async_indexes` table.
1. Run `bundle exec rails gitlab:db:reindex` so that the index is created asynchronously.
2021-12-03 10:10:36 -05:00
1. To verify the index, open the PostgreSQL console using the [GDK ](https://gitlab.com/gitlab-org/gitlab-development-kit/-/blob/main/doc/howto/postgresql.md ) command `gdk psql` and run the command `\d <index_name>` to check that your newly created index exists.