●●索引效率测试结果表明,索引没有作用,看看我的测试哪里出了问题?●●

[复制链接]
查看11 | 回复5 | 2010-3-1 11:20:08 | 显示全部楼层 |阅读模式
drop table tbl_stu_for_index
create table tbl_stu_for_index
(

stu_id varchar(20),

stu_name varchar(20),

stu_sex bit,

stu_score decimal(10,1)
)

declare @a datetime
declare @b datetime
declare @i int
set @a=getdate()
select ceiling(rand()*100)
set @i = 0
while(@i<200000)
begin

insert into tbl_stu_for_index(stu_id,stu_score)

values(@i,ceiling(rand()*100))

set @i=@i+1
end
set @b=getdate()
select datediff(ms,@a,@b)
====
上述代码建表,并插入20万条数据


declare @a datetime
declare @b datetime
declare @i int
set @a=getdate()
select * from tbl_stu_for_index(index =ix_stu_for_index_stu_id)where stu_id=100100
set @b=getdate()
select datediff(ms,@a,@b)
====
上述代码测试没有索引时查询时间,多次查询结果3000毫秒
create index ix_stu_for_index_stu_id
on tbl_stu_for_index(stu_id)
===
建立索引

declare @a datetime
declare @b datetime
declare @i int
set @a=getdate()
select * from tbl_stu_for_index(index =ix_stu_for_index_stu_id)where stu_id=100100
set @b=getdate()
select datediff(ms,@a,@b)
======
使用索引查询时间,依然显示在 3000毫秒
回复

使用道具 举报

千问 | 2010-3-1 11:20:08 | 显示全部楼层
最初由 javadiannet 发布
[B]drop table tbl_stu_for_index
create table tbl_stu_for_index
(

stu_id varchar(20),

stu_name varchar(20),

stu_sex bit,

stu_score decimal(10,1)
)

declare @a datetime
declare @b datetime
declare @i int
set @a=getdate()
select ceiling(rand()*100)
set @i = 0
while(@i<200000)
begin

insert into tbl_stu_for_index(stu_id,stu_score)

values(@i,ceiling(rand()*100))

set @i=@i+1
end
set @b=getdate()
select datediff(ms,@a,@b)
====
上述代码建表,并插入20万条数据


declare @a datetime
declare @b datetime
declare @i int
set @a=getdate()
select * from tbl_stu_for_index(index =ix_stu_for_index_stu_id)where stu_id=100100
set @b=getdate()
select datediff(ms,@a,@b)
====
上述代码测试没有索引时查询时间,多次查询结果3000毫秒
create index ix_stu_for_index_stu_id
on tbl_stu_for_index(stu_id)
===
建立索引

declare @a datetime
declare @b datetime
declare @i int
set @a=getdate()
select * from tbl_stu_for_index(index =ix_stu_for_index_stu_id)where stu_id=100100
set @b=getdate()
select datediff(ms,@a,@b)
======
使用索引查询时间,依然显示在 3000毫秒 [/B]

呵呵,你有没有注意字段类型呢。。。
你明明在表中定义的stu_id字段的类型为varchar(20),查询时却用。。。
这样索引当然失效了。
你把查询条件改为stu_id='100100'试试。
回复

使用道具 举报

千问 | 2010-3-1 11:20:08 | 显示全部楼层
我晕,试试看....
回复

使用道具 举报

千问 | 2010-3-1 11:20:08 | 显示全部楼层
正在测试中,激动ing...
回复

使用道具 举报

千问 | 2010-3-1 11:20:08 | 显示全部楼层
谢谢pydwh,已经搞定!
回复

使用道具 举报

千问 | 2010-3-1 11:20:08 | 显示全部楼层
这种问题很隐蔽,我曾跟踪一个存储过程很久,才发现这个问题。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题

0

回帖

4882万

积分

论坛元老

Rank: 8Rank: 8

积分
48824836
热门排行