所以你必须选择:
- 使用
full text search:
如果数据量很大并且您正在寻找可扩展的数据搜索,则此方法更可取,但更难维护。所以我建议你在该表中添加一个computed column 并在其上放置全文索引:
alter table tablename
add column cmptcolumn as concat_ws(',',EmployeeId,FullName,PhoneNumber,...)
--full text catalog
CREATE FULLTEXT CATALOG catalogName AS DEFAULT;
-- full text index
create full text index on tablename (cmptcolumn)
-- search :
select * from tablename
where contain(cmptcolumn, 'SearchString');
通过全文搜索,您可以搜索同义词以及彼此相关的单词:
select * from tablename
where freetext(cmptcolumn, 'SearchString');
阅读更多关于不同的全文搜索选项here
- 使用搜索查询。再次女巫,您可以从计算列中受益或分别在每列中搜索:
select *
from tablename
where (Fullname like '%'+@fullNameSearchString+'%' or @fullNameSearchString is null)
and (Department = @DepartmentSearchString or @DepartmentSearchString is null)
and ...
虽然第一种方法是搜索 insode 字符串的更快方法,但第二种方法提供更准确的结果。但是“FreeText”也会查找单词的含义,在这种情况下它可能会更慢。
在第二种方法中,无论您采用哪种方式(使用或不使用计算列),都需要在列上设置索引以提高性能,但是使用类似 '%%' 通常不能使用索引应该。