Optimize Indices

This commit is contained in:
Shadowghost
2026-02-07 00:56:38 +01:00
parent 46ad25f47d
commit 8ddc35a1ce
9 changed files with 1984 additions and 38 deletions

View File

@@ -37,11 +37,8 @@ public class BaseItemConfiguration : IEntityTypeConfiguration<BaseItemEntity>
builder.HasIndex(e => e.ParentId);
builder.HasIndex(e => e.OwnerId);
builder.HasIndex(e => e.Name);
builder.HasIndex(e => e.ExtraType);
builder.HasIndex(e => new { e.ExtraType, e.OwnerId });
builder.HasIndex(e => e.PresentationUniqueKey);
builder.HasIndex(e => new { e.Id, e.Type, e.IsFolder, e.IsVirtualItem });
// covering index
builder.HasIndex(e => new { e.TopParentId, e.Id });
// series
@@ -64,6 +61,15 @@ public class BaseItemConfiguration : IEntityTypeConfiguration<BaseItemEntity>
builder.HasIndex(e => new { e.TopParentId, e.MediaType, e.IsVirtualItem, e.DateCreated });
// resume
builder.HasIndex(e => new { e.MediaType, e.TopParentId, e.IsVirtualItem, e.PresentationUniqueKey });
// sorted library queries (e.g., Series sorted by SortName)
builder.HasIndex(e => new { e.Type, e.TopParentId, e.SortName });
// NextUp: per-series episode ordering (index seek + range scan on season/episode)
builder.HasIndex(e => new { e.Type, e.SeriesPresentationUniqueKey, e.ParentIndexNumber, e.IndexNumber });
// Latest TV: GROUP BY SeriesName
builder.HasIndex(e => e.SeriesName);
// Latest TV: episode count per season, season count per series
builder.HasIndex(e => e.SeasonId);
builder.HasIndex(e => e.SeriesId);
builder.HasData(new BaseItemEntity()
{

View File

@@ -15,10 +15,7 @@ public class BaseItemImageInfoConfiguration : IEntityTypeConfiguration<BaseItemI
builder.HasKey(e => e.Id);
builder.HasOne(e => e.Item).WithMany(e => e.Images).HasForeignKey(e => e.ItemId);
// Index for efficient lookups and deletes by ItemId
builder.HasIndex(e => e.ItemId);
// Composite index for filtering by item and image type
// Composite index for filtering by item and image type (also covers ItemId-only lookups)
builder.HasIndex(e => new { e.ItemId, e.ImageType });
}
}

View File

@@ -14,6 +14,6 @@ public class BaseItemProviderConfiguration : IEntityTypeConfiguration<BaseItemPr
{
builder.HasKey(e => new { e.ItemId, e.ProviderId });
builder.HasOne(e => e.Item);
builder.HasIndex(e => new { e.ProviderId, e.ProviderValue, e.ItemId });
builder.HasIndex(e => new { e.ProviderId, e.ItemId, e.ProviderValue });
}
}

View File

@@ -20,9 +20,6 @@ namespace Jellyfin.Database.Implementations.ModelConfiguration
builder
.HasIndex(entity => new { entity.UserId, entity.DeviceId });
builder
.HasIndex(entity => entity.DeviceId);
}
}
}

View File

@@ -14,8 +14,6 @@ public class LinkedChildConfiguration : IEntityTypeConfiguration<LinkedChildEnti
{
builder.ToTable("LinkedChildren");
builder.HasKey(e => new { e.ParentId, e.ChildId });
builder.HasIndex(e => e.ParentId);
builder.HasIndex(e => e.ChildId);
builder.HasIndex(e => new { e.ParentId, e.SortOrder });
builder.HasIndex(e => new { e.ParentId, e.ChildType });
builder.HasIndex(e => new { e.ChildId, e.ChildType });

View File

@@ -13,9 +13,5 @@ public class MediaStreamInfoConfiguration : IEntityTypeConfiguration<MediaStream
public void Configure(EntityTypeBuilder<MediaStreamInfo> builder)
{
builder.HasKey(e => new { e.ItemId, e.StreamIndex });
builder.HasIndex(e => e.StreamIndex);
builder.HasIndex(e => e.StreamType);
builder.HasIndex(e => new { e.StreamIndex, e.StreamType });
builder.HasIndex(e => new { e.StreamIndex, e.StreamType, e.Language });
}
}

View File

