Skip to content

[Performance]: Batched OR on one composite secondary index scans completed queue history #32370

Open

Description

@XuPeng-SH

Summary

A batched pending-work lookup over a single composite secondary index stops using that index when the query ORs equality pairs. In a realistic queue with 1,000 users and 100 historical rows per user and no pending work, an idle query for 128 users reads 12,800 historical base rows to return zero results. The one-user form uses the secondary index and reads no rows. This is a performance finding, not an observed wrong-result bug.

Environment

  • Disposable matrixorigin/matrixone:4.2.8 container, isolated synthetic data.
  • VERSION(): 8.0.30-MatrixOne-v4.2.8; GIT_VERSION(): 10d204b.
  • SQL PREPARE / EXECUTE as in the application; no production data.

Reproduction

Install mysql-connector-python, start a fresh MatrixOne instance, set MO_PASSWORD, and run the following script. It creates an isolated database, 100,000 rows (1,000 users, 100 rows per user), and prints only scan nodes and row counts. Optional MO_HOST, MO_PORT, and MO_USER environment variables select the instance. Use a fresh instance or change the test database name before rerunning.

import os
import re
import mysql.connector

connection = mysql.connector.connect(
    host=os.getenv('MO_HOST', '127.0.0.1'), port=int(os.getenv('MO_PORT', '6001')),
    user=os.getenv('MO_USER', 'root'), password=os.environ['MO_PASSWORD'], use_pure=True,
)
cursor = connection.cursor()
cursor.execute('CREATE DATABASE review_mo_issue1240a')
cursor.execute('USE review_mo_issue1240a')
cursor.execute('CREATE TABLE digits (n INT)')
cursor.execute('INSERT INTO digits VALUES (0),(1),(2),(3),(4),(5),(6),(7),(8),(9)')
cursor.execute('''CREATE TABLE edge_pending_dispatch (
 user_id VARCHAR(128) NOT NULL, session_id VARCHAR(128) NOT NULL,
 run_id VARCHAR(128) NOT NULL, turn_chain_id VARCHAR(128) NOT NULL,
 edge_agent_id VARCHAR(255) NOT NULL, request_id VARCHAR(128) NOT NULL,
 payload_json JSON NOT NULL, result_json LONGTEXT NULL,
 status VARCHAR(16) NOT NULL DEFAULT 'pending', pod_id VARCHAR(128) NULL,
 dispatched_at DATETIME(6) NULL, completed_at DATETIME(6) NULL,
 created_at DATETIME(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6),
 PRIMARY KEY (user_id,session_id,run_id,turn_chain_id,request_id),
 INDEX idx_edge_dispatch_user_status
   (user_id,edge_agent_id,status,created_at,session_id,run_id,turn_chain_id,request_id))''')
cursor.execute('''INSERT INTO edge_pending_dispatch
 (user_id,session_id,run_id,turn_chain_id,edge_agent_id,request_id,payload_json,status)
 SELECT CONCAT('u',LPAD(FLOOR((a.n+b.n*10+c.n*100+d.n*1000+e.n*10000)/100),4,'0')),
 CONCAT('s',LPAD(FLOOR((a.n+b.n*10+c.n*100+d.n*1000+e.n*10000)/100),4,'0')),
 'run','chain',
 CONCAT('e',LPAD(FLOOR((a.n+b.n*10+c.n*100+d.n*1000+e.n*10000)/100),4,'0')),
 CONCAT('q',LPAD(a.n+b.n*10+c.n*100+d.n*1000+e.n*10000,6,'0')), '{}',
 IF((a.n+b.n*10+c.n*100+d.n*1000+e.n*10000)<5000 AND
    MOD((a.n+b.n*10+c.n*100+d.n*1000+e.n*10000),100)=0,'dispatched','completed')
 FROM digits a CROSS JOIN digits b CROSS JOIN digits c CROSS JOIN digits d CROSS JOIN digits e''')
connection.commit()
for n in (1,128):
    sql = ("EXPLAIN ANALYZE SELECT user_id,edge_agent_id FROM edge_pending_dispatch "
           "WHERE status='pending' AND (" + ' OR '.join(['(user_id=%s AND edge_agent_id=%s)']*n)
           + ') GROUP BY user_id,edge_agent_id')
    values = [v for i in range(n) for v in (f'u{i:04}', f'e{i:04}')]
    sql = sql.replace('%s', '?')
    cursor.execute('PREPARE review_plan FROM %s', (sql,))
    variables = [f'@p{i}' for i in range(len(values))]
    cursor.execute('SET ' + ','.join(v+'=%s' for v in variables), values)
    cursor.execute('EXECUTE review_plan USING ' + ','.join(variables))
    print(f'--- {n} users ---')
    plan = '\n'.join(str(cell) for row in cursor.fetchall() for cell in row)
    lines = plan.splitlines()
    for i, line in enumerate(lines):
        if re.search(r'(Index )?Table Scan on', line):
            print(line)
            if i+1 < len(lines): print(lines[i+1])
    cursor.execute('DEALLOCATE PREPARE review_plan')
connection.close()

Observed

Lookup Access path Base-table input rows Output rows
One (user_id, edge_agent_id) pair composite secondary-index lookup 0 0
128 (user_id, edge_agent_id) pairs base-table scan 12,800 0

The secondary index begins with (user_id, edge_agent_id, status), and each OR branch specifies all its leading equality fields (the status predicate is shared). As history accumulates, the batched lookup reads the corresponding user histories even when no row is pending. The application issues this query periodically for live Edge subscribers; repeated scans make idle load depend on accumulated history. Numbers above are EXPLAIN ANALYZE observations, not a throughput benchmark.

Expected

Use selective index access for the equality pairs, or provide an efficient batched lookup that does not read completed history. The query should continue to return the same results.

Related to #32203 (OR across different indexed columns) and #32365 (OR of points on a composite primary key); this reproduction uses multiple points on the same composite secondary index and a shared status predicate. Astra context: matrixorigin/Astra#1240.

Activity

  1. MatrixAdventurer commented on Nov 9, 2026

    @MatrixAdventurer
    Contributor

    分级建议:
    severity/s2 —— 同一复合二级索引上 OR 多值点查退化为基表全扫,空转负载随历史行累积增长,纯性能退化、结果正确,
    模块:
    pkg/sql/plan(多值等值范围构建/索引选择)。
    疑似重复:未发现同因;#32203、#32365 为相邻不同形态(正文已区分)。
    建议负责人:matrix-meow(现 assignee);目录 CODEOWNERS 为 XuPeng-SH/aunjgr/aptend。
    本操作由LW授权的MOI智能体提交

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

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions