Friday, 16 May 2014

Sql server Performance Techniques

1.               Avoid * in SELECT statement
Practice to avoid * in Select statement since SQL Server converts the * to columns name before query execution. One more thing, instead of querying all columns by using * in select statement, give the name of columns which you required.
1.   -- Avoid
2.  SELECT * FROM tblName
3.  --Best practice
4.  SELECT col1,col2,col3 FROM tblName

2.     Use EXISTS instead of IN

Practice to use EXISTS to check existence instead of IN since EXISTS is faster than IN.
1.   -- Avoid 
2.  SELECT Name,Price FROM tblProduct 
3.  where ProductID IN (Select distinct ProductID from tblOrder)
4.  --Best practice 
5.  SELECT Name,Price FROM tblProduct 
6.  where ProductID EXISTS (Select distinct ProductID from tblOrder)

3.    Avoid Having Clause

Practice to avoid Having Clause since it acts as filter over selected rows. Having clause is required if you further wish to filter the result of an aggregations. Don't use HAVING clause for any other purpose.

4.               Create Clustered and Non-Clustered Indexes

Practice to create clustered and non clustered index since indexes helps in to access data fastly. But be careful, more indexes on a tables will slow the INSERT,UPDATE,DELETE operations. Hence try to keep small no of indexes on a table.

5.               Keep clustered index small

Practice to keep clustered index as much as possible since the fields used in clustered index may also used in nonclustered index and data in the database is also stored in the order of clustered index. Hence a large clustered index on a table with a large number of rows increase the size significantly. Please refer the article Effective Clustered Indexes

6.               Avoid Cursors

Practice to avoid cursor since cursor are very slow in performance. Always try to use SQL Server cursor alternative. Please refer the article Cursor Alternative.

7.    Use UNION ALL inplace of UNION

Practice to use UNION ALL in place of UNION since it is faster than UNION as it doesn't sort the result set for distinguished values.

8.               Use Schema name before SQL objects name

Practice to use schema name before SQL object name followed by "." since it helps the SQL Server for finding that object in a specific schema. As a result performance is best.
1.   --Here dbo is schema name
2.  SELECT col1,col2 from dbo.tblName
3.  -- Avoid
4.  SELECT col1,col2 from tblName 
9.    Avoid unnecessary columns in the SELECT list and unnecessary tables in join conditions
·         Selecting unnecessary columns in a Select query adds overhead to the actual query, specially if the unnecessary columns are of LOB types.
·         Including unnecessary tables in join conditions forces the database engine to retrieve and fetch unnecessary data and increases the query execution time.

