Page 1 of 2 12 LastLast
Results 1 to 10 of 14
  1. #1

    Default SQL: Using LIKE statement on a clustered Index


    hi all I'm totally new to SQL optimization....

    which do you think is faster to execute

    select * from process where batch_ID = 'batch000010000'

    or select * from process where ID like 'Proc00001%'

    where ID is a clustered index and Batch_ID is just an ordinary field......


  2. #2

    Default Re: SQL: Using LIKE statement on a clustered Index

    If the field on WHERE condition does not have an index, it is slower much, so #2 statement is faster even with % at the end because index checks at the beginning. But if the % is placed at the beginning (LIKE '%00001%'), the results maybe the same.

  3. #3

    Default Re: SQL: Using LIKE statement on a clustered Index

    better use the second one. using an index is a better way!

    Good Luck!

  4. #4

    Default Re: SQL: Using LIKE statement on a clustered Index

    bro base on my exposure in SQL tuning. 2nd is preferred but if you really want to fully optimized the performance you need to consider the rest of the statement like for example the "*" and type of index, indexing criteria and so on...

  5. #5

    Default Re: SQL: Using LIKE statement on a clustered Index

    ic thx mga bro... ^_^

  6. #6

    Default Re: SQL: Using LIKE statement on a clustered Index

    anhi nalang pod ko mangutana ani nga sql. asa ang mas kusog?

    1)
    select name
    from employee
    where name like 'A%'

    2)
    select name
    from employee
    where substring(name, 1, 1) = 'A'

  7. #7

    Default Re: SQL: Using LIKE statement on a clustered Index

    Definitely #1 if name has an index.

    But of both don't have indexes, my guess is still #1 because #2 uses an additional function to get part of a string while #1 uses native SQL which I think is more optimized. The difference will be small.

    If you have a large set of data, you could try the two statements and compare the results.

  8. #8

    Default Re: SQL: Using LIKE statement on a clustered Index

    Quote Originally Posted by abloyboat
    anhi nalang pod ko mangutana ani nga sql. asa ang mas kusog?

    1)
    select name
    from employee
    where name like 'A%'

    2)
    select name
    from employee
    where substring(name, 1, 1) = 'A'
    I tested the two in a large table without index in MySQL using MyISAM as table type. My guess is the same as cold fusion but #2 is surprisingly faster.

    SELECT count(*) FROM sales1 WHERE itemcode like '048%';
    41.24 sec

    SELECT count(*) FROM sales1 WHERE substring(itemcode,1,3) = '048';
    23.03 sec

    I tested another table, but this time substring goes first.

    SELECT count(*) FROM sales2 WHERE substring(itemcode,1,3) = '048';
    59.18 sec

    SELECT count(*) FROM sales2 WHERE itemcode like '048%';
    1 min 7.81sec

    #2 is still faster, is there a logical explanation to this?

  9. #9

    Default Re: SQL: Using LIKE statement on a clustered Index

    I was wrong. Maybe #2 is difinitely faster.

    My only guess that with substring, the whole itemcode string is not retrieved but only 3 chars and compares 3 chars. While with the like % condition, all of itemcode string is retrieved and compared.

    I also observed on the 2nd table/comparison, the two has small difference percent-wise compared to the first with the order of test swapped. This is due probably also to hard-disk cache that decreases the time on the 2nd tests. So also basing on the observations, having substring comparison is really faster.

    But I think this is only conclusive with MySQL. I am very curious on the results of the above tests using Oracle, MsSQL or PostgreSQL.

  10. #10

    Default Re: SQL: Using LIKE statement on a clustered Index

    Quote Originally Posted by cold_fusion
    My only guess that with substring, the whole itemcode string is not retrieved but only 3 chars and compares 3 chars. While with the like % condition, all of itemcode string is retrieved and compared.
    Ahh.. Ok, That makes sense.

  11.    Advertisement

Page 1 of 2 12 LastLast

Similar Threads

 
  1. Help using MS Access on an inventory program
    By jinkazjinkaz in forum Programming
    Replies: 25
    Last Post: 09-11-2010, 09:37 PM
  2. question about using desktop installer on my laptop
    By Leartes in forum Software & Games (Old)
    Replies: 2
    Last Post: 07-08-2009, 07:40 PM
  3. President Arroyo's statement on the Makati standoff 11-29-07
    By cyberdud3 in forum Politics & Current Events
    Replies: 95
    Last Post: 12-17-2007, 01:12 PM
  4. Replies: 5
    Last Post: 02-08-2006, 11:16 PM
  5. Replies: 3
    Last Post: 07-07-2005, 04:26 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
about us
We are the first Cebu Online Media.

iSTORYA.NET is Cebu's Biggest, Southern Philippines' Most Active, and the Philippines' Strongest Online Community!
follow us
#top