I have a table Person
: id, name
I often have queries like:
select * from Person where name Like "%abc%".
I have 2 questions:
The following code block is an example to add index in an existing table. mysql> ALTER TABLE testalter_tbl ADD INDEX (c); You can drop any INDEX by using the DROP clause along with the ALTER command. Try out the following example to drop the above-created index.
What is indexing? Indexing is a way of sorting a number of records on multiple fields. Creating an index on a field in a table creates another data structure which holds the field value, and a pointer to the record it relates to. This index structure is then sorted, allowing Binary Searches to be performed on it.
The CREATE INDEX statement is used to create indexes in tables. Indexes are used to retrieve data from the database more quickly than otherwise. The users cannot see the indexes, they are just used to speed up searches/queries.
Click where you want to add the index. On the References tab, in the Index group, click Insert Index. In the Index dialog box, you can choose the format for text entries, page numbers, tabs, and leader characters.
in EF 6 you can create indexes like this
Property(t => t.TotalValue)
.HasColumnAnnotation(
IndexAnnotation.AnnotationName,
new IndexAnnotation(new IndexAttribute("IX_inventory_import_total_value", 1))
);
Like operator can be performed with Contains function:
var query = from p in context.Persons
where p.Name.Contains("abc")
select p;
Index must be added by SQL - there is no special construction in EF to create index. You can execute this SQL from DB initialization.
First you must implement custom initializer:
public class MyInitializer : CreateDatabaseIfNotExists<MyContext>
{
protected override void Seed(MyContext context)
{
context.Database.SqlCommand("CREATE INDEX IX_Person_Name ON Person (Name)");
}
}
Then you must register new initializer:
DbDatabase.SetInitializer<MyContext>(new MyInitializer());
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With