From b7443f6042e8c641be969abafad7202459d2b413 Mon Sep 17 00:00:00 2001 From: crobibero Date: Thu, 3 Dec 2020 20:16:11 -0700 Subject: [PATCH 1/2] Optimize FavoritePersons query --- Emby.Server.Implementations/Data/SqliteItemRepository.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Emby.Server.Implementations/Data/SqliteItemRepository.cs b/Emby.Server.Implementations/Data/SqliteItemRepository.cs index 50c7a07d4..f7a6708c9 100644 --- a/Emby.Server.Implementations/Data/SqliteItemRepository.cs +++ b/Emby.Server.Implementations/Data/SqliteItemRepository.cs @@ -5039,9 +5039,9 @@ where AncestorIdText not null and ItemValues.Value not null and ItemValues.Type if (query.User != null && query.IsFavorite.HasValue) { - commandText.Append(" LEFT JOIN TypedBaseItems tbi ON tbi.Name=p.Name AND tbi.Type='"); + commandText.Append(" INNER JOIN TypedBaseItems tbi ON tbi.Name=p.Name AND tbi.Type='"); commandText.Append(typeof(Person).FullName); - commandText.Append("' LEFT JOIN UserDatas ON tbi.UserDataKey=key AND userId=@UserId"); + commandText.Append("' INNER JOIN UserDatas ON tbi.UserDataKey=key AND userId=@UserId"); } var whereClauses = GetPeopleWhereClauses(query, null); From 60b7e49a7f62e5f1ab06e3c85665f900235f2ff5 Mon Sep 17 00:00:00 2001 From: crobibero Date: Fri, 4 Dec 2020 08:00:55 -0700 Subject: [PATCH 2/2] Suggestions from code review --- .../Data/SqliteItemRepository.cs | 23 ++++----- Jellyfin.Server/Migrations/MigrationRunner.cs | 3 +- .../Routines/AddPeopleQueryIndex.cs | 49 +++++++++++++++++++ 3 files changed, 61 insertions(+), 14 deletions(-) create mode 100644 Jellyfin.Server/Migrations/Routines/AddPeopleQueryIndex.cs diff --git a/Emby.Server.Implementations/Data/SqliteItemRepository.cs b/Emby.Server.Implementations/Data/SqliteItemRepository.cs index f7a6708c9..6e1f2feae 100644 --- a/Emby.Server.Implementations/Data/SqliteItemRepository.cs +++ b/Emby.Server.Implementations/Data/SqliteItemRepository.cs @@ -5037,13 +5037,6 @@ where AncestorIdText not null and ItemValues.Value not null and ItemValues.Type var commandText = new StringBuilder("select Distinct p.Name from People p"); - if (query.User != null && query.IsFavorite.HasValue) - { - commandText.Append(" INNER JOIN TypedBaseItems tbi ON tbi.Name=p.Name AND tbi.Type='"); - commandText.Append(typeof(Person).FullName); - commandText.Append("' INNER JOIN UserDatas ON tbi.UserDataKey=key AND userId=@UserId"); - } - var whereClauses = GetPeopleWhereClauses(query, null); if (whereClauses.Count != 0) @@ -5124,6 +5117,16 @@ where AncestorIdText not null and ItemValues.Value not null and ItemValues.Type { var whereClauses = new List(); + if (query.User != null && query.IsFavorite.HasValue) + { + whereClauses.Add(@"p.Name IN ( +SELECT Name FROM TypedBaseItems WHERE UserDataKey IN ( +SELECT key FROM UserDatas WHERE isFavorite=@IsFavorite AND userId=@UserId) +AND Type = @InternalPersonType)"); + statement?.TryBind("@IsFavorite", query.IsFavorite.Value); + statement?.TryBind("@InternalPersonType", typeof(Person).FullName); + } + if (!query.ItemId.Equals(Guid.Empty)) { whereClauses.Add("ItemId=@ItemId"); @@ -5176,12 +5179,6 @@ where AncestorIdText not null and ItemValues.Value not null and ItemValues.Type statement?.TryBind("@NameContains", "%" + query.NameContains + "%"); } - if (query.IsFavorite.HasValue) - { - whereClauses.Add("isFavorite=@IsFavorite"); - statement?.TryBind("@IsFavorite", query.IsFavorite.Value); - } - if (query.User != null) { statement?.TryBind("@UserId", query.User.InternalId); diff --git a/Jellyfin.Server/Migrations/MigrationRunner.cs b/Jellyfin.Server/Migrations/MigrationRunner.cs index aca165408..305660ae6 100644 --- a/Jellyfin.Server/Migrations/MigrationRunner.cs +++ b/Jellyfin.Server/Migrations/MigrationRunner.cs @@ -24,7 +24,8 @@ namespace Jellyfin.Server.Migrations typeof(Routines.MigrateUserDb), typeof(Routines.ReaddDefaultPluginRepository), typeof(Routines.MigrateDisplayPreferencesDb), - typeof(Routines.RemoveDownloadImagesInAdvance) + typeof(Routines.RemoveDownloadImagesInAdvance), + typeof(Routines.AddPeopleQueryIndex) }; /// diff --git a/Jellyfin.Server/Migrations/Routines/AddPeopleQueryIndex.cs b/Jellyfin.Server/Migrations/Routines/AddPeopleQueryIndex.cs new file mode 100644 index 000000000..2521d9952 --- /dev/null +++ b/Jellyfin.Server/Migrations/Routines/AddPeopleQueryIndex.cs @@ -0,0 +1,49 @@ +using System; +using System.IO; +using MediaBrowser.Controller; +using Microsoft.Extensions.Logging; +using SQLitePCL.pretty; + +namespace Jellyfin.Server.Migrations.Routines +{ + /// + /// Migration to add table indexes to optimize the Persons query. + /// + public class AddPeopleQueryIndex : IMigrationRoutine + { + private const string DbFilename = "library.db"; + private readonly ILogger _logger; + private readonly IServerApplicationPaths _serverApplicationPaths; + + /// + /// Initializes a new instance of the class. + /// + /// Instance of the interface. + /// Instance of the interface. + public AddPeopleQueryIndex(ILogger logger, IServerApplicationPaths serverApplicationPaths) + { + _logger = logger; + _serverApplicationPaths = serverApplicationPaths; + } + + /// + public Guid Id => new Guid("DE009B59-BAAE-428D-A810-F67762DC05B8"); + + /// + public string Name => "AddPeopleQueryIndex"; + + /// + public bool PerformOnNewInstall => true; + + /// + public void Perform() + { + var databasePath = Path.Join(_serverApplicationPaths.DataPath, DbFilename); + using var connection = SQLite3.Open(databasePath, ConnectionFlags.ReadWrite, null); + _logger.LogInformation("Creating index idx_TypedBaseItemsUserDataKeyType"); + connection.Execute("CREATE INDEX idx_TypedBaseItemsUserDataKeyType ON TypedBaseItems(UserDataKey, Type);"); + _logger.LogInformation("Creating index idx_PeopleNameListOrder"); + connection.Execute("CREATE INDEX idx_PeopleNameListOrder ON People(Name, ListOrder);"); + } + } +} \ No newline at end of file