SQL Server选择了非聚集索引而不是聚集索引
聚集索引不仅包含索引的key值,还包含表数据;
非聚集索引只包含索引的key值。
SQL Server在有些情况下,有聚集索引和非聚集索引存在时,会选择走非聚集索引,而不走聚集索引。
例子如下:
在 SQL Server 的adventureWorks数据库下,运行如下语句:
select DepartmentID,Name from HumanResources.Department
Department表上有两个索引,一个是departmentid上的聚集索引,另一个是name上的非聚集索引;
该语句的运行结果如下:
DepartmentID Name
12 Document Control
1 Engineering
16 Executive
14 Facilities and Maintenance
10 Finance
9 Human Resources
11 Information Services
4 Marketing
7 Production
8 Production Control
5 Purchasing
13 Quality Assurance
6 Research and Development
3 Sales
15 Shipping and Receiving
2 Tool Design
可以看到结果是按在name的升序排列的,说明使用的是name上的非聚集索引。
如果存在另外一个没有索引的列,则会选择走departmentid上的聚集索引,因为sql server会认为这样的速度比较快。
select DepartmentID,Name,GroupName from HumanResources.Department
DepartmentID Name GroupName
1 Engineering Research and Development
2 Tool Design Research and Development
3 Sales Sales and Marketing
4 Marketing Sales and Marketing
5 Purchasing Inventory Management
6 Research and Development Research and Development
7 Production Manufacturing
8 Production Control Manufacturing
9 Human Resources Executive General and Adm
相关文档:
可以定义一个无论何时用INSERT语句向表中插入数据时都会执行的触发器。
当触发INSERT触发器时,新的数据行就会被插入到触发器表和inserted表中。inserted表是一个逻辑表,它包含了已经插入的数据行的一个副本。inserted表包含了INSERT语句中已记录的插入动作。inserted表还允许引用由初始化INSERT语句而产生的日志数据 ......
一个简单的例子:
先建一个C#类:
引用System.Data.Linq.dll程序集,
using System.Data.Linq.Mapping和
using System.Data.Linq 两个空间。
[Table]
public class Inventory
{
[Column]
public string Make;
[Column]
public string Color;
&nbs ......
注释:只适合单表单列数据,
create database test
go
use test
go
create table users
(
:id int identity(1,1) primary key not null,
:name nvarchar(20)
)
go
create proc sp_Inserts
@Names nvarchar(4000)
as
declare @Name nvarchar(20),@ErrorSum int
:set @ErrorSum = 0
:begin tra ......
使用 CONVERT:
CONVERT (data_type[(length)], expression [, style])
select CONVERT(varchar, getdate(), 120 )
2004-09-12 11:06:08
select replace(replace(replace(CONVERT(varchar, getdate(), 120 ),\'-\',\'\'),\' \',\'\'),\':\',\'\')
20040912110608
select CONVERT(varchar(12) , getdate(), 111 ) ......