【发布时间】:2015-04-09 04:53:11
【问题描述】:
我曾经有一个简单的查询,按名称和日期连接 2 个表。
表 A:
Name Date Field1 Field2 Field3 ....
Abc 05-Apr-15 ... ... ... ...
表 B:
Name Date Field1 Field2 ....
Abc 01-Apr-15 ... ... ...
查询:
select a.*, b.*
from tableA a
outer apply
(select top 1 *
from tableB b
where b.name = a.name
and b.date < a.date) x
此查询大约需要 1 秒才能运行。
由于2个表中的名称字段存在差异,我创建了一个参考表来标准化名称。所以我构建了一个如下查询:
select a.*, b.*
from tableA a
left join refFile ref
on a.name = ref.aName
outer apply
(select top 1 *
from tableB b
left join refFile ref
on b.name = ref.bName
where isnull(ref.stdName, b.name) = isnull(ref.stdName, a.name)
and b.date < a.date) x
现在这个新查询大约需要 5 分钟才能完成。
我想知道有没有更有效的方法?
谢谢!
【问题讨论】:
-
没有任何细节我会说问题是“isnull(ref.stdName, b.name) = isnull(ref.stdName, a.name)”,因为它不能使用任何索引,因为你在特别行政区有职能。也许您可以修复/将正确的名称添加到表中?
-
我们需要查看执行计划,但正如 James 所说,第一次尝试是避免在
WHERE子句中使用函数。 -
尝试将 where 子句更改为:
ref.stdName = a.name or ref.stdName = b.name or a.name = b.name。阅读this了解更多信息 -
当您在 where 子句中使用函数时,在这种情况下为 IsNull,查询会降级。尝试通过“不为空”检查添加“和”条件
标签: sql sql-server performance tsql