Skip to content

[Bug]: 使用索引拿到的数据不正确 #25460

Closed

Description

@ouyuanning

Is there an existing issue for the same bug?

  • I have checked the existing issues.

Branch Name

405ef9a

Commit ID

405ef9a

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

  1. added
    kind/bug
    on Jul 5, 2026
  2. ck89119 commented on Jul 6, 2026

    @ck89119
    Contributor

    定位结论

    根因与 #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 现在无条件维护常规二级索引,即使其列覆盖完整主键;
    • 读取侧的 lookup 统一使用 serial_full,与 DML/索引表写入编码保持一致。

    报告环境 git_version() = 405ef9ae3(#25165,2026-06-26)早于 #25342,因此复现了该 bug。

    实测验证

    在 main(HEAD 2a008285e,已含 #25342)上构造与本 issue 同形状的场景(单列主键 + 覆盖完整主键的复合二级索引 + 前导列为空串 '',再用 UPDATE/DELETE 制造陈旧):

    阶段 走索引 g = '' 不走索引 length(g) = 0
    基线(纯 INSERT 1000 行) 1000 1000
    UPDATE 100 行 + DELETE 50 行之后 850 850

    explain 确认走索引一条确实使用了 Index Table Scan ... prefix_eq。两者完全一致,不再 over-count。

    建议

    1. 升级到包含 fix: maintain covering secondary indexes #25342 的版本后可从根本上避免该问题。
    2. 历史数据需要重建索引:旧版本生成的隐藏索引表本身已经陈旧,升级不会自动修正,需要 drop 后重新 create 该二级索引(如 idx_usage_report_group_pending)。
    3. 4.0-dev / 3.0-dev 目前尚未包含 fix: maintain covering secondary indexes #25342,如相关发布分支需要,建议 backport。
  3. assigned and unassigned on Jul 7, 2026
  4. Repository owner unassigned on Jul 8, 2026
  5. Ariznawlll commented on Jul 8, 2026

    @Ariznawlll
    Collaborator

    Verified on latest main at bd2497354.

    Result: fixed for the tested shape. A composite secondary index that includes the complete primary key stays consistent after UPDATE, DELETE, and subsequent INSERT.

    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:

    where billing_report_group_id = '' and billing_report_status = 'pending'
    where length(billing_report_group_id) = 0 and billing_report_status = 'pending'

    Result before DML:

    index    count=4 sum=12.00000000
    noindex  count=4 sum=12.00000000
    

    Result after updating two rows, deleting one row, and inserting two rows:

    index_after    ids=id006,id007,id008 count=3 sum=21.00000000
    noindex_after  ids=id006,id007,id008 count=3 sum=21.00000000
    

    Added motr regression:

    • suites/14_issue_regression/t/issue_25460_composite_index_full_pk_maintenance.test
    • suites/14_issue_regression/r/issue_25460_composite_index_full_pk_maintenance.result
  6. Ariznawlll commented on Jul 8, 2026

    @Ariznawlll
    Collaborator

    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:

    • Original indexed vs non-indexed predicate comparison before DML.
    • DML sequence: update rows out of the indexed pending set, delete one row, then insert new pending rows.
    • Indexed vs non-indexed comparison after DML using count/sum/group_concat.
    • Added ORDER BY called_at, id LIMIT 2 comparison after DML for index and non-index paths.
    • Added DISTINCT status comparison 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 main bd2497354 after expansion:

    [ISSUE_25460_COMPOSITE_INDEX_FULL_PK_MAINTENANCE] success | stmts=17 ok=17 fail=0
    
  7. Ariznawlll commented on Jul 8, 2026

    @Ariznawlll
    Collaborator

    已验证 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

  8. Ariznawlll commented on Jul 9, 2026

    @Ariznawlll
    Collaborator

    在最新 3.0-dev 上做了一轮验证,验证 commit:

    18addf5d48774999169de91e202f508f55a0bd49
    git_version() = 18addf5d4
    

    验证方式:本地启动该 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:

    select count(*)
    from token_usage_logs
    where billing_report_status = 'pending'
      and billing_report_group_id = '';
    
    select count(*)
    from token_usage_logs
    where billing_report_status = 'pending'
      and length(billing_report_group_id) = 0;

    结果:

    最小复现表:
    after_insert_eq      100
    after_insert_len     100
    after_mutation_eq    100
    after_mutation_len   100
    diff_eq_minus_len    0
    diff_len_minus_eq    0
    
    更接近线上表、带多组二级索引和更多行的用例:
    many_initial_eq          1372
    many_initial_len         1372
    many_after_eq            1224
    many_after_len           1224
    many_diff_eq_minus_len   0
    many_diff_len_minus_eq   0
    

    EXPLAIN 确认 billing_report_group_id = '' 走了 idx_usage_report_group_pending 索引路径;length(billing_report_group_id)=0 走的是不同路径。两条路径的结果一致,未复现 issue 中的 count 不一致问题。

    结论:在最新 3.0-dev@18addf5d4 上,使用等价 schema/index 和本地构造数据暂未复现该问题

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

kind/bugseverity/s-1

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions