Skip to content

Commit

Permalink
add index to speed up filter-by-candidate queries
Browse files Browse the repository at this point in the history
  • Loading branch information
benhiller committed Oct 25, 2020
1 parent b8202b3 commit 3fcbaea
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
17 changes: 17 additions & 0 deletions migrations/20201024221925_vote_index.js
Original file line number Diff line number Diff line change
@@ -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',
);
});
};
9 changes: 8 additions & 1 deletion scripts/import_data.js
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,7 @@ const importVotes = async (
'precinct_portion_id',
'contest_id',
'party_id',
'distinct_ballot',
];
for (const idx of indexes) {
try {
Expand Down Expand Up @@ -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');
Expand Down

0 comments on commit 3fcbaea

Please sign in to comment.