Enable Seqscan Off For Plan Debugging
Set enable_seqscan = off so the planner prefers index paths, compare the plans, then RESET; it is session-level debugging, not a production setting.
Code
-- 小表或低选择性时计划器可能选顺序扫描,让"索引到底有没有被用上"
-- 难以观察。SET enable_seqscan = off 把顺序扫描的代价抬高,强制计划器
-- 改走索引路径,用来对比验证计划差异;它只是会话级调试手段,不是生产开关
SET enable_seqscan = off;
EXPLAIN (COSTS OFF)
SELECT *
FROM users
WHERE email = '[email protected]';
-- 恢复默认,只影响当前会话
RESET enable_seqscan;
-- 生产排查用 EXPLAIN ANALYZE:真实执行并给出实际行数与耗时
EXPLAIN ANALYZE
SELECT id
FROM users
WHERE email = '[email protected]';What the code does
Several statements share this file — SELECT / EXPLAIN, with users among the objects involved.
What the statements actually assemble: a WHERE clause narrowing which rows one statement affects.
Using it
Run the statement against the users table.
Key statements
EXPLAIN (COSTS OFF)
SELECT *
FROM users
WHERE email = '[email protected]';
EXPLAIN ANALYZE
SELECT id
FROM users
WHERE email = '[email protected]';
SET enable_seqscan = off;
Interactive Tools for This Topic
PostgreSQL EXPLAIN AnalyzerAnalyze PostgreSQL EXPLAIN ANALYZE text or FORMAT JSON plans locally to flag Seq Scans, slow nodes, row misestimates, disk sorts, and index hints.SQL DDL to TypeScript ConverterConvert SQL CREATE TABLE DDL statements into strict TypeScript interfaces with nullable, array, enum, JSONB, and Date or ISO string column mappings.Multi-Mode Escape & Unescape CodecEscape and unescape strings safely across JSON, JavaScript, SQL single-quoted literals, POSIX shell single quotes, and RFC 4180 CSV cells in your browser.DOM Event Propagation VisualizerVisualize DOM event capturing, targeting, and bubbling phases in real time. Toggle stopPropagation and stopImmediatePropagation to see how events traverse the tree.
FAQ
- What relations does the SQL touch?
- `users` — referenced directly, no join needed.
Report a problem with this exampleOpens a pre-filled issue on GitHub.