惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

N
News | PayPal Newsroom
P
Proofpoint News Feed
Cyberwarzone
Cyberwarzone
C
Cisco Blogs
SecWiki News
SecWiki News
Know Your Adversary
Know Your Adversary
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Vercel News
Vercel News
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
罗磊的独立博客
NISL@THU
NISL@THU
WordPress大学
WordPress大学
Hacker News - Newest:
Hacker News - Newest: "LLM"
T
Threat Research - Cisco Blogs
AI
AI
Simon Willison's Weblog
Simon Willison's Weblog
Security Archives - TechRepublic
Security Archives - TechRepublic
有赞技术团队
有赞技术团队
L
LINUX DO - 热门话题
Hacker News: Ask HN
Hacker News: Ask HN
V
V2EX
G
GRAHAM CLULEY
TaoSecurity Blog
TaoSecurity Blog
Hugging Face - Blog
Hugging Face - Blog
D
Darknet – Hacking Tools, Hacker News & Cyber Security
F
Fortinet All Blogs
博客园 - 叶小钗
博客园 - 三生石上(FineUI控件)
云风的 BLOG
云风的 BLOG
Recorded Future
Recorded Future
Latest news
Latest news
The Hacker News
The Hacker News
aimingoo的专栏
aimingoo的专栏
T
Troy Hunt's Blog
S
Schneier on Security
I
Intezer
Google DeepMind News
Google DeepMind News
A
Arctic Wolf
Apple Machine Learning Research
Apple Machine Learning Research
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
T
Threatpost
爱范儿
爱范儿
The Register - Security
The Register - Security
S
SegmentFault 最新的问题
Blog — PlanetScale
Blog — PlanetScale
博客园 - 聂微东
宝玉的分享
宝玉的分享
Recent Commits to openclaw:main
Recent Commits to openclaw:main
美团技术团队
B
Blog RSS Feed

DEV Community

