Skip to content

Commit d441c74

Browse files
committed
Add explanation about explicit indexes
1 parent f97ee94 commit d441c74

File tree

3 files changed

+44
-2
lines changed

3 files changed

+44
-2
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
Implicit indexes
2+
================
3+
4+
So-called implicit indexes are named as such because they are created
5+
without the user asking for it. Some RDBMS will do so when you create a
6+
foreign key: they will create an index on the referencing table, using
7+
the referencing columns.
8+
9+
Here are some RDBMS that are known to create indexes when creating a
10+
foreign key:
11+
12+
- `MySQL <https://dev.mysql.com/doc/refman/8.0/en/create-table-foreign-keys.html>`_
13+
- `MariaDB <https://mariadb.com/kb/en/foreign-keys>`_
14+
15+
16+
Some other will not do so, on grands that such indexes are not always
17+
needed, and can be created in many different ways. They instead leave
18+
that responsability to the user:
19+
20+
- `PostgreSQL <https://stackoverflow.com/questions/970562/postgres-and-indexes-on-foreign-keys-and-primary-keys>`_
21+
- `SQLite <https://sqlite.org/foreignkeys.html#fk_indexes>`_
22+
- `SQL Server <https://stackoverflow.com/questions/836167/does-a-foreign-key-automatically-create-an-index>`_
23+
24+
In the case of these systems, the DBAL does the index creation for you
25+
and automatically picks an index name that obeys string length
26+
constraints of the platform you are using. That way, differences between
27+
platforms is reduce because you always end up with an index, be it
28+
created by the RDBMS or by the DBAL
29+
30+
The rationale behind this is that these indexes improve performance, for
31+
instance for checking that a delete operation can be performed on a
32+
referenced table without violating the constraint in the referencing
33+
table.
34+
35+
You can still explicitly create such indexes yourself, and the DBAL will
36+
notice when your index fulfills the indexing and constraint needs of the
37+
implicit index it would create, and will refrain from doing so.
38+
39+
Some RDBMS such as MySQL do something similar and can also drop implicit
40+
indexes that are fulfilled by user-defined indexes.

docs/en/sidebar.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,5 @@
2020
reference/caching
2121
reference/known-vendor-issues
2222
reference/upgrading
23+
24+
explanation/implicit-indexes

lib/Doctrine/DBAL/Schema/Table.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -554,8 +554,8 @@ protected function _addForeignKeyConstraint(ForeignKeyConstraint $constraint)
554554

555555
$this->_fkConstraints[$name] = $constraint;
556556

557-
// Add an explicit index on the foreign key columns.
558-
// If there is already an index that fulfils this requirements drop the request.
557+
// Add an implicit index on the foreign key columns.
558+
// If there is already an index that fulfills these requirements drop the request.
559559
// In the case of __construct calling this method during hydration from schema-details
560560
// all the explicitly added indexes lead to duplicates. This creates computation overhead in this case,
561561
// however no duplicate indexes are ever added (based on columns).

0 commit comments

Comments
 (0)