我有一个相当通用的 CRUD webapp,它根据几个数据库表的内容动态生成页面。我正在使用 Entity Framework 4.0 将这些数据从数据库中提取出来,但是我遇到了严重的性能问题。我已经设法迭代到一个包含足够内容的问题,我可以在下面详细说明。
我有一个包含页面表单列表(〜200)的表格。每个表单都有一个或多个字段(总共约 4000 个),每个字段可能有一些参数(总共约 16000 个)。
我在下面附上了我的模型的屏幕截图:
关联的实体对象如下:
public class Form
{
public int FormID { get; set; }
public string FormName { get; set; }
public IList<FormField> FormFields { get; set; }
}
public class FormField
{
public int FieldID { get; set; }
public string FieldName { get; set; }
public int FormID{ get; set; }
public IList<FormFieldParameter> FormFieldParameters { get; set; }
public Form ParentForm { get; set; }
}
public class FormFieldParameter
{
public int FieldParamID{ get; set; }
public string Value{ get; set; }
public int? FieldID { get; set; }
public FormField ParentField { get; set; }
}
以下代码提取 ID 为“1”的表单的所有数据。
EntityConnection myConnection = new EntityConnection("name=myModel");
if(conn.State != ConnectionState.Open) {
conn.Open();
}
ObjectContext context = new ObjectContext("name=myModel");
context.ContextOptions.LazyLoadingEnabled = false;
ObjectQuery<PageForm> myObjectSet = context.CreateObjectSet<PageForm>()
.Include("FormField.FormFieldParameter");
//Edit: I missed this part out, sorry. In hindsight, this was exactly what was
//causing the issue.
IEnumerable<PageForm> myObjectSetEnumerable = myObjectSet.AsEnumerable();
IQueryable<PageForm> myFilteredObjectSet = myObjectSetEnumerable.Where(c => c.FormID == 1)
.AsQueryable();
List<PageForm> myReturnValue = myFilteredObjectSet.toList();
现在,虽然这确实有效,但它运行得非常糟糕。查询需要一秒钟才能运行,全部用于myFilteredObjectSet.toList()
调用。我在我的数据库上运行了一个分析器以查看导致延迟的原因,并发现正在生成以下查询:
SELECT
[Project1].[FormID] AS [FormID],
[Project1].[FormName] AS [FormName],
[Project1].[C2] AS [C1],
[Project1].[FormID1] AS [FormID1],
[Project1].[FieldID] AS [FieldID],
[Project1].[FieldName] AS [FieldName],
[Project1].[C1] AS [C2],
[Project1].[FieldParamID] AS [FieldParamID],
[Project1].[Value] AS [Value],
[Project1].[FieldID1] AS [FieldID1]
FROM ( SELECT
[Extent1].[FormID] AS [FormID],
[Extent1].[FormName] AS [FormName],
[Join1].[FieldID] AS [FieldID],
[Join1].[FieldName] AS [FieldName],
[Join1].[FormID] AS [FormID1],
[Join1].[FieldParamID] AS [FieldParamID],
[Join1].[Value] AS [Value],
[Join1].[FieldID1] AS [FieldID1],
CASE WHEN ([Join1].[FieldID] IS NULL) THEN CAST(NULL AS int) WHEN ([Join1].[FieldParamID] IS NULL) THEN CAST(NULL AS int) ELSE 1 END AS [C1],
CASE WHEN ([Join1].[FieldID] IS NULL) THEN CAST(NULL AS int) ELSE 1 END AS [C2]
FROM [dbo].[PageForm] AS [Extent1]
LEFT OUTER JOIN (SELECT [Extent2].[FieldID] AS [FieldID], [Extent2].[FieldName] AS [FieldName], [Extent2].[FormID] AS [FormID], [Extent3].[FieldParamID] AS [FieldParamID], [Extent3].[Value] AS [Value], [Extent3].[FieldID] AS [FieldID1]
FROM [dbo].[FormField] AS [Extent2]
LEFT OUTER JOIN [dbo].[FormFieldParameter] AS [Extent3] ON [Extent2].[FieldID] = [Extent3].[FieldID] ) AS [Join1] ON [Extent1].[FormID] = [Join1].[FormID]
) AS [Project1]
ORDER BY [Project1].[FormID] ASC, [Project1].[C2] ASC, [Project1].[FieldID] ASC, [Project1].[C1] ASC
sql profiler 上显示的此查询的持续时间表明此查询需要很长时间才能运行。查询的有趣之处在于,它根本没有过滤——它返回的是整个树!我不明白为什么它会返回所有内容,因为过滤器myObjectSet.Where(c => c.FormID == 1)
非常明确。实际返回的对象仅包含一个条目,这是我所期望的。
我的整个数据访问层都遇到了这个问题,它的性能令人震惊。我不知道为什么生成的查询不包含过滤器 - 也不知道如何告诉它这样做。有人知道答案吗?