日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學(xué)無先后,達(dá)者為師

網(wǎng)站首頁 編程語言 正文

SQL?Server?索引和視圖詳解_MsSql

作者:hoojo ? 更新時(shí)間: 2022-04-01 編程語言

索引

1、 什么是索引

索引就是數(shù)據(jù)表中數(shù)據(jù)和相應(yīng)的存儲(chǔ)位置的列表,利用索引可以提高在表或視圖中的查找數(shù)據(jù)的速度。

2、 索引分類

數(shù)據(jù)庫中索引主要分為兩類:聚集索引和非聚集索引。SQL Server 2005還提供了唯一索引、索引視圖、全文索引、xml索引等等。聚集索引和非聚集索引是數(shù)據(jù)庫引擎中索引的基本類型,是理解其他類型索引的基礎(chǔ)。

聚集索引

聚集索引是值表中數(shù)據(jù)行的物理存儲(chǔ)順序和索引的存儲(chǔ)順序完全相同。聚集索引根據(jù)索引順序物理地重新排列了用戶插入到表中的數(shù)據(jù),因此,每個(gè)表只能創(chuàng)建一個(gè)聚集索引。聚集索引經(jīng)常創(chuàng)建在表中經(jīng)常被搜索到的列或按順序訪問的列上。在默認(rèn)情況下,主鍵約束自動(dòng)創(chuàng)建聚集索引。

非聚集索引

非聚集索引不改變表中數(shù)據(jù)列的物理存儲(chǔ)位置,數(shù)據(jù)與索引分開存儲(chǔ),通過索引指向的地址與表中的數(shù)據(jù)發(fā)生關(guān)系。

非聚集索引沒有改變表中物理行的位置,索引可以在以下情況下使用非聚集索引:

一、如果某個(gè)字段的數(shù)據(jù)唯一性比較高

二、如果查詢所得到的數(shù)據(jù)量比較少

聚集索引和非聚集索引的區(qū)別:

聚集索引

非聚集索引

每個(gè)表只允許創(chuàng)建一個(gè)聚集索引

最多可以有249個(gè)非聚集索引

物理的重排表中的數(shù)據(jù)以符合索引約束

創(chuàng)建一個(gè)鍵值列表,鍵值指向數(shù)據(jù)在數(shù)據(jù)頁中的位置

用于經(jīng)常查找數(shù)據(jù)的列

用于從表中查找單個(gè)值的列

用于從表中查找單個(gè)值的列

其他類型索引

除了以上索引,還有以下類型索引:

a、 唯一索引:如果希望索引鍵都不同,可以創(chuàng)建唯一索引。聚集索引和非聚集索引都可以是唯一索引。

b、 包含新列索引:索引列的最大數(shù)量是16個(gè),索引列的字節(jié)總數(shù)的最高值是900。如果當(dāng)多個(gè)列的字節(jié)總數(shù)大于900,切又想在這些劣種都包含索引是,可以使用包含性列索引

c、 視圖索引:提供視圖查詢效率,可以視圖的索引物理化,也就是說將結(jié)果集永久存儲(chǔ)在索引中,可以創(chuàng)建視圖索引。

d、 XML索引:是與xml數(shù)據(jù)關(guān)聯(lián)的索引形式,是XML二進(jìn)制blob的已拆分持久表示形式

e、 全文索引:一種特殊類型的基于標(biāo)記的功能性功能,用于幫助在字符串中搜索賦值的詞

3、 創(chuàng)建索引

語法

create [unique] [clustered | noclustered]
index index_name
on table_name (column_name ...)
[with fillfactor=x]

unique唯一索引

clustered聚集索引

noclustered非聚集索引

fillfactor填充因子大小,范圍在0-100直接,表示索引頁填滿的空間所占的百分比。

示例

if (exists (select * from sys.indexes where name = 'idx_stu_name'))
    drop index student.idx_stu_name
go
create index idx_stu_name
on
student(name);
 
