What is the need to inform the parameter "Order" in the object of type Indexannotation?

Asked

Viewed 52 times

5

I am creating indexes for some columns that will be used in a query with Where(). The intention is to make the search faster. Until then I was creating like this:

Property(x => x.Documento)
            .HasColumnAnnotation(IndexAnnotation.AnnotationName,
                                 new IndexAnnotation(new IndexAttribute("ix_documento", 1) { IsUnique = false }));

Property(x => x.Vencimento)
            .HasColumnAnnotation(IndexAnnotation.AnnotationName,
                                 new IndexAnnotation(new IndexAttribute("ix_vencimento", 2) { IsUnique = false }));

But I noticed that the class IndexAttribute has a constructor that accepts only the index name.

The description of the parameter order is:

order: A number which will be used to determine column Ordering for multi-column Indexes.

It would be something like that:

A number that will be used to determine column ordering for multi-column index

I didn’t understand that, and whether in my case makes any difference or not.

1 answer

7


First of all, let’s get the exact description of the property Indexattribute.Order.

Obtains or defines a number that determines column sorting for multiple column indices. This will be -1 if no column order has been specified.

Remarks
Multiple column indices are created using the same index name in multiple attributes. The information in these attributes is then merged together to specify the actual database index.

In short, you will use the property Order when you have an Index for multiple columns with the same name.

See this example (with Dataannotations to be simpler to understand) from own Microsoft to better understand how to use:

public class Post
    {
        public int Id { get; set; }
        public string Title { get; set; }
        public string Content { get; set; }
        [Index("IX_BlogIdAndRating", 2)]
        public int Rating { get; set; }
        [Index("IX_BlogIdAndRating", 1)]
        public int BlogId { get; set; }
    }

Note that the properties Rating and BlogId have the same index, where the first column is BlogId, because of order 1.

For your specific example, there is no need to add the order, as the indexes are different. And as stated in the question, you wish only in order to leave the query "faster".

If you want to know more about Indexes, see this question.

  • Reply with more details on Multi-column Index: http://answall.com/a/55137/26552

  • @Murilo I searched for some reference here on the site, but had not found. I will add in reply, thanks.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.