From 3fcbaeae06f5011e407968d6c4ccb6b468a88ffb Mon Sep 17 00:00:00 2001 From: bhiller Date: Sat, 24 Oct 2020 22:24:49 -0700 Subject: [PATCH] add index to speed up filter-by-candidate queries --- migrations/20201024221925_vote_index.js | 17 +++++++++++++++++ scripts/import_data.js | 9 ++++++++- 2 files changed, 25 insertions(+), 1 deletion(-) create mode 100644 migrations/20201024221925_vote_index.js diff --git a/migrations/20201024221925_vote_index.js b/migrations/20201024221925_vote_index.js new file mode 100644 index 0000000..952f62d --- /dev/null +++ b/migrations/20201024221925_vote_index.js @@ -0,0 +1,17 @@ +exports.up = function (knex) { + return knex.schema.table('vote', function (table) { + table.index( + ['election_id', 'tabulator_id', 'batch_id', 'record_id', 'contest_id'], + 'vote_distinct_ballot_index', + ); + }); +}; + +exports.down = function (knex) { + return knex.schema.table('vote', function (table) { + table.dropIndex( + ['election_id', 'tabulator_id', 'batch_id', 'record_id', 'contest_id'], + 'vote_distinct_ballot_index', + ); + }); +}; diff --git a/scripts/import_data.js b/scripts/import_data.js index 9d873a2..c27e0b4 100644 --- a/scripts/import_data.js +++ b/scripts/import_data.js @@ -222,6 +222,7 @@ const importVotes = async ( 'precinct_portion_id', 'contest_id', 'party_id', + 'distinct_ballot', ]; for (const idx of indexes) { try { @@ -324,7 +325,13 @@ const importVotes = async ( } for (const idx of indexes) { - await knex.raw(`CREATE INDEX vote_${idx}_index ON vote (${idx})`); + if (idx === 'distinct_ballot') { + await knex.raw( + `CREATE INDEX vote_${idx}_index ON vote (election_id, tabulator_id, batch_id, record_id, contest_id)`, + ); + } else { + await knex.raw(`CREATE INDEX vote_${idx}_index ON vote (${idx})`); + } } await knex.raw('ANALYZE');