--聯(lián)合索引
if (exists (select * from sys.indexes where name = 'idx_uqe_clu_stu_name_age'))
    drop index student.idx_uqe_clu_stu_name_age
go
create unique clustered index idx_uqe_clu_stu_name_age
on student(name, age);
 
if (exists (select * from sys.indexes where name = 'idx_cid'))
    drop index student.idx_cid
go
 
if (exists (select * from sys.indexes where name = 'idx_cid'))
    drop index student.idx_cid
go
 
--非聚集索引
create nonclustered index idx_cid
on
student (cid)
with fillFactor = 30;    --填充因子
 
--聚集索引
if (exists (select * from sys.indexes where name = 'idx_sex'))
    drop index student.idx_sex
go
create clustered index idx_sex
on
student(sex);
 
--聚集索引
if (exists (select * from sys.indexes where name = 'idx_name'))
    drop index student.idx_name
go
create unique index idx_name
on
student(name);

4、 適合的創(chuàng)建索引的列

當(dāng)數(shù)據(jù)庫的某一列被頻繁的用于數(shù)據(jù)庫查詢時(shí),或者該列用于數(shù)據(jù)庫進(jìn)行排序時(shí)可以創(chuàng)建成索引

5、 不適合創(chuàng)建索引的列

如果列中有幾個(gè)不同的值,或者表中僅包含幾行值,則不推薦為其創(chuàng)建索引。因?yàn)樗饕谒阉鲾?shù)據(jù)所花的時(shí)間比在表中逐行搜索話的時(shí)間更長(zhǎng)。

視圖

1、 什么是視圖

視圖就是一個(gè)虛擬的數(shù)據(jù)表,該數(shù)據(jù)表中的數(shù)據(jù)記錄是有一條查詢語句的查詢結(jié)果得到的。

2、 創(chuàng)建視圖準(zhǔn)則

創(chuàng)建視圖需要考慮一下準(zhǔn)則:

  • 視圖名稱必須遵循標(biāo)識(shí)符的規(guī)則,該名稱不得與該架構(gòu)的如何表的名稱相同
  • 你可以對(duì)其他視圖創(chuàng)建視圖。允許嵌套視圖,但嵌套不得超過32層。視圖最多可以有1024個(gè)字段
  • 不能將規(guī)則和default定義于視圖相關(guān)聯(lián)
  • 視圖的查詢不能包含compute子句、compute by子句或into關(guān)鍵字
  • 定義視圖的查詢不能包含order by子句,除非在select 語句的選擇列表中還有top子句

下列情況必須指定視圖中每列的名稱:

  • 視圖中的如何列都是從算術(shù)表達(dá)式、內(nèi)置函數(shù)或常量派生而來
  • 視圖中有兩列或多列具有相同名稱(通常由于視圖定義包含聯(lián)接,因此來自兩個(gè)或多個(gè)不同的列具有相同的名稱)
  • 希望視圖中的列指定一個(gè)與其原列不同的名稱(也可以在視圖中重命名列)。無論是否重命名,視圖列都回繼承原列的數(shù)據(jù)類型

3、 創(chuàng)建視圖

--創(chuàng)建視圖
if (exists (select * from sys.objects where name = 'v_stu'))
    drop view v_stu
go
create view v_stu
as
select id, name, age, sex from student;

4、 修改視圖

alter view v_stu
as
select id, name, sex from student;
 
alter view v_stu(編號(hào), 名稱, 性別)
as
    select id, name, sex from student
go
select * from v_stu;
 
select * from information_schema.views;

5、 加密視圖

alter view v_stu
as
select id, name, sex from student;
 
alter view v_stu(編號(hào), 名稱, 性別)
as
    select id, name, sex from student
go
select * from v_stu;
 
select * from information_schema.views;

總結(jié)

原文鏈接:https://www.cnblogs.com/hoojo/archive/2011/07/18/2109291.html

欄目分類
最近更新