Solved

Tunning of SQL query

Posted on 2004-08-13
5
296 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
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 25 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 42

Expert Comment

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

Featured Post

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

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

Let's review the features of new SQL Server 2012 (Denali CTP3). It listed as below: PERCENT_RANK(): PERCENT_RANK() function will returns the percentage value of rank of the values among its group. PERCENT_RANK() function value always in be…
Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
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.

803 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