Skip to content

[Bug]: vector index didn't take effect #23158

Closed as not planned
Closed as not planned

Description

Is there an existing issue for the same bug?

  • I have checked the existing issues.

Branch Name

v3.0.4-hotfix

Commit ID

6b326bf

Other Environment Information

- Hardware parameters:
- OS type:
- Others:

Actual Behavior

以下向量查询的 sql 执行计划索引没有生效

sql1:
mysql> EXPLAIN SELECT md5_id FROM ca_comprehensive_dataset ORDER BY l2_distance(question_vector, (SELECT question_vector FROM ca_comprehensive_dataset AS ref WHERE md5_id='0000392a0ed2cf8989c4db9177532db7')) LIMIT 10;
+-------------------------------------------------------------------------------------------------------+
| AP QUERY PLAN ON ONE CN(64 core)                                                                      |
+-------------------------------------------------------------------------------------------------------+
| Project                                                                                               |
|   ->  Sort                                                                                            |
|         Sort Key: l2_distance(ca_comprehensive_dataset.question_vector, ref.question_vector) INTERNAL |
|         Limit: 10                                                                                     |
|         ->  Join                                                                                      |
|               Join Type: SINGLE                                                                       |
|               ->  Table Scan on anli_test.ca_comprehensive_dataset                                    |
|               ->  Table Scan on anli_test.ca_comprehensive_dataset                                    |
|                     Filter Cond: (ref.md5_id = '0000392a0ed2cf8989c4db9177532db7')                    |
|                     Block Filter Cond: (ref.md5_id = '0000392a0ed2cf8989c4db9177532db7')              |
+-------------------------------------------------------------------------------------------------------+

sql2:
SQL:
SELECT a.md5_id,
                           LEFT(a.question, 80) AS question_preview,
                           l2_distance(a.question_vector, ref_vec.question_vector) AS distance
                    FROM ca_comprehensive_dataset AS a
                    JOIN (
                        SELECT question_vector
                        FROM ca_comprehensive_dataset
                        WHERE md5_id = '70eaf7d252f5d4436ade9633340fbb2d'
                    ) AS ref_vec ON 1=1
                    WHERE a.question_vector IS NOT NULL
                    ORDER BY distance ASC
                    LIMIT 10
执行计划:
Project
  ->  Sort
        Sort Key: l2_distance(a.question_vector, ref_vec.question_vector) ASC
        Limit: 10
        ->  Project
              ->  Join
                    Join Type: INNER
                    ->  Table Scan on anli_test.ca_comprehensive_dataset
                          Filter Cond: true, (a.question_vector IS NOT NULL)
                    ->  Table Scan on anli_test.ca_comprehensive_dataset
                          Filter Cond: true, (ca_comprehensive_dataset.md5_id = '70eaf7d252f5d4436ade9633340fbb2d')
                          Block Filter Cond: (ca_comprehensive_dataset.md5_id = '70eaf7d252f5d4436ade9633340fbb2d')
sql3:
WITH sampled AS (
                        SELECT md5_id, question, question_vector
                        FROM ca_comprehensive_dataset
                        WHERE content_type = '品牌实力'
                        ORDER BY created_at DESC
                        LIMIT 200
                    )
                    SELECT a.md5_id AS md5_a,
                           b.md5_id AS md5_b,
                           l2_distance(a.question_vector, b.question_vector) AS distance
                    FROM sampled a
                    JOIN sampled b ON a.md5_id < b.md5_id
                    ORDER BY distance ASC
                    LIMIT 10