@@ -0,0 +1,171 @@
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace Jellyfin.Database.Providers.Sqlite.Migrations
{
/// <inheritdoc />
public partial class IndexOptimizations : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropIndex(
name: "IX_MediaStreamInfos_StreamIndex",
table: "MediaStreamInfos");
migrationBuilder.DropIndex(
name: "IX_MediaStreamInfos_StreamIndex_StreamType",
table: "MediaStreamInfos");
migrationBuilder.DropIndex(
name: "IX_MediaStreamInfos_StreamIndex_StreamType_Language",
table: "MediaStreamInfos");
migrationBuilder.DropIndex(
name: "IX_MediaStreamInfos_StreamType",
table: "MediaStreamInfos");
migrationBuilder.DropIndex(
name: "IX_LinkedChildren_ChildId",
table: "LinkedChildren");
migrationBuilder.DropIndex(
name: "IX_LinkedChildren_ParentId",
table: "LinkedChildren");
migrationBuilder.DropIndex(
name: "IX_Devices_DeviceId",
table: "Devices");
migrationBuilder.DropIndex(
name: "IX_BaseItems_ExtraType",
table: "BaseItems");
migrationBuilder.DropIndex(
name: "IX_BaseItems_Id_Type_IsFolder_IsVirtualItem",
table: "BaseItems");
migrationBuilder.DropIndex(
name: "IX_BaseItemProviders_ProviderId_ProviderValue_ItemId",
table: "BaseItemProviders");
migrationBuilder.DropIndex(
name: "IX_BaseItemImageInfos_ItemId",
table: "BaseItemImageInfos");
migrationBuilder.CreateIndex(
name: "IX_BaseItems_SeasonId",
table: "BaseItems",
column: "SeasonId");
migrationBuilder.CreateIndex(
name: "IX_BaseItems_SeriesId",
table: "BaseItems",
column: "SeriesId");
migrationBuilder.CreateIndex(
name: "IX_BaseItems_SeriesName",
table: "BaseItems",
column: "SeriesName");
migrationBuilder.CreateIndex(
name: "IX_BaseItems_Type_SeriesPresentationUniqueKey_ParentIndexNumber_IndexNumber",
table: "BaseItems",
columns: new[] { "Type", "SeriesPresentationUniqueKey", "ParentIndexNumber", "IndexNumber" });
migrationBuilder.CreateIndex(
name: "IX_BaseItems_Type_TopParentId_SortName",
table: "BaseItems",
columns: new[] { "Type", "TopParentId", "SortName" });
migrationBuilder.CreateIndex(
name: "IX_BaseItemProviders_ProviderId_ItemId_ProviderValue",
table: "BaseItemProviders",
columns: new[] { "ProviderId", "ItemId", "ProviderValue" });
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropIndex(
name: "IX_BaseItems_SeasonId",
table: "BaseItems");
migrationBuilder.DropIndex(
name: "IX_BaseItems_SeriesId",
table: "BaseItems");
migrationBuilder.DropIndex(
name: "IX_BaseItems_SeriesName",
table: "BaseItems");
migrationBuilder.DropIndex(
name: "IX_BaseItems_Type_SeriesPresentationUniqueKey_ParentIndexNumber_IndexNumber",
table: "BaseItems");
migrationBuilder.DropIndex(
name: "IX_BaseItems_Type_TopParentId_SortName",
table: "BaseItems");
migrationBuilder.DropIndex(
name: "IX_BaseItemProviders_ProviderId_ItemId_ProviderValue",
table: "BaseItemProviders");
migrationBuilder.CreateIndex(
name: "IX_MediaStreamInfos_StreamIndex",
table: "MediaStreamInfos",
column: "StreamIndex");
migrationBuilder.CreateIndex(
name: "IX_MediaStreamInfos_StreamIndex_StreamType",
table: "MediaStreamInfos",
columns: new[] { "StreamIndex", "StreamType" });
migrationBuilder.CreateIndex(
name: "IX_MediaStreamInfos_StreamIndex_StreamType_Language",
table: "MediaStreamInfos",
columns: new[] { "StreamIndex", "StreamType", "Language" });
migrationBuilder.CreateIndex(
name: "IX_MediaStreamInfos_StreamType",
table: "MediaStreamInfos",
column: "StreamType");
migrationBuilder.CreateIndex(
name: "IX_LinkedChildren_ChildId",
table: "LinkedChildren",
column: "ChildId");
migrationBuilder.CreateIndex(
name: "IX_LinkedChildren_ParentId",
table: "LinkedChildren",
column: "ParentId");
migrationBuilder.CreateIndex(
name: "IX_Devices_DeviceId",
table: "Devices",
column: "DeviceId");
migrationBuilder.CreateIndex(
name: "IX_BaseItems_ExtraType",
table: "BaseItems",
column: "ExtraType");
migrationBuilder.CreateIndex(
name: "IX_BaseItems_Id_Type_IsFolder_IsVirtualItem",
table: "BaseItems",
columns: new[] { "Id", "Type", "IsFolder", "IsVirtualItem" });
migrationBuilder.CreateIndex(
name: "IX_BaseItemProviders_ProviderId_ProviderValue_ItemId",
table: "BaseItemProviders",
columns: new[] { "ProviderId", "ProviderValue", "ItemId" });
migrationBuilder.CreateIndex(
name: "IX_BaseItemImageInfos_ItemId",
table: "BaseItemImageInfos",
column: "ItemId");
}
}
}

