【发布时间】:2014-04-02 17:01:57
【问题描述】:
假设我有一个数据库,其中包含 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 | |
+---------------------+---------+------+-----+---------+----------------+
如果我对这个数据库进行查询,我选择journey by journey.id 并加入来自operating_days 的所有相关字段并按operating_days.start_date 排序,那么列operating_days.start_date 上的索引会是用于订购它们还是无法用于 db 引擎?
这与 MySQL 和/或 PostgreSQL 引擎有关。
【问题讨论】:
-
很难说,因为这取决于;哪个 RDBMS(MySQL 与 PostgreSQL),每个的 version,每个表中有多少行,实际查询是什么(连接条件,
WHERE子句中的条件,@ 中的数据987654328@...),系统上正在运行哪些其他查询,一些物理存储现实...一般来说,索引开始以operating_days.start_date(然后是其中之一ids) 对某些查询很有用。
标签: mysql sql postgresql indexing many-to-many