Is substr or LIKE faster in Oracle?

后端 未结 6 1570
野的像风
野的像风 2020-12-24 12:30

Would

WHERE substr(my_field,1,6) = \'search\'

or

WHERE my_field LIKE \'search%\'

be faster in Oracle, o

6条回答
  •  [愿得一人]
    2020-12-24 13:20

    If you have no index than there is no difference. Because oracle is doing a full table scan and evaluates the expression for each row. You can put an index on the column to speed up both queries.

    CREATE INDEX my_like_idx
    ON my_table( my_field );
    

    This index is more flexible and speeds up the query using like. It will work for any compare starting with characters and having placeholder (%) at the end. Oracle is doing a index range scan to find all matching rows.

    CREATE INDEX my_substr_idx
    ON my_table( substr( my_field,1,6 ) );
    

    This index speeds up the query with substr. But the index is very special to compare only the first 6 characters.

    If you query for a piece of starting in the middle. Creating a function based index will help.

    WHERE substr(my_field,2,5) = 'earch'
    WHERE my_field like '%earch%'
    

提交回复
热议问题