Showing posts with label tweaks. Show all posts
Showing posts with label tweaks. Show all posts

Friday, March 19, 2010

ASP.NET Performance Framework

ASP.NET Performance Framework: "

At the start of the year, I finished a 5 part series on ASP.NET performance - focusing on largely generic ways to improve website performance rather than specific ASP.NET performance tricks. The series focused on a number of topics, including merging and shrinking files, using modules to remove unecessary headers and setting caching headers, enabling cache busting and automatically generating cache busted referneces in css, as well as an introduction to nginx.


Yesterday I managed to put a number of those things together into a framework which I hope will make it easier for developer to leverage these best practices. The project is called Metsys.WebOp, and you can download it from github. It comes with a sample application, which not only shows how to use the framework, but also has all the documentation you'll hopefully need.


The first part of the framework is a console application which is meant to be run as part of your build process. This is driven by a text file which supports 4 commands - merging files together, shrinking them, generating cache busting hashes and pre-zipping files. Here's what the sample app's command file looks like:


#combine JS files
combine: js\all.js: js\jquery.js, js\jquery.rollover.js, js\web.js

#combine css files
combine: css\merged.css: css\reset.css, css\main.css

#shrink the generated files
shrink: js\all.js, css\merged.css

#generate cache-busting resource hashes
busting: hashes.dat

#pre-zip our files
zip: js\all.js, css\merged.css

