Fix slow count() subqueries in webhooks startup.
Fix slow count() subqueries in webhooks startup.

SQLAlchemy by default when doing a count() on a query will issue the
count over a SELECT subquery, which, if you have a LOT of data you're
selecting, is incredibly slow. For example:

Slow query took 5402.23ms:
SELECT count(*) AS count_1 FROM
(SELECT transaction.table_name AS transaction_table_name,
transaction.record_id AS transaction_record_id, transaction.command AS transaction_command,
transaction.delta AS transaction_delta, transaction.additional_data AS transaction_additional_data,
transaction.id AS transaction_id, transaction.created_at AS transaction_created_at,
transaction.updated_at AS transaction_updated_at, transaction.deleted_at AS transaction_deleted_at,
transaction.namespace_id AS transaction_namespace_id
FROM transaction
WHERE transaction.deleted_at IS NULL AND transaction.table_name = %s AND
transaction.id > %s ORDER BY transaction.id ASC) AS anon_1 with params
('message', -1)

The way to get around this is to use func.count() on the primary key of
what you're trying to count. This works properly even with additional
filters applied.

(There's a reason for this madness, see the sidebar on
http://docs.sqlalchemy.org/en/rel_0_9/orm/tutorial.html#counting.)

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