【问题标题】:Charindex in inner join affects the perfomance内连接中的 Charindex 影响性能
【发布时间】:2016-05-03 03:19:19
【问题描述】:

我有一个有以下查询的程序,

Select 
 a.column
,count(distinct(c.column))
from `table1` a with (nolock)
inner join `table2` b with(`nolock`) on a.id=b.id
inner join `table3` c with (`nolock`) on charindex(c.colum,a.column)>0
group by 
 a.column

table1 有近 36,000 行,查询需要近 40 秒来加载结果集。

a.column 上安装了 fts 索引,它是 xml 数据类型。

我尝试拆分联接,但结果因分组而异。

如何重写此查询以使其更快?

【问题讨论】:

    标签: sql charindex


    【解决方案1】:

    当您在另一列上比较/连接一列时,您必须使用 LIKE 关键字。 CHARINDEX、LEFT 和 RIGHT 函数不允许 SQL 服务器使用索引。 您要问的是 C.column 是否在 A.Column 中,
    解决办法是:
    而不是:

    <CODE>
        charindex(c.column,a.column)>0
    </CODE>
    

    使用

    <CODE>
        WHERE a.column LIKE '%' + c.column + '%'
    </CODE>
    

    或者,尝试去除 c.column,使其与 A.column 匹配。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-02-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多