Question

Say I have a database with 3 tables describing bus timetables:

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?

This is concerning MySQL and/or PostgreSQL engines.

Was it helpful?

Solution

The answer, for postgresql, is it depends.

The query optimizer is constantly worked on, and the execution plan depends on table size, selectivity of your where clauses, and many other factors.

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.

OTHER TIPS

This is a sql related answer, not specific to any 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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top