Issue-12704: added new necessary indexes.

This commit is contained in:
Mykyta Zakharov
2023-05-18 14:28:00 +03:00
committed by GitHub
parent 9640616b56
commit 4b62cb59b7
14 changed files with 114 additions and 9 deletions

View File

@@ -2,6 +2,7 @@ using System.Collections.Generic;
using System.Linq;
using Umbraco.Cms.Infrastructure.Persistence;
using Umbraco.Cms.Infrastructure.Persistence.DatabaseModelDefinitions;
using Umbraco.Cms.Infrastructure.Migrations.Expressions.Execute.Expressions;
using Umbraco.Cms.Infrastructure.Persistence.SqlSyntax;
using Umbraco.Extensions;
@@ -110,6 +111,25 @@ namespace Umbraco.Cms.Infrastructure.Migrations
return indexes.Any(x => x.Item2.InvariantEquals(indexName));
}
protected void CreateIndex<T>(string toCreate)
{
TableDefinition tableDef = DefinitionFactory.GetTableDefinition(typeof(T), Context.SqlContext.SqlSyntax);
IndexDefinition index = tableDef.Indexes.First(x => x.Name == toCreate);
new ExecuteSqlStatementExpression(Context) { SqlStatement = Context.SqlContext.SqlSyntax.Format(index) }
.Execute();
}
protected void DeleteIndex<T>(string toDelete)
{
if (!IndexExists(toDelete))
{
return;
}
TableDefinition tableDef = DefinitionFactory.GetTableDefinition(typeof(T), Context.SqlContext.SqlSyntax);
Delete.Index(toDelete).OnTable(tableDef.Name).Do();
}
protected bool PrimaryKeyExists(string tableName, string primaryKeyName)
{
return SqlSyntax.DoesPrimaryKeyExist(Context.Database, tableName, primaryKeyName);

View File

@@ -77,5 +77,8 @@ public class UmbracoPlan : MigrationPlan
// To 12.0.0
To<V_12_0_0.UseNvarcharInsteadOfNText>("{888A0D5D-51E4-4C7E-AA0A-01306523C7FB}");
To<V_12_0_0.ResetCache>("{539F2F83-FBA7-4C48-81A3-75081A56BB9D}");
// To 12.1.0
To<V_12_1_0.TablesIndexesImprovement>("{1187192D-EDB5-4619-955D-91D48D738871}");
}
}

View File

@@ -0,0 +1,65 @@
using Umbraco.Cms.Infrastructure.Persistence.Dtos;
namespace Umbraco.Cms.Infrastructure.Migrations.Upgrade.V_12_1_0;
public class TablesIndexesImprovement : MigrationBase
{
public TablesIndexesImprovement(IMigrationContext context) : base(context)
{
}
protected override void Migrate()
{
var nodeDtoTrashedIndex = $"IX_{NodeDto.TableName}_ObjectType_trashed_sorted";
DeleteIndex<NodeDto>(nodeDtoTrashedIndex);
CreateIndex<NodeDto>(nodeDtoTrashedIndex);
var redirectUrlCreateDateUtcIndex = $"IX_{RedirectUrlDto.TableName}_culture_hash";
DeleteIndex<RedirectUrlDto>(redirectUrlCreateDateUtcIndex);
CreateIndex<RedirectUrlDto>(redirectUrlCreateDateUtcIndex);
var contentVersionCultureVariationVersionIdIndex = $"IX_{ContentVersionCultureVariationDto.TableName}_VersionId";
DeleteIndex<ContentVersionCultureVariationDto>(contentVersionCultureVariationVersionIdIndex);
CreateIndex<ContentVersionCultureVariationDto>(contentVersionCultureVariationVersionIdIndex);
var contentVersionDtoNodeIdV2Index = $"IX_{ContentVersionDto.TableName}_NodeId";
DeleteIndex<ContentVersionDto>(contentVersionDtoNodeIdV2Index);
CreateIndex<ContentVersionDto>(contentVersionDtoNodeIdV2Index);
var tagRelationshipDtoTagNodeIndex = $"IX_{TagRelationshipDto.TableName}_tagId_nodeId";
DeleteIndex<TagRelationshipDto>(tagRelationshipDtoTagNodeIndex);
CreateIndex<TagRelationshipDto>(tagRelationshipDtoTagNodeIndex);
var tagDtoLanguageGroupIndex = $"IX_{TagDto.TableName}_languageId_group";
DeleteIndex<TagDto>(tagDtoLanguageGroupIndex);
CreateIndex<TagDto>(tagDtoLanguageGroupIndex);
var documentVersionDtoIdPublishedIndex = $"IX_{DocumentVersionDto.TableName}_id_published";
DeleteIndex<DocumentVersionDto>(documentVersionDtoIdPublishedIndex);
CreateIndex<DocumentVersionDto>(documentVersionDtoIdPublishedIndex);
var documentVersionDtoPublishedIndex = $"IX_{DocumentVersionDto.TableName}_published";
DeleteIndex<DocumentVersionDto>(documentVersionDtoPublishedIndex);
CreateIndex<DocumentVersionDto>(documentVersionDtoPublishedIndex);
var logDtoDatestampIndex = $"IX_{LogDto.TableName}_datestamp";
DeleteIndex<LogDto>(logDtoDatestampIndex);
CreateIndex<LogDto>(logDtoDatestampIndex);
var logDtoDatestampHeaderIndex = $"IX_{LogDto.TableName}_datestamp_logheader";
DeleteIndex<LogDto>(logDtoDatestampHeaderIndex);
CreateIndex<LogDto>(logDtoDatestampHeaderIndex);
var propertyDataDtoVersionIdIndex = $"IX_{PropertyDataDto.TableName}_VersionId";
DeleteIndex<PropertyDataDto>(propertyDataDtoVersionIdIndex);
CreateIndex<PropertyDataDto>(propertyDataDtoVersionIdIndex);
var contentNuDtoPublishedIdIndex = $"IX_{ContentNuDto.TableName}_published";
DeleteIndex<ContentNuDto>(contentNuDtoPublishedIdIndex);
CreateIndex<ContentNuDto>(contentNuDtoPublishedIdIndex);
var nodeDtoParentIdNodeObjectTypeIndex = $"IX_{NodeDto.TableName}_parentId_nodeObjectType";
DeleteIndex<NodeDto>(nodeDtoParentIdNodeObjectTypeIndex);
CreateIndex<NodeDto>(nodeDtoParentIdNodeObjectTypeIndex);
}
}

