views:

73

answers:

2

I found one big issue.

I have added the Lower function to indexed column of one of the table to fetch the data. The table contains more then 1 lac records.

While fetching the records, the cpu usage goes to 100%.

I could not understand, how this much drastic change can happen just because of Lower() function.

Please Help!

+4  A: 

When you add LOWER() (or any function) around a column it is no longer possible to use an index (it is no longer SARG-able).

By default, SQL Server is not case sensitive, so you should be able to remove it.

Mitch Wheat
I second this - only if your collation was set to something case-sensitive would LOWER even be necessary - your string comparisons are case-insitive by default.
rwmnau
The column contains the Unicode Characters and when the character are Lowered, the Original occurrence get disturbed. Due to this, the comparison becomes Case Sensitive.
Shivkant
+4  A: 

What you could do, if you really need this query a lot, is create a persisted computed column that uses the LOWER() function. Index that column and you should be fine again:

ALTER TABLE dbo.YourTableName
  ADD LowerFieldName AS LOWER(YourFieldName) PERSISTED

CREATE NONCLUSTERED INDEX IX_YourTableName_LowerFieldName
  ON dbo.YourTableName(YourFieldName)

That would keep a lower-case representation of your field in your table, it's always up to date, and since it's persisted, it's part of your table and doesn't incur the penalty of the LOWER() function. Put an index on it and your search should be as fast as it was before.

Links:

marc_s
Thanks for this. Can u provide me some link which can elaborate and describes more about it.
Shivkant