Solved

Tunning of SQL query

Posted on 2004-08-13
5
272 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
5 Comments
 
LVL 17

Accepted Solution

by:
BillAn1 earned 25 total points
Comment Utility
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 25 total points
Comment Utility
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 42

Expert Comment

by:EugeneZ
Comment Utility
create index  on NAME field - if you have not yet
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

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.
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

743 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now