简体   繁体   中英

Performance: Local vs database - Entity Framework

I have looked around for a simple answer but |I haven't found it (Though if I am being blind or impatient, I be happy for someone to post me the link)

I have the following code in my repository

get
{
    if (context.entity.Local.Count == 0)
    {
        return context.entity;
    }

    return context.entity.Local;
}

I know from common sense that the word local is not querying the database and getting the result set from memory. However, what I would like to know is, how much faster is fetching the result set from local than it is from the database? It is a huge difference?

I am asking as I would like to speed up my web application so I am trying to find weaknesses in the code.

Thanks

First, your common sense makes no sense at all. Local is nothing that is defined at all in EF so it depends on whoever made the repository and could as well refer to something else.

Second - a lot. Easily factor of 1000. THe database is a separate process, involves generating and then parsing SQL. 2x network transfer (or network alyer transfer). Compare that to just reading out a property. 1000 is likely conservative. Not that it may be a lot of time in the database to start with.

It depends on what you DO - but caching in memory and avoiding the database is a valid strategy that can make a lot of difference, performance wise. At the cost of more memory consumption and change synchronization issues. The later is not really relevant for some (never changing) data.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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