Repository navigation
Closed
Labels
Description
Is there an existing issue for the same bug?
- I have checked the existing issues.
Branch Name
Commit ID
Other Environment Information
- Hardware parameters:
- OS type:
- Others:Actual Behavior
//这个走了索引。看到的数据是这样的
mysql> SELECT COUNT(*) FROM taas.token_usage_logs WHERE billing_report_status = 'pending' and billing_report_group_id = '';
+----------+
| COUNT(*) |
+----------+
| 42706 |
+----------+
1 row in set (0.02 sec)
//这个没有走索引,看到数据是另外一个样子
mysql> SELECT COUNT(*) FROM taas.token_usage_logs WHERE billing_report_status = 'pending' and length(billing_report_group_id)=0;
+----------+
| COUNT(*) |
+----------+
| 41857 |
+----------+
1 row in set (0.03 sec)
mysql> desc taas.token_usage_logs;
+--------------------------+----------------+------+------+---------+-------+---------+
| Field | Type | Null | Key | Default | Extra | Comment |
+--------------------------+----------------+------+------+---------+-------+---------+
| id | VARCHAR(64) | NO | PRI | NULL | | |
| token_key_id | VARCHAR(64) | NO | MUL | NULL | | |
| token_key_name | VARCHAR(255) | NO | | | | |
| user_id | VARCHAR(128) | NO | MUL | NULL | | |
| user_name | VARCHAR(255) | NO | | | | |
| provider_id | VARCHAR(64) | NO | MUL | | | |
| provider_name | VARCHAR(255) | NO | | | | |
| model_id | VARCHAR(64) | NO | MUL | NULL | | |
| model_name | VARCHAR(255) | NO | | | | |
| caller_ip | VARCHAR(64) | NO | | | | |
| called_at | BIGINT(64) | NO | MUL | NULL | | |
| input_tokens | BIGINT(64) | NO | | 0 | | |
| output_tokens | BIGINT(64) | NO | | 0 | | |
| cache_read_tokens | BIGINT(64) | NO | | 0 | | |
| cache_creation_tokens | BIGINT(64) | NO | | 0 | | |
| cost | DECIMAL(24,8) | NO | | 0 | | |
| status | VARCHAR(32) | NO | MUL | NULL | | |
| error_type | VARCHAR(128) | NO | | | | |
| error_message | TEXT(0) | NO | | NULL | | |
| latency_ms | BIGINT(64) | NO | | 0 | | |
| request_body | TEXT(0) | NO | | NULL | | |
| response_body | TEXT(0) | NO | | NULL | | |
| provider_attempt_count | BIGINT(64) | NO | | 0 | | |
| billing_report_status | VARCHAR(32) | NO | MUL | pending | | |
| billing_event_key | VARCHAR(255) | NO | | | | |
| billing_event_id | VARCHAR(64) | NO | | | | |
| billing_record_id | VARCHAR(64) | NO | | | | |
| billing_account_id | VARCHAR(128) | NO | | | | |
| billing_rated_credit | DECIMAL(36,18) | NO | | 0 | | |
| billing_credit_balance | DECIMAL(36,18) | NO | | 0 | | |
| billing_credit_available | DECIMAL(36,18) | NO | | 0 | | |
| billing_reported_at | BIGINT(64) | NO | | 0 | | |
| billing_report_attempts | BIGINT(64) | NO | | 0 | | |
| billing_last_error | VARCHAR(4096) | NO | | | | |
| created_at | BIGINT(64) | NO | | NULL | | |
| requested_model | VARCHAR(255) | NO | MUL | | | |
| pricing_mode | VARCHAR(32) | NO | MUL | retail | | |
| enterprise_plan_id | VARCHAR(64) | NO | MUL | | | |
| enterprise_plan_name | VARCHAR(255) | NO | | | | |
| enterprise_contract_no | VARCHAR(128) | NO | MUL | | | |
| enterprise_plan_model_id | VARCHAR(64) | NO | MUL | | | |
| pricing_snapshot | TEXT(0) | YES | | NULL | | |
| billing_report_group_id | VARCHAR(64) | NO | MUL | | | |
| meter_code | VARCHAR(128) | NO | | | | |
| request_body_ref_id | VARCHAR(64) | NO | MUL | | | |
| response_body_ref_id | VARCHAR(64) | NO | MUL | | | |
| request_body_bytes | BIGINT(64) | NO | | 0 | | |
| response_body_bytes | BIGINT(64) | NO | | 0 | | |
| request_body_sha256 | CHAR(64) | NO | | | | |
| response_body_sha256 | CHAR(64) | NO | | | | |
| request_id | VARCHAR(64) | NO | MUL | | | |
| key_hash_prefix | VARCHAR(32) | NO | | | | |
+--------------------------+----------------+------+------+---------+-------+---------+
52 rows in set (1.05 sec)
mysql> show create table taas.token_usage_logs;
+------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| token_usage_logs | CREATE TABLE `token_usage_logs` (
`id` varchar(64) NOT NULL,
`token_key_id` varchar(64) NOT NULL,
`token_key_name` varchar(255) NOT NULL DEFAULT '',
`user_id` varchar(128) NOT NULL,
`user_name` varchar(255) NOT NULL DEFAULT '',
`provider_id` varchar(64) NOT NULL DEFAULT '',
`provider_name` varchar(255) NOT NULL DEFAULT '',
`model_id` varchar(64) NOT NULL,
`model_name` varchar(255) NOT NULL DEFAULT '',
`caller_ip` varchar(64) NOT NULL DEFAULT '',
`called_at` bigint NOT NULL,
`input_tokens` bigint NOT NULL DEFAULT 0,
`output_tokens` bigint NOT NULL DEFAULT 0,
`cache_read_tokens` bigint NOT NULL DEFAULT 0,
`cache_creation_tokens` bigint NOT NULL DEFAULT 0,
`cost` decimal(24,8) NOT NULL DEFAULT 0,
`status` varchar(32) NOT NULL,
`error_type` varchar(128) NOT NULL DEFAULT '',
`error_message` text NOT NULL,
`latency_ms` bigint NOT NULL DEFAULT 0,
`request_body` text NOT NULL,
`response_body` text NOT NULL,
`provider_attempt_count` bigint NOT NULL DEFAULT 0,
`billing_report_status` varchar(32) NOT NULL DEFAULT 'pending',
`billing_event_key` varchar(255) NOT NULL DEFAULT '',
`billing_event_id` varchar(64) NOT NULL DEFAULT '',
`billing_record_id` varchar(64) NOT NULL DEFAULT '',
`billing_account_id` varchar(128) NOT NULL DEFAULT '',
`billing_rated_credit` decimal(36,18) NOT NULL DEFAULT 0,
`billing_credit_balance` decimal(36,18) NOT NULL DEFAULT 0,
`billing_credit_available` decimal(36,18) NOT NULL DEFAULT 0,
`billing_reported_at` bigint NOT NULL DEFAULT 0,
`billing_report_attempts` bigint NOT NULL DEFAULT 0,
`billing_last_error` varchar(4096) NOT NULL DEFAULT '',
`created_at` bigint NOT NULL,
`requested_model` varchar(255) NOT NULL DEFAULT '',
`pricing_mode` varchar(32) NOT NULL DEFAULT 'retail',
`enterprise_plan_id` varchar(64) NOT NULL DEFAULT '',
`enterprise_plan_name` varchar(255) NOT NULL DEFAULT '',
`enterprise_contract_no` varchar(128) NOT NULL DEFAULT '',
`enterprise_plan_model_id` varchar(64) NOT NULL DEFAULT '',
`pricing_snapshot` text DEFAULT NULL,
`billing_report_group_id` varchar(64) NOT NULL DEFAULT '',
`meter_code` varchar(128) NOT NULL DEFAULT '',
`request_body_ref_id` varchar(64) NOT NULL DEFAULT '',
`response_body_ref_id` varchar(64) NOT NULL DEFAULT '',
`request_body_bytes` bigint NOT NULL DEFAULT 0,
`response_body_bytes` bigint NOT NULL DEFAULT 0,
`request_body_sha256` char(64) NOT NULL DEFAULT '',
`response_body_sha256` char(64) NOT NULL DEFAULT '',
`request_id` varchar(64) NOT NULL DEFAULT '',
`key_hash_prefix` varchar(32) NOT NULL DEFAULT '',
PRIMARY KEY (`id`),
KEY `idx_usage_called` (`called_at`),
KEY `idx_usage_user_called` (`user_id`,`called_at`),
KEY `idx_usage_token_called` (`token_key_id`,`called_at`),
KEY `idx_usage_model_called` (`model_id`,`called_at`),
KEY `idx_usage_provider_called` (`provider_id`,`called_at`),
KEY `idx_usage_status_called` (`status`,`called_at`),
KEY `idx_usage_billing_report` (`billing_report_status`,`called_at`),
KEY `idx_usage_billing_report_status` (`billing_report_status`,`status`,`called_at`,`id`),
KEY `idx_usage_pricing_mode_called` (`pricing_mode`,`called_at`),
KEY `idx_usage_enterprise_plan_called` (`enterprise_plan_id`,`called_at`),
KEY `idx_usage_enterprise_contract_called` (`enterprise_contract_no`,`called_at`),
KEY `idx_usage_enterprise_plan_model_called` (`enterprise_plan_model_id`,`called_at`),
KEY `idx_usage_requested_model_called` (`requested_model`,`called_at`),
KEY `idx_usage_request_id` (`request_id`),
KEY `idx_usage_request_body_ref` (`request_body_ref_id`),
KEY `idx_usage_response_body_ref` (`response_body_ref_id`),
KEY `idx_usage_report_group_pending` (`billing_report_group_id`,`billing_report_status`,`called_at`,`id`)
) |
+------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.06 sec)
mysql> select git_version();
+---------------+
| git_version() |
+---------------+
| 405ef9ae3 |
+---------------+
1 row in set (0.02 sec)
Expected Behavior
No response
Steps to Reproduce
如上Additional information
No response
Activity
ck89119 commentedon Jul 6, 2026
定位结论
根因与 #25341 同根:当二级索引的列包含了完整主键(covering full PK)时,旧逻辑不再用 DML 维护这张隐藏索引表,但优化器仍会选中它来读取 leading-prefix 谓词,于是隐藏索引表内容随后续 INSERT/UPDATE/DELETE 逐渐陈旧,导致走索引与不走索引结果不一致(#25341 表现为偏少/为 0,本 issue 表现为偏多)。
本 issue 中
idx_usage_report_group_pending (billing_report_group_id, billing_report_status, called_at, id)的列包含了完整主键id,正好落入这一情形。经核对,这不是空字符串前导列在前缀编码上的问题:行级
PrefixEq、磁盘侧CollectOffsetsByPrefixEqFactory、内存侧pk_filter_mem三条路径对billing_report_group_id = ''的前缀匹配都是正确的(空串终止符0x00会被转义,不会与非空串产生假前缀匹配)。修复状态
该问题已由 #25342(commit
2bf76218f,2026-07-02 合入main)修复:getValidIndexes现在无条件维护常规二级索引,即使其列覆盖完整主键;serial_full,与 DML/索引表写入编码保持一致。报告环境
git_version() = 405ef9ae3(#25165,2026-06-26)早于 #25342,因此复现了该 bug。实测验证
在
main(HEAD2a008285e,已含 #25342)上构造与本 issue 同形状的场景(单列主键 + 覆盖完整主键的复合二级索引 + 前导列为空串'',再用 UPDATE/DELETE 制造陈旧):g = ''length(g) = 0explain确认走索引一条确实使用了Index Table Scan ... prefix_eq。两者完全一致,不再 over-count。建议
drop后重新create该二级索引(如idx_usage_report_group_pending)。4.0-dev/3.0-dev目前尚未包含 fix: maintain covering secondary indexes #25342,如相关发布分支需要,建议 backport。update BVT result for covering index staleness regression test (matri…
fix: only skip PK-redundant indexes, keep maintaining covering ones
fix: maintain covering secondary indexes (cherry-pick #25342) (#25474)
Ariznawlll commentedon Jul 8, 2026
Verified on latest
mainatbd2497354.Result: fixed for the tested shape. A composite secondary index that includes the complete primary key stays consistent after
UPDATE,DELETE, and subsequentINSERT.Test shape:
primary key(id), key idx_usage_report_group_pending(billing_report_group_id, billing_report_status, called_at, id)Validation compared indexed and non-indexed predicates:
Result before DML:
Result after updating two rows, deleting one row, and inserting two rows:
Added motr regression:
suites/14_issue_regression/t/issue_25460_composite_index_full_pk_maintenance.testsuites/14_issue_regression/r/issue_25460_composite_index_full_pk_maintenance.resultAriznawlll commentedon Jul 8, 2026
Additional motr regression expansion added after the initial verification.
Extra coverage now included in
suites/14_issue_regression/t/issue_25460_composite_index_full_pk_maintenance.test:ORDER BY called_at, id LIMIT 2comparison after DML for index and non-index paths.DISTINCT statuscomparison after DML for index and non-index paths.This keeps checking that the composite secondary index containing the full primary key remains consistent for filtering, ordered reads, limit reads, and distinct reads after maintenance operations.
Re-ran motr on latest
mainbd2497354after expansion:Ariznawlll commentedon Jul 8, 2026
已验证 main 分支已修复。
验证版本:main HEAD 2a00828,已包含 #25342 / commit 2bf7621。
验证结果:同形状场景下,走索引
billing_report_group_id = ''和绕索引length(billing_report_group_id)=0结果一致;UPDATE/DELETE 后也一致。同时已补充 MOTR 回归用例:
matrixorigin/motr#39
case: issue_25460_covering_index_stale_count
Ariznawlll commentedon Jul 9, 2026
在最新
3.0-dev上做了一轮验证,验证 commit:验证方式:本地启动该 commit 的单进程 MatrixOne,使用和 issue 中
taas.token_usage_logs关键字段/索引等价的表结构,重点覆盖:billing_report_group_id varchar(64) not null default ''billing_report_status varchar(32) not null default 'pending'(billing_report_group_id, billing_report_status, called_at, id)billing_report_group_id、更新billing_report_status、删除、再插入等场景对比 SQL:
结果:
EXPLAIN确认billing_report_group_id = ''走了idx_usage_report_group_pending索引路径;length(billing_report_group_id)=0走的是不同路径。两条路径的结果一致,未复现 issue 中的 count 不一致问题。结论:在最新
3.0-dev@18addf5d4上,使用等价 schema/index 和本地构造数据暂未复现该问题