数据库-SQL索引相关

索引

 

索引表示数据的一种形式,它提供的数据顺序不同于数据在磁盘上的物理存储顺序。索引的特殊作用是在表内重新排列记录的物理位置。索引基于表的一列,或基于表的多列组合成立。当使用索引时,数据以一种分类排序的方式提供给用户的,排列顺序可以用create index语句来控制。通常,通过在正确的字段上建立索引,可以使性能显著提高,特别是在表之间的链接字段上建立索引。

 

SQL数据库中使用索引的3个主要原因是:

  1.通过使用unique或primary key关键字,增强参照完整性约束。

  2.使基于索引字段内容的数据排序更方便。

  3.优化查询的执行速度。

例子:create index bills_account_id_index on bills (account_id);

drop index bills_account_id_index ;

   多个字段上建立索引:create index id_cmpd_index on bills(account_id, amount);

 

一.索引与连接:

在bills和bank_accounts的account_id字段上建立一个索引,提高效率

create index bills_account_id_index on bills(account_id);

create index bankaccount_account_id on bank_accounts(account_id);

 select b.name name, b.amount amount, bas.balance account_balance

   from bills b,bank_accounts bas

   where b.account_id = bas.account_id;

 

二.聚簇索引:create unique clustered index id_index on bank_accounts(account_id);//不同厂商sql语句不同.聚簇索引时一个十分厂商化得sql功能.

 

小结:索引是由数据库管理系统存储于数据库中的物理对象,用来检索已在数据库排序的数据. 由于索引为检索数据而设计,使用索引和正确的构造查询可以显著地提高性能.

 

附表:

 

create table bills(

name char(30) not null,

amount numeric,

account_id numeric not null);

 

create table bank_accounts(

account_id numeric not null,

type char(30),

balance numeric,

bank char(30));

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值