Create TABLE [dbo].[Student] (
[id] [int] IDENTITY (1, 1) NOT NULL ,
[name] [nvarchar] (50) COLLATE Chinese_PRC_CI_AS NULL ,
[age] [int] NULL
)在Student表上建如下索引:

例1:select * from student

例2:select * from student where id<5

例3:select * from student where name like 'zhang%'

例4:select * from student where age>=18

例5:select * from student where name like 'zhang%' and age>=18

例6:select * from student where name like 'zhang%' and age>=18 and id<10

例7:select * from student where name like '%zhang%'

总结
·创建有用的索引很重要,它能大大提高查询的效率,如例2~例6;
·创建有效的查询语句很重要,它能保证尽可能的使用到索引[1],例7和例3就是最好的例子;
[1].要让查询尽可能使用到索引,就应在查询中尽量避免使用否定运算符,例如NOT、!=、<>、!>、!<、NOT EXISTS、NOT IN和NOT LIKE,避免对字段使用函数,例如:
name like '%zhang%'
age<>15
abs(age)>=18
评论(0)
暂无评论。