Indexes play an important role in SQL query performance, but improper knowledge of how indexes work can lead to degraded performance. In this article I will throw some light on one such practices. We all know that indexes help in select statements and make other statements (insert, update, delete) slower. At the same time if we use a function on an indexed column in a where statement then it does not use the index and hence the query performs the same as when it does not have a index. Let's look ar a realistic example.
Step 1: Create a table Person
- CREATE TABLE [dbo].[Person](
- [ID] [char](800) NULL,
- [FirstName] [char](2000) NULL,
- [LastName] [char](3000) NULL,
- [City] [char](500) NULL
- )
- INSERT INTO Person (ID,FirstName,LastName,City)
- SELECT TOP 1000 ROW_NUMBER() OVER (ORDER BY a.name) RowID,
- ‘Vineet’,
- CASE WHEN ROW_NUMBER() OVER (ORDER BY a.name)%2 = 1 THEN ‘Sanjay’
- ELSE ‘Mahesh’ END,
- CASE WHEN ROW_NUMBER() OVER (ORDER BY a.name)%10 = 1 THEN ‘New Delhi’
- WHEN ROW_NUMBER() OVER (ORDER BY a.name)%10 = 5 THEN ‘Chennai’
- WHEN ROW_NUMBER() OVER (ORDER BY a.name)%10 = 3 THEN ‘Hyderabad’
- ELSE ‘Bangalore’ END
- FROM sys.all_objects a
- CROSS JOIN sys.all_objects b
- GO
- SELECT * FROM Person
- WHERE City = ‘Chennai’
The following is the execution plan:

Look at the query execution plan, SQL Server did a table scan and that is expected because there is no index on the City column.
Step 4: Create a non-clustered index on the City column as in the following:
- CREATE NONCLUSTERED INDEX Index_Person_City
- ON Person (City);
- SELECT * FROM Person
- WHERE City = ‘Chennai’

Step 6: Now let's execute a select query that uses a function on the column name in the where clause/
- SELECT * FROM Person
- WHERE CONVERT(Varchar(500), City) = ‘CHENNAI’

Conclusion
So in summary, the preceding procedure shows that SQL Server was able to do a seek operation on the City column but when a function is used on the column it was not able to do a seek and hence it performed a scan operation.
Danny SchneiderPosted Jul 9, 2015, 1:54 AM
Hi, but what's the message of this? I'mean a split of percentage does not automatically mean a better performance. Which of the both functions is faster? Index scan or key loockup? What's the performance boost in real time?
Karthik Muthu KaruppanPosted Apr 3, 2015, 11:40 AM
Good start
NitinPosted Apr 3, 2015, 6:54 AM
Nice
Vineet KumarPosted Apr 3, 2015, 4:17 AM
Ganesh Saraf Let me know what information is required?
Ganesh SarafPosted Apr 3, 2015, 2:45 AM
Required more explanation.
Pankaj Kumar ChoudharyPosted Apr 2, 2015, 11:08 PM
Really it is a concept making article sir.............
Manvendra SinghPosted Apr 2, 2015, 10:32 PM
Short and crisp. Expecting a series of articles on sql performance :)
Vithal WadjePosted Apr 2, 2015, 10:14 PM
Nice start,keep it up