Showing posts with label Tips. Show all posts
Showing posts with label Tips. Show all posts

T-SQL best practices - 4

Wednesday, 23 September 2009

- The dynamic SQL Performs slower because the optimizer has to create an execution plan for every execution.


-Use table aliasing, even if there is no joining in the query. Aliasing eliminates ambiguity, eases subsequent modification, and further reduces the chance of a typo not being recognized.

-When querying a table, prefix it with the owner (schema), typically dbo. There is a little performance improvement and query becomes more readable

T-SQL best practices - 3

Tuesday, 22 September 2009

Today's tips are not really related to T-SQL,but I hope these will improve the way you approach SQL SERVER.

- Get Certified
- Subscribe to News Groups and blogs to get regular updates
- Read White papers

T-SQL best practices - 2

Monday, 21 September 2009

Today's best practice techniques are


- NOLOCK helps in concurrency and performance but can be dangerous for data integrity

- Use XML datatype to pass table valued parameters to procedures.

- Use SQL Server Management Console with integrated Source Control to do the development


T-SQL best practices - 1

Sunday, 20 September 2009

Hi All!...we decided to write some posts on the best practices in writing T-SQL,which we think helpful to the newbies.We will list three best practices each day for the next seven days and hope this series will be helpful to all the readers.So today's best practices tips are,


Document your database
–Use Extended properties of the table structures to document the database.
–Use tools to generate the documentation and correct where necessary
–Maintain the script for populating the extended properties.
Use Cursors only when
–You cannot find a set based solution in 4 Hours
–You want to utilize already existing logic in other SP
–You are writing a database maintenance task
Use STATISTICS IO ON
–This will reduce the logical reads

We need your comments on this post and share the best practices that you are using in the script(related to the above 3 techniques).

Working with Hijri Date in SQL server

Saturday, 12 September 2009

Here's an example to convert the Hijri Date to Gregorian date in SQL Server


1430/09/23 is today's Hijri date.Lets see how to convert this into Gregorian date


SELECT CONVERT(DATETIME, '23/09/1430', 131)


It would return 2009-09-12 00:00:00.000


The vice-versa,conversion from Gregorian date to Hijri date would be,


SELECT CONVERT(NCHAR, GETDATE(), 131).And this returns 23/09/1430  2:52:17:920PM







Top 10 SQL server Performance tuning tips - 10

Use of performance monitoring tools:

SQL server ships with many tools like Profiler,Database Engine Tuning Advisor,Index Tuning Wizard,PerfMon,SysMon.Make use of these tools to moniter the performance of your query and take the nessesary actions,if required.
Well, with this post we come to an end of our series of Performance Tuning Steps.I hope its helpful to you to you and we will discuss each steps in detail in coming days.

Top 10 SQL server Performance tuning tips - 9

Friday, 11 September 2009

Use of NOLOCK hint

NOLOCK hint is useful to improve the query performance when selecting the rowset from a table which is being accessed by multiple transaction.But this leads to dirty read.A dirty read is where a query reads data from the database without lock protection. Without lock protection, you cannot be guaranteed that the data isn’t changing during the time that the query is running.

Top 10 SQL server Performance tuning tips - 8

Thursday, 10 September 2009

Reduce the NULLs

Reduce the use of NULLs in the database. The use of NULLs in a database can greatly reduce database performance, especially in WHERE clauses.Highly normalized database can achieve this.

Top 10 SQL server Performance tuning tips - 7

Wednesday, 9 September 2009

Remove unwanted columns from the SELECT statement
In queries, don't return column data you don't need. For example, you should not use SELECT * to return all the columns from a table if you don't need all the data from each column. In addition, using SELECT * may prevent the use of covered indexes, further potentially affect query performance.

Top 10 SQL server Performance tuning tips - 6

Tuesday, 8 September 2009

Set Based Approach

Even though procedural approach is easier for most of the programmers,it (Use of Cursor, or, UDF to process rows in a result set) should be avoided when working with large result set.Most of the times cursors can be replaced by CTE(Common Table Expression). Avoiding Cursors with SQL Server 2005

Top 10 SQL server Performance tuning tips - 5

Monday, 7 September 2009

Avoid cursors

