?
Solved

Tunning of SQL query

Posted on 2004-08-13
5
Medium Priority
?
323 Views
Last Modified: 2008-03-17
I have a table which have more than 4m (4,000,000) records.
When i query as
SELECT * FROM TBL_RECORD WHERE NAME = 'test', it takes 2 seconds.
When i use like it takes 4 seconds.
SELECT * FROM TBL_RECORD WHERE NAME like '%test%'

I want to use the like condition in a stored procedure, but can anyone please let me what other statement can i add to faster the query.
0
Comment
Question by:saroopchand
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 17

Accepted Solution

by:
BillAn1 earned 100 total points
ID: 11796403
unfortunately the like '%test%' will slow things down a lot.
If you dont need it, you might want to remove the leading % - like 'test%' will be a much quicker.
The problem is that with a LIKE comparison, it cannot use an index, and it has to scan through the whole text of each field. If you use like 'test%' it at least only has to match the start of the string.
What is your dat like? could you normalise it? If there are only a small (say 1000's ) of distinct values of text, you could put the text into a small table with an integer key, then link this table your data. Then the LIKE only has to be done on much less # of records, and the main fact table can be scanned on an integer index. Much quicker.
0
 
LVL 50

Assisted Solution

by:Lowfatspread
Lowfatspread earned 100 total points
ID: 11796835
as BillAn1 has replied,

the only other aspect to consider is do you need all the columns returned? (less should be quicker)

could you go Case INSENSITIVE (Should be Faster , at expense of Disk space)

If you dont want all columns returned , a covering index of the columns you want plus the name  column
may be faster... (again at the expense of disk on Maintenance I/O)

what is the Data Type of NAME ?

Charindex/Patindex can sometimes be faster

hth



 
0
 
LVL 43

Expert Comment

by:Eugene Z
ID: 11797529
create index  on NAME field - if you have not yet
0

Featured Post

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.
Suggested Courses

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question