SUBSTRING vs LEFT in SQL SERVER

徘徊边缘 提交于 2019-12-21 03:48:35

问题


I posted a question here, but no one answer so I tried to focus what's making my query slow and a question came into my mind. Which one is faster and more efficient? LEFT or SUBSTRING?


回答1:


SQL Server is a database. You dod not ask questions of which string processing function is 'faster'. You ask the questions 'which can use an index?' and 'do I have the required index?'. Is all about data access, because disks are sloooooow, not about shifting CPU registers.

So, Which can use an index? (which one is sargable?). In theory LEFT could use an index, but in practice it usually does not. SUBSTRING cannot. Instead of SUBSTRING use Full Text.

Design your data model to take advantage of sargable expressions, index accordingly. That's all there is to it, there is no magic bullet. Avoid scans.




回答2:


There is no difference at all between left and substring because left is translated to substring in the execution plan.

For example:

select substring(col, 1, 2),
       left(col, 3)
from YourTable

will look like this in the execution plan

<DefinedValue>
  <ColumnReference Column="Expr1004" />
  <ScalarOperator ScalarString="substring([col],(1),(2))">
    <Intrinsic FunctionName="substring">
      <ScalarOperator>
        <Identifier>
          <ColumnReference Column="col" />
        </Identifier>
      </ScalarOperator>
      <ScalarOperator>
        <Const ConstValue="(1)" />
      </ScalarOperator>
      <ScalarOperator>
        <Const ConstValue="(2)" />
      </ScalarOperator>
    </Intrinsic>
  </ScalarOperator>
</DefinedValue>
<DefinedValue>
  <ColumnReference Column="Expr1005" />
  <ScalarOperator ScalarString="substring([col],(1),(3))">
    <Intrinsic FunctionName="substring">
      <ScalarOperator>
        <Identifier>
          <ColumnReference Column="col" />
        </Identifier>
      </ScalarOperator>
      <ScalarOperator>
        <Const ConstValue="(1)" />
      </ScalarOperator>
      <ScalarOperator>
        <Const ConstValue="(3)" />
      </ScalarOperator>
    </Intrinsic>
  </ScalarOperator>
</DefinedValue>



回答3:


This is a good article which benchmarks performance between SUBSTRING, LIKE, CHARINDEX, and LEFT/RIGHT if anyone's interested.

According to the results, on nonindexed columns, LEFT/RIGHT are consistently faster than SUBSTRING.




回答4:


When you use functions on the predicates, your engine will be forced to use Scan operation over Seek operation. Theoretically Left looks favor of using index smartly. But, your engine still don't know the output of Left() function until it get executed. So, it is same for Substring() also.

If you really want to tune your performance of the query, you can replace Left() expression with LIKE expression. Make sure the % wildcard character at the end. This expression will use Index Seek (if you have appropriate index on the column).

Example,

Left(MyColumn, 2) = 'AB' >> MyColumn LIKE 'AB%'

Actually, The LIKE operator (with the % wildcard character at the end), eventually converted into logical seek predicates by engine. So, the above LIKE expression will be rewritten by engine as follow as,

MyColumn LIKE 'AB%' >> MyColumn >= 'AB' and MyColumn < 'AC'

For Substring() you don't have better replacement and you have to think other alternats, like Full Text.




回答5:


Table like this

Id_num  Fname    Minit      Lname       ids
1    Karin    F     Josephs      3
2    Pirkko   O     Koskitalo       56
3        Karin    F     Josephs     16
4        Pirkko   O     Koskitalo        96
1        Karin    F     Josephs      3
2    Pirkko   O     Koskitalo        56

Substring:

Using Substring  give the initial position values and End position values.

Eg:

select SUBSTRING(Fname,2,5) from new_employees
(No column name)
arin
irkko
arin
irkko
arin
irkko

Left:

using Substring give only how many char you want from LEFT Side.

Eg:

select left(Fname,2) from new_employees

(No column name)
Ka
Pi
Ka
Pi
Ka
Pi


来源:https://stackoverflow.com/questions/18892208/substring-vs-left-in-sql-server

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!