From 79ebe5e0c936525f1e563a8ec609196e3ac165f7 Mon Sep 17 00:00:00 2001 From: prinz <281208364@qq.com> Date: Fri, 27 Oct 2023 11:49:17 +0800 Subject: [PATCH 1/2] support secondary index --- .../1.1-create-index.md | 147 ++++++++++++++++++ 1 file changed, 147 insertions(+) create mode 100644 docs/MatrixOne/Reference/SQL-Reference/Data-Definition-Language/1.1-create-index.md diff --git a/docs/MatrixOne/Reference/SQL-Reference/Data-Definition-Language/1.1-create-index.md b/docs/MatrixOne/Reference/SQL-Reference/Data-Definition-Language/1.1-create-index.md new file mode 100644 index 0000000000..ec4edc5089 --- /dev/null +++ b/docs/MatrixOne/Reference/SQL-Reference/Data-Definition-Language/1.1-create-index.md @@ -0,0 +1,147 @@ +# **CREATE INDEX** + +## **语法说明** + +在表中创建索引,以便更加快速高效地查询数据。 + +你无法看到索引,索引只能被用来加速搜索/查询。 + +更新一个包含索引的表需要比更新一个没有索引的表花费更多的时间,这是由于索引本身也需要更新。因此,理想的做法是仅仅在常常被搜索的列(以及表)上面创建索引。 + +索引有两种常见的类型,分别为: + +- Primary Key:即主键索引,即标识在主键列上的索引。 +- Secondary Index:即二级索引,即在非主键上标识的索引。 + +## **语法结构** + +``` +> CREATE [UNIQUE] INDEX index_name +ON tbl_name (key_part,...) +COMMENT 'string' +``` + +### 语法释义 + +#### CREATE UNIQUE INDEX 语法 + +在表上创建一个唯一的索引。不允许使用重复的值:唯一的索引意味着两个行不能拥有相同的索引值。 + +#### CREATE secondary INDEX 语法 + +在表上创建一个次级索引。可以使用重复值和 Null 值。 + +## **示例** + +- 示例 1: + +```sql +drop table if exists t1; +create table t1(id int PRIMARY KEY,name VARCHAR(255),age int); +insert into t1 values(1,"Abby", 24); +insert into t1 values(2,"Bob", 25); +insert into t1 values(3,"Carol", 23); +insert into t1 values(4,"Dora", 29); +create unique index idx on t1(name); +mysql> select * from t1; ++------+-------+------+ +| id | name | age | ++------+-------+------+ +| 1 | Abby | 24 | +| 2 | Bob | 25 | +| 3 | Carol | 23 | +| 4 | Dora | 29 | ++------+-------+------+ +4 rows in set (0.00 sec) + +mysql> show create table t1; ++-------+--------------------------------------------------------------------------------------------------------------------------------------------------+ +| Table | Create Table | ++-------+--------------------------------------------------------------------------------------------------------------------------------------------------+ +| t1 | CREATE TABLE `t1` ( +`id` INT NOT NULL, +`name` VARCHAR(255) DEFAULT NULL, +`age` INT DEFAULT NULL, +PRIMARY KEY (`id`), +UNIQUE KEY `idx` (`name`) +) | ++-------+--------------------------------------------------------------------------------------------------------------------------------------------------+ +1 row in set (0.01 sec) + + +create table t2 ( +col1 bigint primary key, +col2 varchar(25), +col3 float, +col4 varchar(50) +); +create unique index idx on t2(col2) comment 'create varchar index'; +insert into t2 values(1,"Abby", 24,'zbcvdf'); +insert into t2 values(2,"Bob", 25,'zbcvdf'); +insert into t2 values(3,"Carol", 23,'zbcvdf'); +insert into t2 values(4,"Dora", 29,'zbcvdf'); +mysql> select * from t2; ++------+-------+------+--------+ +| col1 | col2 | col3 | col4 | ++------+-------+------+--------+ +| 1 | Abby | 24 | zbcvdf | +| 2 | Bob | 25 | zbcvdf | +| 3 | Carol | 23 | zbcvdf | +| 4 | Dora | 29 | zbcvdf | ++------+-------+------+--------+ +4 rows in set (0.00 sec) +mysql> show create table t2; ++-------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ +| Table | Create Table | ++-------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ +| t2 | CREATE TABLE `t2` ( +`col1` BIGINT NOT NULL, +`col2` VARCHAR(25) DEFAULT NULL, +`col3` FLOAT DEFAULT NULL, +`col4` VARCHAR(50) DEFAULT NULL, +PRIMARY KEY (`col1`), +UNIQUE KEY `idx` (`col2`) COMMENT `create varchar index` +) | ++-------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ +1 row in set (0.01 sec) +``` + +- 示例 2:创建次级索引并查询 + +```sql +CREATE TABLE Employees ( + EmployeeID INT PRIMARY KEY, + FirstName VARCHAR(50), + LastName VARCHAR(50), + Department VARCHAR(50), + Salary INT +); + +INSERT INTO Employees (EmployeeID, FirstName, LastName, Department, Salary) +VALUES (1, 'John', 'Doe', 'HR', 50000); + +INSERT INTO Employees (EmployeeID, FirstName, LastName, Department, Salary) +VALUES (2, 'Jane', 'Smith', 'IT', 60000); + +INSERT INTO Employees (EmployeeID, FirstName, LastName, Department, Salary) +VALUES (3, 'Mark', 'Johnson', 'IT', 55000); + +INSERT INTO Employees (EmployeeID, FirstName, LastName, Department, Salary) +VALUES (4, 'Mary', 'Brown', 'Sales', 48000); + +mysql> CREATE INDEX DepartmentIndex ON Employees (Department); +Query OK, 0 rows affected (0.01 sec) + +mysql> SELECT * FROM Employees WHERE Department = 'IT'; ++------------+-----------+----------+------------+--------+ +| employeeid | firstname | lastname | department | salary | ++------------+-----------+----------+------------+--------+ +| 2 | Jane | Smith | IT | 60000 | +| 3 | Mark | Johnson | IT | 55000 | ++------------+-----------+----------+------------+--------+ +2 rows in set (0.00 sec) +``` + +## 限制 + +当前次级索引无法起到性能加速的作用。 From 087884e9cc3345fb081f1e049bc607af2ae45027 Mon Sep 17 00:00:00 2001 From: prinz <43231571+lacrimosaprinz@users.noreply.github.com> Date: Fri, 27 Oct 2023 17:02:17 +0700 Subject: [PATCH 2/2] Update 1.1-create-index.md --- .../SQL-Reference/Data-Definition-Language/1.1-create-index.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/MatrixOne/Reference/SQL-Reference/Data-Definition-Language/1.1-create-index.md b/docs/MatrixOne/Reference/SQL-Reference/Data-Definition-Language/1.1-create-index.md index ec4edc5089..e45938009a 100644 --- a/docs/MatrixOne/Reference/SQL-Reference/Data-Definition-Language/1.1-create-index.md +++ b/docs/MatrixOne/Reference/SQL-Reference/Data-Definition-Language/1.1-create-index.md @@ -27,7 +27,7 @@ COMMENT 'string' 在表上创建一个唯一的索引。不允许使用重复的值:唯一的索引意味着两个行不能拥有相同的索引值。 -#### CREATE secondary INDEX 语法 +#### CREATE INDEX 语法 在表上创建一个次级索引。可以使用重复值和 Null 值。