查看以下示例。它表明在 unicode 字符串 (nvarchar) 中搜索的错误几乎是在 varchar 字符串中搜索的八倍。与隐式转换相当。正在寻找对此的解释。或者一种更有效地在 nvarchar 字符串中搜索的方法。
use tempdb
create table test
(
testid int identity primary key,
v varchar(36),
nv nvarchar(36),
filler char(500)
)
go
set nocount on
set statistics time off
insert test (v, nv)
select CAST (newid() as varchar(36)),
CAST (newid() as nvarchar(36))
go 1000000
set statistics time on
-- search utf8 string
select COUNT(1) from test where v like '%abcd%' option (maxdop 1)
-- CPU time = 906 ms, elapsed time = 911 ms.
-- search utf8 string using unicode (uses convert_implicit)
select COUNT(1) from test where v like N'%abcd%' option (maxdop 1)
-- CPU time = 6969 ms, elapsed time = 6970 ms.
-- search unicode string
select COUNT(1) from test where nv like N'%abcd%' option (maxdop 1)
-- CPU time = 6844 ms, elapsed time = 6911 ms.
最佳答案
Looking for an explanation for this.
NVarchar 是 16 位,Unicode 比较规则比 ASCII 复杂得多 - 同时支持的各种语言的特殊字符需要引用更多处理。
关于sql-server - SQL Server 在 nvarchar 字符串内部搜索时使用高 CPU,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4717520/