Index Failure

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

Original topic: 索引失效

| username: EricSong

[TiDB Usage Environment] Production Environment
[TiDB Version]
Old Cluster: v4.0.11
New Cluster: v6.5.0
[Encountered Problem: Phenomenon and Impact]
There is a table with nearly 300 million rows of data. The table structure is as follows. Now this table has been migrated from the old cluster to the new cluster using Dumpling + Lightning.

CREATE TABLE `analytics` (
  `day` date NOT NULL,
  `partner_org_id` varchar(60) NOT NULL,
  `partner_name` text DEFAULT NULL,
  `customer_org_id` varchar(60) NOT NULL,
  `customer_name` text DEFAULT NULL,
  `offer_code` varchar(40) NOT NULL,
  `external_id` varchar(60) NOT NULL,
  `license_id` varchar(255) NOT NULL,
  `workload` varchar(60) DEFAULT NULL
  ...(omitted some fields)
  PRIMARY KEY (`day`,`partner_org_id`,`customer_org_id`,`offer_code`,`external_id`,`license_id`) /*T![clustered_index] CLUSTERED */
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin
PARTITION BY RANGE (WEEKDAY(`day`))
(PARTITION `p0` VALUES LESS THAN (1),
 PARTITION `p1` VALUES LESS THAN (2),
 PARTITION `p2` VALUES LESS THAN (3),
 PARTITION `p3` VALUES LESS THAN (4),
 PARTITION `p4` VALUES LESS THAN (5),
 PARTITION `p5` VALUES LESS THAN (6),
 PARTITION `p6` VALUES LESS THAN (7));

EXPLAIN select * from analytics order by day desc limit 5;
Execution plan in the new cluster is as follows

TopN_7	5.00	root    analytics.day:desc, offset:0, count:5
└─TableReader_14	5.00	root	partition:all	data:Limit_13
  └─Limit_13	5.00	cop[tikv]		offset:0, count:5
    └─TableFullScan_12	276107194.00	cop[tikv]	table:analytics	keep order:false, desc

Execution plan in the old cluster is as follows

TopN_24 5.00  root    analytics.day:desc, offset:0, count:5
└─PartitionUnion_28 35.00 root    
  ├─Projection_44 5.00  root    analytics.day, analytics.partner_org_id, analytics.partner_name, analytics.customer_org_id, analytics.customer_name, analytics.offer_code, analytics.external_subscription_id, analytics.subscription_start_date, analytics.subscription_end_date, analytics.is_trial, analytics.is_auto_renew, analytics.available_licenses, analytics.assigned_licenses, analytics.actively_used_licenses, analytics.capacity, analytics.trial_duration, analytics.license_status, analytics.license_id, analytics.workload, analytics.daily_actively_used_licenses, analytics.weekly_actively_used_licenses, analytics.is_sa_display, analytics.decrypted_customer_name
  │ └─IndexLookUp_43  5.00  root    limit embedded(offset:0, count:5)
  │   ├─Limit_42(Build) 5.00  cop[tikv]   offset:0, count:5
  │   │ └─IndexFullScan_40  5.00  cop[tikv] table:subscription_analytics, partition:p0, index:PRIMARY(day, partner_org_id, customer_org_id, offer_code, external_subscription_id, license_id) keep order:true, desc
  │   └─TableRowIDScan_41(Probe)  5.00  cop[tikv] table:subscription_analytics, partition:p0  keep order:false, stats:pseudo
  ├─Projection_64 5.00  root    analytics.day, analytics.partner_org_id, analytics.partner_name, analytics.customer_org_id, analytics.customer_name, analytics.offer_code, analytics.external_subscription_id, analytics.subscription_start_date, analytics.subscription_end_date, analytics.is_trial, analytics.is_auto_renew, analytics.available_licenses, analytics.assigned_licenses, analytics.actively_used_licenses, analytics.capacity, analytics.trial_duration, analytics.license_status, analytics.license_id, analytics.workload, analytics.daily_actively_used_licenses, analytics.weekly_actively_used_licenses, analytics.is_sa_display, analytics.decrypted_customer_name
  │ └─IndexLookUp_63  5.00  root    limit embedded(offset:0, count:5)
  │   ├─Limit_62(Build) 5.00  cop[tikv]   offset:0, count:5
  │   │ └─IndexFullScan_60  5.00  cop[tikv] table:subscription_analytics, partition:p1, index:PRIMARY(day, partner_org_id, customer_org_id, offer_code, external_subscription_id, license_id) keep order:true, desc
  │   └─TableRowIDScan_61(Probe)  5.00  cop[tikv] table:subscription_analytics, partition:p1  keep order:false, stats:pseudo
  ├─Projection_84 5.00  root    analytics.day, analytics.partner_org_id, analytics.partner_name, analytics.customer_org_id, analytics.customer_name, analytics.offer_code, analytics.external_subscription_id, analytics.subscription_start_date, analytics.subscription_end_date, analytics.is_trial, analytics.is_auto_renew, analytics.available_licenses, analytics.assigned_licenses, analytics.actively_used_licenses, analytics.capacity, analytics.trial_duration, analytics.license_status, analytics.license_id, analytics.workload, analytics.daily_actively_used_licenses, analytics.weekly_actively_used_licenses, analytics.is_sa_display, analytics.decrypted_customer_name
  │ └─IndexLookUp_83  5.00  root    limit embedded(offset:0, count:5)
  │   ├─Limit_82(Build) 5.00  cop[tikv]   offset:0, count:5
  │   │ └─IndexFullScan_80  5.00  cop[tikv] table:subscription_analytics, partition:p2, index:PRIMARY(day, partner_org_id, customer_org_id, offer_code, external_subscription_id, license_id) keep order:true, desc
  │   └─TableRowIDScan_81(Probe)  5.00  cop[tikv] table:subscription_analytics, partition:p2  keep order:false, stats:pseudo
  ├─Projection_104  5.00  root    analytics.day, analytics.partner_org_id, analytics.partner_name, analytics.customer_org_id, analytics.customer_name, analytics.offer_code, analytics.external_subscription_id, analytics.subscription_start_date, analytics.subscription_end_date, analytics.is_trial, analytics.is_auto_renew, analytics.available_licenses, analytics.assigned_licenses, analytics.actively_used_licenses, analytics.capacity, analytics.trial_duration, analytics.license_status, analytics.license_id, analytics.workload, analytics.daily_actively_used_licenses, analytics.weekly_actively_used_licenses, analytics.is_sa_display, analytics.decrypted_customer_name
  │ └─IndexLookUp_103 5.00  root    limit embedded(offset:0, count:5)
  │   ├─Limit_102(Build)  5.00  cop[tikv]   offset:0, count:5
  │   │ └─IndexFullScan_100 5.00  cop[tikv] table:subscription_analytics, partition:p3, index:PRIMARY(day, partner_org_id, customer_org_id, offer_code, external_subscription_id, license_id) keep order:true, desc
  │   └─TableRowIDScan_101(Probe) 5.00  cop[tikv] table:subscription_analytics, partition:p3  keep order:false, stats:pseudo
  ├─Projection_124  5.00  root    analytics.day, analytics.partner_org_id, analytics.partner_name, analytics.customer_org_id, analytics.customer_name, analytics.offer_code, analytics.external_subscription_id, analytics.subscription_start_date, analytics.subscription_end_date, analytics.is_trial, analytics.is_auto_renew, analytics.available_licenses, analytics.assigned_licenses, analytics.actively_used_licenses, analytics.capacity, analytics.trial_duration, analytics.license_status, analytics.license_id, analytics.workload, analytics.daily_actively_used_licenses, analytics.weekly_actively_used_licenses, analytics.is_sa_display, analytics.decrypted_customer_name
  │ └─IndexLookUp_123 5.00  root    limit embedded(offset:0, count:5)
  │   ├─Limit_122(Build)  5.00  cop[tikv]   offset:0, count:5
  │   │ └─IndexFullScan_120 5.00  cop[tikv] table:subscription_analytics, partition:p4, index:PRIMARY(day, partner_org_id, customer_org_id, offer_code, external_subscription_id, license_id) keep order:true, desc
  │   └─TableRowIDScan_121(Probe) 5.00  cop[tikv] table:subscription_analytics, partition:p4  keep order:false, stats:pseudo
  ├─Projection_144  5.00  root    analytics.day, analytics.partner_org_id, analytics.partner_name, analytics.customer_org_id, analytics.customer_name, analytics.offer_code, analytics.external_subscription_id, analytics.subscription_start_date, analytics.subscription_end_date, analytics.is_trial, analytics.is_auto_renew, analytics.available_licenses, analytics.assigned_licenses, analytics.actively_used_licenses, analytics.capacity, analytics.trial_duration, analytics.license_status, analytics.license_id, analytics.workload, analytics.daily_actively_used_licenses, analytics.weekly_actively_used_licenses, analytics.is_sa_display, analytics.decrypted_customer_name
  │ └─IndexLookUp_143 5.00  root    limit embedded(offset:0, count:5)
  │   ├─Limit_142(Build)  5.00  cop[tikv]   offset:0, count:5
  │   │ └─IndexFullScan_140 5.00  cop[tikv] table:subscription_analytics, partition:p5, index:PRIMARY(day, partner_org_id, customer_org_id, offer_code, external_subscription_id, license_id) keep order:true, desc
  │   └─TableRowIDScan_141(Probe) 5.00  cop[tikv] table:subscription_analytics, partition:p5  keep order:false, stats:pseudo
  └─Projection_164  5.00  root    analytics.day, analytics.partner_org_id, analytics.partner_name, analytics.customer_org_id, analytics.customer_name, analytics.offer_code, analytics.external_subscription_id, analytics.subscription_start_date, analytics.subscription_end_date, analytics.is_trial, analytics.is_auto_renew, analytics.available_licenses, analytics.assigned_licenses, analytics.actively_used_licenses, analytics.capacity, analytics.trial_duration, analytics.license_status, analytics.license_id, analytics.workload, analytics.daily_actively_used_licenses, analytics.weekly_actively_used_licenses, analytics.is_sa_display, analytics.decrypted_customer_name
    └─IndexLookUp_163 5.00  root    limit embedded(offset:0, count:5)
      ├─Limit_162(Build)  5.00  cop[tikv]   offset:0, count:5
      │ └─IndexFullScan_160 5.00  cop[tikv] table:subscription_analytics, partition:p6, index:PRIMARY(day, partner_org_id, customer_org_id, offer_code, external_subscription_id, license_id) keep order:true, desc
      └─TableRowIDScan_161(Probe) 5.00  cop[tikv] table:subscription_analytics, partition:p6  keep order:false, stats:pseudo

It seems that the index is invalid in the new cluster, and it remains so even after multiple Analyze operations. Has anyone encountered a similar situation? What could be the possible reasons? Thanks!

| username: 芮芮是产品 | Original post link

Delete the index after importing and then rebuild it.

| username: h5n1 | Original post link

Version 4.0.11 does not yet support clustered indexes for non-bigint columns, so it uses index scan + table lookup. From version 6.5 onwards, clustered indexes for strings are supported, and since day is the first column, scanning the entire table is the same as scanning the primary key index. You can create a new table and change it to a non-clustered index for testing:
PRIMARY KEY (day,partner_org_id,customer_org_id,offer_code,external_id,license_id) NONCLUSTERED

| username: swino | Original post link

Rebuild index

| username: tidb菜鸟一只 | Original post link

Using an index is not necessarily faster. Try executing it on both the old and new clusters; the new cluster should be faster.

| username: 小龙虾爱大龙虾 | Original post link

Check if the tables created in the new cluster are clustered tables (where the primary key is the row itself, so a table scan is a primary index scan), and if the old ones are non-clustered tables (which require scanning the primary index and then going back to the table).

| username: 有猫万事足 | Original post link

It seems to be an issue with dynamic partition pruning.

set @@session.tidb_partition_prune_mode = ‘static’

Try setting it this way and see if it works as before.

| username: TiDBer_小阿飞 | Original post link

It seems that your new cluster’s execution plan does not have partition pruning.

| username: zhanggame1 | Original post link

If it is a clustered table, the data is stored in the order of the primary key, and there is no additional index, so your query will perform a full table scan.
I think you can consider adding an index on this field.

| username: 小龙虾爱大龙虾 | Original post link

The old cluster didn’t have partition pruning either, did it? Establishing partitions like this won’t enable partition pruning. TiDB’s partition table functionality still needs improvement.

| username: h5n1 | Original post link

How can you prune without a WHERE clause?

| username: TiDBer_小阿飞 | Original post link

The index has a partition by range (WEEKDAY(day)).

| username: TiDBer_小阿飞 | Original post link

It’s just that his new cluster didn’t use the index. :joy:

| username: EricSong | Original post link

The new cluster doesn’t work. I actually executed it. The old cluster can produce results within 10 seconds, while the new cluster is still running after 10 minutes. So, in reality, it indeed didn’t use the index.

| username: EricSong | Original post link

Yes, the new cluster uses clustered indexes, but I understand that clustered indexes should also quickly return results when the column in the ORDER BY clause appears in the index. I did some tests with small table queries, and when the column in the ORDER BY clause is the first column of the clustered index, although a full table scan occurred, the estRows and limit were consistent, so I understand that the index was still implicitly used. However, when using the same statement on a large table, it resulted in a full table scan.

| username: EricSong | Original post link

I conducted some tests and it seems to be related to partitioned tables. If partitioned tables are not used, although it scans the entire table, the estRows is actually consistent with the limit, and the efficiency is also very high.
Here is my new question: 聚簇索引在分区表上未生效 - TiDB 的问答社区

| username: h5n1 | Original post link

Upload an explain analyze execution plan.

| username: 小龙虾爱大龙虾 | Original post link

Displaying a table scan does not actually scan the entire table. There is a limit operator at the upper level (similar to Oracle’s COUNT STOPKEY), which stops once the specified number of rows is retrieved.

| username: EricSong | Original post link

The new cluster query cannot be completed in half an hour, and the execution plan for explain analyze cannot be output. The explain analyze execution plan for the old cluster is as follows:

TopN_24	10.00	10	root		time:133.3ms, loops:2	partnerhubanalytics.subscription_analytics.day:asc, offset:0, count:10	12.1 KB	N/A
└─PartitionUnion_28	70.00	70	root		time:133.3ms, loops:8		N/A	N/A
  ├─Projection_44	10.00	10	root		time:72.2ms, loops:2, Concurrency:OFF	partnerhubanalytics.subscription_analytics.day, partnerhubanalytics.subscription_analytics.partner_org_id, partnerhubanalytics.subscription_analytics.partner_name, partnerhubanalytics.subscription_analytics.customer_org_id, partnerhubanalytics.subscription_analytics.customer_name, partnerhubanalytics.subscription_analytics.offer_code, partnerhubanalytics.subscription_analytics.external_subscription_id, partnerhubanalytics.subscription_analytics.subscription_start_date, partnerhubanalytics.subscription_analytics.subscription_end_date, partnerhubanalytics.subscription_analytics.is_trial, partnerhubanalytics.subscription_analytics.is_auto_renew, partnerhubanalytics.subscription_analytics.available_licenses, partnerhubanalytics.subscription_analytics.assigned_licenses, partnerhubanalytics.subscription_analytics.actively_used_licenses, partnerhubanalytics.subscription_analytics.capacity, partnerhubanalytics.subscription_analytics.trial_duration, partnerhubanalytics.subscription_analytics.license_status, partnerhubanalytics.subscription_analytics.license_id, partnerhubanalytics.subscription_analytics.workload, partnerhubanalytics.subscription_analytics.daily_actively_used_licenses, partnerhubanalytics.subscription_analytics.weekly_actively_used_licenses, partnerhubanalytics.subscription_analytics.is_sa_display, partnerhubanalytics.subscription_analytics.decrypted_customer_name	12.4 KB	N/A
  │ └─IndexLookUp_43	10.00	10	root		time:72.2ms, loops:2, index_task: {total_time: 1.67ms, fetch_handle: 1.67ms, build: 2.31µs, wait: 1.83µs}, table_task: {total_time: 9.45ms, num: 1, concurrency: 4}	limit embedded(offset:0, count:10)	24.2 KB	N/A
  │   ├─Limit_42(Build)	10.00	10	cop[tikv]		time:1.67ms, loops:1, cop_task: {num: 1, max: 1.54ms, proc_keys: 32, tot_proc: 1ms, rpc_num: 1, rpc_time: 1.54ms, copr_cache: disabled}, tikv_task:{time:1ms, loops:1}	offset:0, count:10	N/A	N/A
  │   │ └─IndexFullScan_40	10.00	32	cop[tikv]	table:sa, partition:p0, index:PRIMARY(day, partner_org_id, customer_org_id, offer_code, external_subscription_id, license_id)	tikv_task:{time:1ms, loops:1}	keep order:true	N/A	N/A
  │   └─TableRowIDScan_41(Probe)	10.00	10	cop[tikv]	table:sa, partition:p0	time:1.54ms, loops:2, cop_task: {num: 5, max: 1.46ms, min: 737.4µs, avg: 1.04ms, p95: 1.46ms, max_proc_keys: 3, p95_proc_keys: 3, tot_proc: 2ms, tot_wait: 1ms, rpc_num: 5, rpc_time: 5.16ms, copr_cache: disabled}, tikv_task:{proc max:1ms, min:0s, p80:1ms, p95:1ms, iters:5, tasks:5}	keep order:false, stats:pseudo	N/A	N/A
  ├─Projection_64	10.00	10	root		time:78.4ms, loops:2, Concurrency:OFF	partnerhubanalytics.subscription_analytics.day, partnerhubanalytics.subscription_analytics.partner_org_id, partnerhubanalytics.subscription_analytics.partner_name, partnerhubanalytics.subscription_analytics.customer_org_id, partnerhubanalytics.subscription_analytics.customer_name, partnerhubanalytics.subscription_analytics.offer_code, partnerhubanalytics.subscription_analytics.external_subscription_id, partnerhubanalytics.subscription_analytics.subscription_start_date, partnerhubanalytics.subscription_analytics.subscription_end_date, partnerhubanalytics.subscription_analytics.is_trial, partnerhubanalytics.subscription_analytics.is_auto_renew, partnerhubanalytics.subscription_analytics.available_licenses, partnerhubanalytics.subscription_analytics.assigned_licenses, partnerhubanalytics.subscription_analytics.actively_used_licenses, partnerhubanalytics.subscription_analytics.capacity, partnerhubanalytics.subscription_analytics.trial_duration, partnerhubanalytics.subscription_analytics.license_status, partnerhubanalytics.subscription_analytics.license_id, partnerhubanalytics.subscription_analytics.workload, partnerhubanalytics.subscription_analytics.daily_actively_used_licenses, partnerhubanalytics.subscription_analytics.weekly_actively_used_licenses, partnerhubanalytics.subscription_analytics.is_sa_display, partnerhubanalytics.subscription_analytics.decrypted_customer_name	12.4 KB	N/A
  │ └─IndexLookUp_63	10.00	10	root		time:78.4ms, loops:2, index_task: {total_time: 2.5ms, fetch_handle: 2.5ms, build: 1.65µs, wait: 1.14µs}, table_task: {total_time: 12.3ms, num: 1, concurrency: 4}	limit embedded(offset:0, count:10)	32.5 KB	N/A
  │   ├─Limit_62(Build)	10.00	10	cop[tikv]		time:2.49ms, loops:1, cop_task: {num: 1, max: 2.5ms, proc_keys: 32, rpc_num: 1, rpc_time: 2.49ms, copr_cache: disabled}, tikv_task:{time:0s, loops:1}	offset:0, count:10	N/A	N/A
  │   │ └─IndexFullScan_60	10.00	32	cop[tikv]	table:sa, partition:p1, index:PRIMARY(day, partner_org_id, customer_org_id, offer_code, external_subscription_id, license_id)	tikv_task:{time:0s, loops:1}	keep order:true	N/A	N/A
  │   └─TableRowIDScan_61(Probe)	10.00	10	cop[tikv]	table:sa, partition:p1	time:1.62ms, loops:2, cop_task: {num: 4, max: 1.55ms, min: 659.2µs, avg: 1.06ms, p95: 1.55ms, max_proc_keys: 3, p95_proc_keys: 3, tot_proc: 3ms, rpc_num: 4, rpc_time: 4.16ms, copr_cache: disabled}, tikv_task:{proc max:1ms, min:0s, p80:1ms, p95:1ms, iters:4, tasks:4}	keep order:false, stats:pseudo	N/A	N/A
  ├─Projection_84	10.00	10	root		time:75.8ms, loops:2, Concurrency:OFF	partnerhubanalytics.subscription_analytics.day, partnerhubanalytics.subscription_analytics.partner_org_id, partnerhubanalytics.subscription_analytics.partner_name, partnerhubanalytics.subscription_analytics.customer_org_id, partnerhubanalytics.subscription_analytics.customer_name, partnerhubanalytics.subscription_analytics.offer_code, partnerhubanalytics.subscription_analytics.external_subscription_id, partnerhubanalytics.subscription_analytics.subscription_start_date, partnerhubanalytics.subscription_analytics.subscription_end_date, partnerhubanalytics.subscription_analytics.is_trial, partnerhubanalytics.subscription_analytics.is_auto_renew, partnerhubanalytics.subscription_analytics.available_licenses, partnerhubanalytics.subscription_analytics.assigned_licenses, partnerhubanalytics.subscription_analytics.actively_used_licenses, partnerhubanalytics.subscription_analytics.capacity, partnerhubanalytics.subscription_analytics.trial_duration, partnerhubanalytics.subscription_analytics.license_status, partnerhubanalytics.subscription_analytics.license_id, partnerhubanalytics.subscription_analytics.workload, partnerhubanalytics.subscription_analytics.daily_actively_used_licenses, partnerhubanalytics.subscription_analytics.weekly_actively_used_licenses, partnerhubanalytics.subscription_analytics.is_sa_display, partnerhubanalytics.subscription_analytics.decrypted_customer_name	12.4 KB	N/A
  │ └─IndexLookUp_83	10.00	10	root		time:75.8ms, loops:2, index_task: {total_time: 1.94ms, fetch_handle: 1.93ms, build: 1.75µs, wait: 1.09µs}, table_task: {total_time: 9.83ms, num: 1, concurrency: 4}	limit embedded(offset:0, count:10)	24.6 KB	N/A
  │   ├─Limit_82(Build)	10.00	10	cop[tikv]		time:1.93ms, loops:1, cop_task: {num: 1, max: 1.87ms, proc_keys: 32, tot_proc: 1ms, rpc_num: 1, rpc_time: 1.86ms, copr_cache: disabled}, tikv_task:{time:1ms, loops:1}	offset:0, count:10	N/A	N/A
  │   │ └─IndexFullScan_80	10.00	32	cop[tikv]	table:sa, partition:p2, index:PRIMARY(day, partner_org_id, customer_org_id, offer_code, external_subscription_id, license_id)	tikv_task:{time:1ms, loops:1}	keep order:true	N/A	N/A
  │   └─TableRowIDScan_81(Probe)	10.00	10	cop[tikv]	table:sa, partition:p2	time:1.27ms, loops:2, cop_task: {num: 4, max: 1.14ms, min: 566.7µs, avg: 841.9µs, p95: 1.14ms, max_proc_keys: 4, p95_proc_keys: 4, tot_proc: 1ms, tot_wait: 2ms, rpc_num: 4, rpc_time: 3.35ms, copr_cache: disabled}, tikv_task:{proc max:1ms, min:0s, p80:1ms, p95:1ms, iters:4, tasks:4}	keep order:false, stats:pseudo	N/A	N/A
  ├─Projection_104	10.00	10	root		time:80.7ms, loops:2, Concurrency:OFF	partnerhubanalytics.subscription_analytics.day, partnerhubanalytics.subscription_analytics.partner_org_id, partnerhubanalytics.subscription_analytics.partner_name, partnerhubanalytics.subscription_analytics.customer_org_id, partnerhubanalytics.subscription_analytics.customer_name, partnerhubanalytics.subscription_analytics.offer_code, partnerhubanalytics.subscription_analytics.external_subscription_id, partnerhubanalytics.subscription_analytics.subscription_start_date, partnerhubanalytics.subscription_analytics.subscription_end_date, partnerhubanalytics.subscription_analytics.is_trial, partnerhubanalytics.subscription_analytics.is_auto_renew, partnerhubanalytics.subscription_analytics.available_licenses, partnerhubanalytics.subscription_analytics.assigned_licenses, partnerhubanalytics.subscription_analytics.actively_used_licenses, partnerhubanalytics.subscription_analytics.capacity, partnerhubanalytics.subscription_analytics.trial_duration, partnerhubanalytics.subscription_analytics.license_status, partnerhubanalytics.subscription_analytics.license_id, partnerhubanalytics.subscription_analytics.workload, partnerhubanalytics.subscription_analytics.daily_actively_used_licenses, partnerhubanalytics.subscription_analytics.weekly_actively_used_licenses, partnerhubanalytics.subscription_analytics.is_sa_display, partnerhubanalytics.subscription_analytics.decrypted_customer_name	12.4 KB	N/A
  │ └─IndexLookUp_103	10.00	10	root		time:80.7ms, loops:2, index_task: {total_time: 792.9µs, fetch_handle: 790.1µs, build: 1.52µs, wait: 1.2µs}, table_task: {total_time: 5.28ms, num: 1, concurrency: 4}	limit embedded(offset:0, count:10)	22.1 KB	N/A
  │   ├─Limit_102(Build)	10.00	10	cop[tikv]		time:786.2µs, loops:1, cop_task: {num: 1, max: 755.5µs, proc_keys: 32, rpc_num: 1, rpc_time: 750.6µs, copr_cache: disabled}, tikv_task:{time:0s, loops:1}	offset:0, count:10	N/A	N/A
  │   │ └─IndexFullScan_100	10.00	32	cop[tikv]	table:sa, partition:p3, index:PRIMARY(day, partner_org_id, customer_org_id, offer_code, external_subscription_id, license_id)	tikv_task:{time:0s, loops:1}	keep order:true	N/A	N/A
  │   └─TableRowIDScan_101(Probe)	10.00	10	cop[tikv]	table:sa, partition:p3	time:1.26ms, loops:2, cop_task: {num: 5, max: 1.19ms, min: 739.9µs, avg: 906.8µs, p95: 1.19ms, max_proc_keys: 4, p95_proc_keys: 4, tot_proc: 2ms, rpc_num: 5, rpc_time: 4.49ms, copr_cache: disabled}, tikv_task:{proc max:1ms, min:0s, p80:1ms, p95:1ms, iters:5, tasks:5}	keep order:false, stats:pseudo	N/A	N/A
  ├─Projection_124	10.00	10	root		time:52.4ms, loops:2, Concurrency:OFF	partnerhubanalytics.subscription_analytics.day, partnerhubanalytics.subscription_analytics.partner_org_id, partnerhubanalytics.subscription_analytics.partner_name, partnerhubanalytics.subscription_analytics.customer_org_id, partnerhubanalytics.subscription_analytics.customer_name, partnerhubanalytics.subscription_analytics.offer_code, partnerhubanalytics.subscription_analytics.external_subscription_id, partnerhubanalytics.subscription_analytics.subscription_start_date, partnerhubanalytics.subscription_analytics.subscription_end_date, partnerhubanalytics.subscription_analytics.is_trial, partnerhubanalytics.subscription_analytics.is_auto_renew, partnerhubanalytics.subscription_analytics.available_licenses, partnerhubanalytics.subscription_analytics.assigned_licenses, partnerhubanalytics.subscription_analytics.actively_used_licenses, partnerhubanalytics.subscription_analytics.capacity, partnerhubanalytics.subscription_analytics.trial_duration, partnerhubanalytics.subscription_analytics.license_status, partnerhubanalytics.subscription_analytics.license_id, partnerhubanalytics.subscription_analytics.workload, partnerhubanalytics.subscription_analytics.daily_actively_used_licenses, partnerhubanalytics.subscription_analytics.weekly_actively_used_licenses, partnerhubanalytics.subscription_analytics.is_sa_display, partnerhubanalytics.subscription_analytics.decrypted_customer_name	12.4 KB	N/A
  │ └─IndexLookUp_123	10.00	10	root		time:52.4ms, loops:2, index_task: {total_time: 1.65ms, fetch_handle: 1.64ms, build: 2.61µs, wait: 1.15µs}, table_task: {total_time: 9.39ms, num: 1, concurrency: 4}	limit embedded(offset:0, count:10)	27.2 KB	N/A
  │   ├─Limit_122(Build)	10.00	10	cop[tikv]		time:1.64ms, loops:1, cop_task: {num: 1, max: 1.5ms, proc_keys: 32, tot_proc: 1ms, rpc_num: 1, rpc_time: 1.5ms, copr_cache: disabled}, tikv_task:{time:1ms, loops:1}	offset:0, count:10	N/A	N/A
  │   │ └─IndexFullScan_120	10.00	32	cop[tikv]	table:sa, partition:p4, index:PRIMARY(day, partner_org_id, customer_org_id, offer_code, external_subscription_id, license_id)	tikv_task:{time:1ms, loops:1}	keep order:true	N/A	N/A
  │   └─TableRowIDScan_121(Probe)	10.00	10	cop[tikv]	table:sa, partition:p4	time:1.75ms, loops:2, cop_task: {num: 5, max: 1.64ms, min: 714.4µs, avg: 1.16ms, p95: 1.64ms, max_proc_keys: 4, p95_proc_keys: 4, tot_proc: 1ms, tot_wait: 1ms, rpc_num: 5, rpc_time: 5.77ms, copr_cache: disabled}, tikv_task:{proc max:1ms, min:0s, p80:1ms, p95:1ms, iters:5, tasks:5}	keep order:false, stats:pseudo	N/A	N/A
  ├─Projection_144	10.00	10	root		time:56ms, loops:2, Concurrency:OFF	partnerhubanalytics.subscription_analytics.day, partnerhubanalytics.subscription_analytics.partner_org_id, partnerhubanalytics.subscription_analytics.partner_name, partnerhubanalytics.subscription_analytics.customer_org_id, partnerhubanalytics.subscription_analytics.customer_name, partnerhubanalytics.subscription_analytics.offer_code, partnerhubanalytics.subscription_analytics.external_subscription_id, partnerhubanalytics.subscription_analytics.subscription_start_date, partnerhubanalytics.subscription_analytics.subscription_end_date, partnerhubanalytics.subscription_analytics.is_trial, partnerhubanalytics.subscription_analytics.is_auto_renew, partnerhubanalytics.subscription_analytics.available_licenses, partnerhubanalytics.subscription_analytics.assigned_licenses, partnerhubanalytics.subscription_analytics.actively_used_licenses, partnerhubanalytics.subscription_analytics.capacity, partnerhubanalytics.subscription_analytics.trial_duration, partnerhubanalytics.subscription_analytics.license_status, partnerhubanalytics.subscription_analytics.license_id
| username: EricSong | Original post link

Thank you for the explanation. I conducted a test using the following statements to create tables:

test_clustered - Clustered index, WEEKDAY partition
test_clustered_seq - Clustered index, monthly partition
test_nonclustered - Non-clustered index, WEEKDAY partition
test_nonclustered_seq - Non-clustered index, monthly partition

-- test.test_clustered definition

CREATE TABLE `test_clustered` (
  `day` date NOT NULL,
  `partner_org_id` varchar(60) NOT NULL,
  `partner_name` text DEFAULT NULL,
  `customer_org_id` varchar(60) NOT NULL,
  `license_id` varchar(255) NOT NULL,
  PRIMARY KEY (`day`,`partner_org_id`,`customer_org_id`,`license_id`) /*T![clustered_index] CLUSTERED */
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin
PARTITION BY RANGE (WEEKDAY(`day`))
(PARTITION `p0` VALUES LESS THAN (1),
 PARTITION `p1` VALUES LESS THAN (2),
 PARTITION `p2` VALUES LESS THAN (3),
 PARTITION `p3` VALUES LESS THAN (4),
 PARTITION `p4` VALUES LESS THAN (5),
 PARTITION `p5` VALUES LESS THAN (6),
 PARTITION `p6` VALUES LESS THAN (7));


-- test.test_clustered_seq definition

CREATE TABLE `test_clustered_seq` (
  `day` date NOT NULL,
  `partner_org_id` varchar(60) NOT NULL,
  `partner_name` text DEFAULT NULL,
  `customer_org_id` varchar(60) NOT NULL,
  `license_id` varchar(255) NOT NULL,
  PRIMARY KEY (`day`,`partner_org_id`,`customer_org_id`,`license_id`) /*T![clustered_index] CLUSTERED */
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin
PARTITION BY RANGE (TO_DAYS(`day`))
(PARTITION `p201912` VALUES LESS THAN (737790),
 PARTITION `p202001` VALUES LESS THAN (737821),
 PARTITION `p202002` VALUES LESS THAN (737850),
 PARTITION `p202003` VALUES LESS THAN (737881),
 PARTITION `p202004` VALUES LESS THAN (737911),
 PARTITION `p202005` VALUES LESS THAN (737942),
 PARTITION `p202006` VALUES LESS THAN (737972),
 PARTITION `p202007` VALUES LESS THAN (738003),
 PARTITION `p202008` VALUES LESS THAN (738034),
 PARTITION `p202009` VALUES LESS THAN (738064),
 PARTITION `p202010` VALUES LESS THAN (738095),
 PARTITION `p202011` VALUES LESS THAN (738125),
 PARTITION `p202012` VALUES LESS THAN (738156),
 PARTITION `p202101` VALUES LESS THAN (738187),
 PARTITION `p202102` VALUES LESS THAN (738215),
 PARTITION `p202103` VALUES LESS THAN (738246),
 PARTITION `p202104` VALUES LESS THAN (738276),
 PARTITION `p202105` VALUES LESS THAN (738307),
 PARTITION `p202106` VALUES LESS THAN (738337),
 PARTITION `p202107` VALUES LESS THAN (738368),
 PARTITION `p202108` VALUES LESS THAN (738399),
 PARTITION `p202109` VALUES LESS THAN (738429),
 PARTITION `p202110` VALUES LESS THAN (738460),
 PARTITION `p202111` VALUES LESS THAN (738490),
 PARTITION `p202112` VALUES LESS THAN (738521),
 PARTITION `p202201` VALUES LESS THAN (738552),
 PARTITION `p202202` VALUES LESS THAN (738580),
 PARTITION `p202203` VALUES LESS THAN (738611),
 PARTITION `p202204` VALUES LESS THAN (738641),
 PARTITION `p202205` VALUES LESS THAN (738672),
 PARTITION `p202206` VALUES LESS THAN (738702),
 PARTITION `p202207` VALUES LESS THAN (738733),
 PARTITION `p202208` VALUES LESS THAN (738764),
 PARTITION `p202209` VALUES LESS THAN (738794),
 PARTITION `p202210` VALUES LESS THAN (738825),
 PARTITION `p202211` VALUES LESS THAN (738855),
 PARTITION `p202212` VALUES LESS THAN (738886),
 PARTITION `p202301` VALUES LESS THAN (738917),
 PARTITION `p202302` VALUES LESS THAN (738945),
 PARTITION `p202303` VALUES LESS THAN (738976),
 PARTITION `p202304` VALUES LESS THAN (739006),
 PARTITION `p202305` VALUES LESS THAN (739037),
 PARTITION `p202306` VALUES LESS THAN (739067),
 PARTITION `p202307` VALUES LESS THAN (739098),
 PARTITION `p202308` VALUES LESS THAN (739129),
 PARTITION `p202309` VALUES LESS THAN (739159),
 PARTITION `p202310` VALUES LESS THAN (739190),
 PARTITION `p202311` VALUES LESS THAN (739220),
 PARTITION `p202312` VALUES LESS THAN (739251));


-- test.test_nonclustered definition

CREATE TABLE `test_nonclustered` (
  `day` date NOT NULL,
  `partner_org_id` varchar(60) NOT NULL,
  `partner_name` text DEFAULT NULL,
  `customer_org_id` varchar(60) NOT NULL,
  `license_id` varchar(255) NOT NULL,
  PRIMARY KEY (`day`,`partner_org_id`,`customer_org_id`,`license_id`) /*T![clustered_index] NONCLUSTERED */
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin
PARTITION BY RANGE (WEEKDAY(`day`))
(PARTITION `p0` VALUES LESS THAN (1),
 PARTITION `p1` VALUES LESS THAN (2),
 PARTITION `p2` VALUES LESS THAN (3),
 PARTITION `p3` VALUES LESS THAN (4),
 PARTITION `p4` VALUES LESS THAN (5),
 PARTITION `p5` VALUES LESS THAN (6),
 PARTITION `p6` VALUES LESS THAN (7));


-- test.test_nonclustered_seq definition

CREATE TABLE `test_nonclustered_seq` (
  `day` date NOT NULL,
  `partner_org_id` varchar(60) NOT NULL,
  `partner_name` text DEFAULT NULL,
  `customer_org_id` varchar(60) NOT NULL,
  `license_id` varchar(255) NOT NULL,
  PRIMARY KEY (`day`,`partner_org_id`,`customer_org_id`,`license_id`) /*T![clustered_index] NONCLUSTERED */
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin
PARTITION BY RANGE (TO_DAYS(`day`))
(PARTITION `p201912` VALUES LESS THAN (737790),
 PARTITION `p202001` VALUES LESS THAN (737821),
 PARTITION `p202002` VALUES LESS THAN (737850),
 PARTITION `p202003` VALUES LESS THAN (737881),
 PARTITION `p202004` VALUES LESS THAN (737911),
 PARTITION `p202005` VALUES LESS THAN (737942),
 PARTITION `p202006` VALUES LESS THAN (737972),
 PARTITION `p202007` VALUES LESS THAN (738003),
 PARTITION `p202008` VALUES LESS THAN (738034),
 PARTITION `p202009` VALUES LESS THAN (738064),
 PARTITION `p202010` VALUES LESS THAN (738095),
 PARTITION `p202011` VALUES LESS THAN (738125),
 PARTITION `p202012` VALUES LESS THAN (738156),
 PARTITION `p202101` VALUES LESS THAN (738187),
 PARTITION `p202102` VALUES LESS THAN (738215),
 PARTITION `p202103` VALUES LESS THAN (738246),
 PARTITION `p202104` VALUES LESS THAN (738276),
 PARTITION `p202105` VALUES LESS THAN (738307),
 PARTITION `p202106` VALUES LESS THAN (738337),
 PARTITION `p202107` VALUES LESS THAN (738368),
 PARTITION `p202108` VALUES LESS THAN (738399),
 PARTITION `p202109` VALUES LESS THAN (738429),
 PARTITION `p202110` VALUES LESS THAN (738460),
 PARTITION `p202111` VALUES LESS THAN (738490),
 PARTITION `p202112` VALUES LESS THAN (738521),
 PARTITION `p202201` VALUES LESS THAN (738552),
 PARTITION `p202202` VALUES LESS THAN (738580),
 PARTITION `p202203` VALUES LESS THAN (738611),
 PARTITION `p202204` VALUES LESS THAN (738641),
 PARTITION `p202205` VALUES LESS THAN (738672),
 PARTITION `p202206` VALUES LESS THAN (738702),
 PARTITION `p202207` VALUES LESS THAN (738733),
 PARTITION `p202208` VALUES LESS THAN (738764),
 PARTITION `p202209` VALUES LESS THAN (738794),
 PARTITION `p202210` VALUES LESS THAN (738825),
 PARTITION `p202211` VALUES LESS THAN (738855),
 PARTITION `p202212` VALUES LESS THAN (738886),
 PARTITION `p202301` VALUES LESS THAN (738917),
 PARTITION `p202302` VALUES LESS THAN (738945),
 PARTITION `p202303` VALUES LESS THAN (738976),
 PARTITION `p202304` VALUES LESS THAN (739006),
 PARTITION `p202305` VALUES LESS THAN (739037),
 PARTITION `p202306` VALUES LESS THAN (739067),
 PARTITION `p202307` VALUES LESS THAN (739098),
 PARTITION `p202308` VALUES LESS THAN (739129),
 PARTITION `p202309` VALUES LESS THAN (739159),
 PARTITION `p202310` VALUES LESS THAN (739190),
 PARTITION `p202311` VALUES LESS THAN (739220),
 PARTITION `p202312` VALUES LESS THAN (739251));

Then I inserted about 100,000 rows of data and executed the following queries after analyze table:

explain analyze select * from test_nonclustered_seq order by day limit 10;
TopN_7	10.00	10	root		time:6.74ms, loops:2	test.test_nonclustered_seq.day, offset:0, count:10	2.37 KB	N/A
└─TableReader_14	10.00	480	root	partition:all	time:6.7ms, loops:3, cop_task: {num: 49, max: 4.44ms, min: 241.6µs, avg: 1.4ms, p95: 3.77ms, max_proc_keys: 2161, p95_proc_keys: 2085, tot_proc: 19ms, tot_wait: 8ms, rpc_num: 49, rpc_time: 68ms, copr_cache_hit_ratio: 0.80, distsql_concurrency: 15}	data:TopN_13	9.75 KB	N/A
  └─TopN_13	10.00	480	cop[tikv]		tikv_task:{proc max:12ms, min:0s, avg: 4.33ms, p80:8ms, p95:12ms, iters:133, tasks:49}, scan_detail: {total_process_keys: 18528, total_process_keys_size: 3460454, total_keys: 18538, get_snapshot_time: 22ms, rocksdb: {key_skipped_count: 18528, block: {cache_hit_count: 85}}}	test.test_nonclustered_seq.day, offset:0, count:10	N/A	N/A
    └─TableFullScan_12	100000.00	100000	cop[tikv]	table:test_nonclustered_seq	tikv_task:{proc max:12ms, min:0s, avg: 4ms, p80:8ms, p95:12ms, iters:133, tasks:49}	keep order:false	N/A	N/A
--------------------------------------------------------------------------------------------------------
explain analyze select * from test_nonclustered order by day limit 10;
TopN_7	10.00	10	root		time:10.7ms, loops:2	test.test_nonclustered.day, offset:0, count:10	2.37 KB	N/A
└─TableReader_14	10.00	70	root	partition:all	time:10.7ms, loops:3, cop_task: {num: 7, max: 10.5ms, min: 810.6µs, avg: 2.26ms, p95: 10.5ms, max_proc_keys: 14225, p95_proc_keys: 14225, tot_proc: 9ms, rpc_num: 7, rpc_time: 15.7ms, copr_cache_hit_ratio: 0.86, distsql_concurrency: 15}	data:TopN_13	5.77 KB	N/A
  └─TopN_13	10.00	70	cop[tikv]		tikv_task:{proc max:16ms, min:12ms, avg: 15.4ms, p80:16ms, p95:16ms, iters:100, tasks:7}, scan_detail: {total_process_keys: 14225, total_process_keys_size: 2656841, total_keys: 14226, get_snapshot_time: 2.73ms, rocksdb: {key_skipped_count: 14225, block: {cache_hit_count: 47}}}	test.test_nonclustered.day, offset:0, count:10	N/A	N/A
    └─TableFullScan_12	100000.00	100000	cop[tikv]	table:test_nonclustered	tikv_task:{proc max:16ms, min:12ms, avg: 14.3ms, p80:16ms, p95:16ms, iters:100, tasks:7}	keep order:false	N/A	N/A
--------------------------------------------------------------------------------------------------------
explain analyze select * from test_clustered_seq order by day desc limit 10;
TopN_7	10.00	10	root		time:3.07s, loops:2	test.test_clustered_seq.day:desc, offset:0, count:10	9.48 KB	N/A
└─TableReader_14	10.00	100000	root	partition:all	time:3.07s, loops:100, cop_task: {num: 10025, max: 1.28ms, min: 197.8µs, avg: 294.1µs, p95: 364.1µs, max_proc_keys: 10, p95_proc_keys: 10, rpc_num: 10027, rpc_time: 2.9s, copr_cache_hit_ratio: 0.00, distsql_concurrency: 1}, backoff{regionMiss: 2ms}	data:Limit_13	3.96 KB	N/A
  └─Limit_13	10.00	100000	cop[tikv]		tikv_task:{proc max:4ms, min:0s, avg: 41.5µs, p80:0s, p95:0s, iters:10025, tasks:10025}, scan_detail: {total_process_keys: 100000, total_process_keys_size: 23877748, total_keys: 110025, get_snapshot_time: 66.1ms, rocksdb: {key_skipped_count: 109975, block: {cache_hit_count: 56992, read_count: 202, read_byte: 5.68 MB, read_time: 3.01ms}}}	offset:0, count:10	N/A	N/A
    └─TableFullScan_12	100000.00	100000	cop[tikv]	table:test_clustered_seq	tikv_task:{proc max:4ms, min:0s, avg: 41.1µs, p80:0s, p95:0s, iters:10025, tasks:10025}	keep order:false, desc	N/A	N/A
--------------------------------------------------------------------------------------------------------
explain analyze select * from test_clustered order by day limit 10;
TopN_7	10.00	10	root		time:3.08s, loops:2	test.test_clustered.day, offset:0, count:10	2.37 KB	N/A
└─TableReader_14	10.00	110000	root	partition:all	time:3.08s, loops:109, cop_task: {num: 11004, max: 16.7ms, min: 184.3µs, avg: 268.6µs, p95: 326.1µs, max_proc_keys: 10, p95_proc_keys: 10, rpc_num: 11004, rpc_time: 2.9s, copr_cache_hit_ratio: 0.00, distsql_concurrency: 1}	data:Limit_13	3.95 KB	N/A
  └─Limit_13	10.00	110000	cop[tikv]		tikv_task:{proc max:4ms, min:0s, avg: 28µs, p80:0s, p95:0s, iters:11004, tasks:11004}, scan_detail: {total_process_keys: 110000, total_process_keys_size: 26265513, total_keys: 121004, get_snapshot_time: 47.1ms, rocksdb: {key_skipped_count: 110000, block: {cache_hit_count: 36493, read_count: 53, read_byte: 1.48 MB, read_time: 889.2µs}}}	offset:0, count:10	N/A	N/A
    └─TableFullScan_12	110000.00	110000	cop[tikv]	table:test_clustered	tikv_task:{proc max:4ms, min:0s, avg: 27.6µs, p80:0s, p95:0s, iters:11004, tasks:11004}	keep order:false	N/A	N/A

Overall, the queries on the clustered index tables are slower, and from the actRows, it is evident that the clustered index indeed scans all records, rather than directly fetching the largest few from the partitions and then passing them to the upstream operator for merge sorting.
Does this mean that for clustered indexes, the partitions are unordered?