If in Entity Framework I have the following:
builder.HasIndex(e => new[] { "CampaignId", "CountyId", "StateId" }).IsUnique();
Are the three individual FKs indexed? In other words, if I call a query which includes CampaignId == 3, will that be fast because CampaignId is effectively indexed by itself? Or should I also do a HasIndex("CampaignId")?
They are not. This just creates the so called composite index.
However, the leading column of the index is used by queries which filter on it. As explained in the documentation link:
Indexes over multiple columns, also known as composite indexes, speed up queries which filter on index's columns, but also queries which only filter on the first columns covered by the index.
So in your sample, you don't need to create individual index for CampaignId
. For other columns you might need to create individual indexes if needed. But keep in mind if these are FK columns (as they seem to be), then you don't need to create individual index(es) as well because EF Core creates that automatically for you:
By convention, an index is created in each property (or set of properties) that are used as a foreign key.
Also note that
EF Core only supports one index per distinct set of properties. If you configure an index on a set of properties that already has an index defined, either by convention or previous configuration, then you will be changing the definition of that index. This is useful if you want to further configure an index that was created by convention.