[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Tunning of SQL query

Posted on 2004-08-13
5
Medium Priority
?
330 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Windocks is an independent port of Docker's open source to Windows.   This article introduces the use of SQL Server in containers, with integrated support of SQL Server database cloning.
An alternative to the "For XML" way of pivoting and concatenating result sets into strings, and an easy introduction to "common table expressions" (CTEs). Being someone who is always looking for alternatives to "work your data", I came across this …
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

656 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