我正在尝试寻找一种性能智能的分辨率来搜索进入应用程序的数据,该数据已删除了字符空间,但在数据库系统中可能以多种方式存在空格。我在想以下内容,但似乎失败了:
select top 1 * where REPLACE(Mfr, ' ', '') = @Mfr and REPLACE(Model, ' ', '') = @Model
我这样做是完全错误的,还有更好的方法吗?目前看来,只有一个具有15万条记录的数据库超时。
答案 0 :(得分:1)
您可以将表达式实现为索引虚拟列,以便快速查找。
首先,您需要创建虚拟列:
alter table t add clean_mfr as replace(Mfr, ' ', '');
alter table t add clean_model as replace(Model, ' ', '');
然后您将它们编入索引:
create index ix1 on t (clean_mfr, clean_model);
现在,您可以再次尝试查询。由于您在两个虚拟列查询中都使用了等于(=
,因此查询应该是瞬时的。
尝试:
select top 1 *
from t
where REPLACE(Mfr, ' ', '') = @Mfr and REPLACE(Model, ' ', '') = @Model
或者:
select top 1 *
from t
where clean_mfr = @Mfr and clean_model = @Model