10.Try not to use COUNT(*) to obtain the record count in a table
·         To get the total row count in a table, we usually use the following Select statement:
http://www.codeproject.com/images/minus.gif Collapse | Copy Code
SELECT COUNT(*) FROM dbo.orders
This query will perform a full table scan to get the row count.
·         The following query would not require a full table scan. (Please note that this might not give you 100% perfect results always, but this is handy only if you don't need a perfect count.)
http://www.codeproject.com/images/minus.gif Collapse | Copy Code
SELECT rows FROM sysindexes 
WHERE id = OBJECT_ID('dbo.Orders') AND indid < 2
10.                       With (NOLOCK)


Wednesday, 14 May 2014

SQL Server Reporting Services 2012 - What's New?

SQL Server Reporting Services 2012 - What's New?

Microsoft have released SQL Server 2012!  I thought I'd blog on the differences for Reporting Services 2012 (and on the SQL language and SQL Server Management Studio in a separate blog).

Summary of Differences for SSRS 2012

The short answer to what's changed in SSRS 2012 is: everything, and nothing.
Everything, because the entire software has been rewritten:
SSRS 2012 menu
The editing tool is no longer called BIDS, and it runs inside Visual Studio 2010, not 2008.
 
Nothing, because once you get inside the software, everything looks the same:
Creating a report in SSRS 2012
Look familiar? It should do if you're used to Reporting Services 2008.
There's one important exception to this rule, which is if you're using Reporting Services within SharePoint.  I'll cover this under a separate heading below.

SQL Server Data Tools and BIDS

Here's a before and after for SSRS 2012:
WhatBeforeAfter
SoftwareVisual Studio 2008Visual Studio 2010
Name for softwareBusiness Intelligence Development Studio (BIDS)SQL Server Data Tools
This sounds like there's been a huge change, and from Microsoft's point of view, there has.  Visual Studio 2010 was completely written using something called WPF.  Unfortunately, SSRS 2008 R2 and Visual Studio 2010 were developed in parallel, which is why SSRS 2008 R2 wasn't included within Visual Studio 2010 when they were each first released.  Microsoft have now rectified this.
Do you care about all of this?  Probably not.  SQL Server Data Tools is virtually identical to BIDS (I include the word virtually to cover myself - I haven't yet found a difference).

SQL Server Reporting Services within SharePoint

There's one caveat for the above nothing-has-really-changed summary: SharePoint.  If you're using SSRS within SharePoint, you have two new features available to you.
  • PowerView is an SSRS add-in for SharePoint Server 2010 Enterprise Edition.  It provides drag-and-drop ad hoc reporting, using a new file format (RDLX).  It's similar to PowerPivot for Excel.
  • Alerts allow you to send emails to report users when data changes in a predefined way (think of them as dynamic subscriptions, and you won't be far wrong).
Wise Owl will be continuing to run standalone Reporting Services courses for the forseeable future, but existing clients are welcome to visit our office to see a demonstration of PowerView and Data Alerts within SharePoint.

Excel and Word 2007 and 2010 Reports

If you choose to send your SSRS 2012 report to Word or Excel, version 2007/2010 rendering is now supported:
Word and Excel reports
You can now render reports as .docx or.xlsx.
 
This feature is also included in Report Builder for SQL Server 2012.

Report Builder Changes

Finally, how have Microsoft upgraded Report Builder?  Answer: hardly at all.  As far as I can tell, there is no such thing as Report Builder 4.0 - just a slightly upgraded version of Report Builder 3.0 for use with SQL Server 2012.
I strongly suspect that Report Builder will eventually be left to die out, to be superseded by PowerPivot for Excel.  I'd be surprised if it receives any more significant upgrades.

Difference Between Sql server 2008R2 and 2012

SQL Server 2008 R2 (SQL Server 10.5) :

1.SQL Server 2008 R2 is codenamed as Kilimanjaro
2.In SQL Server 2008 R2 , rebooting is requisite for OS patching , hence server down time is high
3.SQL Server 2008 R2 does not have this feature of availability groups, hence fast recovery is not possible.
4.The SQL Server 2012 uses 48 bit precision for spatial calculations
5.CONCAT(), FORMAT() and TRY_CONVERT() functions are not available in SQL Server 2008
6.SQL Server 2008 R2 is slow compared to SQL Server 2012.
7.However buffer rate is less because there is no data redundancy in SQL Server 2008 R2
8.Data visualization is not supported in SQL Server 2008 R2
9.Spatial features are not supported more in SQL Server 2008 R2. Instead a traditional way for geographical elements have been set in SQL Server 2008 R2.
10.The Maximum number concurrent connections to SQL Server 2008 is 32767.
11. SQL Server have old CONVERT() function for convert date format. for that ween need to memorizing cryptic style codes like 101 and 103 for converting datetime values to localized presentation formats
CONVERT(DATETIME, '7/24/2010 12:00:00 AM', 101)
12. There is no direct way to cast a money/currency field in SQL Server 2008

SQL Server 2012 (SQL Server 11) :

1.SQL Server 2012 is codenamed as Denali
2.In SQL Server 2012, server down time is reduced by 50% , hence OS patching is not rebooting n times.
3.In SQL Server 2012, high availability and disaster recovery factor has been introduced which duplicates the data and rapidly recovers the loss.
4.The SQL Server 2012 uses 48 bit precision for spatial calculations
5.CONCAT(), FORMAT() and TRY_CONVERT() functions are newly included in SQL Server 2012
6.In SQL Server 2012, the performance is 10 times faster than the predecessor.
7.Buffer rate is high in SQL Server 2012 because of data compression.
8.Data visualization tool is available in SQL Server 2012.This allows snapshots of data.
9.Support for persistent computed columns and extra geographical approach is possible with spatial features in SQL Server 2012.
10.SQL server 2012 has unlimited concurrent connections.

11. SQL Server 2012 come up with new function FORMAT() same as .format() C# function for change the date/currency format etc. 
This example displays the date in Taiwan format. Taiwan uses traditional Chinese characters.
DECLARE @date DATETIME = '12/21/2011';
SELECT FORMAT ( @date, 'MMMM dddd d', 'zh-TW' ) AS FormattedDate;

In US format
DECLARE @date DATETIME = convert(datetime,'2011/01/01 2:00:00');
SELECT FORMAT ( @date, 'yyyy/MM/dd hh:mmConfuseds tt','en-US' ) AS FormattedDate;
Result
FormattedDate
2011/01/01 02:00:00 AM

12. SQL Server 2012 have the solution for cast a money field, this is done by FORMAT() function
Example
Let's see some of the numeric formats that we can display using the FORMAT() function. Personally, I format numbers for currencies and number of decimal characters or for percentage. Here are some examples.

To display the number with currency using locale, use the following example.
Example 1:

DECLARE @money money = '125000';
SELECT FORMAT ( @money, 'C') AS MyMoney;
Result:
$125,000.00

Here, I am getting the currency symbol ‘$’ because my current locale language setting is en-us. I could also display the currency ‘$’ explicitly by using the culture parameter as shown below.
DECLARE @money money = '125000';
SELECT FORMAT ( @money, 'C', 'en-US' ) AS MyMoney;
for more example http://www.databasejournal.com/features/...-2012.html

Tuesday, 13 May 2014

Custom Split function to split comma separated string into table

CREATE FUNCTION dbo.Split(@String nvarchar(4000), @Delimiter char(1))
RETURNS @Results TABLE (Items nvarchar(4000))
AS
BEGIN
DECLARE @INDEX INT
DECLARE @SLICE nvarchar(4000)
-- HAVE TO SET TO 1 SO IT DOESNT EQUAL Z
--     ERO FIRST TIME IN LOOP
SELECT @INDEX = 1
WHILE @INDEX !=0
BEGIN
-- GET THE INDEX OF THE FIRST OCCURENCE OF THE SPLIT CHARACTER
SELECT @INDEX = CHARINDEX(@Delimiter,@STRING)
-- NOW PUSH EVERYTHING TO THE LEFT OF IT INTO THE SLICE VARIABLE
IF @INDEX !=0
SELECT @SLICE = LEFT(@STRING,@INDEX - 1)
ELSE
SELECT @SLICE = @STRING
-- PUT THE ITEM INTO THE RESULTS SET
INSERT INTO @Results(Items) VALUES(@SLICE)
-- CHOP THE ITEM REMOVED OFF THE MAIN STRING
SELECT @STRING = RIGHT(@STRING,LEN(@STRING) - @INDEX)
-- BREAK OUT IF WE ARE DONE
IF LEN(@STRING) = 0 BREAK
END
RETURN
END
Once we create custom function Split() run sample query like as shown below


SELECT items FROM [dbo].[Split] ('861,739,10,1670', ',') 
Once we run above query we will get output like as shown below

Output