Sql 简明教程
SQL - Non-Clustered Index
SQL Non-Clustered Indexes
SQL Non-Clustered 索引类似于聚集索引。当在列上定义时,它会创建一个特殊表,其中包含索引列的副本以及指向表中实际数据位置的指针。但是,与聚集索引不同,非聚集索引无法从物理上对索引列进行排序。
The SQL Non-Clustered index is similar to the Clustered index. When defined on a column, it creates a special table which contains the copy of indexed columns along with a pointer that refers to the location of the actual data in the table. However, unlike Clustered indexes, a Non-Clustered index cannot physically sort the indexed columns.
以下是 SQL 中非聚集索引的一些要点 -
Following are some of the key points of the Non-clustered index in SQL −
-
The non-clustered indexes are a type of index used in databases to speed up the execution time of database queries.
-
These indexes require less storage space than clustered indexes because they do not store the actual data rows.
-
We can create multiple non-clustered indexes on a single table.
为了更好地理解,请观察以下说明非聚集索引的工作原理的图片 -
To get a better understanding, look at the following figure illustrating the working of non-clustered indexes −
假设我们有一个示例数据库表,其中包含两列,名为 ID 和 NAME 。如果在上述表中为名为 ID 的列创建非聚集索引,它将存储 ID 列的副本,同时带有指向表中实际数据特定位置的指针。
Assume we have a sample database table with two columns named ID and NAME. If we create a non-clustered index on a column named ID in the above table, it will store a copy of the ID column with a pointer that points to the specific location of the actual data in the table.
Syntax
以下是 SQL Server 中创建 non-clustered 索引的语法 -
Following is the syntax to create a non-clustered index in SQL Server −
CREATE NONCLUSTERED INDEX index_name
ON table_name (column_name)
在此,
Here,
-
index_name: holds the name of non-clustered index.
-
table_name: holds the name of the table where you want to create the non-clustered index.
-
column_name: holds the name of the column that you want to define the non-clustered index on.
Example
让我们使用以下查询创建一个名为 CUSTOMERS 的表 -
Let us create a table named CUSTOMERS using the following query −
CREATE TABLE CUSTOMERS(
ID INT NOT NULL,
NAME VARCHAR (20) NOT NULL,
AGE INT NOT NULL,
ADDRESS CHAR (25),
SALARY DECIMAL (20, 2),
);
让我们使用以下查询在上述创建的表中插入一些值 -
Let us insert some values into the above-created table using the following query −
INSERT INTO CUSTOMERS VALUES
(7, 'Muffy', '24', 'Indore', 5500),
(1, 'Ramesh', '32', 'Ahmedabad', 2000),
(6, 'Komal', '22', 'Hyderabad', 9000),
(2, 'Khilan', '25', 'Delhi', 1500),
(4, 'Chaitali', '25', 'Mumbai', 6500),
(5, 'Hardik','27', 'Bhopal', 8500),
(3, 'Kaushik', '23', 'Kota', 2000);
该表在 SQL 数据库中成功创建。
The table is successfully created in the SQL database.
ID |
NAME |
AGE |
ADDRESS |
SALARY |
7 |
Muffy |
24 |
Indore |
5500.00 |
1 |
Ramesh |
32 |
Ahmedabad |
2000.00 |
6 |
Komal |
22 |
Hyderabad |
9000.00 |
2 |
Khilan |
25 |
Delhi |
1500.00 |
4 |
Chaitali |
25 |
Mumbai |
6500.00 |
5 |
Hardik |
27 |
Bhopal |
8500.00 |
3 |
Kaushik |
23 |
Kota |
2500.00 |
现在,让我们使用以下查询在名为 single column 的 ID 上创建一个非聚集索引 -
Now, let us create a non-clustered index on a single column named ID using the following query −
CREATE NONCLUSTERED INDEX NON_CLU_ID
ON customers (ID ASC);
Output
执行以上查询后,输出显示如下 −
On executing the above query, the output is displayed as follows −
Commands Completed Successfully.
Verification
让我们使用以下查询检索在 CUSTOMERS 表上创建的所有索引 -
Let us retrieve all the indexes that are created on the CUSTOMERS table using the following query −
EXEC sys.sp_helpindex @objname = N'CUSTOMERS';
正如我们观察到的那样,我们可以在索引列表中找到名为 ID 的列。
As we observe, we can find the column named ID in the list of indexes.
index_name |
index_description |
index_keys |
|
1 |
NON_CLU_ID |
nonclustered located on PRIMARY |
ID |
现在,再次使用以下查询检索 CUSTOMERS 表,以检查该表是否已排序 -
Now, retrieve the CUSTOMERS table again using the following query to check whether the table is sorted or not −
SELECT * FROM CUSTOMERS;
如我们所观察的,非群集索引不会物理地对行进行排序,而是从表数据中创建单独的键值结构。
As we observe, the non-clustered index does not sort the rows physically instead, it creates a separate key-value structure from the table data.
ID |
NAME |
AGE |
ADDRESS |
SALARY |
7 |
Muffy |
24 |
Indore |
5500.00 |
1 |
Ramesh |
32 |
Ahmedabad |
2000.00 |
6 |
Komal |
22 |
Hyderabad |
9000.00 |
2 |
Khilan |
25 |
Delhi |
1500.00 |
4 |
Chaitali |
25 |
Mumbai |
6500.00 |
5 |
Hardik |
27 |
Bhopal |
8500.00 |
3 |
Kaushik |
23 |
Kota |
2500.00 |
Creating Non-Clustered Index on Multiple Columns
我们先考虑先前创建的 CUSTOMERS 表,而不用创建新表。现在,尝试对表的 multiple columns (例如 ID、AGE 和 SALARY)使用以下查询创建非群集索引 −
Instead of creating a new table, let us consider the previously created CUSTOMERS table. Now, try to create a non-clustered index on multiple columns of the table such as ID, AGE and SALARY using the following query −
CREATE NONCLUSTERED INDEX NON_CLUSTERED_ID
ON CUSTOMERS (ID, AGE, SALARY);
Output
以下查询将为 ID、AGE 和 SALARY 创建三个单独的非群集索引。
The below query will create three separate non-clustered indexes for ID, AGE, and SALARY.
Commands Completed Successfully.
Verification
让我们使用以下查询检索在 CUSTOMERS 表上创建的所有索引 -
Let us retrieve all the indexes that are created on the CUSTOMERS table using the following query −
EXEC sys.sp_helpindex @objname = N'CUSTOMERS';
我们观察到,可以在索引列表中找到列名 ID、AGE 和 SALARY。
As we observe, we can find the column names ID, AGE and SALARY columns in the list of indexes.
index_name |
index_description |
index_keys |
|
1 |
NON_CLU_ID |
nonclustered located on PRIMARY |
ID, AGE, SALARY |