简体   繁体   English

EF和LINQ查询数据库速度

[英]EF and LINQ query to database speed

I just wondering if I am wasting my time or is there anything I could to improve this query which in turn will improve performance. 我只是想知道我是在浪费时间还是有什么我可以改进此查询的方法,从而可以提高性能。

Inside a Repository, I am trying to get the 10 most recent items 在存储库中,我正在尝试获取10个最新商品

    public List<entity> GetTop10
    {
        get
        {
            return m_Context.entity.Where(x => x.bool == false).OrderByDescending(x => x.Created).Take(10).ToList();
        }
    }

But this is taking a long time as the table its querying has over 11000 rows in it. 但这要花费很长时间,因为其查询表中有超过11000行。 So my question is, is there anyway I could speed up this kind of query? 所以我的问题是,无论如何我是否可以加快这种查询的速度?

I am trying to get my SQL hat on regarding performance, I know the order would slow it down, but how I could I achieve the same result? 我正在尝试在性能方面使用SQL帽子,我知道顺序会降低它的速度,但是如何获得相同的结果?

Thanks 谢谢

The particular query you posted is a potential candidate for using a filtered index. 您发布的特定查询是使用筛选索引的潜在候选者。 Say you have a SQL table: 假设您有一个SQL表:

CREATE TABLE Employees
(
    ID INT IDENTITY(1,1) PRIMARY KEY,
    Name NVARCHAR(100),
    IsAlive BIT
)

You can imagine that generally you only want to query on employees that have not (yet) died so will end up with SQL like this: 您可以想象,一般来说,您只想查询尚未死亡的员工,因此最终会出现如下SQL:

SELECT Name FROM Employees WHERE IsAlive = 1

So, why not create a filtered index: 因此,为什么不创建过滤索引:

CREATE INDEX IX_Employees_IsAliveTrue 
ON Employees(IsAlive) 
WHERE IsAlive = 1

So now if you query the table it will use this index which may only be a small portion of your table, especially if you've had a recent zombie invasion and 90% of your staff are now the walking dead. 因此,现在查询表时,它将使用该索引,该索引可能只占表的一小部分,尤其是如果您最近有一次僵尸入侵,而现在90%的员工已经死了。

However, an Entity Framework like this: 但是,这样的实体框架:

var nonZombies = from e in db.Employees
                 where e.IsAlive == true
                 select e;

May not be able to use the index (SQL has a problem with filtered indexes and parameterised queries). 可能无法使用索引(SQL在过滤索引和参数化查询方面存在问题)。 To get round this, you can create a view in your database: 为了解决这个问题,您可以在数据库中创建一个视图:

CREATE VIEW NonZombies
AS
SELECT ID, Name, IsAlive FROM Employees WHERE IsAlive = 1

Now you can add that to your framework (how you do this will vary depending on if you are using code/model/database first) and you will now be able to decide which employees deserve urgent attention (like priority access to food and weapons): 现在,您可以将其添加到框架中(操作方式将取决于您是否首先使用代码/模型/数据库而有所不同),并且现在您可以决定哪些雇员值得紧急关注(例如优先获得食物和武器) :

var nonZombies = from e in db.NonZombies
                 select e;

From your LINQ query will be created SQL SELECT similar to this: 从您的LINQ查询中将创建类似于以下内容的SQL SELECT:

SELECT TOP(10) * FROM entity
WHERE bool = 0
ORDER BY Created DESC

Similar because instead of the '*' will server select concrete columns to map these to entity object. 之所以类似,是因为服务器将选择具体的列而不是'*'来将它们映射到实体对象。

If this is too slow for you. 如果这对您来说太慢了。 The error is in the database, not in the EntityFramework. 该错误在数据库中,而不在EntityFramework中。 So try adding some indexes to your table. 因此,尝试向表中添加一些索引。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM