如何提高ASP ?NET MVC应用程序性能?


当前回答

1:获取时间。在你知道放缓在哪里之前,这个问题太宽泛了,无法回答。我正在做的一个项目就有这样的问题;没有日志记录,甚至不知道某些事情需要多长时间;在我们将计时添加到项目中之前,我们只能猜测应用程序的慢部分。

2:如果你有顺序操作,不要害怕轻度多线程。特别是当涉及到阻塞操作时。PLINQ是你的朋友。

3:在发布时预生成MVC视图这将有助于一些“第一页点击”

4:有些人认为存储过程/ADO在速度上有优势。其他人则主张加快英孚教育的发展速度,并对教育层级及其目的进行更明确的划分。我曾见过SQL和使用scpros /Views进行数据检索和存储的变通方法时非常缓慢的设计。同时,你的测试难度也会增加。我们目前正在从ADO转换到EF的代码库的性能并不比旧的手卷模型差(在某些情况下更好)。

5:也就是说,考虑一下应用程序的预热。我们所做的帮助消除大部分EF性能问题的部分工作是添加一个特殊的预热方法。它不预编译任何查询或任何东西,但它有助于大量元数据的加载/生成。在处理Code First模型时,这一点可能更加重要。

6: As others have said, Don't use Session state or ViewState if possible. They are not necessarily performance optimizations that developers think about, but once you start writing more complex web applications, you want responsiveness. Session state precludes this. Imagine a long running query. You decide to open a new window and try a less complex one. Well, you may as well have waited with session state on, because the server will wait until the first request is done before moving to the next one for that session.

7:最小化到数据库的往返。保存你经常使用但实际上不会更改到. net缓存的东西。在可能的情况下尝试批处理插入/更新。

7.1:避免数据访问代码在你的Razor视图没有一个该死的好理由。要不是亲眼所见,我也不会这么说。在构建模型时,他们已经在访问数据了,为什么他们不把数据包括在模型中呢?

其他回答

我还要补充一点:

Use Sprites: Sprites are a great thing to reduce a request. You merge all your images into a single one and use CSS to get to good part of the sprite. Microsoft provides a good library to do it: Sprite and Image Optimization Preview 4. Cache Your server object: If you have some references lists or data which will change rarely, you can cache them into memory instead of querying database every time. Use ADO.NET instead of Entity Framework: EF4 or EF5 are great to reduce development time, but it will be painful to optimize. It's more simple to optimize a stored procedure than Entity Framework. So you should use store procedures as much as possible. Dapper provides a simple way to query and map SQL with very good performance. Cache Page or partial page: MVC provides some easy filter to cache page according to some parameters, so use it. Reduce Database calls: You can create a unique database request that returns multiple objects. Check on Dapper website. Always have a clean architecture: Have a clean n-tiers architecture, even on a small project. It will help you to keep your code clean, and it will be easier to optimize it if needed. You can take a look at this template "Neos-SDI MVC Template" which will create a clean architecture for you with lots of performance improvements by default (check MvcTemplate website).

在您嚷嚷着优化客户端时,不要忘记数据库层。我们有一个应用程序在一夜之间从5秒加载到50秒。

在检查中,我们做了一大堆模式更改。一旦我们刷新统计数据,它突然变得像以前一样灵敏。

我做了上面所有的答案,但还是没有解决我的问题。

最后,我解决了我的网站加载缓慢的问题,在发布配置文件PrecompileBeforePublish设置为真。如果你想使用msbuild,你可以使用这个参数:

 /p:PrecompileBeforePublish=true

这真的很有帮助。现在我的MVC ASP。NET加载速度快10倍。

当通过LINQ访问数据依赖IQueryable…

为什么使用AsQueryable()而不是List()?

... 并利用一个好的存储库模式:

在存储库模式中加载子记录

这将优化数据访问,以确保只加载需要的数据。

Code Climber和这篇博客文章提供了提高应用程序性能的详细方法。

编译后的查询将提高应用程序的性能,但它与ASP没有任何共同之处。净MVC。它将加速每一个db应用程序,所以它不是真正的MVC。