Solved

Tunning of SQL query

Posted on 2004-08-13
5
284 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

I have a large data set and a SSIS package. How can I load this file in multi threading?
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
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.

895 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

17 Experts available now in Live!

Get 1:1 Help Now