I'm building my application with Entity Framework (model first principle). I'm also using MS SQL Server 2008 to store all the data of my application.
After some time of developing I have the following code:
public partial class EventInfo
{
#region Primitive Properties
public virtual int Id
{
get;
set;
}
public virtual string EventName
{
get;
set;
}
public virtual string EventKey
{
get;
set;
}
public virtual System.DateTime DateStamp
{
get;
set;
}
#endregion
}
And Visual Studio database designer have created special chunk of sql code to map this entity to the database:
-- Creating table 'EventInfoSet'
CREATE TABLE [dbo].[EventInfoSet] (
[Id] int IDENTITY(1,1) NOT NULL,
[EventName] nvarchar(max) NOT NULL,
[EventKey] nchar(32) NOT NULL,
[DateStamp] datetime NOT NULL
);
And of course, an index for Id property
-- Creating primary key on [Id] in table 'EventInfoSet'
ALTER TABLE [dbo].[EventInfoSet]
ADD CONSTRAINT [PK_EventInfoSet]
PRIMARY KEY CLUSTERED ([Id] ASC);
The EventKey is string, and actually I use it to store md5 hash (in string representation). But the thing is that my main code looks like:
int cnt = context.EventInfoSet.Where(e => e.EventKey == eventKey).Count();
and
int cnt = context.EventInfoSet.Where(e => e.EventKey == eventKey && e.DateStamp >= dateFrom && e.DateStamp < dateTo).Count();
eventKey here is a string variable. As you can see, I often deal with EventKey property. But my table could contain huge amount of records (up to 5M). And I need this code to work as fast as possible. I didn't find any option to mark EventKey as index property in designer. And I want to know:
- how can I make things faster? do I have to worry about it at all with my code?
- is there any gentle way to force .NET development environment generate index field automatically?
- if there is no gentle way, and I have to update it manually, how I can organize things better to do it automatically?
- may be you give me a link to the article explaining all these things with indexes, cos my knowledge lacks here.
Thanks!