Add better indexes and delete redundant ones.
This commit is contained in:
		
							parent
							
								
									7910e12bc8
								
							
						
					
					
						commit
						b2115f88be
					
				
					 2 changed files with 26 additions and 6 deletions
				
			
		|  | @ -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_author_download') | ||||||
|     ycdldb.sql.execute('DROP INDEX IF EXISTS index_video_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('DROP INDEX IF EXISTS index_video_download_published') | ||||||
|     ycdldb.sql.execute('CREATE INDEX index_video_author_state on videos(author_id, state)') |     # /videos/state?orderby=published | ||||||
|     ycdldb.sql.execute('CREATE INDEX index_video_state on videos(state)') |  | ||||||
|     ycdldb.sql.execute('CREATE INDEX index_video_state_published on videos(state, 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): | def upgrade_all(data_directory): | ||||||
|     ''' |     ''' | ||||||
|     Given the directory containing a ycdl database, apply all of the |     Given the directory containing a ycdl database, apply all of the | ||||||
|  |  | ||||||
|  | @ -1,6 +1,6 @@ | ||||||
| from voussoirkit import sqlhelpers | from voussoirkit import sqlhelpers | ||||||
| 
 | 
 | ||||||
| DATABASE_VERSION = 7 | DATABASE_VERSION = 8 | ||||||
| DB_VERSION_PRAGMA = f''' | DB_VERSION_PRAGMA = f''' | ||||||
| PRAGMA user_version = {DATABASE_VERSION}; | 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_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_published on videos(author_id, published); | ||||||
| CREATE INDEX IF NOT EXISTS index_video_author_state on videos(author_id, state); | 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_id on videos(id); | ||||||
| CREATE INDEX IF NOT EXISTS index_video_published on videos(published); | 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); | CREATE INDEX IF NOT EXISTS index_video_state_published on videos(state, published); | ||||||
| ---------------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------- | ||||||
| COMMIT; | COMMIT; | ||||||
|  |  | ||||||
		Loading…
	
		Reference in a new issue