Authentication Security Deep Dive: From Brute Force to Salted Hashing (With Java Examples) Why AI Systems Don’t Fail — They Drift Spilling beans for how i learn for exam😁"Reinforcement Learning Cheat Sheet" I Replaced Chrome with Safari for AI Browser Automation. Here's What Broke (and What Finally Worked) How Python Borrows Other People's Work The $40 Architecture: Processing 1 Billion API Requests with 99.99% Uptime Vibe Coding: A Workflow Guide (From Zero to SaaS) Most webhook security guides protect the wrong side. The scary part is delivery. Headless CMS for TanStack Start: Build a Blog with Cosmic EU Age Verification App "Hacked in 2 Minutes" — What Actually Happened Comfy Cloud’s delete function does not actually remove files Running AI Models on GPU Cloud Servers: A Beginner Guide Event-driven media intelligence with AWS Step Functions and Bedrock I scored 500 AI prompts across 8 quality dimensions — here's what broke How to Call Google Gemini API from Next.js (Free Tier, No Backend Needed) The Portal Protocol: Reclaiming Human Connection in the Age of AI How to Fix Your Team's Scattered Knowledge Problem With a Self-Hosted Forum Intro to tc Cloud Functors: A Graph-First Mental Model for the Modern Cloud Designing Multi-Tenant Backends With Both Ownership and Team Access I Built a Neumorphic CSS Library with 77+ Components — Here's What I Learned PostgreSQL Performance Optimization: Why Connection Pooling Is Critical at Scale Cómo construí un SaaS multi-rubro para gestionar expensas en Argentina con FastAPI + Vue 3 🚀 I Built an Ethical Hacking Scanner Tool – Open Source Project I Replaced /usage and /context in Claude Code With a Single Statusline A Pythonic Way to Handle Emails (IMAP/SMTP) with Auto-Discovery and AI-Ready Design I Collected 8.9 Million Polymarket Price Points — Here's What I Found About How Markets Really Move EcoTrack AI — Carbon Footprint Tracker & Dashboard Everyone's Using AI. No One Agrees How. 5 self-hosted ebook managers worth trying in 2026 Building Your First AI Agent with LangChain: From Chatbot to Autonomous Assistant Common SOC 2 Failures (Real World) Stop Vibe-Checking Your AI App: A Practical Guide to Evals How to Use SonarQube and SonarScanner Locally to Level Up Your Code Quality Your Next To-Do App Is Dead — I Replaced Mine with an OpenClaw AI Sign a Nostr event in 60 lines of Python using coincurve — no nostr-sdk, no nbxplorer, no rust toolchain ITGC Audit Explained Like You’re in Big 4 Patch Tuesday abril 2026: Microsoft parcha 163 vulnerabilidades y un zero-day en SharePoint Stop scraping everything: a better way to track competitor price changes Listing on MCPize + the Official MCP Registry while routing payments OUTSIDE the marketplace — how I kept 100% of my x402 revenue Building an AI-Powered Risk Intelligence System Using Serverless Architecture Why We Ripped Function Overloading Out of Our AI Toolchain Testing AI-Generated Code: How to Actually Know If It Works SaaS Churn Is Killing Your Business. Here Is What to Do About It (Without a Support Team) The Speed of AI Is No Longer Linear - And Self-Improving Models Are Why How to Implement RBAC for MCP Tools: A Practical Guide for Engineering Teams From Standard Quote to Persuasive Proposal: AI Automation for Arborists I built a CLI that scaffolds complete multi-tenant SaaS apps Axios CVE-2025–62718: The Silent SSRF Bug That Could Be Hiding in Your Node.js App Right Now The dashboard that ended our friendship Data Pipelines Explained Simply (and How to Build Them with Python) The Hidden Cost of AI Systems Nobody Talks About. undefined vs undeclared, and how typeof behaves Switching from file-based jobs to NATS/Kafka in Rust without changing code io_uring Adventures: Rust Servers That Love Syscalls Why Agentic AI is Killing the Traditional Database The POUR principles of web accessibility for developers and designers Quantum Neural Network 3D — A Deep Dive into Interactive WebGL Visualization How To Install Caveman In Codex On macOS And Windows Automation Pipeline Reliability: Why Your Workflow Breaks When Nobody Is Watching I Built an 'Open World' AI Coding Agent — It Works From ANY Folder From Freelancing to Product: A Tech Service Company's SaaS Transformation China's AI Giants: Adding Tencent Hunyuan & ByteDance Doubao to AI University (74 Providers) On the Vibe Coders and Their Lies clerk: Auto-Summarize Your Claude Code Sessions AI Weekly — 2026/04/10–04/17 | The Model Lockdown Is Here, but the Toolchain Is the Real Battleground AI 週報 — 2026/04/10–2026/04/17 模型封鎖潮來了,但工具鏈才是真戰場 Maybe this is how Open-Source apps are born... 🚀 Fine-Tune LLMs with LoRA and QLoRA: 2026 Guide tRPC v11 + Next.js App Router: End-to-End Type Safety Without the Boilerplate ShadCN UI in 2026: Why I Stopped Installing Component Libraries and Started Owning My Components SaaS Billing in React Server Components: Stripe + Supabase Without a Single `useEffect` Join our DEV Weekend Challenge — $1,000 in Prizes Across TEN winners! Submissions Due April 20 at 6:59 AM UTC. Implementing FSRS Spaced Repetition in Flutter + Supabase — Adding Memory Science to an AI Learning App "I Texted My Localhost From the Train — Claude Code Fixed the Bug Before I Got Home" I Built a Sales Prep AI and It Went Deeper Than Expected Design to Code #2: One JSON, Eleven Outputs Solving the 100M-Row Problem: A Summary Table Pattern for High-Volume Push Notification Logs Flutter Web With Wasm: What Actually Changes For Developers I Built 50 Royalty-Free Soundtracks for My Side Project in a Weekend Using AI Music Generation The Vibe Coding Security Checklist: 7 Things to Check Before You Ship Stop Letting Googlebot Guess Fix Your React App's SEO Right Desconstruindo o Streaming do LinkedIn: Como Criar um Engine de Extração de Vídeo de Alta Performance com HLS e FFmpeg (EDA Part-1) EDA (Exploratory Data Analysis) Explained With Real Life — Why Looking at Your Data Is the Most Important Step in Machine Learning Brand Relationship Management at Scale: Our 4-Touch Outreach System for 200+ Brands Why String.fromEnvironment() Might Return an Empty String in Dart JGuardrails 1.0.0 — Hardening Java LLM Apps Against Jailbreaks, Toxicity, and Prompt Injection Plan and Schedule a Full Week of Threads Content From One Claude Conversation Coding Cat Oran Ep3, Five Tables Changed Everything Updated: BFF Pattern I'm done watching freelancers get buried by 200 proposals. So I'm building the alternative. This is my first post BFS Algorithm in Java Step by Step Tutorial with Examples Tracking LLM Pricing Monthly: An Open Dataset for 22 AI Models How We Measure Content ROI on a Comparison Site: Revenue Attribution Without Perfect Data Introducing Nova AI Ops: The AI-Native Operating System for SRE Teams I built a free desktop video downloader for Windows — Grabbit How Talkie OCR Helps Vision-Impaired & Dyslexic Users Read the World Around Them VRCFaceTracking安装和iPhone面捕配置教程,有bug Even CrowdStrike Can't See Your Agents The Automation Gold Rush: What n8n Workflows and Claude Are Opening Up for Developers Right Now
MySQL 的 MVCC:看不见的“时间机器”怎么帮你躲过脏读和幻读?
port smith · 2026-04-27 · via DEV Community

