House of Fusion
Search over 2,500 ColdFusion resources here
  
Home of the ColdFusion Community

Mailing Lists
Home /  Groups /  ColdFusion Talk (CF-Talk)

WHERE Left(str,5) = 'string' VS WHERE str LIKE 'string%'

  << Previous Post |  RSS |  Sort Oldest First |  Sort Latest First |  Subscribe to this Group Next >> 
Any advantage to one over the other?
Michael Grant
09/08/10 01:20 P
With SQL Server, DEFINITELY go with left(str, 4) = 'string'
DURETTE, STEVEN J (ATTASIAIT)
09/08/10 01:23 P
What about mySQL?
Michael Grant
09/08/10 01:28 P
> What about mySQL?
Won Lee
09/08/10 02:11 P
Great suggestion. Thanks.
Michael Grant
09/08/10 02:50 P
mike,
Won Lee
09/08/10 03:45 P
Yes I did. Apparently I can't count. :D
Michael Grant
09/08/10 04:10 P
I ran a quick test....
Won Lee
09/09/10 12:18 A
Rex, wonderful results. Thanks.
Michael Grant
09/15/10 11:07 A
>>The answer is "it depends",
Claude_Schnéegans
09/15/10 11:03 A
Well, I have to apologize! I am going through some training right now
DURETTE, STEVEN J (ATTASIAIT)
09/08/10 03:15 P
Top  |   Reply  |   Original Post  |   RSS Feed  |   Subscribe to this Group
Author:
Michael Grant
09/08/2010 01:20 PM

Any advantage to one over the other?

Top  |   Parent  |   Reply  |   Original Post  |   RSS Feed  |   Subscribe to this Group
Author:
DURETTE, STEVEN J (ATTASIAIT)
09/08/2010 01:23 PM

With SQL Server, DEFINITELY go with left(str, 4) = 'string' It has much less processing overhead. Any advantage to one over the other?

Top  |   Parent  |   Reply  |   Original Post  |   RSS Feed  |   Subscribe to this Group
Author:
Michael Grant
09/08/2010 01:28 PM

What about mySQL? Do you know if this is documented and easy to find? On Wed, Sep 8, 2010 at 1:23 PM, DURETTE, STEVEN J (ATTASIAIT) < sd1985@att.com> wrote: ----- Excess quoted text cut - see Original Post for more -----

Top  |   Parent  |   Reply  |   Original Post  |   RSS Feed  |   Subscribe to this Group
Author:
Won Lee
09/08/2010 02:11 PM

> What about mySQL? > > Do you know if this is documented and easy to find? > > > http://dev.mysql.com/doc/refman/5.6/en/mysql-indexes.html

Top  |   Parent  |   Reply  |   Original Post  |   RSS Feed  |   Subscribe to this Group
Author:
Russ Michaels
09/08/2010 02:26 PM

turn on debug mode, run both versions and then look at the execution time of the cfquery, this will show you which processed faster. On Wed, Sep 8, 2010 at 7:11 PM, W ----- Excess quoted text cut - see Original Post for more -----

Top  |   Parent  |   Reply  |   Original Post  |   RSS Feed  |   Subscribe to this Group
Top  |   Parent  |   Reply  |   Original Post  |   RSS Feed  |   Subscribe to this Group
Author:
Mike Chabot
09/08/2010 03:10 PM

In SQL Server go with "like str%." The reason is that like str% is sargable and functions are not. Functions also have overhead that native set-based SQL does not. I would assume the same is true with mySQL. Native SQL is usually faster than functions as a general rule, unless the equivalent SQL is wildly complex relative to what the function is doing for you. -Mike Chabot ----- Excess quoted text cut - see Original Post for more -----

Top  |   Parent  |   Reply  |   Original Post  |   RSS Feed  |   Subscribe to this Group
Author:
Michael Grant
09/08/2010 03:13 PM

Hmmm. That seems to conflict with what Steven says. Perhaps a blood match is in order? ----- Excess quoted text cut - see Original Post for more -----

Top  |   Parent  |   Reply  |   Original Post  |   RSS Feed  |   Subscribe to this Group
Author:
Judah McAuley
09/08/2010 03:20 PM

Or, if anyone really cares, just write both the queries, fire up the Query Profiler (for MSSQL) and see what the execution plans say. ----- Excess quoted text cut - see Original Post for more -----

Top  |   Parent  |   Reply  |   Original Post  |   RSS Feed  |   Subscribe to this Group
Author:
Won Lee
09/08/2010 03:45 PM

mike, Please let us know what you find out.  I'm very curious of this myself.  As the document clearly states, mysql will use an index when you use a like but don't start the string with a wildcard.  So we know that Left(str,5) = 'string' VS WHERE str LIKE 'string%'both will use an index. The question now becomes the cost of using LEFT vs using the LIKE. BTW, I assume you meant where left(str, 6) = 'string'.

Top  |   Parent  |   Reply  |   Original Post  |   RSS Feed  |   Subscribe to this Group
Author:
Michael Grant
09/08/2010 04:10 PM

