There is a table in SQL Server 2008 R2:
Three columns, three indexes.
It was created using the Entity Framework, everything is simple:
public class Order { public int Id { get; set; } public int GroupBuyingId { get; set; } public GroupBuying GroupBuying { get; set; } public int ParticipantId { get; set; } public Contact Participant { get; set; } } After some time, the demand came to impose a unique constraint on the GroupBuyingId and ParticipantId fields, so I did:
public class OrderConfiguration : IEntityTypeConfiguration<Order> { public void Configure(EntityTypeBuilder<Order> builder) { builder.HasIndex(x => new { x.GroupBuyingId, x.ParticipantId }).IsUnique(); } } But before applying the migration, he automatically looked at what was created there:
public partial class UniqueConstraintForOrder : Migration { protected override void Up(MigrationBuilder migrationBuilder) { migrationBuilder.DropIndex( name: "IX_Orders_GroupBuyingId", table: "Orders"); migrationBuilder.CreateIndex( name: "IX_Orders_GroupBuyingId_ParticipantId", table: "Orders", columns: new[] { "GroupBuyingId", "ParticipantId" }, unique: true); } protected override void Down(MigrationBuilder migrationBuilder) { migrationBuilder.DropIndex( name: "IX_Orders_GroupBuyingId_ParticipantId", table: "Orders"); migrationBuilder.CreateIndex( name: "IX_Orders_GroupBuyingId", table: "Orders", column: "GroupBuyingId"); } } And that's what I do not understand. A new index is created for two fields - this is correct. But for some reason one of the existing indices is demolished (by the way, why not both?), Apparently on the basis that it would be possible to use the composite index?
What is generally recommended in such cases - to leave one composite index or a composite + two separate ones?
It is clear that in the second case the volume occupied by the index will be more, plus, respectively, the time for processing it when inserting new values, but what about the speed of the samples?
Will a composite index work as efficiently as a single index?