Avoid using cursors.It may result in Deadlocks.Deadlocking occurs when two user processes have locks on separate objects and each process is trying to acquire a lock on the object that the other process has. During this kind of situation, SQL Server identifies the trouble and ends the deadlock by automatically choosing one process and aborting the other process, allowing the other process to continue.

Top 10 SQL server Performance tuning tips - 4

Sunday, 6 September 2009

Use of Indexes

  • NON CLUSTERED index should be created on a highly selective field.
  • Order of the fields in the WHERE clause should be same as the index order.
  • Every table in the DB should have a CLUSTERED index.
  • Primary Key may not be suitable for clustered index if the table is extensively used for SELECT.

Top 10 SQL server Performance tuning tips - 3

Saturday, 5 September 2009

Use of OPTIMIZE FOR

OPTIMIZE FOR a little known but powerful SQL Server feature.


DECLARE @ProdCode NVARCHAR(20)
SET @ProdCode = N’100’
SELECT ProdId,MfnDate
FROM Product
WHERE ProdCode = @ProdCode
OPTION (OPTIMIZE FOR (@ ProdCode = N’100’))

The OPTIMIZE FOR hint will force an index seek when the specific value is provided.

More about OPTIMIZE FOR/OPTIMIZE FOR UNKNOWN here

Top 10 SQL server Performance tuning tips - 2

Friday, 4 September 2009

Today's performance tuning tip.

Use of UNION/UNION ALL

UNION move the results to the tempdb, sort it, identify unique entries and retrieve the results.

UNION ALL will not do the sort, and will not identify unique entries. The results will not be unique. This can be used when the results from two queries are distinctly different

UNION will have adverse impact on the number of Reads and Writes and hence the performance.

Top 10 SQL server Performance tuning tips

Thursday, 3 September 2009

I planned to post some performance tuning steps ,mostly DB design related, for the next 10 days.One each day so that the readers wont get bored.So...Here we go..

Use SQL Profiler

Use SQL Profiler check the Duration, Reads and Writes for each Stored Procedure and for each statement within the stored procedure.Typically Reads should not exceed four times the number of rows returned. If reads are abnormally high then indexes may require a re-look or the query has to be split depending on the complexity.Set the duration filter before running the trace to identify the queries that exceed a specific duration.

Inserting values into an identity column

Friday, 19 June 2009

The identity column gets value assigned automatically when a new records inserted into the table.

Its possible to insert a value into an identity column by setting the IDENTITY_INSERT to ON.

Lets look at the following example,

--Test table with an identity column

CREATE TABLE #IDENTESTTABLE
(
ROWNO_COL INT IDENTITY(1,1),
VALUE_COL VARCHAR(15)
)
GO
--Inserting a row into the table
INSERT #IDENTESTTABLE(ROWNO_COL, VALUE_COL) VALUES (1, 'TEST VALUE')
The above insert statement would terminate with the following error,

Msg 544, Level 16, State 1, Line 1

Cannot insert explicit value for identity column in table '#IdenTestTable____000000000004'
when IDENTITY_INSERT is set to OFF.

To avoid this error set IDENTITY_INSERT to ON.Here we go,

SET IDENTITY_INSERT #IDENTESTTABLE ON

INSERT #IDENTESTTABLE(ROWNO_COL, VALUE_COL)
VALUES (1, 'FIRST ROW')

SET IDENTITY_INSERT #IDENTESTTABLE OFF.

More fun with IDENTITY here





Text Concatenation in SQL Server

Sunday, 17 May 2009

To concate the Text for a combination of keys there might be many ways.

Initially I was using either function or cursor for concatenation. But finally I found the below query most useful for the same.

First Create a table called Product with the following data.


Don't forget to create index if your table has huge data.

Then run the below Query:

SELECT
DISTINCT T.ID
,(SELECT ' ' + [DESC] FROM PRODUCT WHERE ID = T.ID FOR XML PATH('')) AS [DESC] FROM PRODUCT T

You will get the output as:



I hope this will help you guys. Share if you have any other good alternative.

Selecting random records in SQL Server

Here's a way to select the random records from a table


select top 5 name 
from sys.objects
order by newid()

This query will return 5 random tables from sys.objects each time you execute it.

Know more about newid() here