The next part is meant to be used from within a MVC application (it wouldn't take too much effort for someone to make it work with WebForms) - first by allowing you to configure the runtime component, and then by providing extension methods to HtmlHelper. Essentially this gives you 4 methods, Html.IncludeJs, Html.IncludeCss, Html.Image and Html.ImageOver. You can also toggle debug mode, which'll make all of this transparent during development (nothing worse than dealing with merged and shrank files in development).


The last part are a couple HttpModule which make everthing possible. The Zip module will returned the pre-zipped files (generated by the build process) should the browser accept zip files. The WebOp module will remove unecessary headers and add caching headers to js, css and images - only really practical if you are also using the cache busting featuers.


You can download the project from http://github.com/kseg/Metsys.WebOp.


You might also be interested in checking out the mscd project, which does a lot of the same stuff, but is probably more mature.

"

Monday, March 15, 2010

Microsoft Ajax Minifier

Microsoft Ajax Minifier: "One good thing I came across at the Microsoft MVP Summit is that Microsoft released Ajax Minifier, a tool to compress the size of Javascript (.JS) and Cascading Style Sheets (.CSS) files. Later I found a Scott Gu post on it. The Microsoft Ajax Minifier is a small tool to remove unnecessary content from your js or css file. It can bring down the size of js file by 60%. You can download the Ajax

"

Tuesday, February 16, 2010

SQL SERVER – Stored Procedure Optimization Tips – Best Practices

SQL SERVER – Stored Procedure Optimization Tips – Best Practices: "

We will go over how to optimize Stored Procedure with making simple changes in the code. Please note there are many more other tips, which we will cover in future articles.




  • Include SET NOCOUNT ON statement: With every SELECT and DML statement, the SQL server returns a message that indicates the number of affected rows by that statement. This information is mostly helpful in debugging the code, but it is useless after that. By setting SET NOCOUNT ON, we can disable the feature of returning this extra information. For stored procedures that contain several statements or contain Transact-SQL loops, setting SET NOCOUNT to ON can provide a significant performance boost because network traffic is greatly reduced.


CREATE PROC dbo.ProcName

AS

SET
NOCOUNT ON;

--Procedure code here

SELECT column1 FROM dbo.TblTable1

-- Reset SET NOCOUNT to OFF

SET NOCOUNT OFF;

GO



  • Use schema name with object name: The object name is qualified if used with schema name. Schema name should be used with the stored procedure name and with all objects referenced inside the stored procedure. This help in directly finding the complied plan instead of searching the objects in other possible schema before finally deciding to use a cached plan, if available. This process of searching and deciding a schema for an object leads to COMPILE lock on stored procedure and decreases the stored procedure’s performance. Therefore, always refer the objects with qualified name in the stored procedure like


SELECT * FROM dbo.MyTable -- Preferred method

-- Instead of

SELECT * FROM MyTable -- Avoid this method

--And finally call the stored procedure with qualified name like:

EXEC dbo.MyProc -- Preferred method

--Instead of

EXEC MyProc -- Avoid this method



  • Do not use the prefix “sp_” in the stored procedure name: If a stored procedure name begins with “SP_,” then SQL server first searches in the master database and then in the current session database. Searching in the master database causes extra overhead and even a wrong result if another stored procedure with the same name is found in master database.



  • Use IF EXISTS (SELECT 1) instead of (SELECT *): To check the existence of a record in another table, we uses the IF EXISTS clause. The IF EXISTS clause returns True if any value is returned from an internal statement, either a single value “1” or all columns of a record or complete recordset. The output of the internal statement is not used. Hence, to minimize the data for processing and network transferring, we should use “1” in the SELECT clause of an internal statement, as shown below:


IF EXISTS (SELECT 1 FROM sysobjects

WHERE name = 'MyTable' AND type = 'U')



  1. V.            Use the sp_executesql stored procedure instead of the EXECUTE statement.

    The sp_executesql stored procedure supports parameters. So, using the sp_executesql stored procedure instead of the EXECUTE statement improve the re-usability of your code. The execution plan of a dynamic statement can be reused only if each and every character, including case, space, comments and parameter, is same for two statements. For example, if we execute the below batch:


DECLARE @Query VARCHAR(100)

DECLARE @Age INT

SET
@Age = 25

SET @Query = 'SELECT * FROM dbo.tblPerson WHERE Age = ' + CONVERT(VARCHAR(3),@Age)

EXEC (@Query)


If we again execute the above batch using different @Age value, then the execution plan for SELECT statement created for @Age =25 would not be reused. However, if we write the above batch as given below,


DECLARE @Query NVARCHAR(100)

SET @Query = N'SELECT * FROM dbo.tblPerson WHERE Age = @Age'

EXECUTE sp_executesql @Query, N'@Age int', @Age = 25


the compiled plan of this SELECT statement will be reused for different value of @Age parameter. The reuse of the existing complied plan will result in improved performance.



  • Try to avoid using SQL Server cursors whenever possible: Cursor uses a lot of resources for overhead processing to maintain current record position in a recordset and this decreases the performance. If we need to process records one-by-one in a loop, then we should use the WHILE clause. Wherever possible, we should replace the cursor-based approach with SET-based approach. Because the SQL Server engine is designed and optimized to perform SET-based operation very fast. Again, please note cursor is also a kind of WHILE Loop.

  • Keep the Transaction as short as possible: The length of transaction affects blocking and deadlocking. Exclusive lock is not released until the end of transaction. In higher isolation level, the shared locks are also aged with transaction. Therefore, lengthy transaction means locks for longer time and locks for longer time turns into blocking. In some cases, blocking also converts into deadlocks. So, for faster execution and less blocking, the transaction should be kept as short as possible.

  • Use TRY-Catch for error handling: Prior to SQL server 2005 version code for error handling, there was a big portion of actual code because an error check statement was written after every t-sql statement. More code always consumes more resources and time. In SQL Server 2005, a new simple way is introduced for the same purpose. The syntax is as follows:


BEGIN TRY

--Your t-sql code goes here

END TRY

BEGIN CATCH

--Your error handling code goes here

END CATCH


Reference: Pinal Dave (http://blog.SQLAuthority.com)


Filed under: Best Practices, Pinal Dave, SQL, SQL Authority, SQL Coding Standards, SQL Optimization, SQL Performance, SQL Query, SQL Scripts, SQL Server, SQL Stored Procedure, SQL Tips and Tricks, T SQL, Technology "

Tuesday, February 9, 2010

FREE eBook: .NET Performance Testing and Optimization (Part 1)

FREE eBook: .NET Performance Testing and Optimization (Part 1): "

In this this first part of complete guide to performance profiling, Paul Glavich and Chris Farrell explain why performance testing is a good idea and walk you through everything you need to know to set up a test environment. This comprehensive guide to getting started is an essential handbook to any programmer looking to set up a .NET testing environment and get the best results out of it. Download your free copy now



"

Saturday, December 26, 2009

How to make ASP.NET 3rd party controls run faster

How to make ASP.NET 3rd party controls run faster: "1. Abstract
The following document will explore the basics of enhancing 3rd party ASP.NET components performance by leveraging the Empty Client (an ASP.NET extension) pipeline.
Subtitle: 


Overview for ASP.NET based components that explores the basics of enhancing 3rd party ASP.NET components performance by leveraging the Empty Client (an ASP.NET..."