feat(docs): enhance interactive demos and improve documentation

- Add new interactive components for frontend routing, browser rendering pipeline, and database transactions
- Improve existing demos with better visuals, explanations, and examples
- Update documentation structure and content for better clarity
- Add new utility scripts and update package.json with new commands
- Fix formatting and alignment in documentation tables
This commit is contained in:
sanbuphy
2026-02-13 22:10:03 +08:00
parent 599052b2e0
commit d174ceea32
88 changed files with 26273 additions and 15539 deletions
@@ -1,13 +1,96 @@
<template>
<div class="demo-container">
<div class="btree-demo">
<div class="demo-header">
<h4>{{ title }}</h4>
<p class="hint">{{ description }}</p>
<span class="icon">🌳</span>
<span class="title">B+ 树索引演示</span>
<span class="subtitle">理解数据库如何快速查找数据</span>
</div>
<div class="demo-content">
<el-alert type="info" :closable="false">
B+索引演示组件占位符 - 待实现具体交互
</el-alert>
<div class="intro-text">
想象你要在<span class="highlight">字典</span>里找一个字你会先看目录定位到首字母的区域再在这个区域里找具体页码B+ 就是这样的<span class="highlight">多层目录</span>让数据库在 10 亿条数据中 3 次就能找到目标
</div>
<div class="comparison">
<div class="compare-card scan">
<div class="card-header">
<span class="icon">🐢</span>
<span class="title">全表扫描</span>
</div>
<div class="card-content">
<div class="data-rows">
<div v-for="i in 20" :key="i" class="data-row" :class="{ found: scanMode === 'found' && i === targetId }">
<span class="row-id">{{ String(i).padStart(3, '0') }}</span>
<span class="row-name">用户{{ i }}</span>
</div>
</div>
<div class="scan-info">
<p v-if="!scanMode">👆 点击"开始查找"看全表扫描有多慢</p>
<p v-else-if="scanMode === 'scanning'">正在扫描... {{ scanCount }} </p>
<p v-else class="found"> 找到了扫描了 {{ scanCount }} 条记录耗时 {{ scanTime }}</p>
</div>
<button v-if="!scanMode" @click="startScan" class="btn">开始查找</button>
</div>
</div>
<div class="compare-card index">
<div class="card-header">
<span class="icon"></span>
<span class="title">索引查找</span>
</div>
<div class="card-content">
<div class="tree-structure">
<div class="tree-level root">
<div class="node-label">根节点</div>
<div class="node">1-100</div>
</div>
<div class="tree-level intermediate">
<div class="node-label">中间节点</div>
<div class="node">1-10</div>
</div>
<div class="tree-level leaf">
<div class="node-label">叶子节点</div>
<div
v-for="i in 10"
:key="i"
class="node leaf-node"
:class="{ found: indexMode === 'found' && i === targetId }"
>
{{ i }}
</div>
</div>
</div>
<div class="index-info">
<p v-if="!indexMode">👆 点击"开始查找"看索引有多快</p>
<p v-else-if="indexMode === 'searching'">正在搜索... {{ indexStep }} </p>
<p v-else class="found"> 找到了只用了 {{ indexSteps.length }} 耗时 {{ indexTime }}</p>
</div>
<button v-if="!indexMode" @click="startIndex" class="btn">开始查找</button>
</div>
</div>
</div>
<div class="stats-box">
<div class="stat-item">
<div class="stat-label">数据量</div>
<div class="stat-value">100 万条</div>
</div>
<div class="stat-item">
<div class="stat-label">全表扫描</div>
<div class="stat-value slow">平均 50 万次比较</div>
</div>
<div class="stat-item">
<div class="stat-label">B+ 树索引</div>
<div class="stat-value fast"> 3 次比较</div>
</div>
<div class="stat-item">
<div class="stat-label">速度提升</div>
<div class="stat-value highlight">10 万倍+</div>
</div>
</div>
<div class="info-box">
<span class="icon">💡</span>
<strong>核心原理</strong>B+ 树通过"矮胖"的设计让树的高度只有 3-4 每层可以存储成百上千个键值所以 10 亿数据也只需要 3 次磁盘 I/O这就是数据库查询飞快的秘密
</div>
</div>
</template>
@@ -15,36 +98,292 @@
<script setup>
import { ref } from 'vue'
const title = ref('B+树索引演示')
const description = ref('展示数据库中B+树索引的结构和工作原理,理解索引如何加速数据查询')
const targetId = ref(7)
const scanMode = ref(null)
const scanCount = ref(0)
const scanTime = ref(0)
const indexMode = ref(null)
const indexStep = ref(0)
const indexSteps = ref([])
const indexTime = ref(0)
const startScan = () => {
scanMode.value = 'scanning'
scanCount.value = 0
let count = 0
const interval = setInterval(() => {
count += Math.floor(Math.random() * 3) + 1
scanCount.value = count
if (count >= targetId.value) {
clearInterval(interval)
scanMode.value = 'found'
scanTime.value = (count * 0.001).toFixed(3)
}
}, 30)
}
const startIndex = () => {
indexMode.value = 'searching'
indexStep.value = 0
indexSteps.value = ['根节点', '中间节点', '叶子节点']
const steps = ['根节点 (1-100)', '中间节点 (1-10)', '叶子节点 (找到 7)']
let currentStep = 0
const interval = setInterval(() => {
currentStep++
indexStep.value = currentStep
if (currentStep >= 3) {
clearInterval(interval)
indexMode.value = 'found'
indexTime.value = (0.003).toFixed(3)
}
}, 500)
}
</script>
<style scoped>
.demo-container {
.btree-demo {
border: 1px solid var(--vp-c-divider);
border-radius: 8px;
padding: 20px;
background: var(--vp-c-bg-soft);
padding: 1rem;
margin: 1rem 0;
}
.demo-header {
margin-bottom: 20px;
display: flex;
align-items: center;
gap: 0.5rem;
margin-bottom: 0.75rem;
}
.demo-header h4 {
margin: 0 0 8px 0;
color: var(--vp-c-text-1);
.demo-header .icon { font-size: 1.25rem; }
.demo-header .title { font-weight: bold; font-size: 1rem; }
.demo-header .subtitle { color: var(--vp-c-text-2); font-size: 0.85rem; margin-left: 0.5rem; }
.intro-text {
font-size: 0.9rem;
color: var(--vp-c-text-2);
line-height: 1.6;
margin-bottom: 1rem;
padding: 0.75rem;
background: var(--vp-c-bg);
border-radius: 6px;
}
.hint {
margin: 0;
font-size: 14px;
.intro-text .highlight {
color: var(--vp-c-brand-1);
font-weight: 500;
}
.comparison {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 1rem;
margin-bottom: 1rem;
}
@media (max-width: 768px) {
.comparison {
grid-template-columns: 1fr;
}
}
.compare-card {
background: var(--vp-c-bg);
border-radius: 8px;
overflow: hidden;
border: 1px solid var(--vp-c-divider);
}
.card-header {
display: flex;
align-items: center;
gap: 0.5rem;
padding: 0.5rem 0.75rem;
background: var(--vp-c-bg-soft);
border-bottom: 1px solid var(--vp-c-divider);
}
.card-header .icon { font-size: 1rem; }
.card-header .title { font-weight: 600; font-size: 0.85rem; }
.card-content {
padding: 0.75rem;
}
.data-rows {
display: grid;
grid-template-columns: repeat(5, 1fr);
gap: 4px;
margin-bottom: 0.75rem;
}
.data-row {
display: flex;
flex-direction: column;
align-items: center;
padding: 4px;
background: var(--vp-c-bg-soft);
border-radius: 4px;
font-size: 0.65rem;
transition: background 0.2s;
}
.data-row.found {
background: rgba(34, 197, 94, 0.2);
border: 1px solid #22c55e;
}
.row-id {
font-weight: 600;
color: var(--vp-c-text-2);
}
.demo-content {
.row-name {
color: var(--vp-c-text-3);
}
.tree-structure {
display: flex;
flex-direction: column;
gap: 16px;
align-items: center;
gap: 0.5rem;
margin-bottom: 0.75rem;
}
.tree-level {
display: flex;
flex-direction: column;
align-items: center;
gap: 4px;
}
.node-label {
font-size: 0.65rem;
color: var(--vp-c-text-3);
}
.node {
padding: 6px 12px;
background: var(--vp-c-brand);
color: white;
border-radius: 6px;
font-size: 0.75rem;
font-weight: 500;
min-width: 60px;
text-align: center;
}
.leaf {
display: flex;
flex-wrap: wrap;
gap: 4px;
justify-content: center;
}
.leaf-node {
min-width: 28px;
padding: 4px 8px;
background: var(--vp-c-bg-soft);
color: var(--vp-c-text-1);
border: 1px solid var(--vp-c-divider);
}
.leaf-node.found {
background: rgba(34, 197, 94, 0.2);
border-color: #22c55e;
color: #22c55e;
}
.scan-info, .index-info {
text-align: center;
font-size: 0.75rem;
color: var(--vp-c-text-2);
margin-bottom: 0.75rem;
min-height: 40px;
display: flex;
align-items: center;
justify-content: center;
}
.scan-info .found, .index-info .found {
color: #22c55e;
font-weight: 500;
}
.btn {
width: 100%;
padding: 8px;
background: var(--vp-c-brand);
color: white;
border: none;
border-radius: 6px;
font-size: 0.85rem;
cursor: pointer;
transition: background 0.2s;
}
.btn:hover {
background: var(--vp-c-brand-1);
}
.stats-box {
display: grid;
grid-template-columns: repeat(4, 1fr);
gap: 0.5rem;
margin-bottom: 0.75rem;
}
@media (max-width: 640px) {
.stats-box {
grid-template-columns: repeat(2, 1fr);
}
}
.stat-item {
background: var(--vp-c-bg);
padding: 0.75rem;
border-radius: 6px;
text-align: center;
}
.stat-label {
font-size: 0.7rem;
color: var(--vp-c-text-3);
margin-bottom: 0.25rem;
}
.stat-value {
font-size: 0.9rem;
font-weight: 600;
color: var(--vp-c-text-1);
}
.stat-value.slow {
color: #ef4444;
}
.stat-value.fast {
color: #22c55e;
}
.stat-value.highlight {
color: var(--vp-c-brand-1);
}
.info-box {
background: var(--vp-c-bg-alt);
padding: 0.75rem;
border-radius: 6px;
font-size: 0.85rem;
color: var(--vp-c-text-2);
line-height: 1.5;
}
.info-box .icon { margin-right: 0.25rem; }
</style>
@@ -1,50 +1,365 @@
<template>
<div class="demo-container">
<div class="data-evolution-demo">
<div class="demo-header">
<h4>{{ title }}</h4>
<p class="hint">{{ description }}</p>
<span class="icon">📊</span>
<span class="title">数据存储演进</span>
<span class="subtitle">从记事本到数据库的演变</span>
</div>
<div class="demo-content">
<el-alert type="info" :closable="false">
数据库演进历程演示组件占位符 - 待实现具体交互
</el-alert>
<div class="intro-text">
想象你在经营一家<span class="highlight">书店</span>从记在小本本上到用 Excel 管理再到用专业的数据库系统每一步演变都是为了解决数据量增长带来的新问题
</div>
<div class="evolution-stages">
<div
v-for="(stage, i) in stages"
:key="stage.id"
class="stage"
:class="{ active: activeStage === stage.id }"
@click="activeStage = activeStage === stage.id ? null : stage.id"
>
<div class="stage-icon">{{ stage.icon }}</div>
<div class="stage-name">{{ stage.name }}</div>
<div class="stage-simple">{{ stage.simple }}</div>
<div class="stage-capacity">{{ stage.capacity }}</div>
<div v-if="i < stages.length - 1" class="arrow"></div>
</div>
</div>
<div v-if="!activeStage" class="hint-text">
👆 点击上方任意阶段查看详细特点
</div>
<Transition name="fade">
<div v-if="activeStage" class="stage-detail">
<div class="detail-header">
<span class="detail-icon">{{ currentStage?.icon }}</span>
<span class="detail-title">{{ currentStage?.name }}</span>
<span class="detail-capacity">{{ currentStage?.capacity }}</span>
</div>
<div class="detail-content">
<div class="pros-cons">
<div class="pros">
<div class="list-title"> 优点</div>
<ul>
<li v-for="pro in currentStage?.pros" :key="pro">{{ pro }}</li>
</ul>
</div>
<div class="cons">
<div class="list-title"> 缺点</div>
<ul>
<li v-for="con in currentStage?.cons" :key="con">{{ con }}</li>
</ul>
</div>
</div>
<div v-if="currentStage?.example" class="example-box">
<div class="example-label">🌰 举个例子</div>
<div class="example-content">{{ currentStage?.example }}</div>
</div>
</div>
</div>
</Transition>
<div class="info-box">
<span class="icon">💡</span>
<strong>核心思想</strong>数据存储方式的演进本质上是<span class="highlight">用更复杂的系统解决数据量增长带来的问题</span>"能用""好用"再到"专业"每一步都是为了提升效率保证安全支持更大的规模
</div>
</div>
</template>
<script setup>
import { ref } from 'vue'
import { ref, computed } from 'vue'
const title = ref('数据库演进历程演示')
const description = ref('展示数据库从层次数据库、网状数据库到关系数据库、NoSQL数据库的演进过程')
const activeStage = ref(null)
const stages = ref([
{
id: 1,
icon: '📒',
name: '记事本',
simple: '手工记录',
capacity: '100 条',
pros: ['零门槛,拿笔就能写', '简单直接,随时查看'],
cons: ['查询困难,需要人工计算', '容易丢失,无法备份', '无法统计和分析'],
example: '你在笔记本上记:2024-01-15,张三买了《百年孤独》,59元。想统计上个月卖了多少,得一页页翻。'
},
{
id: 2,
icon: '📊',
name: 'Excel',
simple: '电子表格',
capacity: '100 万条',
pros: ['自动求和、排序、筛选', '界面直观,容易上手', '支持简单的公式计算'],
cons: ['容量有限,数据量大就卡死', '难以协作,容易冲突', '数据冗余,重复信息多'],
example: '你用 Excel 管理订单,张三买了 100 本书,他的地址和电话重复写了 100 次。如果他换电话,得修改 100 行。'
},
{
id: 3,
icon: '🗄️',
name: '数据库',
simple: '专业系统',
capacity: '亿级+',
pros: ['海量数据,毫秒查询', '多人协作,不会冲突', '数据安全,自动备份', '消除冗余,统一管理'],
cons: ['需要学习 SQL 语言', '搭建和维护成本高', '小项目有点"杀鸡用牛刀"'],
example: '亚马逊用数据库管理 10 亿订单。张三的地址只存一次,无论他买多少本书,查询他的所有订单只需 0.01 秒。'
}
])
const currentStage = computed(() => {
return stages.value.find(s => s.id === activeStage.value)
})
</script>
<style scoped>
.demo-container {
.data-evolution-demo {
border: 1px solid var(--vp-c-divider);
border-radius: 8px;
padding: 20px;
background: var(--vp-c-bg-soft);
padding: 1rem;
margin: 1rem 0;
}
.demo-header {
margin-bottom: 20px;
display: flex;
align-items: center;
gap: 0.5rem;
margin-bottom: 0.75rem;
}
.demo-header h4 {
margin: 0 0 8px 0;
.demo-header .icon { font-size: 1.25rem; }
.demo-header .title { font-weight: bold; font-size: 1rem; }
.demo-header .subtitle { color: var(--vp-c-text-2); font-size: 0.85rem; margin-left: 0.5rem; }
.intro-text {
font-size: 0.9rem;
color: var(--vp-c-text-2);
line-height: 1.6;
margin-bottom: 1rem;
padding: 0.75rem;
background: var(--vp-c-bg);
border-radius: 6px;
}
.intro-text .highlight {
color: var(--vp-c-brand-1);
font-weight: 500;
}
.evolution-stages {
display: flex;
align-items: flex-start;
gap: 0.25rem;
padding: 0.75rem;
background: var(--vp-c-bg);
border-radius: 6px;
overflow-x: auto;
}
.stage {
display: flex;
flex-direction: column;
align-items: center;
min-width: 90px;
position: relative;
cursor: pointer;
padding: 0.5rem;
border-radius: 8px;
transition: all 0.2s ease;
}
.stage:hover {
background: var(--vp-c-bg-soft);
}
.stage.active {
background: var(--vp-c-brand-soft);
}
.stage-icon {
width: 40px;
height: 40px;
border-radius: 8px;
background: var(--vp-c-brand);
display: flex;
align-items: center;
justify-content: center;
font-size: 1.25rem;
margin-bottom: 0.5rem;
transition: transform 0.2s ease;
}
.stage:hover .stage-icon {
transform: scale(1.1);
}
.stage-name {
font-size: 0.8rem;
font-weight: 500;
color: var(--vp-c-text-1);
}
.hint {
margin: 0;
font-size: 14px;
color: var(--vp-c-text-2);
.stage-simple {
font-size: 0.7rem;
color: var(--vp-c-brand-1);
margin-top: 0.2rem;
font-weight: 500;
}
.demo-content {
.stage-capacity {
font-size: 0.65rem;
color: var(--vp-c-text-3);
margin-top: 0.2rem;
padding: 2px 6px;
background: var(--vp-c-bg-soft);
border-radius: 4px;
}
.arrow {
position: absolute;
right: -12px;
top: 20px;
color: var(--vp-c-text-3);
font-size: 1rem;
}
.hint-text {
text-align: center;
font-size: 0.85rem;
color: var(--vp-c-text-3);
margin-top: 0.75rem;
}
.stage-detail {
background: var(--vp-c-bg);
border-radius: 8px;
padding: 1rem;
margin-top: 0.75rem;
border: 1px solid var(--vp-c-divider);
}
.detail-header {
display: flex;
flex-direction: column;
gap: 16px;
align-items: center;
gap: 0.5rem;
margin-bottom: 0.75rem;
}
.detail-icon {
font-size: 1.5rem;
}
.detail-title {
font-weight: 600;
font-size: 1rem;
color: var(--vp-c-text-1);
flex: 1;
}
.detail-capacity {
font-size: 0.75rem;
color: var(--vp-c-brand-1);
font-weight: 500;
padding: 4px 8px;
background: var(--vp-c-brand-soft);
border-radius: 4px;
}
.pros-cons {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 1rem;
margin-bottom: 0.75rem;
}
@media (max-width: 640px) {
.pros-cons {
grid-template-columns: 1fr;
}
}
.pros, .cons {
padding: 0.75rem;
border-radius: 6px;
}
.pros {
background: rgba(34, 197, 94, 0.05);
border: 1px solid rgba(34, 197, 94, 0.2);
}
.cons {
background: rgba(239, 68, 68, 0.05);
border: 1px solid rgba(239, 68, 68, 0.2);
}
.list-title {
font-size: 0.8rem;
font-weight: 600;
margin-bottom: 0.5rem;
}
.pros ul, .cons ul {
margin: 0;
padding-left: 1.2rem;
font-size: 0.85rem;
line-height: 1.6;
}
.pros li {
color: var(--vp-c-text-2);
margin-bottom: 0.25rem;
}
.cons li {
color: var(--vp-c-text-2);
margin-bottom: 0.25rem;
}
.example-box {
background: var(--vp-c-bg-soft);
padding: 0.75rem;
border-radius: 6px;
border-left: 3px solid var(--vp-c-brand);
}
.example-label {
font-size: 0.8rem;
font-weight: 500;
color: var(--vp-c-text-2);
margin-bottom: 0.5rem;
}
.example-content {
font-size: 0.85rem;
color: var(--vp-c-text-1);
line-height: 1.5;
}
.fade-enter-active,
.fade-leave-active {
transition: opacity 0.2s ease, transform 0.2s ease;
}
.fade-enter-from,
.fade-leave-to {
opacity: 0;
transform: translateY(-10px);
}
.info-box {
background: var(--vp-c-bg-alt);
padding: 0.75rem;
border-radius: 6px;
font-size: 0.85rem;
color: var(--vp-c-text-2);
margin-top: 0.75rem;
line-height: 1.5;
}
.info-box .icon { margin-right: 0.25rem; }
.info-box .highlight {
color: var(--vp-c-brand-1);
font-weight: 500;
}
</style>
@@ -59,33 +59,60 @@ const startSearch = async () => {
</script>
<template>
<div class="db-demo">
<div class="controls">
<div class="control-item">
<span>查找 ID: </span>
<div class="db-index-demo">
<div class="demo-header">
<span class="icon">🔍</span>
<span class="title">索引查找演示</span>
<span class="subtitle">全表扫描 vs 索引查找</span>
</div>
<div class="intro-text">
想象你在<span class="highlight">图书馆</span>找一本叫"数据库原理"的书如果没有目录你得一排排书架找有了索书号直接去对应区域拿数据库索引就像这个<span class="highlight">索书号</span>让查找速度从"翻遍所有书"变成"直接定位"
</div>
<div class="controls-area">
<div class="input-group">
<label>查找 ID:</label>
<el-input-number
v-model="searchQuery"
:min="1"
:max="DATA_SIZE"
size="small"
:disabled="isSearching"
/>
</div>
<el-radio-group v-model="mode" size="small">
<el-radio-button label="scan">全表扫描 (O(n))</el-radio-button>
<el-radio-button label="index">索引查找 (O(log n))</el-radio-button>
</el-radio-group>
<el-button
type="primary"
<div class="mode-selector">
<button
class="mode-btn"
:class="{ active: mode === 'scan' }"
@click="mode = 'scan'"
:disabled="isSearching"
>
🐢 全表扫描 O(n)
</button>
<button
class="mode-btn"
:class="{ active: mode === 'index' }"
@click="mode = 'index'"
:disabled="isSearching"
>
索引查找 O(log n)
</button>
</div>
<button
class="search-btn"
@click="startSearch"
:loading="isSearching"
size="small"
>开始查询</el-button
:disabled="isSearching"
>
{{ isSearching ? '查找中...' : '开始查找' }}
</button>
</div>
<div class="visualization-area">
<!-- Full Scan Visualization -->
<div v-if="mode === 'scan'" class="view-container">
<div v-if="mode === 'scan'" class="view-container scan-view">
<div class="grid">
<div
v-for="(item, index) in data"
@@ -93,19 +120,20 @@ const startSearch = async () => {
class="data-block"
:class="{
active: index === scanCurrentIndex,
found: searchResult && searchResult.id === item.id
found: searchResult && searchResult.id === item.id,
dimmed: scanCurrentIndex >= 0 && index > scanCurrentIndex
}"
>
{{ item.id }}
</div>
</div>
<p class="desc">
全表扫描数据库必须逐行检查数据直到找到匹配项数据越多速度越慢
<p class="view-desc">
全表扫描数据库<span class="highlight">逐个翻书架</span>一样必须逐行检查数据直到找到匹配项数据越多速度越慢
</p>
</div>
<!-- Index Visualization -->
<div v-else class="view-container">
<div v-else class="view-container index-view">
<div class="grid">
<div
v-for="(item, index) in data"
@@ -114,150 +142,293 @@ const startSearch = async () => {
:class="{
visited: treeActiveNodes.includes(index),
found: searchResult && searchResult.id === item.id,
dimmed:
treeActiveNodes.length > 0 && !treeActiveNodes.includes(index)
dimmed: treeActiveNodes.length > 0 && !treeActiveNodes.includes(index)
}"
>
{{ item.id }}
</div>
</div>
<p class="desc">
索引查找类似二分查找或 B+
每次比较都能排除掉一半或更多的数据极快地定位目标
<p class="view-desc">
索引查找类似<span class="highlight">查字典</span>通过二分查找或 B+ 每次比较都能排除掉一半或更多的数据极快地定位目标
</p>
</div>
</div>
<div class="stats" v-if="!isSearching && searchResult">
<div class="stats-box" v-if="!isSearching && searchResult">
<div class="stat-item">
<span class="label">查找结果:</span>
<span class="value">{{ searchResult.value }}</span>
<span class="stat-icon">🎯</span>
<div class="stat-content">
<div class="stat-label">查找结果</div>
<div class="stat-value">{{ searchResult.value }}</div>
</div>
</div>
<div class="stat-item">
<span class="label">操作次数:</span>
<span class="value highlight"
>{{
mode === 'scan' ? scanCurrentIndex + 1 : treeActiveNodes.length
}}
</span
>
<span class="stat-icon">{{ mode === 'scan' ? '🐢' : '⚡' }}</span>
<div class="stat-content">
<div class="stat-label">操作次数</div>
<div class="stat-value" :class="mode">
{{ mode === 'scan' ? scanCurrentIndex + 1 : treeActiveNodes.length }}
</div>
</div>
</div>
</div>
<div class="info-box">
<span class="icon">💡</span>
<strong>核心思想</strong>索引是用<span class="highlight">空间换时间</span>的经典案例虽然需要额外空间存储索引结构但能让查询速度提升成千上万倍就像图书馆的目录卡片虽占位置但找书快太多了
</div>
</div>
</template>
<style scoped>
.db-demo {
padding: 20px;
border: 1px solid #e4e7ed;
.db-index-demo {
border: 1px solid var(--vp-c-divider);
border-radius: 8px;
background: #ffffff;
font-family: sans-serif;
background: var(--vp-c-bg-soft);
padding: 1rem;
margin: 1rem 0;
}
.controls {
display: flex;
gap: 15px;
margin-bottom: 20px;
flex-wrap: wrap;
align-items: center;
padding-bottom: 15px;
border-bottom: 1px solid #eee;
}
.control-item {
.demo-header {
display: flex;
align-items: center;
gap: 8px;
font-size: 14px;
gap: 0.5rem;
margin-bottom: 0.75rem;
}
.view-container {
min-height: 200px;
.demo-header .icon { font-size: 1.25rem; }
.demo-header .title { font-weight: bold; font-size: 1rem; }
.demo-header .subtitle { color: var(--vp-c-text-2); font-size: 0.85rem; margin-left: 0.5rem; }
.intro-text {
font-size: 0.9rem;
color: var(--vp-c-text-2);
line-height: 1.6;
margin-bottom: 1rem;
padding: 0.75rem;
background: var(--vp-c-bg);
border-radius: 6px;
}
.intro-text .highlight {
color: var(--vp-c-brand-1);
font-weight: 500;
}
.controls-area {
background: var(--vp-c-bg);
padding: 0.75rem;
border-radius: 6px;
margin-bottom: 1rem;
display: flex;
flex-direction: column;
align-items: center;
gap: 0.75rem;
}
.input-group {
display: flex;
align-items: center;
gap: 0.5rem;
}
.input-group label {
font-size: 0.85rem;
color: var(--vp-c-text-2);
font-weight: 500;
}
.mode-selector {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 0.5rem;
}
.mode-btn {
padding: 0.5rem 0.75rem;
background: var(--vp-c-bg-soft);
border: 1px solid var(--vp-c-divider);
border-radius: 6px;
font-size: 0.8rem;
cursor: pointer;
transition: all 0.2s;
color: var(--vp-c-text-1);
}
.mode-btn:hover:not(:disabled) {
background: var(--vp-c-bg-soft);
border-color: var(--vp-c-brand);
}
.mode-btn.active {
background: var(--vp-c-brand-soft);
border-color: var(--vp-c-brand);
color: var(--vp-c-brand-1);
font-weight: 500;
}
.mode-btn:disabled {
opacity: 0.5;
cursor: not-allowed;
}
.search-btn {
width: 100%;
padding: 0.75rem;
background: var(--vp-c-brand);
color: white;
border: none;
border-radius: 6px;
font-size: 0.9rem;
font-weight: 500;
cursor: pointer;
transition: background 0.2s;
}
.search-btn:hover:not(:disabled) {
background: var(--vp-c-brand-1);
}
.search-btn:disabled {
opacity: 0.6;
cursor: not-allowed;
}
.visualization-area {
background: var(--vp-c-bg);
padding: 0.75rem;
border-radius: 6px;
margin-bottom: 1rem;
}
.view-container {
max-height: 300px;
overflow-y: auto;
}
.grid {
display: flex;
flex-wrap: wrap;
gap: 6px;
justify-content: center;
margin-bottom: 15px;
margin-bottom: 0.75rem;
}
.data-block {
width: 32px;
height: 32px;
background: #f0f2f5;
background: var(--vp-c-bg-soft);
display: flex;
align-items: center;
justify-content: center;
font-size: 12px;
font-size: 0.7rem;
border-radius: 4px;
color: #606266;
color: var(--vp-c-text-2);
transition: all 0.3s;
border: 1px solid transparent;
border: 1px solid var(--vp-c-divider);
}
.data-block.active {
background: #409eff;
background: var(--vp-c-brand);
color: white;
transform: scale(1.15);
box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
border-color: #409eff;
box-shadow: 0 2px 8px rgba(37, 99, 235, 0.3);
border-color: var(--vp-c-brand);
z-index: 1;
}
.data-block.found {
background: #67c23a;
background: #22c55e;
color: white;
transform: scale(1.2);
box-shadow: 0 0 15px rgba(103, 194, 58, 0.5);
border-color: #67c23a;
box-shadow: 0 0 12px rgba(34, 197, 94, 0.5);
border-color: #22c55e;
z-index: 2;
font-weight: bold;
}
.tree-node.visited {
background: #e6a23c;
color: white;
transform: scale(1.1);
box-shadow: 0 2px 8px rgba(230, 162, 60, 0.4);
z-index: 1;
}
.tree-node.dimmed {
.data-block.dimmed {
opacity: 0.2;
filter: grayscale(100%);
}
.desc {
font-size: 14px;
color: #909399;
text-align: center;
margin-top: 10px;
max-width: 600px;
.tree-node.visited {
background: #f59e0b;
color: white;
transform: scale(1.1);
box-shadow: 0 2px 8px rgba(245, 158, 11, 0.4);
z-index: 1;
}
.stats {
margin-top: 20px;
padding: 15px;
background: #fdf6ec;
border-radius: 4px;
display: flex;
justify-content: space-around;
.view-desc {
text-align: center;
font-size: 0.8rem;
color: var(--vp-c-text-2);
line-height: 1.5;
margin: 0;
}
.view-desc .highlight {
color: var(--vp-c-brand-1);
font-weight: 500;
}
.stats-box {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 0.75rem;
margin-bottom: 1rem;
}
.stat-item {
background: var(--vp-c-bg);
padding: 0.75rem;
border-radius: 6px;
display: flex;
flex-direction: column;
align-items: center;
gap: 5px;
gap: 0.75rem;
}
.label {
font-size: 12px;
color: #909399;
.stat-icon {
font-size: 1.5rem;
}
.value {
font-size: 16px;
font-weight: bold;
color: #303133;
.stat-content {
flex: 1;
}
.value.highlight {
color: #e6a23c;
font-size: 20px;
.stat-label {
font-size: 0.7rem;
color: var(--vp-c-text-3);
margin-bottom: 0.25rem;
}
.stat-value {
font-size: 1rem;
font-weight: 600;
color: var(--vp-c-text-1);
}
.stat-value.scan {
color: #ef4444;
}
.stat-value:not(.scan) {
color: #22c55e;
}
.info-box {
background: var(--vp-c-bg-alt);
padding: 0.75rem;
border-radius: 6px;
font-size: 0.85rem;
color: var(--vp-c-text-2);
line-height: 1.5;
}
.info-box .icon { margin-right: 0.25rem; }
.info-box .highlight {
color: var(--vp-c-brand-1);
font-weight: 500;
}
</style>
@@ -1,50 +1,320 @@
<template>
<div class="demo-container">
<div class="relation-demo">
<div class="demo-header">
<h4>{{ title }}</h4>
<p class="hint">{{ description }}</p>
<span class="icon">🔗</span>
<span class="title">外键关系演示</span>
<span class="subtitle">理解表与表之间如何关联</span>
</div>
<div class="demo-content">
<el-alert type="info" :closable="false">
数据库关系演示组件占位符 - 待实现具体交互
</el-alert>
<div class="intro-text">
想象你在管理一个<span class="highlight">家族谱系</span>"家谱表"记录每个人"婚姻表"记录谁和谁结婚了两张表通过"人名"关联起来这就是<span class="highlight">外键</span>的作用
</div>
<div class="tables-container">
<div class="table-card users-table">
<div class="table-header">
<span class="table-icon">👥</span>
<span class="table-name">用户表 (users)</span>
<span class="table-badge">主表</span>
</div>
<div class="table-content">
<div class="table-row header">
<div class="cell primary-key">🔑 user_id</div>
<div class="cell">name</div>
<div class="cell">phone</div>
<div class="cell">address</div>
</div>
<div
v-for="user in users"
:key="user.user_id"
class="table-row"
:class="{ highlighted: highlightedUserId === user.user_id }"
@mouseenter="highlightedUserId = user.user_id"
@mouseleave="highlightedUserId = null"
>
<div class="cell primary-key">{{ user.user_id }}</div>
<div class="cell">{{ user.name }}</div>
<div class="cell">{{ user.phone }}</div>
<div class="cell">{{ user.address }}</div>
</div>
</div>
</div>
<div class="relation-arrow">
<div class="arrow-line"></div>
<div class="arrow-head"></div>
<div class="relation-label">user_id (外键) user_id (主键)</div>
</div>
<div class="table-card orders-table">
<div class="table-header">
<span class="table-icon">📦</span>
<span class="table-name">订单表 (orders)</span>
<span class="table-badge">从表</span>
</div>
<div class="table-content">
<div class="table-row header">
<div class="cell primary-key">🔑 order_id</div>
<div class="cell">book_name</div>
<div class="cell foreign-key">🔗 user_id</div>
<div class="cell">price</div>
</div>
<div
v-for="order in filteredOrders"
:key="order.order_id"
class="table-row"
:class="{ highlighted: highlightedUserId === order.user_id }"
>
<div class="cell primary-key">{{ order.order_id }}</div>
<div class="cell">{{ order.book_name }}</div>
<div class="cell foreign-key">{{ order.user_id }}</div>
<div class="cell">{{ order.price }}</div>
</div>
</div>
</div>
</div>
<div class="explanation-box">
<div class="explanation-title">💡 核心概念</div>
<div class="explanation-content">
<p><strong>主键Primary Key</strong>用户表的 <code>user_id</code> 是主键唯一标识每个用户</p>
<p><strong>外键Foreign Key</strong>订单表的 <code>user_id</code> 是外键指向用户表的主键</p>
<p><strong>关联查询</strong>通过外键数据库可以快速找到"订单 001 是用户 101 买的"然后去用户表查到"用户 101 是张三"</p>
</div>
</div>
<div class="info-box">
<span class="icon">🎯</span>
<strong>核心优势</strong>外键消除了数据冗余张三的地址只存一次无论他买多少本书如果要修改地址只需改用户表的一行所有订单自动关联到新地址
</div>
</div>
</template>
<script setup>
import { ref } from 'vue'
import { ref, computed } from 'vue'
const title = ref('数据库关系演示')
const description = ref('展示关系数据库中表与表之间的关系,包括一对一、一对多、多对多关系')
const highlightedUserId = ref(null)
const users = ref([
{ user_id: 101, name: '张三', phone: '138xxxx', address: '北京' },
{ user_id: 102, name: '李四', phone: '139xxxx', address: '上海' },
{ user_id: 103, name: '王五', phone: '137xxxx', address: '广州' }
])
const orders = ref([
{ order_id: '001', book_name: '百年孤独', user_id: 101, price: 59 },
{ order_id: '002', book_name: '活着', user_id: 101, price: 39 },
{ order_id: '003', book_name: '三体', user_id: 101, price: 99 },
{ order_id: '004', book_name: '百年孤独', user_id: 102, price: 59 },
{ order_id: '005', book_name: '红楼梦', user_id: 102, price: 79 },
{ order_id: '006', book_name: '西游记', user_id: 103, price: 69 }
])
const filteredOrders = computed(() => {
if (!highlightedUserId.value) {
return orders.value
}
return orders.value.filter(order => order.user_id === highlightedUserId.value)
})
</script>
<style scoped>
.demo-container {
.relation-demo {
border: 1px solid var(--vp-c-divider);
border-radius: 8px;
padding: 20px;
background: var(--vp-c-bg-soft);
padding: 1rem;
margin: 1rem 0;
}
.demo-header {
margin-bottom: 20px;
display: flex;
align-items: center;
gap: 0.5rem;
margin-bottom: 0.75rem;
}
.demo-header h4 {
margin: 0 0 8px 0;
.demo-header .icon { font-size: 1.25rem; }
.demo-header .title { font-weight: bold; font-size: 1rem; }
.demo-header .subtitle { color: var(--vp-c-text-2); font-size: 0.85rem; margin-left: 0.5rem; }
.intro-text {
font-size: 0.9rem;
color: var(--vp-c-text-2);
line-height: 1.6;
margin-bottom: 1rem;
padding: 0.75rem;
background: var(--vp-c-bg);
border-radius: 6px;
}
.intro-text .highlight {
color: var(--vp-c-brand-1);
font-weight: 500;
}
.tables-container {
display: grid;
grid-template-columns: 1fr auto 1fr;
gap: 1rem;
align-items: start;
margin-bottom: 1rem;
}
@media (max-width: 960px) {
.tables-container {
grid-template-columns: 1fr;
}
.relation-arrow {
transform: rotate(90deg);
margin: 0.5rem 0;
}
}
.table-card {
background: var(--vp-c-bg);
border-radius: 8px;
overflow: hidden;
border: 1px solid var(--vp-c-divider);
}
.table-header {
display: flex;
align-items: center;
gap: 0.5rem;
padding: 0.5rem 0.75rem;
background: var(--vp-c-bg-soft);
border-bottom: 1px solid var(--vp-c-divider);
}
.table-icon { font-size: 1rem; }
.table-name { font-weight: 600; font-size: 0.85rem; flex: 1; }
.table-badge {
font-size: 0.7rem;
padding: 2px 6px;
border-radius: 4px;
background: var(--vp-c-brand-soft);
color: var(--vp-c-brand-1);
}
.table-content {
max-height: 280px;
overflow-y: auto;
}
.table-row {
display: grid;
grid-template-columns: 1fr 1.2fr 1fr 0.8fr;
border-bottom: 1px solid var(--vp-c-divider);
transition: background 0.2s;
}
.table-row:last-child {
border-bottom: none;
}
.table.row.header {
background: var(--vp-c-bg-soft);
font-weight: 600;
}
.table-row.header .cell {
font-size: 0.75rem;
color: var(--vp-c-text-2);
padding: 0.5rem 0.25rem;
}
.table-row .cell {
font-size: 0.75rem;
padding: 0.5rem 0.25rem;
color: var(--vp-c-text-1);
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
.table-row.highlighted {
background: rgba(34, 197, 94, 0.1);
}
.cell.primary-key {
color: var(--vp-c-brand-1);
font-weight: 500;
}
.cell.foreign-key {
color: #f59e0b;
font-weight: 500;
}
.relation-arrow {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
padding: 1rem 0;
}
.arrow-line {
width: 2px;
height: 40px;
background: linear-gradient(to right, var(--vp-c-brand), #f59e0b);
margin-bottom: 4px;
}
.arrow-head {
color: #f59e0b;
font-size: 1.25rem;
margin-bottom: 0.5rem;
}
.relation-label {
font-size: 0.65rem;
color: var(--vp-c-text-2);
text-align: center;
max-width: 120px;
line-height: 1.3;
}
.explanation-box {
background: var(--vp-c-bg);
border-radius: 6px;
padding: 0.75rem;
margin-bottom: 0.75rem;
border: 1px solid var(--vp-c-divider);
}
.explanation-title {
font-size: 0.85rem;
font-weight: 600;
margin-bottom: 0.5rem;
color: var(--vp-c-text-1);
}
.hint {
margin: 0;
font-size: 14px;
.explanation-content p {
font-size: 0.8rem;
color: var(--vp-c-text-2);
margin: 0.25rem 0;
line-height: 1.5;
}
.demo-content {
display: flex;
flex-direction: column;
gap: 16px;
.explanation-content code {
background: var(--vp-c-bg-soft);
padding: 2px 4px;
border-radius: 3px;
font-family: var(--vp-font-family-mono);
font-size: 0.75rem;
color: var(--vp-c-brand-1);
}
.info-box {
background: var(--vp-c-bg-alt);
padding: 0.75rem;
border-radius: 6px;
font-size: 0.85rem;
color: var(--vp-c-text-2);
line-height: 1.5;
}
.info-box .icon { margin-right: 0.25rem; }
</style>
@@ -1,13 +1,63 @@
<template>
<div class="demo-container">
<div class="optimization-demo">
<div class="demo-header">
<h4>{{ title }}</h4>
<p class="hint">{{ description }}</p>
<span class="icon"></span>
<span class="title">查询优化演示</span>
<span class="subtitle">常见错误与正确做法对比</span>
</div>
<div class="demo-content">
<el-alert type="info" :closable="false">
查询优化演示组件占位符 - 待实现具体交互
</el-alert>
<div class="intro-text">
很多时候查询慢不是因为<span class="highlight">数据库性能差</span>而是因为 SQL 写错了下面这些错误你可能每天都在犯
</div>
<div class="pitfalls-list">
<div
v-for="(pitfall, index) in pitfalls"
:key="index"
class="pitfall-card"
:class="{ expanded: expandedIndex === index }"
>
<div class="pitfall-header" @click="expandedIndex = expandedIndex === index ? null : index">
<div class="pitfall-number">{{ index + 1 }}</div>
<div class="pitfall-title">{{ pitfall.title }}</div>
<div class="expand-icon">{{ expandedIndex === index ? '▼' : '▶' }}</div>
</div>
<Transition name="expand">
<div v-if="expandedIndex === index" class="pitfall-content">
<div class="code-comparison">
<div class="code-section wrong">
<div class="section-label"> 错误写法</div>
<pre><code>{{ pitfall.wrong }}</code></pre>
<div class="impact"> {{ pitfall.impact }}</div>
</div>
<div class="code-section correct">
<div class="section-label"> 正确写法</div>
<pre><code>{{ pitfall.correct }}</code></pre>
<div class="benefit">💡 {{ pitfall.benefit }}</div>
</div>
</div>
<div class="explanation">
<strong>原理</strong>{{ pitfall.explanation }}
</div>
</div>
</Transition>
</div>
</div>
<div class="tips-box">
<div class="tips-title">📝 优化建议清单</div>
<div class="tips-list">
<div v-for="(tip, i) in tips" :key="i" class="tip-item">
<span class="tip-icon"></span>
<span class="tip-text">{{ tip }}</span>
</div>
</div>
</div>
<div class="info-box">
<span class="icon">🎯</span>
<strong>核心原则</strong>不要让数据库做"多余的工作"索引失效全表扫描返回不必要的数据这些都是最常见的性能杀手写出高效 SQL 的关键<span class="highlight">理解数据库如何执行你的查询</span>
</div>
</div>
</template>
@@ -15,36 +65,301 @@
<script setup>
import { ref } from 'vue'
const title = ref('查询优化演示')
const description = ref('展示数据库查询优化的原理和方法,包括执行计划分析和索引优化')
const expandedIndex = ref(0)
const pitfalls = ref([
{
title: '在索引列上使用函数',
wrong: "SELECT * FROM users WHERE YEAR(created_at) = 2024;",
correct: "SELECT * FROM users WHERE created_at >= '2024-01-01' AND created_at < '2025-01-01';",
impact: '索引失效,全表扫描',
benefit: '可以使用索引,查询速度提升 1000 倍',
explanation: '当对列使用函数时,数据库必须先计算每一行的函数值,无法使用索引。把函数移到等号右边,或用范围查询代替。'
},
{
title: '隐式类型转换',
wrong: "SELECT * FROM users WHERE user_id = '123'; -- user_id 是 int",
correct: "SELECT * FROM users WHERE user_id = 123;",
impact: '索引失效,每次都要类型转换',
benefit: '直接使用索引',
explanation: '字符串和数字比较时,数据库会隐式转换,导致索引失效。确保比较的类型和列定义的类型一致。'
},
{
title: 'LIKE 以 % 开头',
wrong: "SELECT * FROM users WHERE name LIKE '%张三%';",
correct: "SELECT * FROM users WHERE name LIKE '张三%';",
impact: '无法使用索引,全表扫描',
benefit: '可以使用索引进行前缀匹配',
explanation: '索引是按照顺序存储的,% 开头的模糊查询无法利用顺序。如果只需要前缀匹配,把 % 放到后面。'
},
{
title: 'SELECT * 返回所有列',
wrong: "SELECT * FROM users WHERE user_id = 1;",
correct: "SELECT user_id, name, email FROM users WHERE user_id = 1;",
impact: '增加网络传输和内存消耗,无法使用覆盖索引',
benefit: '减少传输量,可能使用覆盖索引',
explanation: '只查询需要的列。如果索引包含了所有需要的列,数据库可以直接从索引返回数据,不需要查表(覆盖索引)。'
}
])
const tips = ref([
'为 WHERE、JOIN、ORDER BY 的列创建索引',
'避免在索引列上使用函数或表达式',
'用 EXPLAIN 分析查询执行计划',
'只查询需要的列,避免 SELECT *',
'批量操作代替单条操作',
'考虑使用覆盖索引减少回表'
])
</script>
<style scoped>
.demo-container {
.optimization-demo {
border: 1px solid var(--vp-c-divider);
border-radius: 8px;
padding: 20px;
background: var(--vp-c-bg-soft);
padding: 1rem;
margin: 1rem 0;
}
.demo-header {
margin-bottom: 20px;
display: flex;
align-items: center;
gap: 0.5rem;
margin-bottom: 0.75rem;
}
.demo-header h4 {
margin: 0 0 8px 0;
.demo-header .icon { font-size: 1.25rem; }
.demo-header .title { font-weight: bold; font-size: 1rem; }
.demo-header .subtitle { color: var(--vp-c-text-2); font-size: 0.85rem; margin-left: 0.5rem; }
.intro-text {
font-size: 0.9rem;
color: var(--vp-c-text-2);
line-height: 1.6;
margin-bottom: 1rem;
padding: 0.75rem;
background: var(--vp-c-bg);
border-radius: 6px;
}
.intro-text .highlight {
color: var(--vp-c-brand-1);
font-weight: 500;
}
.pitfalls-list {
display: flex;
flex-direction: column;
gap: 0.75rem;
margin-bottom: 1rem;
}
.pitfall-card {
background: var(--vp-c-bg);
border: 1px solid var(--vp-c-divider);
border-radius: 6px;
overflow: hidden;
}
.pitfall-header {
display: flex;
align-items: center;
gap: 0.75rem;
padding: 0.75rem;
cursor: pointer;
transition: background 0.2s;
}
.pitfall-header:hover {
background: var(--vp-c-bg-soft);
}
.pitfall-number {
width: 24px;
height: 24px;
background: var(--vp-c-brand);
color: white;
border-radius: 50%;
display: flex;
align-items: center;
justify-content: center;
font-size: 0.75rem;
font-weight: 600;
flex-shrink: 0;
}
.pitfall-title {
flex: 1;
font-size: 0.85rem;
font-weight: 500;
color: var(--vp-c-text-1);
}
.hint {
.expand-icon {
font-size: 0.65rem;
color: var(--vp-c-text-3);
flex-shrink: 0;
}
.pitfall-content {
padding: 0 0.75rem 0.75rem;
}
.code-comparison {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 0.75rem;
margin-bottom: 0.75rem;
}
@media (max-width: 768px) {
.code-comparison {
grid-template-columns: 1fr;
}
}
.code-section {
border-radius: 6px;
overflow: hidden;
}
.code-section.wrong {
background: rgba(239, 68, 68, 0.05);
border: 1px solid rgba(239, 68, 68, 0.2);
}
.code-section.correct {
background: rgba(34, 197, 94, 0.05);
border: 1px solid rgba(34, 197, 94, 0.2);
}
.section-label {
padding: 0.5rem 0.75rem;
font-size: 0.75rem;
font-weight: 600;
}
.code-section.wrong .section-label {
color: #ef4444;
background: rgba(239, 68, 68, 0.1);
}
.code-section.correct .section-label {
color: #22c55e;
background: rgba(34, 197, 94, 0.1);
}
.code-section pre {
margin: 0;
font-size: 14px;
padding: 0.75rem;
overflow-x: auto;
background: rgba(0, 0, 0, 0.02);
}
.code-section code {
font-family: var(--vp-font-family-mono);
font-size: 0.7rem;
line-height: 1.5;
color: var(--vp-c-brand-1);
}
.impact, .benefit {
padding: 0.5rem 0.75rem;
font-size: 0.75rem;
}
.impact {
color: #ef4444;
background: rgba(239, 68, 68, 0.1);
}
.benefit {
color: #22c55e;
background: rgba(34, 197, 94, 0.1);
}
.explanation {
font-size: 0.8rem;
color: var(--vp-c-text-2);
line-height: 1.5;
}
.tips-box {
background: var(--vp-c-bg);
border-radius: 6px;
padding: 0.75rem;
margin-bottom: 0.75rem;
border: 1px solid var(--vp-c-divider);
}
.tips-title {
font-size: 0.85rem;
font-weight: 600;
margin-bottom: 0.5rem;
color: var(--vp-c-text-1);
}
.tips-list {
display: grid;
grid-template-columns: repeat(2, 1fr);
gap: 0.5rem;
}
@media (max-width: 640px) {
.tips-list {
grid-template-columns: 1fr;
}
}
.tip-item {
display: flex;
align-items: baseline;
gap: 0.5rem;
font-size: 0.8rem;
color: var(--vp-c-text-2);
}
.demo-content {
display: flex;
flex-direction: column;
gap: 16px;
.tip-icon {
color: #22c55e;
font-weight: bold;
flex-shrink: 0;
}
.tip-text {
line-height: 1.4;
}
.expand-enter-active,
.expand-leave-active {
transition: all 0.3s ease;
overflow: hidden;
}
.expand-enter-from,
.expand-leave-to {
max-height: 0;
opacity: 0;
}
.expand-enter-to,
.expand-leave-from {
max-height: 1000px;
opacity: 1;
}
.info-box {
background: var(--vp-c-bg-alt);
padding: 0.75rem;
border-radius: 6px;
font-size: 0.85rem;
color: var(--vp-c-text-2);
line-height: 1.5;
}
.info-box .icon { margin-right: 0.25rem; }
.info-box .highlight {
color: var(--vp-c-brand-1);
font-weight: 500;
}
</style>
@@ -71,21 +71,31 @@ const setHover = (id) => {
<template>
<div class="relational-demo">
<div class="demo-header">
<span class="icon">📊</span>
<span class="title">关系型数据演示</span>
<span class="subtitle">Excel 模式 vs 数据库模式</span>
</div>
<div class="intro-text">
想象你在管理一个<span class="highlight">书店订单</span> Excel 每个订单都重复写顾客信息用关系型数据库时顾客信息单独存一张表订单表只存顾客 ID就像把<span class="highlight">通讯录和订单分开</span>而不是每笔订单都抄一遍地址
</div>
<div class="tabs">
<div
<button
class="tab"
:class="{ active: activeTab === 'excel' }"
@click="activeTab = 'excel'"
>
Excel 模式 (单表)
</div>
<div
📋 Excel 模式 (单表)
</button>
<button
class="tab"
:class="{ active: activeTab === 'db' }"
@click="activeTab = 'db'"
>
数据库模式 (多表关联)
</div>
🗄 数据库模式 (多表关联)
</button>
</div>
<div class="content-area">
@@ -117,7 +127,7 @@ const setHover = (id) => {
</div>
<div class="note error">
<p> <strong>问题</strong> "张三"的信息重复存储了 3 </p>
<p>如果张三换了电话你需要修改 3 行数据很容易漏改</p>
<p>如果张三换了电话你需要修改 3 行数据很容易漏改这叫<span class="highlight">数据冗余</span></p>
</div>
</div>
@@ -126,7 +136,7 @@ const setHover = (id) => {
<div class="db-layout">
<!-- Users Table -->
<div class="db-table users-table">
<div class="table-title">用户表 (Users)</div>
<div class="table-title">👥 用户表 (Users)</div>
<table>
<thead>
<tr>
@@ -143,7 +153,7 @@ const setHover = (id) => {
@mouseenter="setHover(u.id)"
@mouseleave="setHover(null)"
>
<td>{{ u.id }}</td>
<td class="primary-key">{{ u.id }}</td>
<td>{{ u.name }}</td>
<td>{{ u.phone }}</td>
</tr>
@@ -153,12 +163,13 @@ const setHover = (id) => {
<!-- Connection Lines (Visual only, simplified) -->
<div class="connector">
<div class="arrow"> 关联 (Join) </div>
<div class="arrow-label">🔗 外键关联</div>
<div class="arrow"> Join </div>
</div>
<!-- Orders Table -->
<div class="db-table orders-table">
<div class="table-title">订单表 (Orders)</div>
<div class="table-title">📦 订单表 (Orders)</div>
<table>
<thead>
<tr>
@@ -179,134 +190,239 @@ const setHover = (id) => {
<td>{{ o.id }}</td>
<td>{{ o.book }}</td>
<td>{{ o.price }}</td>
<td class="highlight-cell">{{ o.user_id }}</td>
<td class="highlight-cell foreign-key">{{ o.user_id }}</td>
</tr>
</tbody>
</table>
</div>
</div>
<div class="note success">
<p> <strong>优势</strong> 订单表只存 "用户 ID"</p>
<p> <strong>优势</strong> 订单表只存 "用户 ID"不重复存用户信息</p>
<p>
鼠标悬停在某一行看看它们是如何自动关联修改用户表一次所有订单都会自动更新
鼠标悬停在用户表或订单表的某一行看看它们是如何通过 <span class="highlight">外键自动关联</span>修改用户表一次所有订单都会自动更新
</p>
</div>
</div>
</div>
<div class="info-box">
<span class="icon">💡</span>
<strong>核心思想</strong>关系型数据库通过<span class="highlight">拆表 + 外键</span>消除冗余就像把通讯录和记账本分开记账本只写"姓名"查账时再去通讯录找详细信息这样改一次电话所有记录都更新
</div>
</div>
</template>
<style scoped>
.relational-demo {
border: 1px solid #e4e7ed;
border: 1px solid var(--vp-c-divider);
border-radius: 8px;
background: #fff;
overflow: hidden;
margin: 20px 0;
font-family: sans-serif;
background: var(--vp-c-bg-soft);
padding: 1rem;
margin: 1rem 0;
}
.demo-header {
display: flex;
align-items: center;
gap: 0.5rem;
margin-bottom: 0.75rem;
}
.demo-header .icon { font-size: 1.25rem; }
.demo-header .title { font-weight: bold; font-size: 1rem; }
.demo-header .subtitle { color: var(--vp-c-text-2); font-size: 0.85rem; margin-left: 0.5rem; }
.intro-text {
font-size: 0.9rem;
color: var(--vp-c-text-2);
line-height: 1.6;
margin-bottom: 1rem;
padding: 0.75rem;
background: var(--vp-c-bg);
border-radius: 6px;
}
.intro-text .highlight {
color: var(--vp-c-brand-1);
font-weight: 500;
}
.tabs {
display: flex;
background: #f5f7fa;
border-bottom: 1px solid #e4e7ed;
gap: 0.5rem;
margin-bottom: 1rem;
}
.tab {
padding: 12px 24px;
flex: 1;
padding: 0.75rem;
background: var(--vp-c-bg);
border: 1px solid var(--vp-c-divider);
border-radius: 6px;
font-size: 0.85rem;
cursor: pointer;
font-size: 14px;
color: #606266;
border-right: 1px solid #e4e7ed;
transition: all 0.2s;
color: var(--vp-c-text-2);
font-weight: 500;
}
.tab:hover {
background: var(--vp-c-bg-soft);
border-color: var(--vp-c-brand);
}
.tab.active {
background: #fff;
color: #409eff;
font-weight: bold;
border-bottom: 2px solid #409eff;
margin-bottom: -1px;
background: var(--vp-c-brand-soft);
border-color: var(--vp-c-brand);
color: var(--vp-c-brand-1);
}
.content-area {
padding: 20px;
background: var(--vp-c-bg);
padding: 0.75rem;
border-radius: 6px;
margin-bottom: 1rem;
}
.table-wrapper {
overflow-x: auto;
margin-bottom: 0.75rem;
}
table {
width: 100%;
border-collapse: collapse;
font-size: 13px;
font-size: 0.8rem;
}
th,
td {
border: 1px solid #ebeef5;
padding: 8px 12px;
border: 1px solid var(--vp-c-divider);
padding: 0.5rem 0.75rem;
text-align: left;
}
th {
background: #fafafa;
font-weight: bold;
color: #303133;
background: var(--vp-c-bg-soft);
font-weight: 600;
color: var(--vp-c-text-1);
}
.highlight-col {
background: #ecf5ff;
color: #409eff;
background: rgba(245, 158, 11, 0.1);
color: #f59e0b;
}
.highlight-cell {
background: #f0f9eb;
color: #67c23a;
font-weight: bold;
background: rgba(239, 68, 68, 0.1);
color: #ef4444;
font-weight: 500;
}
.primary-key {
color: var(--vp-c-brand-1);
font-weight: 500;
}
.foreign-key {
color: #f59e0b;
font-weight: 500;
}
.excel-view .highlight-cell {
background: #fef0f0;
color: #f56c6c;
background: rgba(239, 68, 68, 0.1);
color: #ef4444;
}
.db-layout {
display: flex;
gap: 20px;
gap: 1rem;
align-items: flex-start;
flex-wrap: wrap;
}
.db-table {
flex: 1;
min-width: 280px;
border: 1px solid #ebeef5;
border-radius: 4px;
border: 1px solid var(--vp-c-divider);
border-radius: 6px;
overflow: hidden;
}
.table-title {
background: #f2f6fc;
padding: 8px 12px;
font-weight: bold;
font-size: 13px;
color: #606266;
border-bottom: 1px solid #ebeef5;
background: var(--vp-c-bg-soft);
padding: 0.5rem 0.75rem;
font-weight: 600;
font-size: 0.8rem;
color: var(--vp-c-text-1);
border-bottom: 1px solid var(--vp-c-divider);
}
.connector {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
padding-top: 50px;
font-size: 12px;
color: #909399;
padding-top: 1.5rem;
font-size: 0.75rem;
color: var(--vp-c-text-2);
min-width: 100px;
}
.arrow-label {
font-weight: 500;
margin-bottom: 0.25rem;
}
.arrow {
color: var(--vp-c-brand-1);
}
tr.active {
background: #ecf5ff;
background: rgba(34, 197, 94, 0.1);
}
.note {
margin-top: 15px;
padding: 12px;
border-radius: 4px;
font-size: 13px;
line-height: 1.6;
padding: 0.75rem;
border-radius: 6px;
font-size: 0.85rem;
line-height: 1.5;
}
.note p {
margin: 0.25rem 0;
}
.note.error {
background: #fef0f0;
color: #f56c6c;
background: rgba(239, 68, 68, 0.05);
border: 1px solid rgba(239, 68, 68, 0.2);
color: var(--vp-c-text-2);
}
.note.success {
background: #f0f9eb;
color: #67c23a;
background: rgba(34, 197, 94, 0.05);
border: 1px solid rgba(34, 197, 94, 0.2);
color: var(--vp-c-text-2);
}
.note .highlight {
color: var(--vp-c-brand-1);
font-weight: 500;
}
.info-box {
background: var(--vp-c-bg-alt);
padding: 0.75rem;
border-radius: 6px;
font-size: 0.85rem;
color: var(--vp-c-text-2);
line-height: 1.5;
}
.info-box .icon { margin-right: 0.25rem; }
.info-box .highlight {
color: var(--vp-c-brand-1);
font-weight: 500;
}
</style>
@@ -1,302 +1,372 @@
<script setup>
import { ref, computed } from 'vue'
const users = ref([
{ id: 101, name: '张三', score: 100 },
{ id: 102, name: '李四', score: 85 },
{ id: 103, name: '王五', score: 120 },
{ id: 104, name: '赵六', score: 90 }
])
const commands = [
{
label: '查询所有用户',
sql: 'SELECT * FROM users;',
action: 'read_all'
},
{
label: '查询分数 > 90',
sql: 'SELECT * FROM users WHERE score > 90;',
action: 'read_filter'
},
{
label: '给张三加 10 分',
sql: 'UPDATE users SET score = score + 10 WHERE name = "张三";',
action: 'update_add'
}
]
const currentSql = ref('')
const terminalOutput = ref([])
const displayedUsers = ref([...users.value])
const isAnimating = ref(false)
const execute = async (cmd) => {
if (isAnimating.value) return
isAnimating.value = true
currentSql.value = cmd.sql
// Reset display for read operations
if (cmd.action.startsWith('read')) {
displayedUsers.value = [] // clear first
await new Promise((r) => setTimeout(r, 300))
}
terminalOutput.value.push({ type: 'cmd', text: `> ${cmd.sql}` })
await new Promise((r) => setTimeout(r, 500))
if (cmd.action === 'read_all') {
displayedUsers.value = [...users.value]
terminalOutput.value.push({
type: 'result',
text: `Returned ${users.value.length} rows.`
})
} else if (cmd.action === 'read_filter') {
displayedUsers.value = users.value.filter((u) => u.score > 90)
terminalOutput.value.push({
type: 'result',
text: `Returned ${displayedUsers.value.length} rows.`
})
} else if (cmd.action === 'update_add') {
const target = users.value.find((u) => u.name === '张三')
if (target) {
target.score += 10
displayedUsers.value = [...users.value] // refresh display
terminalOutput.value.push({ type: 'success', text: `Updated 1 row.` })
}
}
// Scroll terminal to bottom
setTimeout(() => {
const term = document.querySelector('.sql-terminal-body')
if (term) term.scrollTop = term.scrollHeight
}, 100)
isAnimating.value = false
}
</script>
<template>
<div class="sql-playground">
<div class="left-panel">
<div class="panel-header">数据库表: users</div>
<div class="table-container">
<table>
<thead>
<tr>
<th>id</th>
<th>name</th>
<th>score</th>
</tr>
</thead>
<tbody>
<tr v-for="u in displayedUsers" :key="u.id" class="fade-in">
<td>{{ u.id }}</td>
<td>{{ u.name }}</td>
<td>
<span
:class="{ 'score-up': u.name === '张三' && u.score > 100 }"
>{{ u.score }}</span
>
</td>
</tr>
<tr v-if="displayedUsers.length === 0">
<td colspan="3" class="empty-hint">无数据或正在查询...</td>
</tr>
</tbody>
</table>
<div class="sql-playground-demo">
<div class="demo-header">
<span class="icon">💻</span>
<span class="title">SQL 练习场</span>
<span class="subtitle">体验 SQL CRUD 操作</span>
</div>
<div class="intro-text">
SQL 就像和数据库<span class="highlight">对话</span>你说"给我找所有年龄大于 25 的用户"数据库就会执行查询并返回结果即使不会编程也能很快上手
</div>
<div class="playground-container">
<div class="operation-selector">
<button
v-for="op in operations"
:key="op.key"
class="op-btn"
:class="{ active: currentOp === op.key }"
@click="currentOp = op.key"
>
<span class="op-icon">{{ op.icon }}</span>
<span class="op-name">{{ op.name }}</span>
<span class="op-keyword">{{ op.keyword }}</span>
</button>
</div>
<div class="content-area">
<div class="example-section">
<div class="section-title">📝 示例 SQL</div>
<div class="code-block">
<pre><code>{{ currentOperation.example }}</code></pre>
</div>
</div>
<div class="explanation-section">
<div class="section-title">💡 逐词翻译</div>
<div class="explanation-list">
<div v-for="(item, i) in currentOperation.explanation" :key="i" class="explanation-item">
<span class="keyword">{{ item.keyword }}</span>
<span class="meaning">{{ item.meaning }}</span>
</div>
</div>
</div>
</div>
<div class="result-section">
<div class="section-title">📊 返回结果</div>
<div class="result-table">
<div class="table-header">
<div v-for="col in currentOperation.result.columns" :key="col" class="header-cell">
{{ col }}
</div>
</div>
<div class="table-body">
<div v-for="(row, i) in currentOperation.result.rows" :key="i" class="table-row">
<div v-for="(cell, j) in row" :key="j" class="table-cell">
{{ cell }}
</div>
</div>
</div>
</div>
</div>
</div>
<div class="right-panel">
<div class="panel-header">SQL 终端</div>
<div class="sql-terminal-body">
<div v-for="(line, i) in terminalOutput" :key="i" :class="line.type">
{{ line.text }}
</div>
<div class="cursor-line">_</div>
</div>
<div class="warning-box" v-if="currentOperation.warning">
<span class="icon"></span>
<span v-html="currentOperation.warning"></span>
</div>
<div class="actions">
<div class="action-label">常用指令:</div>
<div class="btn-group">
<button
v-for="cmd in commands"
:key="cmd.label"
@click="execute(cmd)"
:disabled="isAnimating"
class="cmd-btn"
>
{{ cmd.label }}
</button>
</div>
<div class="current-sql" v-if="currentSql">
执行中: <code>{{ currentSql }}</code>
</div>
</div>
<div class="info-box">
<span class="icon">🎯</span>
<strong>核心概念</strong>CRUD 涵盖了所有数据管理的基本需求无论是淘宝微信抖音它们的数据库操作本质上就是这四种
</div>
</div>
</template>
<script setup>
import { ref, computed } from 'vue'
const currentOp = ref('SELECT')
const operations = {
SELECT: {
key: 'SELECT',
name: '查询',
icon: '🔍',
keyword: 'SELECT ... FROM',
example: "SELECT name, age FROM users WHERE age > 25;",
explanation: [
{ keyword: 'SELECT name, age', meaning: '选择 name 和 age 这两列' },
{ keyword: 'FROM users', meaning: '从 users 这张表' },
{ keyword: 'WHERE age > 25', meaning: '在 age 大于 25 的条件下' }
],
result: {
columns: ['name', 'age'],
rows: [
['李四', 30],
['王五', 28]
]
}
},
INSERT: {
key: 'INSERT',
name: '插入',
icon: '',
keyword: 'INSERT INTO',
example: "INSERT INTO users (name, age, city) VALUES ('赵六', 35, '广州');",
explanation: [
{ keyword: 'INSERT INTO users', meaning: '插入到 users 表' },
{ keyword: '(name, age, city)', meaning: '这几列' },
{ keyword: "VALUES ('赵六', 35, '广州')", meaning: '值分别是...' }
],
result: {
columns: ['结果'],
rows: [['✅ 成功插入 1 行']]
},
warning: '<strong>注意:</strong>字符串要用单引号包围,数字不需要引号。'
},
UPDATE: {
key: 'UPDATE',
name: '更新',
icon: '✏️',
keyword: 'UPDATE ... SET',
example: "UPDATE users SET age = age + 1 WHERE city = '北京';",
explanation: [
{ keyword: 'UPDATE users', meaning: '更新 users 表' },
{ keyword: 'SET age = age + 1', meaning: '把 age 设为 age + 1' },
{ keyword: "WHERE city = '北京'", meaning: '只修改城市为北京的行' }
],
result: {
columns: ['结果'],
rows: [['✅ 成功更新 2 行']]
},
warning: '<strong>重要警告:</strong>如果忘记写 WHERE,会修改<strong>所有行</strong>!这是最危险的操作之一。'
},
DELETE: {
key: 'DELETE',
name: '删除',
icon: '🗑️',
keyword: 'DELETE FROM',
example: 'DELETE FROM users WHERE user_id = 4;',
explanation: [
{ keyword: 'DELETE FROM users', meaning: '从 users 表删除' },
{ keyword: 'WHERE user_id = 4', meaning: '只删除 user_id 为 4 的行' }
],
result: {
columns: ['结果'],
rows: [['✅ 成功删除 1 行']]
},
warning: '<strong>重要警告:</strong>和 UPDATE 一样,如果忘记写 WHERE,会删除<strong>整张表</strong>的所有数据!'
}
}
const currentOperation = computed(() => operations[currentOp.value])
</script>
<style scoped>
.sql-playground {
display: flex;
border: 1px solid #333;
.sql-playground-demo {
border: 1px solid var(--vp-c-divider);
border-radius: 8px;
background: #1e1e1e;
color: #ccc;
font-family: 'Consolas', monospace;
overflow: hidden;
min-height: 350px;
background: var(--vp-c-bg-soft);
padding: 1rem;
margin: 1rem 0;
}
.left-panel {
flex: 1;
border-right: 1px solid #444;
.demo-header {
display: flex;
align-items: center;
gap: 0.5rem;
margin-bottom: 0.75rem;
}
.demo-header .icon { font-size: 1.25rem; }
.demo-header .title { font-weight: bold; font-size: 1rem; }
.demo-header .subtitle { color: var(--vp-c-text-2); font-size: 0.85rem; margin-left: 0.5rem; }
.intro-text {
font-size: 0.9rem;
color: var(--vp-c-text-2);
line-height: 1.6;
margin-bottom: 1rem;
padding: 0.75rem;
background: var(--vp-c-bg);
border-radius: 6px;
}
.intro-text .highlight {
color: var(--vp-c-brand-1);
font-weight: 500;
}
.operation-selector {
display: grid;
grid-template-columns: repeat(4, 1fr);
gap: 0.5rem;
margin-bottom: 1rem;
}
@media (max-width: 640px) {
.operation-selector {
grid-template-columns: repeat(2, 1fr);
}
}
.op-btn {
display: flex;
flex-direction: column;
}
.right-panel {
flex: 1.2;
display: flex;
flex-direction: column;
background: #252526;
}
.panel-header {
background: #333;
padding: 8px 15px;
font-size: 12px;
font-weight: bold;
color: #fff;
border-bottom: 1px solid #444;
}
.table-container {
padding: 15px;
flex: 1;
}
table {
width: 100%;
border-collapse: collapse;
font-size: 13px;
}
th,
td {
border: 1px solid #444;
padding: 6px 10px;
text-align: left;
}
th {
color: #569cd6;
}
td {
color: #ce9178;
}
td:first-child {
color: #b5cea8;
} /* id color */
.sql-terminal-body {
flex: 1;
padding: 15px;
font-size: 13px;
overflow-y: auto;
max-height: 200px;
}
.cmd {
color: #fff;
margin-top: 5px;
}
.result {
color: #aaa;
margin-bottom: 5px;
}
.success {
color: #67c23a;
margin-bottom: 5px;
}
.cursor-line {
animation: blink 1s infinite;
}
.actions {
padding: 15px;
background: #2d2d2d;
border-top: 1px solid #444;
}
.action-label {
font-size: 12px;
margin-bottom: 8px;
color: #999;
}
.btn-group {
display: flex;
gap: 8px;
flex-wrap: wrap;
}
.cmd-btn {
background: #0e639c;
border: none;
color: white;
padding: 6px 12px;
border-radius: 3px;
align-items: center;
gap: 4px;
padding: 0.75rem 0.5rem;
background: var(--vp-c-bg);
border: 1px solid var(--vp-c-divider);
border-radius: 6px;
cursor: pointer;
font-size: 12px;
transition: background 0.2s;
}
.cmd-btn:hover {
background: #1177bb;
}
.cmd-btn:disabled {
opacity: 0.5;
cursor: not-allowed;
transition: all 0.2s;
}
.current-sql {
margin-top: 10px;
font-size: 12px;
color: #888;
}
.current-sql code {
color: #dcdcaa;
background: transparent;
.op-btn:hover {
background: var(--vp-c-bg-soft);
}
.fade-in {
animation: fadeIn 0.3s ease-in;
.op-btn.active {
background: var(--vp-c-brand-soft);
border-color: var(--vp-c-brand);
}
@keyframes fadeIn {
from {
opacity: 0;
transform: translateY(5px);
}
to {
opacity: 1;
transform: translateY(0);
.op-icon { font-size: 1.25rem; }
.op-name { font-size: 0.8rem; font-weight: 500; }
.op-keyword { font-size: 0.65rem; color: var(--vp-c-text-3); font-family: monospace; }
.content-area {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 1rem;
margin-bottom: 1rem;
}
@media (max-width: 768px) {
.content-area {
grid-template-columns: 1fr;
}
}
.score-up {
color: #67c23a;
font-weight: bold;
animation: pulse 0.5s;
.example-section, .explanation-section {
background: var(--vp-c-bg);
border-radius: 6px;
padding: 0.75rem;
border: 1px solid var(--vp-c-divider);
}
@keyframes pulse {
0% {
transform: scale(1);
}
50% {
transform: scale(1.4);
}
100% {
transform: scale(1);
}
.section-title {
font-size: 0.8rem;
font-weight: 600;
margin-bottom: 0.5rem;
color: var(--vp-c-text-1);
}
.empty-hint {
.code-block {
background: var(--vp-c-bg-soft);
border-radius: 4px;
padding: 0.75rem;
overflow-x: auto;
}
.code-block code {
font-family: var(--vp-font-family-mono);
font-size: 0.75rem;
color: var(--vp-c-brand-1);
line-height: 1.5;
}
.explanation-list {
display: flex;
flex-direction: column;
gap: 0.5rem;
}
.explanation-item {
display: flex;
align-items: baseline;
gap: 0.5rem;
font-size: 0.8rem;
}
.keyword {
font-family: var(--vp-font-family-mono);
color: var(--vp-c-brand-1);
font-weight: 500;
flex-shrink: 0;
}
.meaning {
color: var(--vp-c-text-2);
line-height: 1.4;
}
.result-section {
background: var(--vp-c-bg);
border-radius: 6px;
padding: 0.75rem;
border: 1px solid var(--vp-c-divider);
margin-bottom: 0.75rem;
}
.result-table {
border: 1px solid var(--vp-c-divider);
border-radius: 4px;
overflow: hidden;
}
.table-header {
display: grid;
background: var(--vp-c-bg-soft);
border-bottom: 1px solid var(--vp-c-divider);
}
.header-cell {
padding: 0.5rem 0.75rem;
font-size: 0.75rem;
font-weight: 600;
color: var(--vp-c-text-2);
text-align: center;
color: #666;
font-style: italic;
padding: 20px;
border: none;
}
.table-body {
display: flex;
flex-direction: column;
}
.table-row {
display: grid;
border-bottom: 1px solid var(--vp-c-divider);
}
.table-row:last-child {
border-bottom: none;
}
.table-cell {
padding: 0.5rem 0.75rem;
font-size: 0.8rem;
color: var(--vp-c-text-1);
text-align: center;
}
.warning-box {
background: rgba(239, 68, 68, 0.05);
border: 1px solid rgba(239, 68, 68, 0.2);
border-radius: 6px;
padding: 0.75rem;
margin-bottom: 0.75rem;
font-size: 0.85rem;
color: var(--vp-c-text-2);
line-height: 1.5;
}
.warning-box .icon {
margin-right: 0.25rem;
}
.info-box {
background: var(--vp-c-bg-alt);
padding: 0.75rem;
border-radius: 6px;
font-size: 0.85rem;
color: var(--vp-c-text-2);
line-height: 1.5;
}
.info-box .icon { margin-right: 0.25rem; }
</style>
@@ -1,50 +1,318 @@
<template>
<div class="demo-container">
<div class="acid-demo">
<div class="demo-header">
<h4>{{ title }}</h4>
<p class="hint">{{ description }}</p>
<span class="icon">🔒</span>
<span class="title">事务 ACID 特性演示</span>
<span class="subtitle">理解事务如何保证数据安全</span>
</div>
<div class="demo-content">
<el-alert type="info" :closable="false">
事务ACID特性演示组件占位符 - 待实现具体交互
</el-alert>
<div class="intro-text">
想象<span class="highlight">银行转账</span>A 转给 B 100 这个操作包含两步 A 100 B 100如果只扣了钱但没到账就是灾难事务保证这两步<span class="highlight">要么全成功要么全失败</span>
</div>
<div class="acid-cards">
<div
v-for="item in acidItems"
:key="item.key"
class="acid-card"
:class="{ active: activeItem === item.key }"
@click="activeItem = activeItem === item.key ? null : item.key"
>
<div class="card-icon">{{ item.icon }}</div>
<div class="card-letter">{{ item.letter }}</div>
<div class="card-name">{{ item.name }}</div>
<div class="card-meaning">{{ item.meaning }}</div>
</div>
</div>
<Transition name="fade">
<div v-if="activeItem" class="detail-panel">
<div class="detail-header">
<span class="detail-icon">{{ currentItem?.icon }}</span>
<span class="detail-title">{{ currentItem?.name }} ({{ currentItem?.letter }})</span>
</div>
<div class="detail-content">
<div class="explanation">
<strong>含义</strong>{{ currentItem?.explanation }}
</div>
<div class="example">
<div class="example-label">🌰 银行转账例子</div>
<div class="example-text">{{ currentItem?.example }}</div>
</div>
</div>
</div>
</Transition>
<div v-if="!activeItem" class="hint-text">
👆 点击上方任意特性查看详细解释
</div>
<div class="scenario-box">
<div class="scenario-title">🎯 12306 抢票场景</div>
<div class="scenario-content">
<p><strong>场景</strong>用户 A B 同时看到还剩 1 张票同时点击购买</p>
<p><strong>没有事务</strong>A 扣库存B 也扣库存同一张票卖给了两个人</p>
<p><strong>有事务隔离性</strong>A 的操作加锁B 必须等待A 买完后库存变为 0B 看到的是"已售罄"</p>
</div>
</div>
<div class="info-box">
<span class="icon">💡</span>
<strong>核心思想</strong>ACID 四个特性共同保证了数据在高并发环境下的<span class="highlight">不丢不乱不冲突</span>这就是为什么所有涉及资金订单的系统都必须使用数据库事务
</div>
</div>
</template>
<script setup>
import { ref } from 'vue'
import { ref, computed } from 'vue'
const title = ref('事务ACID特性演示')
const description = ref('通过可视化方式演示事务的原子性、一致性、隔离性和持久性')
const activeItem = ref(null)
const acidItems = ref([
{
key: 'atomicity',
letter: 'A',
icon: '⚛️',
name: '原子性',
meaning: 'Atomicity',
explanation: '事务中的操作要么全部成功,要么全部失败,不会出现"做了一半"的情况。',
example: '转账时,扣款和入账必须同时成功。如果扣款成功但入账失败,系统会自动回滚,把钱退回去。'
},
{
key: 'consistency',
letter: 'C',
icon: '⚖️',
name: '一致性',
meaning: 'Consistency',
explanation: '事务执行前后,数据都必须处于合法状态,满足所有约束条件。',
example: '转账前后,A 和 B 的余额总和必须不变。票卖完了,库存必须是 0,不能是负数。'
},
{
key: 'isolation',
letter: 'I',
icon: '🔒',
name: '隔离性',
meaning: 'Isolation',
explanation: '多个事务同时执行时,互不干扰,每个事务都感觉不到其他事务的存在。',
example: 'A 在买票时,B 看到的结果应该是"已售罄"或"还剩 1 张",不会看到 A 买了一半的中间状态(比如库存变成了 0.5)。'
},
{
key: 'durability',
letter: 'D',
icon: '💾',
name: '持久性',
meaning: 'Durability',
explanation: '事务一旦提交,结果就会永久保存,即使断电、宕机也不会丢失。',
example: '订单成功后,即使服务器立刻断电,已售出的票记录也不会消失。重启服务器后,数据依然在。'
}
])
const currentItem = computed(() => {
return acidItems.value.find(item => item.key === activeItem.value)
})
</script>
<style scoped>
.demo-container {
.acid-demo {
border: 1px solid var(--vp-c-divider);
border-radius: 8px;
padding: 20px;
background: var(--vp-c-bg-soft);
padding: 1rem;
margin: 1rem 0;
}
.demo-header {
margin-bottom: 20px;
display: flex;
align-items: center;
gap: 0.5rem;
margin-bottom: 0.75rem;
}
.demo-header h4 {
margin: 0 0 8px 0;
.demo-header .icon { font-size: 1.25rem; }
.demo-header .title { font-weight: bold; font-size: 1rem; }
.demo-header .subtitle { color: var(--vp-c-text-2); font-size: 0.85rem; margin-left: 0.5rem; }
.intro-text {
font-size: 0.9rem;
color: var(--vp-c-text-2);
line-height: 1.6;
margin-bottom: 1rem;
padding: 0.75rem;
background: var(--vp-c-bg);
border-radius: 6px;
}
.intro-text .highlight {
color: var(--vp-c-brand-1);
font-weight: 500;
}
.acid-cards {
display: grid;
grid-template-columns: repeat(4, 1fr);
gap: 0.75rem;
margin-bottom: 1rem;
}
@media (max-width: 768px) {
.acid-cards {
grid-template-columns: repeat(2, 1fr);
}
}
.acid-card {
background: var(--vp-c-bg);
border: 1px solid var(--vp-c-divider);
border-radius: 8px;
padding: 0.75rem;
text-align: center;
cursor: pointer;
transition: all 0.2s;
}
.acid-card:hover {
background: var(--vp-c-bg-soft);
}
.acid-card.active {
background: var(--vp-c-brand-soft);
border-color: var(--vp-c-brand);
}
.card-icon {
font-size: 1.5rem;
margin-bottom: 0.25rem;
}
.card-letter {
font-size: 1.25rem;
font-weight: bold;
color: var(--vp-c-brand-1);
margin-bottom: 0.25rem;
}
.card-name {
font-size: 0.85rem;
font-weight: 600;
margin-bottom: 0.25rem;
}
.card-meaning {
font-size: 0.7rem;
color: var(--vp-c-text-3);
font-family: monospace;
}
.detail-panel {
background: var(--vp-c-bg);
border-radius: 8px;
padding: 1rem;
margin-bottom: 0.75rem;
border: 1px solid var(--vp-c-divider);
}
.detail-header {
display: flex;
align-items: center;
gap: 0.5rem;
margin-bottom: 0.75rem;
}
.detail-icon {
font-size: 1.5rem;
}
.detail-title {
font-weight: 600;
font-size: 1rem;
color: var(--vp-c-text-1);
}
.hint {
margin: 0;
font-size: 14px;
color: var(--vp-c-text-2);
}
.demo-content {
.detail-content {
display: flex;
flex-direction: column;
gap: 16px;
gap: 0.75rem;
}
.explanation {
font-size: 0.85rem;
color: var(--vp-c-text-2);
line-height: 1.5;
}
.example {
background: var(--vp-c-bg-soft);
padding: 0.75rem;
border-radius: 6px;
border-left: 3px solid var(--vp-c-brand);
}
.example-label {
font-size: 0.8rem;
font-weight: 500;
color: var(--vp-c-text-2);
margin-bottom: 0.5rem;
}
.example-text {
font-size: 0.85rem;
color: var(--vp-c-text-1);
line-height: 1.5;
}
.hint-text {
text-align: center;
font-size: 0.85rem;
color: var(--vp-c-text-3);
margin-bottom: 0.75rem;
}
.scenario-box {
background: rgba(34, 197, 94, 0.05);
border: 1px solid rgba(34, 197, 94, 0.2);
border-radius: 6px;
padding: 0.75rem;
margin-bottom: 0.75rem;
}
.scenario-title {
font-size: 0.85rem;
font-weight: 600;
margin-bottom: 0.5rem;
color: var(--vp-c-text-1);
}
.scenario-content p {
font-size: 0.8rem;
color: var(--vp-c-text-2);
margin: 0.25rem 0;
line-height: 1.5;
}
.fade-enter-active,
.fade-leave-active {
transition: opacity 0.2s ease, transform 0.2s ease;
}
.fade-enter-from,
.fade-leave-to {
opacity: 0;
transform: translateY(-10px);
}
.info-box {
background: var(--vp-c-bg-alt);
padding: 0.75rem;
border-radius: 6px;
font-size: 0.85rem;
color: var(--vp-c-text-2);
line-height: 1.5;
}
.info-box .icon { margin-right: 0.25rem; }
.info-box .highlight {
color: var(--vp-c-brand-1);
font-weight: 500;
}
</style>