alphagov/release — pull request #51 by alext (2014-02-26 10:55:04+00:00)
Title: Moar index speedup.

Add a db index to the deployments table. The index view results in lots of queries of the deployments table which currently doesn't have any indices.

Prior to the index:
```
mysql> explain SELECT `deployments`.* FROM `deployments` WHERE `deployments`.`application_id` = 7 AND `deployments`.`environment` = 'staging' ORDER BY created_at DESC LIMIT 1\G
*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: deployments
type: ALL
possible_keys: NULL
key: NULL
key_len: NULL
ref: NULL
rows: 16742
Extra: Using where; Using filesort
1 row in set (0.00 sec)
```

With the index:
```
mysql> explain SELECT `deployments`.* FROM `deployments` WHERE `deployments`.`application_id` = 7 AND `deployments`.`environment` = 'staging' ORDER BY created_at DESC LIMIT 1\G
*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: deployments
type: ref
possible_keys: index_deployments_on_application_id_etc
key: index_deployments_on_application_id_etc
key_len: 263
ref: const,const
rows: 21
Extra: Using where
1 row in set (0.01 sec)
```