Performance Optimization Cheat Sheet
Performance Optimization Cheat Sheet
Rules of thumb
• Do not sacrifice the maintainability of your code to premature optimization.
• Be realistic and think like an “engineer”.
• Be pragmatic and ensure your efforts have observable results and give value.
Database tier
Schema
• Every table must have a primary key.
• Tables should have relationships.
• Put indexes on columns where you filter records on. But remember: too many
indexes can have an adverse impact on the performance.
Queries
• Keep an eye on EF queries using Glimpse. If a query is slow, use a stored
procedure.
If after all your optimizations, you still have slow queries, consider creating a
denormalised “read” database for your queries. But remember, this comes with the cost
of maintaining two databases in sync. A simpler approach is to use caching.
1
Performance Optimization By: Mosh Hamedani
Application tier
• On pages where you have costly queries on data that doesn’t change frequently,
use OutputCache to cache the rendered HTML.
• You can also store the results of the query in cache (using MemoryCache), but
use this approach only in actions that are used for displaying data, not modifying
it.
• Async does not improve performance. It improves scalability given that you’re
not using a single instance of SQL Server on the back-end. You should be using
a SQL cluster, or a NoSQL database (eg MongoDB, RavenDB, etc) or SQL
Azure.
2
Performance Optimization By: Mosh Hamedani
Client Tier
• Put JS and CSS files in bundles.
• Put the script bundles near the end of the <body> element. Modernizr is an
exception. It needs to be in the head.
• Render HTML markup on the client. That’s the case with single page
applications (SPA).
• Compress images.
• Use image sprites. This is beyond the scope of the course and you need to read
about them yourself.
• Reduce the data you store in cookies because they’re sent back and forth with
every request.
• Use content delivery networks (CDN). Again, beyond the scope of the course.
Implementations vary depending on where you host your application.