[{"createTime":1735734952000,"id":1,"img":"hwy_ms_500_252.jpeg","link":"https://activity.huaweicloud.com/cps.html?fromacct=261f35b6-af54-4511-a2ca-910fa15905d1&utm_source=V1g3MDY4NTY=&utm_medium=cps&utm_campaign=201905","name":"华为云秒杀","status":9,"txt":"华为云38元秒杀","type":1,"updateTime":1735747411000,"userId":3},{"createTime":1736173885000,"id":2,"img":"txy_480_300.png","link":"https://cloud.tencent.com/act/cps/redirect?redirect=1077&cps_key=edb15096bfff75effaaa8c8bb66138bd&from=console","name":"腾讯云秒杀","status":9,"txt":"腾讯云限量秒杀","type":1,"updateTime":1736173885000,"userId":3},{"createTime":1736177492000,"id":3,"img":"aly_251_140.png","link":"https://www.aliyun.com/minisite/goods?userCode=pwp8kmv3","memo":"","name":"阿里云","status":9,"txt":"阿里云2折起","type":1,"updateTime":1736177492000,"userId":3},{"createTime":1735660800000,"id":4,"img":"vultr_560_300.png","link":"https://www.vultr.com/?ref=9603742-8H","name":"Vultr","status":9,"txt":"Vultr送$100","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":5,"img":"jdy_663_320.jpg","link":"https://3.cn/2ay1-e5t","name":"京东云","status":9,"txt":"京东云特惠专区","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":6,"img":"new_ads.png","link":"https://www.iodraw.com/ads","name":"发布广告","status":9,"txt":"发布广告","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":7,"img":"yun_910_50.png","link":"https://activity.huaweicloud.com/discount_area_v5/index.html?fromacct=261f35b6-af54-4511-a2ca-910fa15905d1&utm_source=aXhpYW95YW5nOA===&utm_medium=cps&utm_campaign=201905","name":"底部","status":9,"txt":"高性能云服务器2折起","type":2,"updateTime":1735660800000,"userId":3}]
索引 DDL
<>索引概述
索引:指向行记录的指针以加快数据检索速度
快速找到表里的行记录
避免全表扫描
提高查询性能
以下 TiDB 约束默认提供索引:
* Primary Key
* Unique Key
可以通过 CREATE INDEX 语句创建
* 索引信息系统表: information_schema.tidb_indexes,索引名称: KEY_NAME
* SHOW INDEX FROM table_name; 获取索引的定义
<>Unique Key 和索引
唯一键具有以下特点:
* 唯一标识表中的单个行
* 接受 NULL
* 每张表允许多个唯一键 tidb> CREATE TABLE test.dropme ( -> id BIGINT AUTO_RANDOM, ->
nameCHAR(35) NOT NULL, -> PRIMARY KEY (id), -> UNIQUE KEY (name) -> ); tidb>
SELECT key_name, column_name, index_id FROM information_schema.tidb_indexes ->
WHERE table_schema='test' AND table_name='dropme'; +----------+-------------+--
--------+ | key_name | column_name | index_id | +----------+-------------+------
----+ | PRIMARY | id | 0 | | name | name | 1 | +----------+-------------+------
----+
<>二级索引
* 二级索引:主键以外的索引
* 唯一二级索引:具有约束条件的二级索引
创建二级索引:
创建表时使用 INDEX 或 KEY 关键字
对现有的表:
ALTER TABLE ... ADD INDEX/KEY ... CREATE INDEX ... ON ... tidb> CREATE TABLE
test.ggg (id INT, name CHAR(30), KEY(name)); tidb> CREATE INDEX ggg_idx ON test.
ggg (id); tidb> CREATE TABLE test.gg (id INT, id2 INT, name CHAR(30), INDEX(name
)); tidb> ALTER TABLE test.gg ADD KEY (id); tidb> ALTER TABLE test.gg ADD INDEX
(id2);
<>复合 Primary Key
复合主键需要两列或更多列才能唯一标识行
组成复合主键的各个列单独并不需要保证唯一性
infromation_schema.tidb_indexes COLUMN_NAME SEQ_IN_INDEX CREATE TABLE `stars` (
`name` char(20) NOT NULL DEFAULT '', `mass` float NOT NULL DEFAULT 0.0 COMMENT
'10**24 kg', ... `distance_from_earth` float COMMENT 'light year', `
discover_date` datetime NOT NULL, PRIMARY KEY (`name`, `discover_date`) ); name
和 discover_date 两个列共同组成了一个复合主键。