1

I'm having a few performance bottlenecks in my queries. What happens is whenever i introduce a property as a byte in my entity, EF 4.1 casts it to an int prior to working with it. The given code will explain:

var segmentQuery = workUnit.SegmentRepository.GetQuery()
                                             .Where(x => x.FileId == file.Id)
                                             .Where(x => x.StateValue == (byte)SegmentState.Unhandeled)
                                             .OrderBy(x => x.Index);

Translates nicely to:

SELECT 
....
FROM ( SELECT 
    [Extent1].[Id] AS [Id], 
    ...
    [Extent1].[StateValue] AS [StateValue]
    FROM [Segments] AS [Extent1]
    WHERE ([Extent1].[FileId] = @p__linq__0) AND (0 = [Extent1].[StateValue])
)  AS [Project1]
ORDER BY [Project1].[Index] ASC

However, in the case above: StateValue actually is an integer which is way to much for my requirements (4 different states), but when changing it to a byte, i get:

SELECT ...
FROM ( SELECT 
    [Extent1].[Id] AS [Id], 
      ...
    [Extent1].[StateValue] AS [StateValue]
    FROM [Segments] AS [Extent1]
    WHERE ([Extent1].[FileId] = @p__linq__0) AND (0 = ( CAST( [Extent1].[StateValue] AS int)))
)  AS [Project1]
ORDER BY [Project1].[Index] ASC

Since this table might contain more then 100 000 of rows one day, its space efficient (although luckily not essential) for its State field to only occupy 1 byte, however, changing to bytes kill my queries.

Did i do something wrong? Is there anything i can do? is this 'problem' known?

thanks!

** UPDATE **

[Flags]
public enum SegmentState : byte
{
    Unhandeled,
    Downloaded,
    Invalid,
    Assembled
}

and in my entity:

/// <summary>
/// Dont use this, use SegmentState instead
/// </summary>
[Required]
public byte StateValue
{
    get { return _stateValue; }
    set { _stateValue = value; }
}

public SegmentState State
{
    get { return (SegmentState)StateValue; }
    set 
    {
        if (State != value)
        {
            StateValue = (byte)value;
            RaisePropertyChanged(StatePropertyName);
        }
    }
}
4

1 回答 1

0

真的不要使用EF——这将是你一生中最大的痛苦。看看 C# 中的 MASSIVE 和 Dynamics - 它会让你大开眼界;-)

于 2011-06-10T21:34:23.990 回答