View File

@@ -360,8 +360,6 @@ namespace Jellyfin.Server.Implementations.Migrations
b.HasKey("Id");
b.HasIndex("ExtraType");
b.HasIndex("Name");
b.HasIndex("OwnerId");
@@ -372,6 +370,12 @@ namespace Jellyfin.Server.Implementations.Migrations
b.HasIndex("PresentationUniqueKey");
b.HasIndex("SeasonId");
b.HasIndex("SeriesId");
b.HasIndex("SeriesName");
b.HasIndex("ExtraType", "OwnerId");
b.HasIndex("TopParentId", "Id");
@@ -380,9 +384,9 @@ namespace Jellyfin.Server.Implementations.Migrations
b.HasIndex("Type", "TopParentId", "PresentationUniqueKey");
b.HasIndex("Type", "TopParentId", "StartDate");
b.HasIndex("Type", "TopParentId", "SortName");
b.HasIndex("Id", "Type", "IsFolder", "IsVirtualItem");
b.HasIndex("Type", "TopParentId", "StartDate");
b.HasIndex("MediaType", "TopParentId", "IsVirtualItem", "PresentationUniqueKey");
@@ -394,6 +398,8 @@ namespace Jellyfin.Server.Implementations.Migrations
b.HasIndex("Type", "SeriesPresentationUniqueKey", "IsFolder", "IsVirtualItem");
b.HasIndex("Type", "SeriesPresentationUniqueKey", "ParentIndexNumber", "IndexNumber");
b.HasIndex("Type", "SeriesPresentationUniqueKey", "PresentationUniqueKey", "SortName");
b.HasIndex("IsFolder", "TopParentId", "IsVirtualItem", "PresentationUniqueKey", "DateCreated");
@@ -450,8 +456,6 @@ namespace Jellyfin.Server.Implementations.Migrations
b.HasKey("Id");
b.HasIndex("ItemId");
b.HasIndex("ItemId", "ImageType");
b.ToTable("BaseItemImageInfos");
@@ -490,7 +494,7 @@ namespace Jellyfin.Server.Implementations.Migrations
b.HasKey("ItemId", "ProviderId");
b.HasIndex("ProviderId", "ProviderValue", "ItemId");
b.HasIndex("ProviderId", "ItemId", "ProviderValue");
b.ToTable("BaseItemProviders");
@@ -811,10 +815,6 @@ namespace Jellyfin.Server.Implementations.Migrations
b.HasKey("ParentId", "ChildId");
b.HasIndex("ChildId");
b.HasIndex("ParentId");
b.HasIndex("ChildId", "ChildType");
b.HasIndex("ParentId", "ChildType");
@@ -1000,14 +1000,6 @@ namespace Jellyfin.Server.Implementations.Migrations
b.HasKey("ItemId", "StreamIndex");
b.HasIndex("StreamIndex");
b.HasIndex("StreamType");
b.HasIndex("StreamIndex", "StreamType");
b.HasIndex("StreamIndex", "StreamType", "Language");
b.ToTable("MediaStreamInfos");
b.HasAnnotation("Sqlite:UseSqlReturningClause", false);
@@ -1211,8 +1203,6 @@ namespace Jellyfin.Server.Implementations.Migrations
b.HasKey("Id");
b.HasIndex("DeviceId");
b.HasIndex("AccessToken", "DateLastActivity");
b.HasIndex("DeviceId", "DateLastActivity");