From b2115f88be6275380ad2de305189aa6297941bcb Mon Sep 17 00:00:00 2001 From: Ethan Dalool Date: Fri, 2 Oct 2020 11:22:47 -0700 Subject: [PATCH] Add better indexes and delete redundant ones. --- utilities/database_upgrader.py | 25 +++++++++++++++++++++++-- ycdl/constants.py | 7 +++---- 2 files changed, 26 insertions(+), 6 deletions(-) diff --git a/utilities/database_upgrader.py b/utilities/database_upgrader.py index 6b79ae1..694cd63 100644 --- a/utilities/database_upgrader.py +++ b/utilities/database_upgrader.py @@ -154,10 +154,31 @@ def upgrade_6_to_7(ycdldb): ycdldb.sql.execute('DROP INDEX IF EXISTS index_video_author_download') ycdldb.sql.execute('DROP INDEX IF EXISTS index_video_download') ycdldb.sql.execute('DROP INDEX IF EXISTS index_video_download_published') - ycdldb.sql.execute('CREATE INDEX index_video_author_state on videos(author_id, state)') - ycdldb.sql.execute('CREATE INDEX index_video_state on videos(state)') + # /videos/state?orderby=published ycdldb.sql.execute('CREATE INDEX index_video_state_published on videos(state, published)') +def upgrade_7_to_8(ycdldb): + ''' + In this version, indexes were optimized by adding indexes that satisfy the + major use cases, and deleting indexes that are redundant in the presence of + another multi-column index. + ''' + # /channel?orderby=published + ycdldb.sql.execute(''' + CREATE INDEX IF NOT EXISTS index_video_author_published on videos(author_id, published); + ''') + # /channel/state?orderby=published + ycdldb.sql.execute(''' + CREATE INDEX IF NOT EXISTS index_video_author_state_published + on videos(author_id, state, published); + ''') + # Redundant due to (author, published) + ycdldb.sql.execute('DROP INDEX IF EXISTS index_video_author') + # Redundant due to (author, state, published) + ycdldb.sql.execute('DROP INDEX IF EXISTS index_video_author_state') + # Redundant due to (state, published) + ycdldb.sql.execute('DROP INDEX IF EXISTS index_video_state') + def upgrade_all(data_directory): ''' Given the directory containing a ycdl database, apply all of the diff --git a/ycdl/constants.py b/ycdl/constants.py index 1dfa023..37dc42a 100644 --- a/ycdl/constants.py +++ b/ycdl/constants.py @@ -1,6 +1,6 @@ from voussoirkit import sqlhelpers -DATABASE_VERSION = 7 +DATABASE_VERSION = 8 DB_VERSION_PRAGMA = f''' PRAGMA user_version = {DATABASE_VERSION}; ''' @@ -36,11 +36,10 @@ CREATE TABLE IF NOT EXISTS videos( ); CREATE INDEX IF NOT EXISTS index_channel_id on channels(id); -CREATE INDEX IF NOT EXISTS index_video_author on videos(author_id); -CREATE INDEX IF NOT EXISTS index_video_author_state on videos(author_id, state); +CREATE INDEX IF NOT EXISTS index_video_author_published on videos(author_id, published); +CREATE INDEX IF NOT EXISTS index_video_author_state_published on videos(author_id, state, published); CREATE INDEX IF NOT EXISTS index_video_id on videos(id); CREATE INDEX IF NOT EXISTS index_video_published on videos(published); -CREATE INDEX IF NOT EXISTS index_video_state on videos(state); CREATE INDEX IF NOT EXISTS index_video_state_published on videos(state, published); ---------------------------------------------------------------------------------------------------- COMMIT;