Repository navigation
Description
Is there an existing issue for performance?
- I have checked the existing issues.
Environment
- Official v4.2.5 release
1a44f3db3056e32975fde86ca0ab69a094996a3f; official package SHA256a0e0aad81a914d6cd7081f68bbfb7695cba20544ca4c8f37358f4005067b8da1. - Actual newly built main
bdbbd177921067ab1b0956ea45f378c41ba5e2b8, confirmed throughSELECT VERSION(), git_version()in each server. Production source unchanged; standardmake buildexited 0. - Linux x86_64, kernel 6.18.44, 9 visible CPUs, approximately 9.7 GiB RAM; service
GOMAXPROCS=4,GOMEMLIMIT=6GiB; single-machine CN/TN/logservice deployment. - Two independent fresh fixtures per version, sequential tests, separate storage; main never opened release storage. No SQL retries, no fault injection, no concurrent build.
Details of Performance
A queue application selects and locks only the first eligible item, but this prevents another transaction from updating an unreturned item at the end of the queue. This remains true with a (status,id) secondary index that is actually selected by the optimizer.
Minimal two-session reproduction:
CREATE DATABASE queue_lock_perf;
USE queue_lock_perf;
CREATE TABLE app_lock_queue(id INT PRIMARY KEY, status VARCHAR(16), c INT);
-- Insert ids 1..256, all status='active', c=0.
-- One SQL setup without external data:
INSERT INTO app_lock_queue
SELECT 1+a.n+4*b.n+16*c.n+64*d.n,'active',0
FROM (SELECT 0 n UNION ALL SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3) a
CROSS JOIN (SELECT 0 n UNION ALL SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3) b
CROSS JOIN (SELECT 0 n UNION ALL SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3) c
CROSS JOIN (SELECT 0 n UNION ALL SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3) d;
CREATE INDEX queue_status ON app_lock_queue(status,id);Session A:
START TRANSACTION;
SELECT id FROM app_lock_queue
WHERE status='active' ORDER BY id LIMIT 1 FOR UPDATE;
-- Returns only id=1. Leave this transaction open.Session B, after A has fetched its result:
START TRANSACTION;
UPDATE app_lock_queue SET c=c+1 WHERE id=256;
COMMIT;
-- Waits for A even though id=256 was not returned by A.Commit A and B completes. Repeat without FOR UPDATE in A as a paired control; B completes before A commits. Also repeat before creating the secondary index. Neither removing the index nor ensuring the ordinary secondary index is actually used eliminates the blocking in the locking case.
Actual real SQL results, 3 repetitions per cell in each of two independent fixtures per version:
| Version | A query | Index | B completes before A release | B UPDATE + COMMIT |
|---|---|---|---|---|
| 4.2.5 | non-locking | absent/present | 12/12 | 1.921–4.603 ms |
| 4.2.5 | FOR UPDATE | absent/present | 0/12 | 252.341–253.834 ms |
| main bdbbd17 | non-locking | absent/present | 12/12 | 2.706–3.524 ms |
| main bdbbd17 | FOR UPDATE | absent/present | 0/12 | 252.123–255.687 ms |
The controller deliberately releases A after a 250 ms observation window. The ~253 ms values therefore measure application-induced blocking through A's hold interval, not an intrinsic fixed database delay. Every B update affected one row and committed after release; there were no SQL errors/retries.
Actual indexed locking plan on main:
Project
-> Sort
Sort Key: __mo_index_pri_col INTERNAL
Limit: 1
-> Lock
-> Index Table Scan on app_lock_queue.queue_status
Filter Cond: prefix_eq(__mo_index_idx_col),
serial_extract(__mo_index_idx_col, 0, VARCHAR) = 'active'
Block Filter Cond: prefix_eq(__mo_index_idx_col)
Without the index, the corresponding plan is Sort(Limit:1) -> Lock -> Table Scan. The top-N decision occurs above Lock, consistent with locking candidate rows before reducing to one returned queue item. We directly measured blocking for unreturned id=256; we did not enumerate every acquired lock or assert all 256 rows were locked. This is a performance/concurrency report, not a claim that SQL forbids locking rows visited by an access path.
Desired behavior: a bounded ordered queue claim should avoid serializing updates to unrelated, unreturned queue items where a safe implementation can do so. Please investigate reducing lock scope around top-N/index access, including concurrent qualification changes and transaction semantics. A semantic rewrite must not silently weaken locking correctness.
Additional information
- This is distinct from [Bug]: SELECT ... LIMIT 1 FOR UPDATE 等锁结束后读到持锁事务提交前的旧值 #30245 (a locking SELECT silently returns an old value after lock wait) and [Bug]: Full TPCC suite accumulates long-lived row locks and cascades into lock wait timeouts #29429 (long-lived locks/transaction completion stall in the full TPCC suite). Here the holder is deliberately alive, release works promptly, and the measurable concern is unnecessary contention induced by the plan for a one-item queue claim.
- The 256-row fixture is below main's default
max-row-lock-count=20000; this is not inferred from crossing that threshold. - Full scripts, bindings, transaction/commit timestamps and plans are retained in the stability campaign checkpoint,
episode1255/locking_read_probe.pyandindex-ddl-{release,main}-{0,1}/locking-read-summary.json. The SQL above is self-contained after inserting ids 1..256; no private business dataset is required. - No direct MySQL comparison was executed, and no large-scale throughput improvement is claimed. The demonstrated outcome is unrelated-row blocking on both actual versions with and without the actual index path.
Activity