Yes I did. Apparently I can't count. :D On Wed, Sep 8, 2010 at 3:45 PM, W ----- Excess quoted text cut - see Original Post for more -----

Top  |   Parent  |   Reply  |   Original Post  |   RSS Feed  |   Subscribe to this Group
Author:
Michael Grant
09/08/2010 08:18 PM

Getting to actually test this hasn't been as easy as I'd hoped. If I can get something definitive I'll post. On Wed, Sep 8, 2010 at 3:45 PM, W ----- Excess quoted text cut - see Original Post for more -----

Top  |   Parent  |   Reply  |   Original Post  |   RSS Feed  |   Subscribe to this Group
Author:
Won Lee
09/09/2010 12:18 AM

I ran a quick test.... CREATE TABLE HoF (          ID INT NOT NULL AUTO_INCREMENT PRIMARY KEY,          LastName VARCHAR(100)        ) ENGINE = InnoDB; insert into HoF (LastName) values ('Smith'); insert into HoF (LastName) values ('Smithville'); insert into HoF (LastName) values ('Jones'); insert into HoF (LastName) values ('Smithy'); insert into HoF (LastName) values ('Smit'); select lastname from HoF where left(lastname, 5) = 'smith'; select lastname from HoF where lastname like 'smith%'; explain extended select lastname from HoF where left(lastname, 5) = 'smith'; explain extended select lastname from HoF where lastname like 'smith%'; create index lname_index on HoF (lastname(100)); explain extended select lastname from HoF where left(lastname, 5) = 'smith'; explain extended select lastname from HoF where lastname like 'smith%'; Conclusion, which seems pretty obvious now, is that Like is the better route.  LEFT will have to read every row so it can execute the LEFT function against it while the LIKE column will filter out columns that it doesn't meet the condition. It's really late and I had a tough day at work so please correct me if anyone sees anything wrong with my analysis. W

Top  |   Parent  |   Reply  |   Original Post  |   RSS Feed  |   Subscribe to this Group
Author:
rex
09/14/2010 09:48 PM

Over 100,000 queries this is what I got (in ms):     LIKE   LEFT MyISAM      16,215.60      16,069.00 InnoDB      16,168.60      15,987.80 MSSQL Server      28,268.60      26,775.20 Won's Test        1,184.40        1,168.80 INDEXED MyISAM      16,256.20      16,303.20 INDEXED InnoDB      16,112.40      16,119.00 INDEXED MSSQL Server      24,509.40      25,337.40 INDEXED Won's Test        1,156.60        1,159.00 LEFT() wins for unindexed columns, LIKE wins for indexed columns. The answer is "it depends", but eventually the answer becomes "it won't matter".  These days, the sql engine/optimizer is smart enough to decide how to approach the problem. "It depends" on certain things:     how wide your column is     if the column is indexed     how wide the search string is     how many records returned     how many records being searched     datatype of the column     etc. etc. Then eventually "it won't matter", since the results would only be nanoseconds apart (if you are comparing exactly the same columns).  This is over 100K queries and they're only ms apart.  I'd love to see other people's test results though! W ----- Excess quoted text cut - see Original Post for more -----

Top  |   Parent  |   Reply  |   Original Post  |   RSS Feed  |   Subscribe to this Group
Author:
Won Lee
09/14/2010 10:36 PM

Interesting.  Thanks for the results.  There probably is another route we could test.  We could right a function in C, compile it , and add it to mysql.  I've never done it myself but I did read that it may make your query faster.  I think I also I read that it might slow it down. BTW according to the mysql explain, the real reason why the LIKE wins in this case is when you start to join other tables.  In my example the DB server will execute against every row with the LEFT statement while the execution plan for the LIKE will try execute against 40% of the rows in the db with another table.

Top  |   Parent  |   Reply  |   Original Post  |   RSS Feed  |   Subscribe to this Group
Top  |   Parent  |   Reply  |   Original Post  |   RSS Feed  |   Subscribe to this Group
Author:
Claude_Schnéegans
09/15/2010 11:03 AM

  >>The answer is "it depends", Frankly, according to your results, I would rather answer "the difference is marginal".

Top  |   Parent  |   Reply  |   Original Post  |   RSS Feed  |   Subscribe to this Group
Author:
DURETTE, STEVEN J (ATTASIAIT)
09/08/2010 03:15 PM

Well, I have to apologize! I am going through some training right now that is telling me exactly that. I was always told before that using like was bad, but apparently it is better. Steve In SQL Server go with "like str%." The reason is that like str% is sargable and functions are not. Functions also have overhead that native set-based SQL does not. I would assume the same is true with mySQL. Native SQL is usually faster than functions as a general rule, unless the equivalent SQL is wildly complex relative to what the function is doing for you. -Mike Chabot ----- Excess quoted text cut - see Original Post for more -----


<< Previous Thread Today's Threads Next Thread >>

Search cf-talk

February 08, 2012

<<   <   Today   >   >>
Su Mo Tu We Th Fr Sa
       1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29