执行计划:
Project
  ->  Sort
        Sort Key: l2_distance(a.question_vector, b.question_vector) ASC
        Limit: 10
        ->  Project
              ->  Join
                    Join Type: INNER
                    Join Cond: (ca_comprehensive_dataset.md5_id < ca_comprehensive_dataset.md5_id)
                    ->  Sort
                          Sort Key: ca_comprehensive_dataset.created_at DESC
                          Limit: 200
                          ->  Join
                                Join Type: INDEX
                                Join Cond: (ca_comprehensive_dataset.md5_id = #[1,0])
                                Runtime Filter Build: #[-1,0]
                                ->  Table Scan on anli_test.ca_comprehensive_dataset [ForceOneCN]
                                      Filter Cond: (ca_comprehensive_dataset.content_type = '品牌实力')
                                      Runtime Filter Probe: ca_comprehensive_dataset.md5_id
                                ->  Index Table Scan on ca_comprehensive_dataset.idx_comprehensive_content_type [ForceOneCN]
                                      Filter Cond: prefix_eq(#[0,0])
                                      Block Filter Cond: prefix_eq(#[0,0])
                    ->  Sort
                          Sort Key: ca_comprehensive_dataset.created_at DESC
                          Limit: 200
                          ->  Join
                                Join Type: INDEX
                                Join Cond: (ca_comprehensive_dataset.md5_id = #[1,0])
                                Runtime Filter Build: #[-1,0]
                                ->  Table Scan on anli_test.ca_comprehensive_dataset [ForceOneCN]
                                      Filter Cond: (ca_comprehensive_dataset.content_type = '品牌实力')
                                      Runtime Filter Probe: ca_comprehensive_dataset.md5_id
                                ->  Index Table Scan on ca_comprehensive_dataset.idx_comprehensive_content_type [ForceOneCN]
                                      Filter Cond: prefix_eq(#[0,0])
                                      Block Filter Cond: prefix_eq(#[0,0])

Expected Behavior

No response

Steps to Reproduce

ddl:
SET foreign_key_checks = 0;set experimental_fulltext_index=1;set experimental_ivf_index=1;drop database if exists anli_test;create database anli_test;use anli_test;CREATE TABLE ca_comprehensive_dataset (md5_id varchar(255) NOT NULL,question text DEFAULT NULL,answer json DEFAULT NULL,source_type varchar(255) DEFAULT NULL,content_type varchar(255) DEFAULT NULL,keyword varchar(255) DEFAULT NULL,question_vector vecf64(1024) DEFAULT NULL COMMENT '摘要的向量集',allow_access varchar(511) DEFAULT NULL,allow_identities varchar(512) DEFAULT NULL,delete_flag int DEFAULT NULL,created_at timestamp DEFAULT CURRENT_TIMESTAMP(),updated_at timestamp DEFAULT CURRENT_TIMESTAMP() ON UPDATE CURRENT_TIMESTAMP(),PRIMARY KEY (md5_id),KEY idx_comprehensive_allow_access (allow_access),KEY idx_comprehensive_allow_identities (allow_identities),KEY idx_comprehensive_content_type (content_type));

load data url s3option {'endpoint'='http://cos.ap-guangzhou.myqcloud.com','access_key_id'='xx','secret_access_key'='xx','bucket'='mo-load-guangzhou-1308875761', 'filepath'='mo-big-data/ca_ai_ca_comprehensive_dataset.csv'} into table ca_comprehensive_dataset FIELDS TERMINATED BY ',' ENCLOSED BY '\"' LINES TERMINATED BY '\n' PARALLEL 'TRUE';

Additional information

No response

Activity

  1. ghost added this to the MO-202511 milestone on Nov 26, 2025
  2. ghost assigned on Nov 26, 2025
  3. ghost added
    kind/bug
    on Nov 26, 2025
  4. ghost added
    severity/s0
    on Nov 26, 2025
  5. ghost added theissue type on Nov 26, 2025
  6. aunjgr commented on Dec 15, 2025

    @aunjgr
    Contributor

    向量参数通过子查询传入的查询,难以用到目前方式实现的向量索引。

    已经改成两个查询来达到需求。

  7. modified the milestones: MO-202511, MO-202602 on Feb 3, 2026
  8. assigned and unassigned on May 11, 2026
  9. ck89119 commented on Jul 1, 2026

    @ck89119
    Contributor

    不好处理,需要拆成两个查询来做,主流向量数据库除了pg,都不支持单条sql

    Image
  10. assigned and unassigned on Jul 1, 2026
  11. 4 remaining items

  12. XuPeng-SH commented on Jul 5, 2026

    @XuPeng-SH
    Contributor

    向量是比较新的负载,pg vector行为是下限!

  13. added and removed
    severity/s0
    on Jul 5, 2026
  14. added
    severity/s0
    and removed on Sep 29, 2026
  15. ck89119 commented on Sep 29, 2026

    @ck89119
    Contributor

    经 PostgreSQL 18.3 + pgvector 0.8.2 实测,SQL2 和 SQL3 保持原查询结构时,PG 同样不支持利用向量索引加速,HNSW 和 IVF-FLAT 均未命中。这里指向量索引不生效,SQL 本身可以正常执行。

  16. Ariznawlll commented on Oct 2, 2026

    @Ariznawlll
    Collaborator

    测试版本

    • MatrixOne 审核基线:main@d50a2d475a406829f67127fef7b2ad4abc1c1d3d
    • 原问题记录版本:6b326bfc7579da1019291cdd9ffc5d771a4806e8
    • 外部对照:PostgreSQL 18.3 + pgvector 0.8.2
    • 文档合入:Ariznawlll/mo-test-reports@733aa7cdbec7c0043236d9d230933503efbb4a96
    • 测试日期:2026-10-02

    原问题验证

    原 SQL 的查询向量来自标量子查询、派生表或同表自连接,需要先完成关系运算才能得到 reference vector。当前规划器不会把这种运行期关系结果改写为单查询向量 Top-K probe,允许执行为 Table Scan / Join / Sort;SQL 本身可执行,限制是 HNSW/IVFFLAT 没有生效。研发明确给出的当前使用方式是拆成两条查询。

    Happy Path 覆盖

    • 第一条查询读取 reference vector;
    • 第二条查询将已取得的 vector 作为 literal 或 prepared parameter,执行 ORDER BY l2_distance(indexed_column, ?) LIMIT K;
    • 该拆分形态属于当前向量索引可识别的查询面。

    Unhappy Path 覆盖

    • 标量子查询、派生表、CTE 或 Join 另一侧产生查询向量时,不保证命中 HNSW/IVFFLAT;
    • 同一采样集合的 pairwise vector self-join 同样不属于单查询 Top-K 索引支持面;
    • 本限制不豁免扫描结果错误、距离/排序错误、panic、资源泄漏,或拆分成支持形态后仍无法使用已就绪索引等独立问题。

    白盒验证

    该 issue 没有需要验收的 MatrixOne 修复 PR。研发确认当前能力边界,并补充 PostgreSQL 18.3 + pgvector 0.8.2 对原 SQL2/SQL3 的 HNSW/IVFFLAT 计划对照同样未命中。对应限制已写入兼容性文档 VECTOR-002。

    回归测试

    未把“当前不使用索引”写成 MOTR 的正确期望:MOTR 固化该计划会阻止未来能力增强,而且 SQL 结果正确性与是否选择向量索引是两个不同契约。当前通过兼容性文档记录支持范围和两阶段改写方式。

    测试证据

    测试结论

    测试通过。

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

Metadata

Metadata

Assignees

Labels

kind/bugseverity/s0

Type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions