Fix performance regression in delta sync.
Fix performance regression in delta sync.

Summary:
Perhaps an explanation is in order.

Recall that InnoDB automatically creates indexes on foreign keys. So let's say
you start with a `transaction` table that looks like this (some fields omitted
for simplicity):
```
| transaction | CREATE TABLE `transaction` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `namespace_id` int(11) NOT NULL,
  `deleted_at` datetime DEFAULT NULL,
  `created_at` datetime NOT NULL,
  `object_type` varchar(20) NOT NULL,
  `record_id` int(11) NOT NULL,
  `command` enum('insert','update','delete') NOT NULL,
  PRIMARY KEY (`id`),
  KEY `namespace_id` (`namespace_id`),
  CONSTRAINT `transaction_ibfk_1` FOREIGN KEY (`namespace_id`) REFERENCES `namespace` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 |
```

Now you want to get the transaction rows for a given `namespace_id` starting at
a given point. So basically
```
mysql> SELECT * FROM transaction WHERE namespace_id=22 AND id>3141
    -> ORDER BY id ASC LIMIT 100;
```
No problem! Secondary indices in an InnoDB table actually have the primary key
values concatenated to them, so the `namespace_id` index can be used to
efficiently satisfy the ORDER BY. See
http://www.psce.com/blog/2012/05/17/can-mysql-use-primary-key-values-from-a-secondary-index/
for more.

Okay, now let's say you identify another query that's slow, and fix it by
adding a multi-column index:
```
mysql> ALTER TABLE transaction ADD INDEX `namespace_id_created_at`
    -> (`namespace_id, created_at`);
```

The query is fast, and you move on with your life.
Unfortunately, when you do this, InnoDB just decides that you don't need that
foreign key index on `namespace_id` any more. So your table looks like this:
```
| transaction | CREATE TABLE `transaction` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `namespace_id` int(11) NOT NULL,
  `deleted_at` datetime DEFAULT NULL,
  `created_at` datetime NOT NULL,
  `object_type` varchar(20) NOT NULL,
  `record_id` int(11) NOT NULL,
  `command` enum('insert','update','delete') NOT NULL,
  PRIMARY KEY (`id`),
  KEY `ix_transaction_namespace_id_created_at` (`namespace_id`, `created_at`),
  CONSTRAINT `transaction_ibfk_1` FOREIGN KEY (`namespace_id`) REFERENCES `namespace` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 |
```

This means that your original query can no longer be satisfied with an index,
and can become DEADLY SLOW.

We could fix this by explicitly re-adding an index on `namespace_id`. But in
our case, we actually already have an index
```
`namespace_id_deleted_at` (`namespace_id`, `deleted_at`)`
```
from the days of soft-deletion, so it's more expedient to trivially change the
query to
```
mysql> SELECT * FROM transaction USE INDEX (`namespace_id_deleted_at`)
    -> WHERE namespace_id=22 AND id>3141 AND
    -> deleted_at IS NULL ORDER BY id ASC LIMIT 100;
```
Because `deleted_at` is explicitly constant here, and because MySQL needs to
die in a fire, this is again fast.

Test Plan: Unit tests; experimentation.

Reviewers: mg

Reviewed By: mg

Subscribers: kav-ya, spang

Differential Revision: https://review.inboxapp.com/D1212

ErinCall/sync-engine,PriviPK/privipk-sync-engine,Eagles2F/sync-engine,jobscore/sync-engine,EthanBlackburn/sync-engine,EthanBlackburn/sync-engine,ErinCall/sync-engine,wakermahmud/sync-engine,EthanBlackburn/sync-engine,ErinCall/sync-engine,closeio/nylas,wakermahmud/sync-engine,gale320/sync-engine,nylas/sync-engine,ErinCall/sync-engine,Eagles2F/sync-engine,nylas/sync-engine,wakermahmud/sync-engine,PriviPK/privipk-sync-engine,closeio/nylas,gale320/sync-engine,gale320/sync-engine,Eagles2F/sync-engine,PriviPK/privipk-sync-engine,jobscore/sync-engine,wakermahmud/sync-engine,closeio/nylas,wakermahmud/sync-engine,EthanBlackburn/sync-engine,closeio/nylas,Eagles2F/sync-engine,gale320/sync-engine,gale320/sync-engine,nylas/sync-engine,ErinCall/sync-engine,nylas/sync-engine,Eagles2F/sync-engine,PriviPK/privipk-sync-engine,PriviPK/privipk-sync-engine,EthanBlackburn/sync-engine,jobscore/sync-engine,jobscore/sync-engine