View File

@@ -3,7 +3,7 @@ namespace Umbraco.Cms.Infrastructure.Persistence.DatabaseAnnotations;
/// <summary>
/// Attribute that represents an Index
/// </summary>
[AttributeUsage(AttributeTargets.Property)]
[AttributeUsage(AttributeTargets.Property, AllowMultiple = true)]
public class IndexAttribute : Attribute
{
public IndexAttribute(IndexTypes indexType) => IndexType = indexType;

View File

@@ -55,8 +55,14 @@ public static class DefinitionFactory
}
// Creates an index definition and adds it to the collection on the table definition
IndexAttribute? indexAttribute = propertyInfo.FirstAttribute<IndexAttribute>();
if (indexAttribute != null)
IEnumerable<IndexAttribute>? indexAttributes = propertyInfo.MultipleAttribute<IndexAttribute>();
if (indexAttributes == null)
{
continue;
}
foreach (IndexAttribute indexAttribute in indexAttributes)
{
IndexDefinition indexDefinition =
GetIndexDefinition(modelType, propertyInfo, indexAttribute, columnName, tableName);

View File

@@ -10,12 +10,15 @@ namespace Umbraco.Cms.Infrastructure.Persistence.Dtos;
[ExplicitColumns]
public class ContentNuDto
{
public const string TableName = Constants.DatabaseSchema.Tables.NodeData;
[Column("nodeId")]
[PrimaryKeyColumn(AutoIncrement = false, Name = "PK_cmsContentNu", OnColumns = "nodeId, published")]
[ForeignKey(typeof(ContentDto), Column = "nodeId", OnDelete = Rule.Cascade)]
public int NodeId { get; set; }
[Column("published")]
[Index(IndexTypes.NonClustered, Name = "IX_" + TableName + "_published", ForColumns = "published,nodeId,rv", IncludeColumns = "dataRaw")]
public bool Published { get; set; }
/// <summary>

View File

@@ -18,7 +18,7 @@ internal class ContentVersionCultureVariationDto
[Column("versionId")]
[ForeignKey(typeof(ContentVersionDto))]
[Index(IndexTypes.UniqueNonClustered, Name = "IX_" + TableName + "_VersionId", ForColumns = "versionId,languageId")]
[Index(IndexTypes.UniqueNonClustered, Name = "IX_" + TableName + "_VersionId", ForColumns = "versionId,languageId", IncludeColumns = "id,name,date,availableUserId")]
public int VersionId { get; set; }
[Column("languageId")]

View File

@@ -19,7 +19,7 @@ public class ContentVersionDto
[Column("nodeId")]
[ForeignKey(typeof(ContentDto))]
[Index(IndexTypes.NonClustered, Name = "IX_" + TableName + "_NodeId", ForColumns = "nodeId,current", IncludeColumns = "id,versionDate,text,userId")]
[Index(IndexTypes.NonClustered, Name = "IX_" + TableName + "_NodeId", ForColumns = "nodeId,current", IncludeColumns = "id,versionDate,text,userId,preventCleanup")]
public int NodeId { get; set; }
[Column("versionDate")] // TODO: db rename to 'updateDate'

View File

@@ -9,11 +9,12 @@ namespace Umbraco.Cms.Infrastructure.Persistence.Dtos;
[ExplicitColumns]
public class DocumentVersionDto
{
private const string TableName = Constants.DatabaseSchema.Tables.DocumentVersion;
public const string TableName = Constants.DatabaseSchema.Tables.DocumentVersion;
[Column("id")]
[PrimaryKeyColumn(AutoIncrement = false)]
[ForeignKey(typeof(ContentVersionDto))]
[Index(IndexTypes.NonClustered, Name = "IX_" + TableName + "_id_published", ForColumns = "id,published", IncludeColumns = "templateId")]
public int Id { get; set; }
[Column("templateId")]
@@ -22,6 +23,7 @@ public class DocumentVersionDto
public int? TemplateId { get; set; }
[Column("published")]
[Index(IndexTypes.NonClustered, Name = "IX_" + TableName + "_published", ForColumns = "published", IncludeColumns = "id,templateId")]
public bool Published { get; set; }
[ResultColumn]

View File

@@ -35,14 +35,14 @@ internal class LogDto
[NullSetting(NullSetting = NullSettings.Null)]
public string? EntityType { get; set; }
// TODO: Should we have an index on this since we allow searching on it?
[Column("Datestamp")]
[Constraint(Default = SystemMethods.CurrentDateTime)]
[Index(IndexTypes.NonClustered, Name = "IX_" + TableName + "_datestamp", ForColumns = "Datestamp,userId,NodeId")]
public DateTime Datestamp { get; set; }
// TODO: Should we have an index on this since we allow searching on it?
[Column("logHeader")]
[Length(50)]
[Index(IndexTypes.NonClustered, Name = "IX_" + TableName + "_datestamp_logheader", ForColumns = "Datestamp,logHeader")]
public string Header { get; set; } = null!;
[Column("logComment")]

View File

@@ -26,7 +26,7 @@ public class NodeDto
[Column("parentId")]
[ForeignKey(typeof(NodeDto))]
[Index(IndexTypes.NonClustered, Name = "IX_" + TableName + "_ParentId")]
[Index(IndexTypes.NonClustered, Name = "IX_" + TableName + "_parentId_nodeObjectType", ForColumns = "parentID,nodeObjectType", IncludeColumns = "trashed,nodeUser,level,path,sortOrder,uniqueID,text,createDate")]
public int ParentId { get; set; }
// NOTE: This index is primarily for the nucache data lookup, see https://github.com/umbraco/Umbraco-CMS/pull/8365#issuecomment-673404177
@@ -40,6 +40,7 @@ public class NodeDto
public string Path { get; set; } = null!;
[Column("sortOrder")]
[Index(IndexTypes.NonClustered, Name = "IX_" + TableName + "_ObjectType_trashed_sorted", ForColumns = "nodeObjectType,trashed,sortOrder,id", IncludeColumns = "uniqueID,parentID,level,path,nodeUser,text,createDate")]
public int SortOrder { get; set; }
[Column("trashed")]

View File

@@ -9,6 +9,8 @@ namespace Umbraco.Cms.Infrastructure.Persistence.Dtos;
[ExplicitColumns]
internal class RedirectUrlDto
{
public const string TableName = Constants.DatabaseSchema.Tables.RedirectUrl;
public RedirectUrlDto() => CreateDateUtc = DateTime.UtcNow;
// notes
@@ -31,6 +33,7 @@ internal class RedirectUrlDto
[Column("createDateUtc")]
[NullSetting(NullSetting = NullSettings.NotNull)]
[Index(IndexTypes.NonClustered, Name = "IX_" + TableName + "_culture_hash", ForColumns = "createDateUtc", IncludeColumns = "culture,url,urlHash,contentKey")]
public DateTime CreateDateUtc { get; set; }
[Column("url")]

View File

@@ -17,6 +17,7 @@ internal class TagDto
[Column("group")]
[Length(100)]
[Index(IndexTypes.NonClustered, Name = "IX_" + TableName + "_languageId_group", ForColumns = "languageId,group", IncludeColumns = "id,tag")]
public string Group { get; set; } = null!;
[Column("languageId")]

View File

@@ -14,6 +14,7 @@ internal class TagRelationshipDto
[Column("nodeId")]
[PrimaryKeyColumn(AutoIncrement = false, Name = "PK_cmsTagRelationship", OnColumns = "nodeId, propertyTypeId, tagId")]
[ForeignKey(typeof(ContentDto), Name = "FK_cmsTagRelationship_cmsContent", Column = "nodeId")]
[Index(IndexTypes.NonClustered, Name = "IX_" + TableName + "_tagId_nodeId", ForColumns = "tagId,nodeId", IncludeColumns = "propertyTypeId")]
public int NodeId { get; set; }
[Column("tagId")]