Explain the function of clustered indexes

Note:
This topic has been translated from a Chinese forum by GPT and might contain errors.

Original topic: 帮忙解释一下聚簇索引的作用

| username: tidb狂热爱好者

To improve efficiency, please provide the following information. Clear problem descriptions can be resolved faster:
【TiDB Usage Environment】Production
【TiDB Version】6.0
【Encountered Problem】
【Reproduction Path】What operations were performed to encounter the problem
【Problem Phenomenon and Impact】
CREATE TABLE t (a BIGINT, b VARCHAR(255), PRIMARY KEY(a, b) CLUSTERED);
I want to ask, after creating a clustered index, will all queries involving ‘b’ be very fast?
【Attachments】
Inserting data will reduce one network write for index data.
Equality condition queries involving only the primary key will reduce one network read.
Range condition queries involving only the primary key will reduce multiple network reads.
Equality or range condition queries involving only the prefix of the primary key will reduce multiple network reads.

CREATE TABLE t (a BIGINT, b VARCHAR(255), c VARCHAR(255), PRIMARY KEY(a, b, c) CLUSTERED); Can I also put multiple VARCHAR fields in it?

Please provide the version information of each component, such as cdc/tikv, which can be obtained by executing cdc version/tikv-server --version.

| username: 长安是只喵 | Original post link

Yes, anything involving B will be very fast, but the premise is that it must match this clustered index.

| username: ealam_小羽 | Original post link

TiDB clustered indexes and non-clustered indexes are similar to MySQL, both in terms of querying data, whether directly index-to-data or index-to-primary key-to-data.


Documentation: 聚簇索引 | PingCAP 文档中心

They are not without drawbacks either.


Personally, I believe that from a business and maintenance perspective, it is best to distinguish between data primary keys and business primary keys. If you use the business primary key as the data primary key, it is easy to encounter table design flaws. Modifying the primary key can impact users, and switching can be troublesome from a business standpoint.

| username: cs58_dba | Original post link

The images you provided are not visible. Please provide the text content that you need translated.