LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-17-2006, 06:03 PM   #1
rjcrews
Member
 
Registered: Apr 2004
Distribution: Debian
Posts: 193

Rep: Reputation: 30
MS SQL - speed question.


I know, I know.

Quick question though, I think should be fairly trivial ( I am new to MSSQL and databases in general)

2 queries, 1 is fast, the other is slow. MS SQL database.
Slow:
Code:
WHERE field1='%' OR field2='abcdef'
--or--

Code:
WHERE field1 LIKE '%' AND field2='abcdef'
Why is the second one slower? I always thought using LIKE was slower. Would the data type matter (if the field is an integer field?) The field is not indexed, the database has over 3 millions rows. field2 is indexed. Thoughts on this are appreciated. I am having a hard time finding speed differences concerning AND and OR. This is a pretty simple query, no joins or unions. All things are equal except that portion of the query.
 
Old 11-17-2006, 07:42 PM   #2
kstan
Member
 
Registered: Sep 2004
Location: Malaysia, Johor
Distribution: Dual boot MacOS X/Ubuntu 9.10
Posts: 851

Rep: Reputation: 31
definitely,
LIKE '%' ==> it search and return eveything to your
field1='%'==> not very sure, probably it search '%' only.
I think the same thing applied to all kind of database...
Ks
 
Old 11-17-2006, 07:45 PM   #3
rjcrews
Member
 
Registered: Apr 2004
Distribution: Debian
Posts: 193

Original Poster
Rep: Reputation: 30
Yea you know what, I posted it backwards. The one with LIKE is faster. That is what I cannot understand.


See , i am under the impression (possible incorrectly) that it matters what type of field it is, say integer or varchar. Also, trying to find out if there is differences between AND and OR.

I am making the database bigger, and searching around various SQL sites to try to find an answer.

EDIT** i posted correct, the one with LIKE is quicker, still cannot figure why

Last edited by rjcrews; 11-17-2006 at 07:46 PM.
 
Old 11-17-2006, 08:27 PM   #4
gruntwerk
Member
 
Registered: Dec 2003
Location: PA
Distribution: fc9
Posts: 89

Rep: Reputation: 15
one would think if field2 is indexed, you would use that field first.

(* i know, the server should optimize that and do that on its own, but why not write it correctly just incase..*)

as for speed difference..

case one
the server is look at each row, checking the first expression
if it matches
DONE
if not
looking at the second expression
if it matches
done
endifs

Case 2
looking at first expression
if it matches
does it match second expression
if it does
DONE
endifs
the server is looking at each row and evaluating the first expression and then then second.

as i stated at the top, the server is probably rearraning your query so that it is looking at the indexed field first.

however in case one, your have an OR, so it needs to look at field one for every single row...

in case two... so, it is only looking at the rows that matter (*i.e. field2 already matches*), AND THEN checking the to see if it matches the first expression.

SO, I would venture to guess that the LIKE statement is not the cause of the speed difference, but rather the OR statement.
 
Old 11-17-2006, 10:45 PM   #5
rjcrews
Member
 
Registered: Apr 2004
Distribution: Debian
Posts: 193

Original Poster
Rep: Reputation: 30
Good point, I was thinking of it like that.

Finding every single row probably isn't ideal. If it is optimizing itself, which it should be as i understand it, using the indexed field and the AND statement is pretty quick.

The OR statement requires checking every field every time, which is dumb.


Thanks!
 
Old 11-18-2006, 08:16 AM   #6
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 65
Does mssql do explain? That's always useful when trying to figure out performance.

Another thing, it's possible that the query optimiser recognises that LIKE '%' matches any non-null value, and doesn't have to do any work to check it...
 
Old 11-18-2006, 03:11 PM   #7
gruntwerk
Member
 
Registered: Dec 2003
Location: PA
Distribution: fc9
Posts: 89

Rep: Reputation: 15
I thought about explain as well.
quick google didnt lead me to the tool that M$ sells for that function...
cant be built in... that would be too handy...
 
Old 11-18-2006, 03:26 PM   #8
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 65
Quote:
Originally Posted by gruntwerk
I thought about explain as well.
quick google didnt lead me to the tool that M$ sells for that function...
cant be built in... that would be too handy...
Bummer. Switch to postgres, it kicks butt.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Simple SQL question joelhop Programming 1 10-06-2006 01:39 PM
SQL Question FlowState Programming 7 09-29-2006 04:13 AM
sql plus question puishor Linux - Software 3 10-23-2005 02:09 PM
SQL question jpc82 Programming 5 08-13-2005 11:05 AM
SQL Question jacksmash Programming 7 01-18-2004 10:10 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:45 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration