From 8629d509e4cb8d803fe1195c2861b98868854e51 Mon Sep 17 00:00:00 2001 From: Luke Pulverenti Date: Wed, 6 Jul 2016 01:13:26 -0400 Subject: [PATCH] speed up db upgrade --- MediaBrowser.Api/StartupWizardService.cs | 2 +- .../Persistence/CleanDatabaseScheduledTask.cs | 93 ++++++++++++------- .../Persistence/SqliteItemRepository.cs | 2 +- 3 files changed, 61 insertions(+), 36 deletions(-) diff --git a/MediaBrowser.Api/StartupWizardService.cs b/MediaBrowser.Api/StartupWizardService.cs index a59cc6909..87562b126 100644 --- a/MediaBrowser.Api/StartupWizardService.cs +++ b/MediaBrowser.Api/StartupWizardService.cs @@ -117,7 +117,7 @@ namespace MediaBrowser.Api config.EnableStandaloneMusicKeys = true; config.EnableCaseSensitiveItemIds = true; //config.EnableFolderView = true; - config.SchemaVersion = 101; + config.SchemaVersion = 107; } public void Post(UpdateStartupConfiguration request) diff --git a/MediaBrowser.Server.Implementations/Persistence/CleanDatabaseScheduledTask.cs b/MediaBrowser.Server.Implementations/Persistence/CleanDatabaseScheduledTask.cs index b11a3e496..4235f6073 100644 --- a/MediaBrowser.Server.Implementations/Persistence/CleanDatabaseScheduledTask.cs +++ b/MediaBrowser.Server.Implementations/Persistence/CleanDatabaseScheduledTask.cs @@ -142,52 +142,77 @@ namespace MediaBrowser.Server.Implementations.Persistence } } - private async Task UpdateToLatestSchema(CancellationToken cancellationToken, IProgress progress) + private Task UpdateToLatestSchema(CancellationToken cancellationToken, IProgress progress) { - var itemIds = _libraryManager.GetItemIds(new InternalItemsQuery - { - IsCurrentSchema = false, - ExcludeItemTypes = new[] { typeof(LiveTvProgram).Name } - }); + return UpdateToLatestSchema(0, 0, null, cancellationToken, progress); + } - var numComplete = 0; - var numItems = itemIds.Count; + private async Task UpdateToLatestSchema(int queryStartIndex, int progressStartIndex, int? totalRecordCount, CancellationToken cancellationToken, IProgress progress) + { + IEnumerable items; + int numItemsToSave; + var pageSize = 2000; + + if (totalRecordCount.HasValue) + { + var list = _libraryManager.GetItemList(new InternalItemsQuery + { + IsCurrentSchema = false, + ExcludeItemTypes = new[] { typeof(LiveTvProgram).Name }, + StartIndex = queryStartIndex, + Limit = pageSize + + }).ToList(); + + items = list; + numItemsToSave = list.Count; + } + else + { + var itemsResult = _libraryManager.GetItemsResult(new InternalItemsQuery + { + IsCurrentSchema = false, + ExcludeItemTypes = new[] { typeof(LiveTvProgram).Name }, + StartIndex = queryStartIndex, + Limit = pageSize + }); + + totalRecordCount = itemsResult.TotalRecordCount; + items = itemsResult.Items; + numItemsToSave = itemsResult.Items.Length; + } + + var numItems = totalRecordCount.Value; _logger.Debug("Upgrading schema for {0} items", numItems); - foreach (var itemId in itemIds) + if (numItemsToSave > 0) { - cancellationToken.ThrowIfCancellationRequested(); - - if (itemId != Guid.Empty) + try { - // Somehow some invalid data got into the db. It probably predates the boundary checking - var item = _libraryManager.GetItemById(itemId); - - if (item != null) - { - try - { - await _itemRepo.SaveItem(item, cancellationToken).ConfigureAwait(false); - } - catch (OperationCanceledException) - { - throw; - } - catch (Exception ex) - { - _logger.ErrorException("Error saving item", ex); - } - } + await _itemRepo.SaveItems(items, cancellationToken).ConfigureAwait(false); + } + catch (OperationCanceledException) + { + throw; + } + catch (Exception ex) + { + _logger.ErrorException("Error saving item", ex); } - numComplete++; - double percent = numComplete; + progressStartIndex += pageSize; + double percent = progressStartIndex; percent /= numItems; progress.Report(percent * 100); - } - progress.Report(100); + var newStartIndex = queryStartIndex + (pageSize - numItemsToSave); + await UpdateToLatestSchema(newStartIndex, progressStartIndex, totalRecordCount, cancellationToken, progress).ConfigureAwait(false); + } + else + { + progress.Report(100); + } } private async Task CleanDeadItems(CancellationToken cancellationToken, IProgress progress) diff --git a/MediaBrowser.Server.Implementations/Persistence/SqliteItemRepository.cs b/MediaBrowser.Server.Implementations/Persistence/SqliteItemRepository.cs index 4a70ddc2e..31388661b 100644 --- a/MediaBrowser.Server.Implementations/Persistence/SqliteItemRepository.cs +++ b/MediaBrowser.Server.Implementations/Persistence/SqliteItemRepository.cs @@ -95,7 +95,7 @@ namespace MediaBrowser.Server.Implementations.Persistence private IDbCommand _updateInheritedRatingCommand; private IDbCommand _updateInheritedTagsCommand; - public const int LatestSchemaVersion = 101; + public const int LatestSchemaVersion = 107; /// /// Initializes a new instance of the class.