SELECT [BusinessEntityID],[NationalIDNumber],[LoginID],[OrganizationNode]
FROM [AdventureWorks2014].[HumanResources].[Employee]
where BusinessEntityID = 5
Run Code Online (Sandbox Code Playgroud)
在实际执行计划中执行查询后,我得到了 SQL Server 调用的 convert_implicit() 函数。
SELECT [BusinessEntityID],[NationalIDNumber],[LoginID],[OrganizationNode]
FROM [AdventureWorks2014].[HumanResources].[Employee]
where BusinessEntityID = convert(int,5)
Run Code Online (Sandbox Code Playgroud)
显然,当我将标量值显式转换为整数类型时,不会调用 convert_implicit() 函数。有什么方法可以避免在不使用显式转换的情况下对标量 int 值进行隐式转换。另外我想知道隐式转换、显式转换和根本没有转换之间的性能差异。
我在 name 列上创建了一个带有主键的表,表的排序基于 name 列
create table TestPrimaryKey (Name varchar(20) primary key,id int identity)
insert into TestPrimaryKey (SomeText) values ('john')
insert into TestPrimaryKey (SomeText) values ('ryan')
insert into TestPrimaryKey (SomeText) values ('jennifer')
insert into TestPrimaryKey (SomeText) values ('martha')
insert into TestPrimaryKey (SomeText) values ('luke')
insert into TestPrimaryKey (SomeText) values ('alex')
insert into TestPrimaryKey (SomeText) values ('wayne')
Run Code Online (Sandbox Code Playgroud)
现在我将主键上的聚集索引更改为非聚集索引,排序仍然基于名称列
begin tran
alter table dbo.TestPrimaryKey drop constraint PK__TestPrim__BC2905030401D947
alter table dbo.TestPrimaryKey add constraint PK__TestPrim__BC2905030401D947 primary key nonclustered (SomeText)
commit tran
Run Code Online (Sandbox Code Playgroud)
现在我正在另一个 (id) 列上创建一个唯一的聚集索引,但顺序仍然相同 …
sql-server primary-key clustered-index nonclustered-index table