[英]Are indexes on the join table used in many-to-many relationships?
Say I have a database with 3 tables describing bus timetables: 假设我有一个数据库,其中包含3个描述公交时刻表的表:
journey
+--------------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------------+-------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| start | time | NO | | NULL | |
| end | time | NO | | NULL | |
| route_id | int(11) | NO | MUL | NULL | |
+--------------+-------------+------+-----+---------+----------------+
operating_days
+------------+---------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------+---------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| start_date | date | NO | MUL | NULL | |
| end_date | date | NO | | NULL | |
+------------+---------+------+-----+---------+----------------+
journey_operating_days
+---------------------+---------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------------------+---------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| journey_id | int(11) | NO | MUL | NULL | |
| operating_days_id | int(11) | NO | MUL | NULL | |
+---------------------+---------+------+-----+---------+----------------+
If I do a query on this database, where I select a journey
by journey.id
and join in all the related fields from operating_days
and order by operating_days.start_date
, would an index on column operating_days.start_date
be used to order them or would it be unusable for the db engine? 如果我做这个数据库,在这里我选择的查询journey
由journey.id
并从所有相关领域加入operating_days
和ORDER BY operating_days.start_date
,将在列的索引operating_days.start_date
用于排序他们还是会对于数据库引擎不可用?
This is concerning MySQL and/or PostgreSQL engines. 这涉及MySQL和/或PostgreSQL引擎。
The answer, for postgresql, is it depends . 对于postgresql,答案取决于它 。
The query optimizer is constantly worked on, and the execution plan depends on table size, selectivity of your where clauses, and many other factors. 查询优化器一直在工作,执行计划取决于表大小,where子句的选择性以及许多其他因素。
Often people find: they create an index, and it is not used in queries. 人们经常会发现:他们创建索引,但查询中不使用该索引。 Reason: a sequential scan can be cheaper for the database than accessing the index, and then the data. 原因:对于数据库而言,顺序扫描比访问索引然后访问数据便宜。
Query optimization is the subject of dedicated mailing lists. 查询优化是专用邮件列表的主题。
This is a sql related answer, not specific to any RDBMS. 这是与SQL相关的答案,并非特定于任何RDBMS。 An Index is primarely used to get data in/out of memory quickly(by storing information on the location of the data in the file system). 索引主要用于快速获取数据(通过在文件系统中存储有关数据位置的信息)将数据移入/移出内存。 Ordering (usually) takes place after all data has been pulled, so NO an index in most cases will not be used when you are ordering/sorting your data. 排序(通常)是在提取所有数据之后进行的,因此在大多数情况下,在对数据进行排序/排序时不会使用索引。 Because it already has been pulled out of the database. 因为它已经被拉出数据库了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.