port smith

MySQL 的 MVCC:那台你从没启动过,却一直在为你倒带的“时间机器”

你有没有在图书馆借过一本《数据库原理》?翻开时目录还是旧版,而管理员其实在你借走后悄悄上架了修订版——但你完全不受影响。又或者,多人协同编辑一份文档,每人打开时看到的都是自己那一刻的快照,互不干扰。这并非魔法,而是 MySQL InnoDB 引擎中 MVCC(多版本并发控制)在 quietly work:一台无需你点开、不用你设置、却始终为你精准回溯数据状态的“隐形时间机器”。

一、快照、版本链与 ReadView:MVCC 的三驾马车

InnoDB 从不只存一行数据的一个版本。每行记录自带两个隐藏字段:trx_id(最后修改该行的事务 ID)和 roll_ptr(指向 undo 日志中前一版本的指针),由此串成一条“版本链”。当事务开启,InnoDB 并不复制整库,而是生成一个轻量级的 ReadView——相当于为这次查询配了一副“专属眼镜”。它记录三项关键信息:当前活跃事务 ID 列表、最小未提交事务 ID(min_trx_id)、最大已分配事务 ID(max_trx_id)。后续所有普通 SELECT 都依据这套规则判断:“这个版本,我能不能看见?”

判定逻辑极简:仅当某行版本的 trx_id 小于 min_trx_id,或等于当前事务 ID,或属于已提交且不在活跃列表中的事务,才对本次查询可见。

二、RR 与 RC:同一句 SQL,为何结果“变脸”?

核心差异,就在 ReadView 的生成时机。

✅ RC(读已提交):每次 SELECT 都新建 ReadView;

✅ RR(可重复读):仅事务首次 SELECT 时创建 ReadView,后续复用。

来个小实验(建议在空库执行):

CREATE TABLE t (id INT PRIMARY KEY, v INT);
INSERT INTO t VALUES (1, 10);
-- 事务 A(RR):START TRANSACTION; SELECT * FROM t; → 返回 (1,10)
-- 事务 B(RC):START TRANSACTION; UPDATE t SET v=20 WHERE id=1; COMMIT;
-- 事务 A 再次 SELECT * FROM t; → 仍返回 (1,10)(RR 复用快照)
-- 事务 C(RC)此时 SELECT → 立刻看到 (1,20)(新建 ReadView)

Enter fullscreen mode Exit fullscreen mode

RR 的“可重复读”,不是数据没变,而是你始终透过同一副眼镜看世界——时间被定格在第一次查询那一刻。

三、幻读:MVCC 解决了吗?答案是——一半。

普通 SELECT(即快照读)在 RR 级别下确实规避了幻读:新插入的行若 trx_id 大于 ReadView 的 max_trx_id,或属于未提交事务,一律不可见。但一旦触发 当前读(如 SELECT ... FOR UPDATEUPDATEINSERT ... SELECT),InnoDB 会绕过 MVCC,直接加锁并访问最新数据——此时若另一事务插入了满足条件的新行,幻读便真实发生。因此,RR 的“防幻读”本质是 MVCC + 间隙锁(Gap Lock)双保险:MVCC 负责让你“看不见”,间隙锁负责让别人“插不进”。

四、生产陷阱:Undo 日志爆满、长事务拖垮性能

MVCC 依赖 undo 日志保存历史版本。若长期不清理,undo 表空间持续膨胀,甚至触发 innodb_undo_log_truncated 告警。更危险的是长事务:它会让 ReadView 持有极老的 min_trx_id,导致大量旧版本无法回收,undo 日志堆积如山,最终拖慢整个实例。

三个立刻见效的优化动作:

  1. 定期查看 SHOW ENGINE INNODB STATUS,重点关注 History list length(历史链长度),超 10 万需警惕;
  2. 设置 max_execution_time = 30000(单位毫秒),自动中断超时查询;
  3. 执行 SELECT * FROM information_schema.INNODB_TRX WHERE TIME_TO_SEC(TIMEDIFF(NOW(), TRX_STARTED)) > 300,及时 KILL 运行超 5 分钟的事务。

五、总结:MVCC 不是银弹,而是一场精密协奏

它不解决写-写冲突(仍需行锁),不替代应用层的数据校验,更不是 MyISAM 那种“无事务”的代名词。它的强大,源于与 InnoDB 存储引擎、隔离级别策略、锁机制的深度咬合。理解 MVCC,不只是记住 trx_id 规则,更是看清:每一次“看似无锁”的读,背后都有时间戳的精准裁决;每一次“理所当然”的一致性,都依赖 undo 日志的沉默承重。

真正的数据库高手,从不迷信机制——而是在读懂设计边界之后,让工具,真正为人所用。