易截截图软件、单文件、免安装、纯绿色、仅160KB

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


相关文档:

SQL触发器 实例教学


        ... 
      )
     用到的功能有: 
        1.如果我更改了学生的学号,我希望他的借书记录仍然与这个学生相关(也就是同时更改借书记录表的学号); 
        2.如果该学生已经 ......

ORACLE SQL语句优化


1) 选择最有效率的表名顺序(只在基于规则的优化器中有效):
ORACLE的解析器按照从右到左的顺序处理from子句中的表名,from子句中写在最后的表(基础表 driving table)将被最先处理,在from子句中包含多个表的情况下,你必须选择记录条数最少的表作为基础表。如果有3个以上的表连接查询, 那就需要选择交叉表(intersection ......

sql 各种字符串合并方法 第二种方法好(利用函数)

--3.3.1 使用游标法进行字符串合并处理的示例。
--处理的数据
CREATE TABLE tb(col1 varchar(10),col2 int)
INSERT tb SELECT 'a',1
UNION ALL SELECT 'a',2
UNION ALL SELECT 'b',1
UNION ALL SELECT 'b',2
UNION ALL SELECT 'b',3
--合并处理
--定义结果集表变量
DECLARE @t TABLE(col1 varchar(10),col2 varch ......

SQL日期格式大全

CONVERT
将某种数据类型的表达式显式转换为另一种数据类型。由于某些需求经常用到取日期格式的不同.现以下可在
SQL Server中 将日期格式化.
SQL Server 支持使用科威特算法的阿拉伯样式中的数据格式。
在表中,左侧的两列表示将 datetime 或 smalldatetime 转换为字符数据的 style 值。给 style 值加 100,可获得包 ......
© 2009 ej38.com All Rights Reserved. 关于E健网联系我们 | 站点地图 | 赣ICP备09004571号