【问题标题】:Oracle - SQL Statement Poor Performance - Fuzzy Matching LogicOracle - SQL 语句性能不佳 - 模糊匹配逻辑
【发布时间】:2018-04-04 10:11:58
【问题描述】:

我有一个模糊匹配要求.. for ex;

Table1 - T1Col1, T1col2, T1col3, T1col4, T1col5. 
Table 2 - T2Col1, T2col2, T2col3, T2col4, T2col5. 

所以我的要求是

  • T1 - 不需要所有字段,即 T1col1、T1col2、T1col3、T1col4、 T1col5 不是空值,但在某些情况下 T1Col2 是 已填充且 T1Col3、T1Col4 和 5 为空。最好的情况在这里 是不是所有字段都不是空值,最坏的情况是除了 T1Col1 休息 的字段为空。

  • 我想出了一个模糊逻辑匹配,这样如果至少一个字段是 匹配 then 'where' 子句应该通过。

select count(*) from T1, T2
where
  Nvl(T1COl1, nvl(T2Col1, 'x')) = nvl(T2Col1, 'x') and
  Nvl(T1COl2, nvl(T2Col2, 'x') ) = nvl(T2Col2, 'x') and
  Nvl(T1COl3, nvl(T2Col3, 'x'))  = nvl(T2Col3, 'x') and
  Nvl(T1COl4, nvl(T2Col4, 'x')) = nvl(T2Col4 'x') and
  and substr(T1COl5, 1,1) = T2Col5
  ;

T1 和 T2 中的记录数分别为 243000 和 55000 条记录 当我运行上述语句时,它需要 1426.809 秒并给了我 11349 条记录。看起来它的性能很差。 是因为在 where 子句中使用了 substr 还是使用了太多 NVL?

您能帮我看看如何提高我的查询性能还是有更好的方法来进行匹配?

【问题讨论】:

  • 为什么从 T1、T2 中选择 count(*)。为什么是交叉产品?你可以使用连接吗?
  • @PraneetNadkar,OP 正在进行连接。 (旧样式,隐式连接。)
  • 今日提示:切换到现代显式 JOIN 语法。更容易编写(没有错误),更容易阅读(和维护),并且在需要时更容易转换为外连接。
  • 我在 Oracle 上运行此查询。更新了我原来的帖子。

标签: sql oracle query-performance


【解决方案1】:

也许只是个人喜好,但我会这样写:

select count(*)
  from t1
      ,t2
 where (t1col1 is null or t1col1 = t2col1)
   and (t1col2 is null or t1col2 = t2col2)
   and (t1col3 is null or t1col3 = t2col3)
   and (t1col4 is null or t1col4 = t2col4)
   and substr(t1col5, 1, 1) = t2col5;
  • 更清晰的逻辑,t1 列中的 null 值是可以的。
  • 无需 nvl 计算
  • 可以在列上使用索引。

此查询中的逻辑略有不同。此查询不匹配 t1col1 中的值“x”与 t2col1 中的空值。

【讨论】:

    猜你喜欢
    • 2016-10-24
    • 1970-01-01
    • 2019-05-08
    • 2022-01-08
    • 1970-01-01
    • 1970-01-01
    • 2021-08-19
    • 1970-01-01
    • 2014-07-24
    相关资源
    最近更新 更多