为什么我找不到缓存LINQ to Entity查询的任何好处?

时间:2009-10-12 09:18:39

标签: entity-framework caching

请查看我的源代码。

public class EntityQuery
{
    public static Func<AdventureWork2008Container, IQueryable<SalesPerson>> selectQuery = CompiledQuery.Compile
    (
        (AdventureWork2008Container aw) =>
        (
            from s in aw.SalesPerson
            join e in aw.Employee on s.BusinessEntityID equals e.BusinessEntityID
            join p in aw.Person on s.BusinessEntityID equals p.BusinessEntityID
            join bea in aw.BusinessEntityAddress on s.BusinessEntityID equals bea.BusinessEntityID
            join a in aw.Address on bea.AddressID equals a.AddressID
            join sp in aw.StateProvince on a.StateProvince equals sp
            select s
        )
    );

    public decimal Select(AdventureWork2008Container aw)
    {
        SalesPerson result = selectQuery(aw).First();
        return result.SalesYTD;
    }

    public decimal Select2(AdventureWork2008Container aw)
    {            
        SalesPerson result =
        (
            from s in aw.SalesPerson
            join e in aw.Employee on s.BusinessEntityID equals e.BusinessEntityID
            join p in aw.Person on s.BusinessEntityID equals p.BusinessEntityID
            join bea in aw.BusinessEntityAddress on s.BusinessEntityID equals bea.BusinessEntityID
            join a in aw.Address on bea.AddressID equals a.AddressID
            join sp in aw.StateProvince on a.StateProvince equals sp
            select s
        ).First();

        return result.SalesYTD;
    }
}

我尝试调用Select方法大约1000次,并调用Select2方法大约1000次。但结果显示Select2方法比Select方法快约0.005秒。(0.052 / 0.057 s。)此外,此捕获不包括创建EntityQuery对象的时间。

我的源代码出了什么问题?

PS。以下代码显示了如何调用方法。

    private void button1_Click(object sender, EventArgs e)
    {
        using (AdventureWork2008Container aw = new AdventureWork2008Container())
        {
            EntityQuery eq = new EntityQuery();

            eq.Select(aw);
            long lastTime = DateTime.Now.Ticks;

            for (int i = 0; i < 1000; i++)
            {
                eq.Select(aw);
            }
            listBox1.Items.Add("Select 1 : " + ShowTime(lastTime));
        }

        GC.Collect();
    }

    private void button2_Click(object sender, EventArgs e)
    {
        using (AdventureWork2008Container aw = new AdventureWork2008Container())
        {
            EntityQuery eq = new EntityQuery();

            eq.Select2(aw);
            long lastTime = DateTime.Now.Ticks;

            for (int i = 0; i < 1000; i++)
            {
                eq.Select2(aw);
            }
            listBox1.Items.Add("Select 2 : " + ShowTime(lastTime));
        }

        GC.Collect();
    }

1 个答案:

答案 0 :(得分:0)

在Select2()中生成的代码是

SELECT TOP 1 * FROM ...

在Select()中,聚合在请求之上完成。不确定,但它可能导致.Compile()结果被丢弃。

尝试将.First()移动到已编译的查询中。

public static Func<AdventureWork2008Container, SalesPerson> selectQuery = CompiledQuery.Compile
(
    (AdventureWork2008Container aw) =>
    (
        from s in aw.SalesPerson
        join e in aw.Employee on s.BusinessEntityID equals e.BusinessEntityID
        join p in aw.Person on s.BusinessEntityID equals p.BusinessEntityID
        join bea in aw.BusinessEntityAddress on s.BusinessEntityID equals bea.BusinessEntityID
        join a in aw.Address on bea.AddressID equals a.AddressID
        join sp in aw.StateProvince on a.StateProvince equals sp
        select s
    ).First();
);

希望这会有所帮助。