SQL Server数据库测试索引的空间换时间

SQL Server数据库中,我们经常使用索引来提高查询的效率,但是如果索引使用不当就会适得其反。本文我们介绍一种测试索引空间换时间的操作,以方便找到适合利用索引的时机,接下来就让我们一起来了解一下这部分内容吧。

 
 
 
  1. create table tb_test(  
  2.        t_id number,  
  3.        t_name nvarchar2(30)  
  4. );  
  5. create sequence seq_tid  
  6. start with 1  
  7. increment by 1  
  8. maxvalue 999999  
  9. minvalue 1  
  10. cache 10 

--项该表中插入99999条记录

 
 
 
  1. begin  
  2.        for n in 1..99999 loop  
  3.            insert into tb_test values(seq_tid.nextval,'1234');  
  4.        end loop;  
  5. end;  
  6. --select count(*) from tb_test;  
  7. --select * from tb_test;  
  8. --select * from tb_test where t_id = 99999

--此处以下为测试代码,分别在创建索引和没有创建索引的情况下运行。
--你会发现创建索引后后运行的时间比没有创建索引所需要的时间少很多。

 
 
 
  1. declare  
  2. num number;  
  3. begin  
  4.      for n in 1..9999 loop  
  5.          select t_id into num  from tb_test where t_id=n;  
  6.      end loop;  
  7.      dbms_output.put_line('nunm='||num);  
  8. end; 

--创建对应的索引suncs_space 为自己创建的一个表空间。

 
 
 
  1. create index testid on tb_test(t_id) tablespace suncs_space; 

--删除索引

 
 
 
  1. drop index testid; 

关于SQL Server数据库测试索引的空间换时间的操作就介绍到这里了,希望本次的介绍能够对您有所帮助。

【编辑推荐】

  1. Oracle数据库rman环境配置详解
  2. Oracle数据库不能使用索引的原因定位
  3. Oracle数据库rman常用命令的使用示例
  4. Oracle性能分析工具statpack安装使用详解
  5. 如何查看和修改Oracle数据库服务器端的字符集
THE END