[{"data":1,"prerenderedAt":5026},["ShallowReactive",2],{"content-/topics/engineering/database-index-query-optimization-guide":3},{"_path":4,"_dir":5,"_draft":6,"_partial":6,"_locale":7,"title":8,"description":9,"date":10,"category":5,"tags":11,"author":17,"featured":18,"series":19,"seriesOrder":20,"readingTime":21,"image":22,"body":23,"_type":5020,"_id":5021,"_source":5022,"_file":5023,"_stem":5024,"_extension":5025},"/topics/engineering/database-index-query-optimization-guide","engineering",false,"","数据库索引与查询优化完全指南：从慢 SQL 到稳定 100ms 内响应","索引不是背概念，而是用来换时间和稳定性的工程武器。本文从真实业务场景出发，讲清慢 SQL 的定位方法、联合索引设计、分页优化、N+1 查询治理、EXPLAIN 解读，以及什么时候不该加索引。","2026-03-07",[12,13,14,15,16],"数据库索引","查询优化","MySQL","SQL 性能","后端工程","小明",true,"large-scale-application-architecture",3,18,"/images/articles/database-index-query-optimization-guide-cover.jpg",{"type":24,"children":25,"toc":4931},"root",[26,34,40,45,50,55,60,90,101,106,142,147,156,161,198,203,207,214,219,242,252,259,266,271,277,282,288,293,299,304,420,425,433,436,442,448,453,458,1071,1076,1094,1100,1105,1111,1116,1122,1127,1133,1138,1144,1149,1179,1184,1197,1207,1210,1216,1221,1226,1232,1237,1260,1265,1283,1295,1301,1306,1311,1344,1349,1352,1366,1378,1383,1389,1454,1459,1487,1493,1550,1555,1722,1726,1756,1761,1779,1792,1803,1808,1823,1828,1851,1854,1860,1866,1871,1902,1907,1912,1918,1923,1946,1951,2008,2013,2036,2041,2077,2083,2088,2096,2101,2115,2120,2150,2155,2169,2181,2187,2192,2203,2208,2231,2243,2246,2252,2258,2263,2306,2311,2317,2367,2372,2395,2400,2410,2428,2441,2446,2636,2641,2647,3058,3071,3085,3096,3101,3107,3112,3151,3156,3180,3185,3231,3235,3258,3270,3273,3279,3285,3290,3303,3308,3313,3319,3324,3376,3381,3394,3407,3412,3435,3440,3458,3463,3481,3487,3492,3515,3520,3543,3546,3552,3557,3571,3591,3596,3647,3652,3665,3678,3684,3689,3694,3712,3717,3722,3728,3732,3746,3751,3780,3799,3809,3812,3818,3824,3829,3834,3857,3862,3885,3891,3896,3919,3924,3930,3935,3953,3958,3961,3967,3972,3980,3992,3998,4016,4022,4040,4046,4051,4064,4069,4072,4078,4083,4089,4112,4118,4123,4128,4134,4157,4163,4194,4200,4205,4238,4244,4249,4366,4372,4377,4400,4405,4408,4414,4420,4425,4448,4454,4459,4482,4487,4493,4498,4503,4526,4531,4534,4540,4545,4550,4558,4563,4586,4591,4614,4619,4622,4628,4633,4638,4691,4696,4736,4741,4781,4786,4826,4829,4834,4839,4899,4904,4912,4917,4925],{"type":27,"tag":28,"props":29,"children":31},"element","h1",{"id":30},"数据库索引与查询优化完全指南从慢-sql-到稳定-100ms-内响应",[32],{"type":33,"value":8},"text",{"type":27,"tag":35,"props":36,"children":37},"p",{},[38],{"type":33,"value":39},"凌晨 1 点，报警把人叫醒。",{"type":27,"tag":35,"props":41,"children":42},{},[43],{"type":33,"value":44},"不是服务挂了，也不是机房断电，而是一个看起来非常“普通”的列表页把数据库拖到了 95% CPU。",{"type":27,"tag":35,"props":46,"children":47},{},[48],{"type":33,"value":49},"那条 SQL 甚至不复杂：查订单、按时间排序、带一个状态筛选，再翻到第 201 页。",{"type":27,"tag":35,"props":51,"children":52},{},[53],{"type":33,"value":54},"产品同学会觉得：这不就“查个列表”吗？",{"type":27,"tag":35,"props":56,"children":57},{},[58],{"type":33,"value":59},"工程师真正头疼的是另一层现实：",{"type":27,"tag":61,"props":62,"children":63},"ul",{},[64,70,75,80,85],{"type":27,"tag":65,"props":66,"children":67},"li",{},[68],{"type":33,"value":69},"这条查询 3 个月前还很快",{"type":27,"tag":65,"props":71,"children":72},{},[73],{"type":33,"value":74},"表里数据从 80 万涨到了 2800 万",{"type":27,"tag":65,"props":76,"children":77},{},[78],{"type":33,"value":79},"新加的筛选条件让原索引失效",{"type":27,"tag":65,"props":81,"children":82},{},[83],{"type":33,"value":84},"高峰期每秒会打进来几百次",{"type":27,"tag":65,"props":86,"children":87},{},[88],{"type":33,"value":89},"你今天修不好，明天客服就要在群里发红色截图",{"type":27,"tag":35,"props":91,"children":92},{},[93,95],{"type":33,"value":94},"数据库性能问题最讨厌的地方在于：",{"type":27,"tag":96,"props":97,"children":98},"strong",{},[99],{"type":33,"value":100},"它一开始不痛，痛起来像牙神经。",{"type":27,"tag":35,"props":102,"children":103},{},[104],{"type":33,"value":105},"很多人学索引时，只记住了几个口号：",{"type":27,"tag":61,"props":107,"children":108},{},[109,114,119,131],{"type":27,"tag":65,"props":110,"children":111},{},[112],{"type":33,"value":113},"最左前缀原则",{"type":27,"tag":65,"props":115,"children":116},{},[117],{"type":33,"value":118},"不要在索引列上做函数",{"type":27,"tag":65,"props":120,"children":121},{},[122,124],{"type":33,"value":123},"避免 ",{"type":27,"tag":125,"props":126,"children":128},"code",{"className":127},[],[129],{"type":33,"value":130},"select *",{"type":27,"tag":65,"props":132,"children":133},{},[134,136],{"type":33,"value":135},"分页要小心 ",{"type":27,"tag":125,"props":137,"children":139},{"className":138},[],[140],{"type":33,"value":141},"offset",{"type":27,"tag":35,"props":143,"children":144},{},[145],{"type":33,"value":146},"这些都对，但只背口号，到了线上还是会翻车。因为真正的问题不是“某条规则有没有背下来”，而是：",{"type":27,"tag":148,"props":149,"children":150},"blockquote",{},[151],{"type":27,"tag":35,"props":152,"children":153},{},[154],{"type":33,"value":155},"你能不能把“为什么这条 SQL 慢”拆解成一组可验证的工程判断？",{"type":27,"tag":35,"props":157,"children":158},{},[159],{"type":33,"value":160},"这篇文章不准备再讲一遍教科书目录，而是直接围绕真实业务，把查询优化拆成一套能落地的方法：",{"type":27,"tag":162,"props":163,"children":164},"ol",{},[165,170,175,180,193],{"type":27,"tag":65,"props":166,"children":167},{},[168],{"type":33,"value":169},"怎样判断瓶颈到底在 SQL、索引、数据分布还是调用方式",{"type":27,"tag":65,"props":171,"children":172},{},[173],{"type":33,"value":174},"怎样设计真正有用的联合索引，而不是“看到慢就加一个”",{"type":27,"tag":65,"props":176,"children":177},{},[178],{"type":33,"value":179},"怎样处理深分页、范围查询、排序、覆盖索引、回表这些高频痛点",{"type":27,"tag":65,"props":181,"children":182},{},[183,185,191],{"type":33,"value":184},"怎样治理 ",{"type":27,"tag":125,"props":186,"children":188},{"className":187},[],[189],{"type":33,"value":190},"N+1",{"type":33,"value":192},"、宽表、冷热数据混放这些系统性问题",{"type":27,"tag":65,"props":194,"children":195},{},[196],{"type":33,"value":197},"什么时候不要优化，什么时候该升维到缓存、归档或拆分",{"type":27,"tag":35,"props":199,"children":200},{},[201],{"type":33,"value":202},"如果你已经被慢查询折磨过，这篇会比“索引是什么”更接近你要的答案。",{"type":27,"tag":204,"props":205,"children":206},"hr",{},[],{"type":27,"tag":208,"props":209,"children":211},"h2",{"id":210},"一先统一认知慢查询问题80-不是数据库不行",[212],{"type":33,"value":213},"一、先统一认知：慢查询问题，80% 不是“数据库不行”",{"type":27,"tag":35,"props":215,"children":216},{},[217],{"type":33,"value":218},"一个请求慢，很多团队的第一反应是：",{"type":27,"tag":61,"props":220,"children":221},{},[222,227,232,237],{"type":27,"tag":65,"props":223,"children":224},{},[225],{"type":33,"value":226},"数据库要扩容了",{"type":27,"tag":65,"props":228,"children":229},{},[230],{"type":33,"value":231},"Redis 没配好",{"type":27,"tag":65,"props":233,"children":234},{},[235],{"type":33,"value":236},"ORM 太烂",{"type":27,"tag":65,"props":238,"children":239},{},[240],{"type":33,"value":241},"机器不够",{"type":27,"tag":35,"props":243,"children":244},{},[245,247],{"type":33,"value":246},"这些都可能是原因，但在线上更常见的真相是：",{"type":27,"tag":96,"props":248,"children":249},{},[250],{"type":33,"value":251},"查询方式和数据分布变了，而代码还在假装世界没变。",{"type":27,"tag":253,"props":254,"children":256},"h3",{"id":255},"_11-典型的三个误判",[257],{"type":33,"value":258},"1.1 典型的三个误判",{"type":27,"tag":260,"props":261,"children":263},"h4",{"id":262},"误判-1只看平均值不看尾延迟",[264],{"type":33,"value":265},"误判 1：只看平均值，不看尾延迟",{"type":27,"tag":35,"props":267,"children":268},{},[269],{"type":33,"value":270},"平均 80ms，看起来不慢；但 P95 可能 900ms，P99 可能 4s。真正把用户体验打穿的，往往是尾部。",{"type":27,"tag":260,"props":272,"children":274},{"id":273},"误判-2只看单条-sql不看调用次数",[275],{"type":33,"value":276},"误判 2：只看单条 SQL，不看调用次数",{"type":27,"tag":35,"props":278,"children":279},{},[280],{"type":33,"value":281},"单次 20ms 的查询看起来正常，但一个接口里调了 40 次，照样把接口拖成 800ms。",{"type":27,"tag":260,"props":283,"children":285},{"id":284},"误判-3只看执行计划不看业务形态",[286],{"type":33,"value":287},"误判 3：只看执行计划，不看业务形态",{"type":27,"tag":35,"props":289,"children":290},{},[291],{"type":33,"value":292},"同样一条 SQL，在测试环境 10 万行数据下很快，在生产 3000 万行、分布极度倾斜时就完全不是一回事。",{"type":27,"tag":253,"props":294,"children":296},{"id":295},"_12-优化之前先回答这四个问题",[297],{"type":33,"value":298},"1.2 优化之前，先回答这四个问题",{"type":27,"tag":35,"props":300,"children":301},{},[302],{"type":33,"value":303},"每次开始查慢 SQL，我都会先拉一张最小诊断卡：",{"type":27,"tag":305,"props":306,"children":307},"table",{},[308,332],{"type":27,"tag":309,"props":310,"children":311},"thead",{},[312],{"type":27,"tag":313,"props":314,"children":315},"tr",{},[316,322,327],{"type":27,"tag":317,"props":318,"children":319},"th",{},[320],{"type":33,"value":321},"问题",{"type":27,"tag":317,"props":323,"children":324},{},[325],{"type":33,"value":326},"为什么重要",{"type":27,"tag":317,"props":328,"children":329},{},[330],{"type":33,"value":331},"例子",{"type":27,"tag":333,"props":334,"children":335},"tbody",{},[336,355,373,396],{"type":27,"tag":313,"props":337,"children":338},{},[339,345,350],{"type":27,"tag":340,"props":341,"children":342},"td",{},[343],{"type":33,"value":344},"慢的是哪一段？",{"type":27,"tag":340,"props":346,"children":347},{},[348],{"type":33,"value":349},"避免把网络、序列化、锁等待误判成 SQL",{"type":27,"tag":340,"props":351,"children":352},{},[353],{"type":33,"value":354},"接口 2s，其中 SQL 只占 180ms",{"type":27,"tag":313,"props":356,"children":357},{},[358,363,368],{"type":27,"tag":340,"props":359,"children":360},{},[361],{"type":33,"value":362},"是偶发还是稳定复现？",{"type":27,"tag":340,"props":364,"children":365},{},[366],{"type":33,"value":367},"决定是抖动问题还是结构问题",{"type":27,"tag":340,"props":369,"children":370},{},[371],{"type":33,"value":372},"只有大促期间慢，说明跟热点数据有关",{"type":27,"tag":313,"props":374,"children":375},{},[376,381,386],{"type":27,"tag":340,"props":377,"children":378},{},[379],{"type":33,"value":380},"是一条 SQL 慢，还是很多条叠加？",{"type":27,"tag":340,"props":382,"children":383},{},[384],{"type":33,"value":385},"决定先优化语句还是先减次数",{"type":27,"tag":340,"props":387,"children":388},{},[389,394],{"type":27,"tag":125,"props":390,"children":392},{"className":391},[],[393],{"type":33,"value":190},{"type":33,"value":395}," 往往比单条慢 SQL 更致命",{"type":27,"tag":313,"props":397,"children":398},{},[399,404,409],{"type":27,"tag":340,"props":400,"children":401},{},[402],{"type":33,"value":403},"数据规模和分布是什么样？",{"type":27,"tag":340,"props":405,"children":406},{},[407],{"type":33,"value":408},"索引是否有效取决于选择性",{"type":27,"tag":340,"props":410,"children":411},{},[412,418],{"type":27,"tag":125,"props":413,"children":415},{"className":414},[],[416],{"type":33,"value":417},"status = 1",{"type":33,"value":419}," 命中 95% 数据时几乎没筛选力",{"type":27,"tag":35,"props":421,"children":422},{},[423],{"type":33,"value":424},"如果这四个问题没答清楚，你很可能会开始一种经典无效劳动：",{"type":27,"tag":148,"props":426,"children":427},{},[428],{"type":27,"tag":35,"props":429,"children":430},{},[431],{"type":33,"value":432},"一边盲目加索引，一边祈祷数据库自己变快。",{"type":27,"tag":204,"props":434,"children":435},{},[],{"type":27,"tag":208,"props":437,"children":439},{"id":438},"二定位慢-sql不要上来就改先确认是哪个环节在耗时",[440],{"type":33,"value":441},"二、定位慢 SQL：不要上来就改，先确认是哪个环节在耗时",{"type":27,"tag":253,"props":443,"children":445},{"id":444},"_21-建立请求-sql链路而不是只盯数据库面板",[446],{"type":33,"value":447},"2.1 建立“请求 → SQL”链路，而不是只盯数据库面板",{"type":27,"tag":35,"props":449,"children":450},{},[451],{"type":33,"value":452},"很多团队有数据库监控，但没有“某个接口触发了哪些 SQL”的串联视角。这会导致一个问题：你看到了慢 SQL，却不知道是哪个功能打出来的。",{"type":27,"tag":35,"props":454,"children":455},{},[456],{"type":33,"value":457},"一个更实用的做法是：在应用层统一打点，把请求 ID、接口名、SQL 模板、耗时、返回行数串起来。",{"type":27,"tag":459,"props":460,"children":464},"pre",{"className":461,"code":462,"language":463,"meta":7,"style":7},"language-ts shiki shiki-themes github-dark","// Node.js 示例：在数据库访问层统一记录 SQL 指标\nexport async function timedQuery\u003CT>(\n  name: string,\n  sql: string,\n  params: unknown[],\n  requestId: string,\n): Promise\u003CT[]> {\n  const startedAt = Date.now()\n\n  try {\n    const rows = await db.query\u003CT>(sql, params)\n\n    logger.info('db.query', {\n      requestId,\n      name,\n      sqlTemplate: sql,\n      elapsedMs: Date.now() - startedAt,\n      rowCount: rows.length,\n    })\n\n    return rows\n  } catch (error) {\n    logger.error('db.query.failed', {\n      requestId,\n      name,\n      sqlTemplate: sql,\n      elapsedMs: Date.now() - startedAt,\n      error: error instanceof Error ? error.message : String(error),\n    })\n\n    throw error\n  }\n}\n","ts",[465],{"type":27,"tag":125,"props":466,"children":467},{"__ignoreMap":7},[468,480,522,547,568,591,612,643,677,686,700,746,754,784,793,802,811,839,856,865,873,887,906,932,940,948,956,980,1023,1031,1039,1053,1062],{"type":27,"tag":469,"props":470,"children":473},"span",{"class":471,"line":472},"line",1,[474],{"type":27,"tag":469,"props":475,"children":477},{"style":476},"--shiki-default:#6A737D",[478],{"type":33,"value":479},"// Node.js 示例：在数据库访问层统一记录 SQL 指标\n",{"type":27,"tag":469,"props":481,"children":483},{"class":471,"line":482},2,[484,490,495,500,506,512,517],{"type":27,"tag":469,"props":485,"children":487},{"style":486},"--shiki-default:#F97583",[488],{"type":33,"value":489},"export",{"type":27,"tag":469,"props":491,"children":492},{"style":486},[493],{"type":33,"value":494}," async",{"type":27,"tag":469,"props":496,"children":497},{"style":486},[498],{"type":33,"value":499}," function",{"type":27,"tag":469,"props":501,"children":503},{"style":502},"--shiki-default:#B392F0",[504],{"type":33,"value":505}," timedQuery",{"type":27,"tag":469,"props":507,"children":509},{"style":508},"--shiki-default:#E1E4E8",[510],{"type":33,"value":511},"\u003C",{"type":27,"tag":469,"props":513,"children":514},{"style":502},[515],{"type":33,"value":516},"T",{"type":27,"tag":469,"props":518,"children":519},{"style":508},[520],{"type":33,"value":521},">(\n",{"type":27,"tag":469,"props":523,"children":524},{"class":471,"line":20},[525,531,536,542],{"type":27,"tag":469,"props":526,"children":528},{"style":527},"--shiki-default:#FFAB70",[529],{"type":33,"value":530},"  name",{"type":27,"tag":469,"props":532,"children":533},{"style":486},[534],{"type":33,"value":535},":",{"type":27,"tag":469,"props":537,"children":539},{"style":538},"--shiki-default:#79B8FF",[540],{"type":33,"value":541}," string",{"type":27,"tag":469,"props":543,"children":544},{"style":508},[545],{"type":33,"value":546},",\n",{"type":27,"tag":469,"props":548,"children":550},{"class":471,"line":549},4,[551,556,560,564],{"type":27,"tag":469,"props":552,"children":553},{"style":527},[554],{"type":33,"value":555},"  sql",{"type":27,"tag":469,"props":557,"children":558},{"style":486},[559],{"type":33,"value":535},{"type":27,"tag":469,"props":561,"children":562},{"style":538},[563],{"type":33,"value":541},{"type":27,"tag":469,"props":565,"children":566},{"style":508},[567],{"type":33,"value":546},{"type":27,"tag":469,"props":569,"children":571},{"class":471,"line":570},5,[572,577,581,586],{"type":27,"tag":469,"props":573,"children":574},{"style":527},[575],{"type":33,"value":576},"  params",{"type":27,"tag":469,"props":578,"children":579},{"style":486},[580],{"type":33,"value":535},{"type":27,"tag":469,"props":582,"children":583},{"style":538},[584],{"type":33,"value":585}," unknown",{"type":27,"tag":469,"props":587,"children":588},{"style":508},[589],{"type":33,"value":590},"[],\n",{"type":27,"tag":469,"props":592,"children":594},{"class":471,"line":593},6,[595,600,604,608],{"type":27,"tag":469,"props":596,"children":597},{"style":527},[598],{"type":33,"value":599},"  requestId",{"type":27,"tag":469,"props":601,"children":602},{"style":486},[603],{"type":33,"value":535},{"type":27,"tag":469,"props":605,"children":606},{"style":538},[607],{"type":33,"value":541},{"type":27,"tag":469,"props":609,"children":610},{"style":508},[611],{"type":33,"value":546},{"type":27,"tag":469,"props":613,"children":615},{"class":471,"line":614},7,[616,621,625,630,634,638],{"type":27,"tag":469,"props":617,"children":618},{"style":508},[619],{"type":33,"value":620},")",{"type":27,"tag":469,"props":622,"children":623},{"style":486},[624],{"type":33,"value":535},{"type":27,"tag":469,"props":626,"children":627},{"style":502},[628],{"type":33,"value":629}," Promise",{"type":27,"tag":469,"props":631,"children":632},{"style":508},[633],{"type":33,"value":511},{"type":27,"tag":469,"props":635,"children":636},{"style":502},[637],{"type":33,"value":516},{"type":27,"tag":469,"props":639,"children":640},{"style":508},[641],{"type":33,"value":642},"[]> {\n",{"type":27,"tag":469,"props":644,"children":646},{"class":471,"line":645},8,[647,652,657,662,667,672],{"type":27,"tag":469,"props":648,"children":649},{"style":486},[650],{"type":33,"value":651},"  const",{"type":27,"tag":469,"props":653,"children":654},{"style":538},[655],{"type":33,"value":656}," startedAt",{"type":27,"tag":469,"props":658,"children":659},{"style":486},[660],{"type":33,"value":661}," =",{"type":27,"tag":469,"props":663,"children":664},{"style":508},[665],{"type":33,"value":666}," Date.",{"type":27,"tag":469,"props":668,"children":669},{"style":502},[670],{"type":33,"value":671},"now",{"type":27,"tag":469,"props":673,"children":674},{"style":508},[675],{"type":33,"value":676},"()\n",{"type":27,"tag":469,"props":678,"children":680},{"class":471,"line":679},9,[681],{"type":27,"tag":469,"props":682,"children":683},{"emptyLinePlaceholder":18},[684],{"type":33,"value":685},"\n",{"type":27,"tag":469,"props":687,"children":689},{"class":471,"line":688},10,[690,695],{"type":27,"tag":469,"props":691,"children":692},{"style":486},[693],{"type":33,"value":694},"  try",{"type":27,"tag":469,"props":696,"children":697},{"style":508},[698],{"type":33,"value":699}," {\n",{"type":27,"tag":469,"props":701,"children":703},{"class":471,"line":702},11,[704,709,714,718,723,728,733,737,741],{"type":27,"tag":469,"props":705,"children":706},{"style":486},[707],{"type":33,"value":708},"    const",{"type":27,"tag":469,"props":710,"children":711},{"style":538},[712],{"type":33,"value":713}," rows",{"type":27,"tag":469,"props":715,"children":716},{"style":486},[717],{"type":33,"value":661},{"type":27,"tag":469,"props":719,"children":720},{"style":486},[721],{"type":33,"value":722}," await",{"type":27,"tag":469,"props":724,"children":725},{"style":508},[726],{"type":33,"value":727}," db.",{"type":27,"tag":469,"props":729,"children":730},{"style":502},[731],{"type":33,"value":732},"query",{"type":27,"tag":469,"props":734,"children":735},{"style":508},[736],{"type":33,"value":511},{"type":27,"tag":469,"props":738,"children":739},{"style":502},[740],{"type":33,"value":516},{"type":27,"tag":469,"props":742,"children":743},{"style":508},[744],{"type":33,"value":745},">(sql, params)\n",{"type":27,"tag":469,"props":747,"children":749},{"class":471,"line":748},12,[750],{"type":27,"tag":469,"props":751,"children":752},{"emptyLinePlaceholder":18},[753],{"type":33,"value":685},{"type":27,"tag":469,"props":755,"children":757},{"class":471,"line":756},13,[758,763,768,773,779],{"type":27,"tag":469,"props":759,"children":760},{"style":508},[761],{"type":33,"value":762},"    logger.",{"type":27,"tag":469,"props":764,"children":765},{"style":502},[766],{"type":33,"value":767},"info",{"type":27,"tag":469,"props":769,"children":770},{"style":508},[771],{"type":33,"value":772},"(",{"type":27,"tag":469,"props":774,"children":776},{"style":775},"--shiki-default:#9ECBFF",[777],{"type":33,"value":778},"'db.query'",{"type":27,"tag":469,"props":780,"children":781},{"style":508},[782],{"type":33,"value":783},", {\n",{"type":27,"tag":469,"props":785,"children":787},{"class":471,"line":786},14,[788],{"type":27,"tag":469,"props":789,"children":790},{"style":508},[791],{"type":33,"value":792},"      requestId,\n",{"type":27,"tag":469,"props":794,"children":796},{"class":471,"line":795},15,[797],{"type":27,"tag":469,"props":798,"children":799},{"style":508},[800],{"type":33,"value":801},"      name,\n",{"type":27,"tag":469,"props":803,"children":805},{"class":471,"line":804},16,[806],{"type":27,"tag":469,"props":807,"children":808},{"style":508},[809],{"type":33,"value":810},"      sqlTemplate: sql,\n",{"type":27,"tag":469,"props":812,"children":814},{"class":471,"line":813},17,[815,820,824,829,834],{"type":27,"tag":469,"props":816,"children":817},{"style":508},[818],{"type":33,"value":819},"      elapsedMs: Date.",{"type":27,"tag":469,"props":821,"children":822},{"style":502},[823],{"type":33,"value":671},{"type":27,"tag":469,"props":825,"children":826},{"style":508},[827],{"type":33,"value":828},"() ",{"type":27,"tag":469,"props":830,"children":831},{"style":486},[832],{"type":33,"value":833},"-",{"type":27,"tag":469,"props":835,"children":836},{"style":508},[837],{"type":33,"value":838}," startedAt,\n",{"type":27,"tag":469,"props":840,"children":841},{"class":471,"line":21},[842,847,852],{"type":27,"tag":469,"props":843,"children":844},{"style":508},[845],{"type":33,"value":846},"      rowCount: rows.",{"type":27,"tag":469,"props":848,"children":849},{"style":538},[850],{"type":33,"value":851},"length",{"type":27,"tag":469,"props":853,"children":854},{"style":508},[855],{"type":33,"value":546},{"type":27,"tag":469,"props":857,"children":859},{"class":471,"line":858},19,[860],{"type":27,"tag":469,"props":861,"children":862},{"style":508},[863],{"type":33,"value":864},"    })\n",{"type":27,"tag":469,"props":866,"children":868},{"class":471,"line":867},20,[869],{"type":27,"tag":469,"props":870,"children":871},{"emptyLinePlaceholder":18},[872],{"type":33,"value":685},{"type":27,"tag":469,"props":874,"children":876},{"class":471,"line":875},21,[877,882],{"type":27,"tag":469,"props":878,"children":879},{"style":486},[880],{"type":33,"value":881},"    return",{"type":27,"tag":469,"props":883,"children":884},{"style":508},[885],{"type":33,"value":886}," rows\n",{"type":27,"tag":469,"props":888,"children":890},{"class":471,"line":889},22,[891,896,901],{"type":27,"tag":469,"props":892,"children":893},{"style":508},[894],{"type":33,"value":895},"  } ",{"type":27,"tag":469,"props":897,"children":898},{"style":486},[899],{"type":33,"value":900},"catch",{"type":27,"tag":469,"props":902,"children":903},{"style":508},[904],{"type":33,"value":905}," (error) {\n",{"type":27,"tag":469,"props":907,"children":909},{"class":471,"line":908},23,[910,914,919,923,928],{"type":27,"tag":469,"props":911,"children":912},{"style":508},[913],{"type":33,"value":762},{"type":27,"tag":469,"props":915,"children":916},{"style":502},[917],{"type":33,"value":918},"error",{"type":27,"tag":469,"props":920,"children":921},{"style":508},[922],{"type":33,"value":772},{"type":27,"tag":469,"props":924,"children":925},{"style":775},[926],{"type":33,"value":927},"'db.query.failed'",{"type":27,"tag":469,"props":929,"children":930},{"style":508},[931],{"type":33,"value":783},{"type":27,"tag":469,"props":933,"children":935},{"class":471,"line":934},24,[936],{"type":27,"tag":469,"props":937,"children":938},{"style":508},[939],{"type":33,"value":792},{"type":27,"tag":469,"props":941,"children":943},{"class":471,"line":942},25,[944],{"type":27,"tag":469,"props":945,"children":946},{"style":508},[947],{"type":33,"value":801},{"type":27,"tag":469,"props":949,"children":951},{"class":471,"line":950},26,[952],{"type":27,"tag":469,"props":953,"children":954},{"style":508},[955],{"type":33,"value":810},{"type":27,"tag":469,"props":957,"children":959},{"class":471,"line":958},27,[960,964,968,972,976],{"type":27,"tag":469,"props":961,"children":962},{"style":508},[963],{"type":33,"value":819},{"type":27,"tag":469,"props":965,"children":966},{"style":502},[967],{"type":33,"value":671},{"type":27,"tag":469,"props":969,"children":970},{"style":508},[971],{"type":33,"value":828},{"type":27,"tag":469,"props":973,"children":974},{"style":486},[975],{"type":33,"value":833},{"type":27,"tag":469,"props":977,"children":978},{"style":508},[979],{"type":33,"value":838},{"type":27,"tag":469,"props":981,"children":983},{"class":471,"line":982},28,[984,989,994,999,1004,1009,1013,1018],{"type":27,"tag":469,"props":985,"children":986},{"style":508},[987],{"type":33,"value":988},"      error: error ",{"type":27,"tag":469,"props":990,"children":991},{"style":486},[992],{"type":33,"value":993},"instanceof",{"type":27,"tag":469,"props":995,"children":996},{"style":502},[997],{"type":33,"value":998}," Error",{"type":27,"tag":469,"props":1000,"children":1001},{"style":486},[1002],{"type":33,"value":1003}," ?",{"type":27,"tag":469,"props":1005,"children":1006},{"style":508},[1007],{"type":33,"value":1008}," error.message ",{"type":27,"tag":469,"props":1010,"children":1011},{"style":486},[1012],{"type":33,"value":535},{"type":27,"tag":469,"props":1014,"children":1015},{"style":502},[1016],{"type":33,"value":1017}," String",{"type":27,"tag":469,"props":1019,"children":1020},{"style":508},[1021],{"type":33,"value":1022},"(error),\n",{"type":27,"tag":469,"props":1024,"children":1026},{"class":471,"line":1025},29,[1027],{"type":27,"tag":469,"props":1028,"children":1029},{"style":508},[1030],{"type":33,"value":864},{"type":27,"tag":469,"props":1032,"children":1034},{"class":471,"line":1033},30,[1035],{"type":27,"tag":469,"props":1036,"children":1037},{"emptyLinePlaceholder":18},[1038],{"type":33,"value":685},{"type":27,"tag":469,"props":1040,"children":1042},{"class":471,"line":1041},31,[1043,1048],{"type":27,"tag":469,"props":1044,"children":1045},{"style":486},[1046],{"type":33,"value":1047},"    throw",{"type":27,"tag":469,"props":1049,"children":1050},{"style":508},[1051],{"type":33,"value":1052}," error\n",{"type":27,"tag":469,"props":1054,"children":1056},{"class":471,"line":1055},32,[1057],{"type":27,"tag":469,"props":1058,"children":1059},{"style":508},[1060],{"type":33,"value":1061},"  }\n",{"type":27,"tag":469,"props":1063,"children":1065},{"class":471,"line":1064},33,[1066],{"type":27,"tag":469,"props":1067,"children":1068},{"style":508},[1069],{"type":33,"value":1070},"}\n",{"type":27,"tag":35,"props":1072,"children":1073},{},[1074],{"type":33,"value":1075},"这段代码看起来很普通，但它解决了一个管理问题：",{"type":27,"tag":61,"props":1077,"children":1078},{},[1079,1084,1089],{"type":27,"tag":65,"props":1080,"children":1081},{},[1082],{"type":33,"value":1083},"接口为什么慢，可以回溯到具体 SQL",{"type":27,"tag":65,"props":1085,"children":1086},{},[1087],{"type":33,"value":1088},"某条 SQL 是谁在调用，可以按接口聚合",{"type":27,"tag":65,"props":1090,"children":1091},{},[1092],{"type":33,"value":1093},"优化前后能不能量化，可以做对比",{"type":27,"tag":253,"props":1095,"children":1097},{"id":1096},"_22-先排除三种假慢-sql",[1098],{"type":33,"value":1099},"2.2 先排除三种“假慢 SQL”",{"type":27,"tag":35,"props":1101,"children":1102},{},[1103],{"type":33,"value":1104},"真正开始调 SQL 之前，我一般会先排除这三类问题：",{"type":27,"tag":260,"props":1106,"children":1108},{"id":1107},"a-锁等待",[1109],{"type":33,"value":1110},"A. 锁等待",{"type":27,"tag":35,"props":1112,"children":1113},{},[1114],{"type":33,"value":1115},"查询本身不一定慢，但如果它需要等别的事务释放锁，看起来就会很慢。",{"type":27,"tag":260,"props":1117,"children":1119},{"id":1118},"b-连接池耗尽",[1120],{"type":33,"value":1121},"B. 连接池耗尽",{"type":27,"tag":35,"props":1123,"children":1124},{},[1125],{"type":33,"value":1126},"应用拿不到连接，等待时间会被误记到数据库查询头上。",{"type":27,"tag":260,"props":1128,"children":1130},{"id":1129},"c-大对象序列化",[1131],{"type":33,"value":1132},"C. 大对象序列化",{"type":27,"tag":35,"props":1134,"children":1135},{},[1136],{"type":33,"value":1137},"SQL 已经执行完了，但 ORM 把几千条记录映射成复杂对象，或者应用还做了一轮 JSON 拼装，最终接口依旧慢。",{"type":27,"tag":253,"props":1139,"children":1141},{"id":1140},"_23-三个必须看的指标",[1142],{"type":33,"value":1143},"2.3 三个必须看的指标",{"type":27,"tag":35,"props":1145,"children":1146},{},[1147],{"type":33,"value":1148},"在数据库侧，至少要看：",{"type":27,"tag":162,"props":1150,"children":1151},{},[1152,1157,1168],{"type":27,"tag":65,"props":1153,"children":1154},{},[1155],{"type":33,"value":1156},"查询耗时分位数：P50 / P95 / P99",{"type":27,"tag":65,"props":1158,"children":1159},{},[1160,1162],{"type":33,"value":1161},"扫描行数：",{"type":27,"tag":125,"props":1163,"children":1165},{"className":1164},[],[1166],{"type":33,"value":1167},"rows examined",{"type":27,"tag":65,"props":1169,"children":1170},{},[1171,1173],{"type":33,"value":1172},"返回行数：",{"type":27,"tag":125,"props":1174,"children":1176},{"className":1175},[],[1177],{"type":33,"value":1178},"rows sent",{"type":27,"tag":35,"props":1180,"children":1181},{},[1182],{"type":33,"value":1183},"如果你看到：",{"type":27,"tag":61,"props":1185,"children":1186},{},[1187,1192],{"type":27,"tag":65,"props":1188,"children":1189},{},[1190],{"type":33,"value":1191},"扫描 50 万行",{"type":27,"tag":65,"props":1193,"children":1194},{},[1195],{"type":33,"value":1196},"返回 20 行",{"type":27,"tag":35,"props":1198,"children":1199},{},[1200,1202],{"type":33,"value":1201},"那几乎已经能下第一结论：",{"type":27,"tag":96,"props":1203,"children":1204},{},[1205],{"type":33,"value":1206},"筛选条件和索引结构不匹配。",{"type":27,"tag":204,"props":1208,"children":1209},{},[],{"type":27,"tag":208,"props":1211,"children":1213},{"id":1212},"三索引到底在解决什么问题",[1214],{"type":33,"value":1215},"三、索引到底在解决什么问题",{"type":27,"tag":35,"props":1217,"children":1218},{},[1219],{"type":33,"value":1220},"索引不是魔法。",{"type":27,"tag":35,"props":1222,"children":1223},{},[1224],{"type":33,"value":1225},"它本质上是在用额外的存储空间和写入成本，换取更快的定位能力、更少的扫描范围，以及更稳定的排序路径。",{"type":27,"tag":253,"props":1227,"children":1229},{"id":1228},"_31-一个务实的理解方式",[1230],{"type":33,"value":1231},"3.1 一个务实的理解方式",{"type":27,"tag":35,"props":1233,"children":1234},{},[1235],{"type":33,"value":1236},"把一张大表想象成一个巨大的仓库：",{"type":27,"tag":61,"props":1238,"children":1239},{},[1240,1250],{"type":27,"tag":65,"props":1241,"children":1242},{},[1243,1248],{"type":27,"tag":96,"props":1244,"children":1245},{},[1246],{"type":33,"value":1247},"全表扫描",{"type":33,"value":1249},"：像员工每次都从第一个货架一路走到最后一个货架",{"type":27,"tag":65,"props":1251,"children":1252},{},[1253,1258],{"type":27,"tag":96,"props":1254,"children":1255},{},[1256],{"type":33,"value":1257},"有索引的查找",{"type":33,"value":1259},"：像先看仓库地图，直接走到可能有货的区域",{"type":27,"tag":35,"props":1261,"children":1262},{},[1263],{"type":33,"value":1264},"真正有价值的不是“走得快”，而是：",{"type":27,"tag":61,"props":1266,"children":1267},{},[1268,1273,1278],{"type":27,"tag":65,"props":1269,"children":1270},{},[1271],{"type":33,"value":1272},"少走弯路",{"type":27,"tag":65,"props":1274,"children":1275},{},[1276],{"type":33,"value":1277},"少翻无关货架",{"type":27,"tag":65,"props":1279,"children":1280},{},[1281],{"type":33,"value":1282},"找完以后不用再跑回仓库深处补资料",{"type":27,"tag":35,"props":1284,"children":1285},{},[1286,1288,1293],{"type":33,"value":1287},"最后这件事，对应的就是",{"type":27,"tag":96,"props":1289,"children":1290},{},[1291],{"type":33,"value":1292},"覆盖索引",{"type":33,"value":1294},"。",{"type":27,"tag":253,"props":1296,"children":1298},{"id":1297},"_32-为什么加了索引还是慢",[1299],{"type":33,"value":1300},"3.2 为什么“加了索引还是慢”",{"type":27,"tag":35,"props":1302,"children":1303},{},[1304],{"type":33,"value":1305},"这是最常见的现场问题。",{"type":27,"tag":35,"props":1307,"children":1308},{},[1309],{"type":33,"value":1310},"通常有 6 种原因：",{"type":27,"tag":162,"props":1312,"children":1313},{},[1314,1319,1324,1329,1334,1339],{"type":27,"tag":65,"props":1315,"children":1316},{},[1317],{"type":33,"value":1318},"索引列选择性太差",{"type":27,"tag":65,"props":1320,"children":1321},{},[1322],{"type":33,"value":1323},"联合索引顺序不对",{"type":27,"tag":65,"props":1325,"children":1326},{},[1327],{"type":33,"value":1328},"查询条件破坏了索引可用性",{"type":27,"tag":65,"props":1330,"children":1331},{},[1332],{"type":33,"value":1333},"排序和过滤没有共用同一条索引路径",{"type":27,"tag":65,"props":1335,"children":1336},{},[1337],{"type":33,"value":1338},"回表成本太高",{"type":27,"tag":65,"props":1340,"children":1341},{},[1342],{"type":33,"value":1343},"优化的不是主问题，真正的问题是调用次数爆炸",{"type":27,"tag":35,"props":1345,"children":1346},{},[1347],{"type":33,"value":1348},"别小看第 6 条。很多接口优化，最后不是靠某个高级索引，而是靠“把 28 次查询变成 3 次”。",{"type":27,"tag":204,"props":1350,"children":1351},{},[],{"type":27,"tag":208,"props":1353,"children":1355},{"id":1354},"四先学会看-explain但别把它当成占卜",[1356,1358,1364],{"type":33,"value":1357},"四、先学会看 ",{"type":27,"tag":125,"props":1359,"children":1361},{"className":1360},[],[1362],{"type":33,"value":1363},"EXPLAIN",{"type":33,"value":1365},"，但别把它当成占卜",{"type":27,"tag":35,"props":1367,"children":1368},{},[1369,1371,1376],{"type":33,"value":1370},"很多人第一次学优化，就是背 ",{"type":27,"tag":125,"props":1372,"children":1374},{"className":1373},[],[1375],{"type":33,"value":1363},{"type":33,"value":1377}," 的字段。背完还是不会用，因为不知道该看哪个字段、怎么和业务语句对应。",{"type":27,"tag":35,"props":1379,"children":1380},{},[1381],{"type":33,"value":1382},"这里给你一个够用的阅读顺序。",{"type":27,"tag":253,"props":1384,"children":1386},{"id":1385},"_41-一条典型的慢-sql",[1387],{"type":33,"value":1388},"4.1 一条典型的慢 SQL",{"type":27,"tag":459,"props":1390,"children":1394},{"className":1391,"code":1392,"language":1393,"meta":7,"style":7},"language-sql shiki shiki-themes github-dark","SELECT id, user_id, total_amount, created_at\nFROM orders\nWHERE tenant_id = 1024\n  AND status = 'paid'\n  AND created_at >= '2026-02-01 00:00:00'\nORDER BY created_at DESC\nLIMIT 20 OFFSET 4000;\n","sql",[1395],{"type":27,"tag":125,"props":1396,"children":1397},{"__ignoreMap":7},[1398,1406,1414,1422,1430,1438,1446],{"type":27,"tag":469,"props":1399,"children":1400},{"class":471,"line":472},[1401],{"type":27,"tag":469,"props":1402,"children":1403},{},[1404],{"type":33,"value":1405},"SELECT id, user_id, total_amount, created_at\n",{"type":27,"tag":469,"props":1407,"children":1408},{"class":471,"line":482},[1409],{"type":27,"tag":469,"props":1410,"children":1411},{},[1412],{"type":33,"value":1413},"FROM orders\n",{"type":27,"tag":469,"props":1415,"children":1416},{"class":471,"line":20},[1417],{"type":27,"tag":469,"props":1418,"children":1419},{},[1420],{"type":33,"value":1421},"WHERE tenant_id = 1024\n",{"type":27,"tag":469,"props":1423,"children":1424},{"class":471,"line":549},[1425],{"type":27,"tag":469,"props":1426,"children":1427},{},[1428],{"type":33,"value":1429},"  AND status = 'paid'\n",{"type":27,"tag":469,"props":1431,"children":1432},{"class":471,"line":570},[1433],{"type":27,"tag":469,"props":1434,"children":1435},{},[1436],{"type":33,"value":1437},"  AND created_at >= '2026-02-01 00:00:00'\n",{"type":27,"tag":469,"props":1439,"children":1440},{"class":471,"line":593},[1441],{"type":27,"tag":469,"props":1442,"children":1443},{},[1444],{"type":33,"value":1445},"ORDER BY created_at DESC\n",{"type":27,"tag":469,"props":1447,"children":1448},{"class":471,"line":614},[1449],{"type":27,"tag":469,"props":1450,"children":1451},{},[1452],{"type":33,"value":1453},"LIMIT 20 OFFSET 4000;\n",{"type":27,"tag":35,"props":1455,"children":1456},{},[1457],{"type":33,"value":1458},"这个语句在后台订单列表里非常常见：",{"type":27,"tag":61,"props":1460,"children":1461},{},[1462,1467,1472,1477,1482],{"type":27,"tag":65,"props":1463,"children":1464},{},[1465],{"type":33,"value":1466},"有租户隔离",{"type":27,"tag":65,"props":1468,"children":1469},{},[1470],{"type":33,"value":1471},"有状态筛选",{"type":27,"tag":65,"props":1473,"children":1474},{},[1475],{"type":33,"value":1476},"有时间范围",{"type":27,"tag":65,"props":1478,"children":1479},{},[1480],{"type":33,"value":1481},"有排序",{"type":27,"tag":65,"props":1483,"children":1484},{},[1485],{"type":33,"value":1486},"还有深分页",{"type":27,"tag":253,"props":1488,"children":1490},{"id":1489},"_42-看执行计划时先看这几个点",[1491],{"type":33,"value":1492},"4.2 看执行计划时先看这几个点",{"type":27,"tag":459,"props":1494,"children":1496},{"className":1391,"code":1495,"language":1393,"meta":7,"style":7},"EXPLAIN SELECT id, user_id, total_amount, created_at\nFROM orders\nWHERE tenant_id = 1024\n  AND status = 'paid'\n  AND created_at >= '2026-02-01 00:00:00'\nORDER BY created_at DESC\nLIMIT 20 OFFSET 4000;\n",[1497],{"type":27,"tag":125,"props":1498,"children":1499},{"__ignoreMap":7},[1500,1508,1515,1522,1529,1536,1543],{"type":27,"tag":469,"props":1501,"children":1502},{"class":471,"line":472},[1503],{"type":27,"tag":469,"props":1504,"children":1505},{},[1506],{"type":33,"value":1507},"EXPLAIN SELECT id, user_id, total_amount, created_at\n",{"type":27,"tag":469,"props":1509,"children":1510},{"class":471,"line":482},[1511],{"type":27,"tag":469,"props":1512,"children":1513},{},[1514],{"type":33,"value":1413},{"type":27,"tag":469,"props":1516,"children":1517},{"class":471,"line":20},[1518],{"type":27,"tag":469,"props":1519,"children":1520},{},[1521],{"type":33,"value":1421},{"type":27,"tag":469,"props":1523,"children":1524},{"class":471,"line":549},[1525],{"type":27,"tag":469,"props":1526,"children":1527},{},[1528],{"type":33,"value":1429},{"type":27,"tag":469,"props":1530,"children":1531},{"class":471,"line":570},[1532],{"type":27,"tag":469,"props":1533,"children":1534},{},[1535],{"type":33,"value":1437},{"type":27,"tag":469,"props":1537,"children":1538},{"class":471,"line":593},[1539],{"type":27,"tag":469,"props":1540,"children":1541},{},[1542],{"type":33,"value":1445},{"type":27,"tag":469,"props":1544,"children":1545},{"class":471,"line":614},[1546],{"type":27,"tag":469,"props":1547,"children":1548},{},[1549],{"type":33,"value":1453},{"type":27,"tag":35,"props":1551,"children":1552},{},[1553],{"type":33,"value":1554},"重点优先级可以这样排：",{"type":27,"tag":305,"props":1556,"children":1557},{},[1558,1579],{"type":27,"tag":309,"props":1559,"children":1560},{},[1561],{"type":27,"tag":313,"props":1562,"children":1563},{},[1564,1569,1574],{"type":27,"tag":317,"props":1565,"children":1566},{},[1567],{"type":33,"value":1568},"字段",{"type":27,"tag":317,"props":1570,"children":1571},{},[1572],{"type":33,"value":1573},"重点看什么",{"type":27,"tag":317,"props":1575,"children":1576},{},[1577],{"type":33,"value":1578},"意义",{"type":27,"tag":333,"props":1580,"children":1581},{},[1582,1637,1659,1681],{"type":27,"tag":313,"props":1583,"children":1584},{},[1585,1594,1625],{"type":27,"tag":340,"props":1586,"children":1587},{},[1588],{"type":27,"tag":125,"props":1589,"children":1591},{"className":1590},[],[1592],{"type":33,"value":1593},"type",{"type":27,"tag":340,"props":1595,"children":1596},{},[1597,1603,1605,1611,1612,1618,1619],{"type":27,"tag":125,"props":1598,"children":1600},{"className":1599},[],[1601],{"type":33,"value":1602},"ref",{"type":33,"value":1604}," / ",{"type":27,"tag":125,"props":1606,"children":1608},{"className":1607},[],[1609],{"type":33,"value":1610},"range",{"type":33,"value":1604},{"type":27,"tag":125,"props":1613,"children":1615},{"className":1614},[],[1616],{"type":33,"value":1617},"index",{"type":33,"value":1604},{"type":27,"tag":125,"props":1620,"children":1622},{"className":1621},[],[1623],{"type":33,"value":1624},"ALL",{"type":27,"tag":340,"props":1626,"children":1627},{},[1628,1630,1635],{"type":33,"value":1629},"越接近 ",{"type":27,"tag":125,"props":1631,"children":1633},{"className":1632},[],[1634],{"type":33,"value":1624},{"type":33,"value":1636}," 越危险",{"type":27,"tag":313,"props":1638,"children":1639},{},[1640,1649,1654],{"type":27,"tag":340,"props":1641,"children":1642},{},[1643],{"type":27,"tag":125,"props":1644,"children":1646},{"className":1645},[],[1647],{"type":33,"value":1648},"key",{"type":27,"tag":340,"props":1650,"children":1651},{},[1652],{"type":33,"value":1653},"实际用了哪个索引",{"type":27,"tag":340,"props":1655,"children":1656},{},[1657],{"type":33,"value":1658},"有没有走到你以为会走的索引",{"type":27,"tag":313,"props":1660,"children":1661},{},[1662,1671,1676],{"type":27,"tag":340,"props":1663,"children":1664},{},[1665],{"type":27,"tag":125,"props":1666,"children":1668},{"className":1667},[],[1669],{"type":33,"value":1670},"rows",{"type":27,"tag":340,"props":1672,"children":1673},{},[1674],{"type":33,"value":1675},"预估扫描多少行",{"type":27,"tag":340,"props":1677,"children":1678},{},[1679],{"type":33,"value":1680},"数字大通常意味着过滤不充分",{"type":27,"tag":313,"props":1682,"children":1683},{},[1684,1693,1717],{"type":27,"tag":340,"props":1685,"children":1686},{},[1687],{"type":27,"tag":125,"props":1688,"children":1690},{"className":1689},[],[1691],{"type":33,"value":1692},"Extra",{"type":27,"tag":340,"props":1694,"children":1695},{},[1696,1702,1704,1710,1711],{"type":27,"tag":125,"props":1697,"children":1699},{"className":1698},[],[1700],{"type":33,"value":1701},"Using filesort",{"type":33,"value":1703},"、",{"type":27,"tag":125,"props":1705,"children":1707},{"className":1706},[],[1708],{"type":33,"value":1709},"Using temporary",{"type":33,"value":1703},{"type":27,"tag":125,"props":1712,"children":1714},{"className":1713},[],[1715],{"type":33,"value":1716},"Using index",{"type":27,"tag":340,"props":1718,"children":1719},{},[1720],{"type":33,"value":1721},"能快速发现排序和临时表问题",{"type":27,"tag":35,"props":1723,"children":1724},{},[1725],{"type":33,"value":1183},{"type":27,"tag":61,"props":1727,"children":1728},{},[1729,1738,1747],{"type":27,"tag":65,"props":1730,"children":1731},{},[1732],{"type":27,"tag":125,"props":1733,"children":1735},{"className":1734},[],[1736],{"type":33,"value":1737},"type = ALL",{"type":27,"tag":65,"props":1739,"children":1740},{},[1741],{"type":27,"tag":125,"props":1742,"children":1744},{"className":1743},[],[1745],{"type":33,"value":1746},"rows = 4200000",{"type":27,"tag":65,"props":1748,"children":1749},{},[1750],{"type":27,"tag":125,"props":1751,"children":1753},{"className":1752},[],[1754],{"type":33,"value":1755},"Extra = Using where; Using filesort",{"type":27,"tag":35,"props":1757,"children":1758},{},[1759],{"type":33,"value":1760},"基本可以确定：",{"type":27,"tag":162,"props":1762,"children":1763},{},[1764,1769,1774],{"type":27,"tag":65,"props":1765,"children":1766},{},[1767],{"type":33,"value":1768},"没有高效利用索引做过滤",{"type":27,"tag":65,"props":1770,"children":1771},{},[1772],{"type":33,"value":1773},"排序无法直接沿索引完成",{"type":27,"tag":65,"props":1775,"children":1776},{},[1777],{"type":33,"value":1778},"数据量一上来，查询会明显抖动",{"type":27,"tag":253,"props":1780,"children":1782},{"id":1781},"_43-explain-的最大误区",[1783,1785,1790],{"type":33,"value":1784},"4.3 ",{"type":27,"tag":125,"props":1786,"children":1788},{"className":1787},[],[1789],{"type":33,"value":1363},{"type":33,"value":1791}," 的最大误区",{"type":27,"tag":35,"props":1793,"children":1794},{},[1795,1797,1802],{"type":33,"value":1796},"它不是实际运行结果，而是",{"type":27,"tag":96,"props":1798,"children":1799},{},[1800],{"type":33,"value":1801},"优化器的估计",{"type":33,"value":1294},{"type":27,"tag":35,"props":1804,"children":1805},{},[1806],{"type":33,"value":1807},"所以正确姿势不是：",{"type":27,"tag":148,"props":1809,"children":1810},{},[1811],{"type":27,"tag":35,"props":1812,"children":1813},{},[1814,1816,1821],{"type":33,"value":1815},"看完 ",{"type":27,"tag":125,"props":1817,"children":1819},{"className":1818},[],[1820],{"type":33,"value":1363},{"type":33,"value":1822}," 就宣布破案。",{"type":27,"tag":35,"props":1824,"children":1825},{},[1826],{"type":33,"value":1827},"而是：",{"type":27,"tag":162,"props":1829,"children":1830},{},[1831,1836,1841,1846],{"type":27,"tag":65,"props":1832,"children":1833},{},[1834],{"type":33,"value":1835},"看执行计划",{"type":27,"tag":65,"props":1837,"children":1838},{},[1839],{"type":33,"value":1840},"结合真实数据规模和统计信息判断",{"type":27,"tag":65,"props":1842,"children":1843},{},[1844],{"type":33,"value":1845},"上线前在接近生产的数据量下验证",{"type":27,"tag":65,"props":1847,"children":1848},{},[1849],{"type":33,"value":1850},"对比优化前后耗时、扫描行数和 CPU",{"type":27,"tag":204,"props":1852,"children":1853},{},[],{"type":27,"tag":208,"props":1855,"children":1857},{"id":1856},"五联合索引怎么设计才不是排列组合式瞎试",[1858],{"type":33,"value":1859},"五、联合索引怎么设计，才不是“排列组合式瞎试”",{"type":27,"tag":253,"props":1861,"children":1863},{"id":1862},"_51-一个常见错误每个筛选列都单独建索引",[1864],{"type":33,"value":1865},"5.1 一个常见错误：每个筛选列都单独建索引",{"type":27,"tag":35,"props":1867,"children":1868},{},[1869],{"type":33,"value":1870},"比如有人会这么建：",{"type":27,"tag":459,"props":1872,"children":1874},{"className":1391,"code":1873,"language":1393,"meta":7,"style":7},"CREATE INDEX idx_orders_tenant_id ON orders (tenant_id);\nCREATE INDEX idx_orders_status ON orders (status);\nCREATE INDEX idx_orders_created_at ON orders (created_at);\n",[1875],{"type":27,"tag":125,"props":1876,"children":1877},{"__ignoreMap":7},[1878,1886,1894],{"type":27,"tag":469,"props":1879,"children":1880},{"class":471,"line":472},[1881],{"type":27,"tag":469,"props":1882,"children":1883},{},[1884],{"type":33,"value":1885},"CREATE INDEX idx_orders_tenant_id ON orders (tenant_id);\n",{"type":27,"tag":469,"props":1887,"children":1888},{"class":471,"line":482},[1889],{"type":27,"tag":469,"props":1890,"children":1891},{},[1892],{"type":33,"value":1893},"CREATE INDEX idx_orders_status ON orders (status);\n",{"type":27,"tag":469,"props":1895,"children":1896},{"class":471,"line":20},[1897],{"type":27,"tag":469,"props":1898,"children":1899},{},[1900],{"type":33,"value":1901},"CREATE INDEX idx_orders_created_at ON orders (created_at);\n",{"type":27,"tag":35,"props":1903,"children":1904},{},[1905],{"type":33,"value":1906},"看起来很勤奋，实际上经常不如一个设计正确的联合索引。",{"type":27,"tag":35,"props":1908,"children":1909},{},[1910],{"type":33,"value":1911},"因为业务查询通常不是“只按一个条件查”，而是多条件一起出现。",{"type":27,"tag":253,"props":1913,"children":1915},{"id":1914},"_52-联合索引的正确思路",[1916],{"type":33,"value":1917},"5.2 联合索引的正确思路",{"type":27,"tag":35,"props":1919,"children":1920},{},[1921],{"type":33,"value":1922},"设计联合索引时，不要先问“字段重要不重要”，而要先问：",{"type":27,"tag":162,"props":1924,"children":1925},{},[1926,1931,1936,1941],{"type":27,"tag":65,"props":1927,"children":1928},{},[1929],{"type":33,"value":1930},"哪些条件最稳定、最常出现？",{"type":27,"tag":65,"props":1932,"children":1933},{},[1934],{"type":33,"value":1935},"哪些条件筛选力更强？",{"type":27,"tag":65,"props":1937,"children":1938},{},[1939],{"type":33,"value":1940},"查询还需要排序吗？",{"type":27,"tag":65,"props":1942,"children":1943},{},[1944],{"type":33,"value":1945},"是否希望做到覆盖索引？",{"type":27,"tag":35,"props":1947,"children":1948},{},[1949],{"type":33,"value":1950},"对于这条查询：",{"type":27,"tag":459,"props":1952,"children":1954},{"className":1391,"code":1953,"language":1393,"meta":7,"style":7},"SELECT id, user_id, total_amount, created_at\nFROM orders\nWHERE tenant_id = 1024\n  AND status = 'paid'\n  AND created_at >= '2026-02-01 00:00:00'\nORDER BY created_at DESC\nLIMIT 20;\n",[1955],{"type":27,"tag":125,"props":1956,"children":1957},{"__ignoreMap":7},[1958,1965,1972,1979,1986,1993,2000],{"type":27,"tag":469,"props":1959,"children":1960},{"class":471,"line":472},[1961],{"type":27,"tag":469,"props":1962,"children":1963},{},[1964],{"type":33,"value":1405},{"type":27,"tag":469,"props":1966,"children":1967},{"class":471,"line":482},[1968],{"type":27,"tag":469,"props":1969,"children":1970},{},[1971],{"type":33,"value":1413},{"type":27,"tag":469,"props":1973,"children":1974},{"class":471,"line":20},[1975],{"type":27,"tag":469,"props":1976,"children":1977},{},[1978],{"type":33,"value":1421},{"type":27,"tag":469,"props":1980,"children":1981},{"class":471,"line":549},[1982],{"type":27,"tag":469,"props":1983,"children":1984},{},[1985],{"type":33,"value":1429},{"type":27,"tag":469,"props":1987,"children":1988},{"class":471,"line":570},[1989],{"type":27,"tag":469,"props":1990,"children":1991},{},[1992],{"type":33,"value":1437},{"type":27,"tag":469,"props":1994,"children":1995},{"class":471,"line":593},[1996],{"type":27,"tag":469,"props":1997,"children":1998},{},[1999],{"type":33,"value":1445},{"type":27,"tag":469,"props":2001,"children":2002},{"class":471,"line":614},[2003],{"type":27,"tag":469,"props":2004,"children":2005},{},[2006],{"type":33,"value":2007},"LIMIT 20;\n",{"type":27,"tag":35,"props":2009,"children":2010},{},[2011],{"type":33,"value":2012},"一个更合理的索引可能是：",{"type":27,"tag":459,"props":2014,"children":2016},{"className":1391,"code":2015,"language":1393,"meta":7,"style":7},"CREATE INDEX idx_orders_tenant_status_created_at\nON orders (tenant_id, status, created_at DESC);\n",[2017],{"type":27,"tag":125,"props":2018,"children":2019},{"__ignoreMap":7},[2020,2028],{"type":27,"tag":469,"props":2021,"children":2022},{"class":471,"line":472},[2023],{"type":27,"tag":469,"props":2024,"children":2025},{},[2026],{"type":33,"value":2027},"CREATE INDEX idx_orders_tenant_status_created_at\n",{"type":27,"tag":469,"props":2029,"children":2030},{"class":471,"line":482},[2031],{"type":27,"tag":469,"props":2032,"children":2033},{},[2034],{"type":33,"value":2035},"ON orders (tenant_id, status, created_at DESC);\n",{"type":27,"tag":35,"props":2037,"children":2038},{},[2039],{"type":33,"value":2040},"这样设计的原因是：",{"type":27,"tag":61,"props":2042,"children":2043},{},[2044,2055,2066],{"type":27,"tag":65,"props":2045,"children":2046},{},[2047,2053],{"type":27,"tag":125,"props":2048,"children":2050},{"className":2049},[],[2051],{"type":33,"value":2052},"tenant_id",{"type":33,"value":2054},"：租户隔离，几乎所有查询都会带",{"type":27,"tag":65,"props":2056,"children":2057},{},[2058,2064],{"type":27,"tag":125,"props":2059,"children":2061},{"className":2060},[],[2062],{"type":33,"value":2063},"status",{"type":33,"value":2065},"：高频筛选条件",{"type":27,"tag":65,"props":2067,"children":2068},{},[2069,2075],{"type":27,"tag":125,"props":2070,"children":2072},{"className":2071},[],[2073],{"type":33,"value":2074},"created_at",{"type":33,"value":2076},"：既参与范围，又参与排序",{"type":27,"tag":253,"props":2078,"children":2080},{"id":2079},"_53-最左前缀原则别背成咒语",[2081],{"type":33,"value":2082},"5.3 最左前缀原则，别背成咒语",{"type":27,"tag":35,"props":2084,"children":2085},{},[2086],{"type":33,"value":2087},"所谓最左前缀，不是让你机械地记“从左到右不能断”，而是提醒你：",{"type":27,"tag":35,"props":2089,"children":2090},{},[2091],{"type":27,"tag":96,"props":2092,"children":2093},{},[2094],{"type":33,"value":2095},"索引能高效使用的前提，是查询条件要能沿着索引定义的顺序逐步缩小范围。",{"type":27,"tag":35,"props":2097,"children":2098},{},[2099],{"type":33,"value":2100},"例如索引：",{"type":27,"tag":459,"props":2102,"children":2104},{"className":1391,"code":2103,"language":1393,"meta":7,"style":7},"(tenant_id, status, created_at)\n",[2105],{"type":27,"tag":125,"props":2106,"children":2107},{"__ignoreMap":7},[2108],{"type":27,"tag":469,"props":2109,"children":2110},{"class":471,"line":472},[2111],{"type":27,"tag":469,"props":2112,"children":2113},{},[2114],{"type":33,"value":2103},{"type":27,"tag":35,"props":2116,"children":2117},{},[2118],{"type":33,"value":2119},"通常以下查询都比较友好：",{"type":27,"tag":459,"props":2121,"children":2123},{"className":1391,"code":2122,"language":1393,"meta":7,"style":7},"WHERE tenant_id = 1024\nWHERE tenant_id = 1024 AND status = 'paid'\nWHERE tenant_id = 1024 AND status = 'paid' AND created_at >= '2026-02-01'\n",[2124],{"type":27,"tag":125,"props":2125,"children":2126},{"__ignoreMap":7},[2127,2134,2142],{"type":27,"tag":469,"props":2128,"children":2129},{"class":471,"line":472},[2130],{"type":27,"tag":469,"props":2131,"children":2132},{},[2133],{"type":33,"value":1421},{"type":27,"tag":469,"props":2135,"children":2136},{"class":471,"line":482},[2137],{"type":27,"tag":469,"props":2138,"children":2139},{},[2140],{"type":33,"value":2141},"WHERE tenant_id = 1024 AND status = 'paid'\n",{"type":27,"tag":469,"props":2143,"children":2144},{"class":471,"line":20},[2145],{"type":27,"tag":469,"props":2146,"children":2147},{},[2148],{"type":33,"value":2149},"WHERE tenant_id = 1024 AND status = 'paid' AND created_at >= '2026-02-01'\n",{"type":27,"tag":35,"props":2151,"children":2152},{},[2153],{"type":33,"value":2154},"但如果你写：",{"type":27,"tag":459,"props":2156,"children":2158},{"className":1391,"code":2157,"language":1393,"meta":7,"style":7},"WHERE status = 'paid' AND created_at >= '2026-02-01'\n",[2159],{"type":27,"tag":125,"props":2160,"children":2161},{"__ignoreMap":7},[2162],{"type":27,"tag":469,"props":2163,"children":2164},{"class":471,"line":472},[2165],{"type":27,"tag":469,"props":2166,"children":2167},{},[2168],{"type":33,"value":2157},{"type":27,"tag":35,"props":2170,"children":2171},{},[2172,2174,2179],{"type":33,"value":2173},"那前导列 ",{"type":27,"tag":125,"props":2175,"children":2177},{"className":2176},[],[2178],{"type":33,"value":2052},{"type":33,"value":2180}," 没用上，效果就会大打折扣。",{"type":27,"tag":253,"props":2182,"children":2184},{"id":2183},"_54-索引列顺序到底先放高选择性还是高频等值列",[2185],{"type":33,"value":2186},"5.4 索引列顺序，到底先放高选择性还是高频等值列",{"type":27,"tag":35,"props":2188,"children":2189},{},[2190],{"type":33,"value":2191},"这题经常被讲得像面试脑筋急转弯。真实世界的答案是：",{"type":27,"tag":148,"props":2193,"children":2194},{},[2195],{"type":27,"tag":35,"props":2196,"children":2197},{},[2198],{"type":27,"tag":96,"props":2199,"children":2200},{},[2201],{"type":33,"value":2202},"优先让索引匹配稳定的高频访问模式，其次兼顾筛选力和排序路径。",{"type":27,"tag":35,"props":2204,"children":2205},{},[2206],{"type":33,"value":2207},"一个经验公式：",{"type":27,"tag":61,"props":2209,"children":2210},{},[2211,2216,2221,2226],{"type":27,"tag":65,"props":2212,"children":2213},{},[2214],{"type":33,"value":2215},"高频等值条件优先",{"type":27,"tag":65,"props":2217,"children":2218},{},[2219],{"type":33,"value":2220},"能显著缩小范围的条件靠前",{"type":27,"tag":65,"props":2222,"children":2223},{},[2224],{"type":33,"value":2225},"范围条件通常放在后面",{"type":27,"tag":65,"props":2227,"children":2228},{},[2229],{"type":33,"value":2230},"用于排序的列要考虑是否能跟前面的过滤共同走索引",{"type":27,"tag":35,"props":2232,"children":2233},{},[2234,2236,2241],{"type":33,"value":2235},"不是所有场景都把“选择性最高”的列放最前面。比如多租户系统里，",{"type":27,"tag":125,"props":2237,"children":2239},{"className":2238},[],[2240],{"type":33,"value":2052},{"type":33,"value":2242}," 即使选择性一般，也常常必须放前面，因为它决定了绝大多数查询的访问边界。",{"type":27,"tag":204,"props":2244,"children":2245},{},[],{"type":27,"tag":208,"props":2247,"children":2249},{"id":2248},"六三个真实业务场景看看索引怎么落地",[2250],{"type":33,"value":2251},"六、三个真实业务场景，看看索引怎么落地",{"type":27,"tag":253,"props":2253,"children":2255},{"id":2254},"场景-1订单列表为什么越翻越慢",[2256],{"type":33,"value":2257},"场景 1：订单列表为什么越翻越慢",{"type":27,"tag":35,"props":2259,"children":2260},{},[2261],{"type":33,"value":2262},"后台经常有这样的深分页：",{"type":27,"tag":459,"props":2264,"children":2266},{"className":1391,"code":2265,"language":1393,"meta":7,"style":7},"SELECT id, user_id, total_amount, created_at\nFROM orders\nWHERE tenant_id = 1024\nORDER BY created_at DESC\nLIMIT 20 OFFSET 20000;\n",[2267],{"type":27,"tag":125,"props":2268,"children":2269},{"__ignoreMap":7},[2270,2277,2284,2291,2298],{"type":27,"tag":469,"props":2271,"children":2272},{"class":471,"line":472},[2273],{"type":27,"tag":469,"props":2274,"children":2275},{},[2276],{"type":33,"value":1405},{"type":27,"tag":469,"props":2278,"children":2279},{"class":471,"line":482},[2280],{"type":27,"tag":469,"props":2281,"children":2282},{},[2283],{"type":33,"value":1413},{"type":27,"tag":469,"props":2285,"children":2286},{"class":471,"line":20},[2287],{"type":27,"tag":469,"props":2288,"children":2289},{},[2290],{"type":33,"value":1421},{"type":27,"tag":469,"props":2292,"children":2293},{"class":471,"line":549},[2294],{"type":27,"tag":469,"props":2295,"children":2296},{},[2297],{"type":33,"value":1445},{"type":27,"tag":469,"props":2299,"children":2300},{"class":471,"line":570},[2301],{"type":27,"tag":469,"props":2302,"children":2303},{},[2304],{"type":33,"value":2305},"LIMIT 20 OFFSET 20000;\n",{"type":27,"tag":35,"props":2307,"children":2308},{},[2309],{"type":33,"value":2310},"问题不在于“只取 20 条”，而在于数据库为了跳过前 20000 条，往往还是要扫描并排序大量记录。",{"type":27,"tag":260,"props":2312,"children":2314},{"id":2313},"更好的方案基于游标的翻页",[2315],{"type":33,"value":2316},"更好的方案：基于游标的翻页",{"type":27,"tag":459,"props":2318,"children":2320},{"className":1391,"code":2319,"language":1393,"meta":7,"style":7},"SELECT id, user_id, total_amount, created_at\nFROM orders\nWHERE tenant_id = 1024\n  AND created_at \u003C '2026-03-07 09:30:12'\nORDER BY created_at DESC\nLIMIT 20;\n",[2321],{"type":27,"tag":125,"props":2322,"children":2323},{"__ignoreMap":7},[2324,2331,2338,2345,2353,2360],{"type":27,"tag":469,"props":2325,"children":2326},{"class":471,"line":472},[2327],{"type":27,"tag":469,"props":2328,"children":2329},{},[2330],{"type":33,"value":1405},{"type":27,"tag":469,"props":2332,"children":2333},{"class":471,"line":482},[2334],{"type":27,"tag":469,"props":2335,"children":2336},{},[2337],{"type":33,"value":1413},{"type":27,"tag":469,"props":2339,"children":2340},{"class":471,"line":20},[2341],{"type":27,"tag":469,"props":2342,"children":2343},{},[2344],{"type":33,"value":1421},{"type":27,"tag":469,"props":2346,"children":2347},{"class":471,"line":549},[2348],{"type":27,"tag":469,"props":2349,"children":2350},{},[2351],{"type":33,"value":2352},"  AND created_at \u003C '2026-03-07 09:30:12'\n",{"type":27,"tag":469,"props":2354,"children":2355},{"class":471,"line":570},[2356],{"type":27,"tag":469,"props":2357,"children":2358},{},[2359],{"type":33,"value":1445},{"type":27,"tag":469,"props":2361,"children":2362},{"class":471,"line":593},[2363],{"type":27,"tag":469,"props":2364,"children":2365},{},[2366],{"type":33,"value":2007},{"type":27,"tag":35,"props":2368,"children":2369},{},[2370],{"type":33,"value":2371},"对应索引：",{"type":27,"tag":459,"props":2373,"children":2375},{"className":1391,"code":2374,"language":1393,"meta":7,"style":7},"CREATE INDEX idx_orders_tenant_created_at\nON orders (tenant_id, created_at DESC);\n",[2376],{"type":27,"tag":125,"props":2377,"children":2378},{"__ignoreMap":7},[2379,2387],{"type":27,"tag":469,"props":2380,"children":2381},{"class":471,"line":472},[2382],{"type":27,"tag":469,"props":2383,"children":2384},{},[2385],{"type":33,"value":2386},"CREATE INDEX idx_orders_tenant_created_at\n",{"type":27,"tag":469,"props":2388,"children":2389},{"class":471,"line":482},[2390],{"type":27,"tag":469,"props":2391,"children":2392},{},[2393],{"type":33,"value":2394},"ON orders (tenant_id, created_at DESC);\n",{"type":27,"tag":35,"props":2396,"children":2397},{},[2398],{"type":33,"value":2399},"这类优化在管理后台、消息列表、动态流里都非常值。",{"type":27,"tag":35,"props":2401,"children":2402},{},[2403,2408],{"type":27,"tag":96,"props":2404,"children":2405},{},[2406],{"type":33,"value":2407},"真实收益",{"type":33,"value":2409},"：我们在一个订单中心里把深分页改成游标翻页后：",{"type":27,"tag":61,"props":2411,"children":2412},{},[2413,2418,2423],{"type":27,"tag":65,"props":2414,"children":2415},{},[2416],{"type":33,"value":2417},"P95：820ms → 96ms",{"type":27,"tag":65,"props":2419,"children":2420},{},[2421],{"type":33,"value":2422},"扫描行数：约 18 万 → 20",{"type":27,"tag":65,"props":2424,"children":2425},{},[2426],{"type":33,"value":2427},"高峰 CPU：下降 31%",{"type":27,"tag":253,"props":2429,"children":2431},{"id":2430},"场景-2商品详情页的-n1-查询",[2432,2434,2439],{"type":33,"value":2433},"场景 2：商品详情页的 ",{"type":27,"tag":125,"props":2435,"children":2437},{"className":2436},[],[2438],{"type":33,"value":190},{"type":33,"value":2440}," 查询",{"type":27,"tag":35,"props":2442,"children":2443},{},[2444],{"type":33,"value":2445},"很多详情页会先查商品，再逐个查库存、价格、活动、评论摘要：",{"type":27,"tag":459,"props":2447,"children":2449},{"className":461,"code":2448,"language":463,"meta":7,"style":7},"const products = await db.query\u003CProduct>(\n  'SELECT id, title FROM products WHERE category_id = ?',\n  [categoryId],\n)\n\nfor (const product of products) {\n  product.stock = await db.queryOne\u003Cnumber>(\n    'SELECT available FROM inventory WHERE product_id = ?',\n    [product.id],\n  )\n}\n",[2450],{"type":27,"tag":125,"props":2451,"children":2452},{"__ignoreMap":7},[2453,2495,2507,2515,2523,2530,2562,2601,2613,2621,2629],{"type":27,"tag":469,"props":2454,"children":2455},{"class":471,"line":472},[2456,2461,2466,2470,2474,2478,2482,2486,2491],{"type":27,"tag":469,"props":2457,"children":2458},{"style":486},[2459],{"type":33,"value":2460},"const",{"type":27,"tag":469,"props":2462,"children":2463},{"style":538},[2464],{"type":33,"value":2465}," products",{"type":27,"tag":469,"props":2467,"children":2468},{"style":486},[2469],{"type":33,"value":661},{"type":27,"tag":469,"props":2471,"children":2472},{"style":486},[2473],{"type":33,"value":722},{"type":27,"tag":469,"props":2475,"children":2476},{"style":508},[2477],{"type":33,"value":727},{"type":27,"tag":469,"props":2479,"children":2480},{"style":502},[2481],{"type":33,"value":732},{"type":27,"tag":469,"props":2483,"children":2484},{"style":508},[2485],{"type":33,"value":511},{"type":27,"tag":469,"props":2487,"children":2488},{"style":502},[2489],{"type":33,"value":2490},"Product",{"type":27,"tag":469,"props":2492,"children":2493},{"style":508},[2494],{"type":33,"value":521},{"type":27,"tag":469,"props":2496,"children":2497},{"class":471,"line":482},[2498,2503],{"type":27,"tag":469,"props":2499,"children":2500},{"style":775},[2501],{"type":33,"value":2502},"  'SELECT id, title FROM products WHERE category_id = ?'",{"type":27,"tag":469,"props":2504,"children":2505},{"style":508},[2506],{"type":33,"value":546},{"type":27,"tag":469,"props":2508,"children":2509},{"class":471,"line":20},[2510],{"type":27,"tag":469,"props":2511,"children":2512},{"style":508},[2513],{"type":33,"value":2514},"  [categoryId],\n",{"type":27,"tag":469,"props":2516,"children":2517},{"class":471,"line":549},[2518],{"type":27,"tag":469,"props":2519,"children":2520},{"style":508},[2521],{"type":33,"value":2522},")\n",{"type":27,"tag":469,"props":2524,"children":2525},{"class":471,"line":570},[2526],{"type":27,"tag":469,"props":2527,"children":2528},{"emptyLinePlaceholder":18},[2529],{"type":33,"value":685},{"type":27,"tag":469,"props":2531,"children":2532},{"class":471,"line":593},[2533,2538,2543,2547,2552,2557],{"type":27,"tag":469,"props":2534,"children":2535},{"style":486},[2536],{"type":33,"value":2537},"for",{"type":27,"tag":469,"props":2539,"children":2540},{"style":508},[2541],{"type":33,"value":2542}," (",{"type":27,"tag":469,"props":2544,"children":2545},{"style":486},[2546],{"type":33,"value":2460},{"type":27,"tag":469,"props":2548,"children":2549},{"style":538},[2550],{"type":33,"value":2551}," product",{"type":27,"tag":469,"props":2553,"children":2554},{"style":486},[2555],{"type":33,"value":2556}," of",{"type":27,"tag":469,"props":2558,"children":2559},{"style":508},[2560],{"type":33,"value":2561}," products) {\n",{"type":27,"tag":469,"props":2563,"children":2564},{"class":471,"line":614},[2565,2570,2575,2579,2583,2588,2592,2597],{"type":27,"tag":469,"props":2566,"children":2567},{"style":508},[2568],{"type":33,"value":2569},"  product.stock ",{"type":27,"tag":469,"props":2571,"children":2572},{"style":486},[2573],{"type":33,"value":2574},"=",{"type":27,"tag":469,"props":2576,"children":2577},{"style":486},[2578],{"type":33,"value":722},{"type":27,"tag":469,"props":2580,"children":2581},{"style":508},[2582],{"type":33,"value":727},{"type":27,"tag":469,"props":2584,"children":2585},{"style":502},[2586],{"type":33,"value":2587},"queryOne",{"type":27,"tag":469,"props":2589,"children":2590},{"style":508},[2591],{"type":33,"value":511},{"type":27,"tag":469,"props":2593,"children":2594},{"style":538},[2595],{"type":33,"value":2596},"number",{"type":27,"tag":469,"props":2598,"children":2599},{"style":508},[2600],{"type":33,"value":521},{"type":27,"tag":469,"props":2602,"children":2603},{"class":471,"line":645},[2604,2609],{"type":27,"tag":469,"props":2605,"children":2606},{"style":775},[2607],{"type":33,"value":2608},"    'SELECT available FROM inventory WHERE product_id = ?'",{"type":27,"tag":469,"props":2610,"children":2611},{"style":508},[2612],{"type":33,"value":546},{"type":27,"tag":469,"props":2614,"children":2615},{"class":471,"line":679},[2616],{"type":27,"tag":469,"props":2617,"children":2618},{"style":508},[2619],{"type":33,"value":2620},"    [product.id],\n",{"type":27,"tag":469,"props":2622,"children":2623},{"class":471,"line":688},[2624],{"type":27,"tag":469,"props":2625,"children":2626},{"style":508},[2627],{"type":33,"value":2628},"  )\n",{"type":27,"tag":469,"props":2630,"children":2631},{"class":471,"line":702},[2632],{"type":27,"tag":469,"props":2633,"children":2634},{"style":508},[2635],{"type":33,"value":1070},{"type":27,"tag":35,"props":2637,"children":2638},{},[2639],{"type":33,"value":2640},"如果列表一次有 30 个商品，这段代码就是 31 次查询。",{"type":27,"tag":260,"props":2642,"children":2644},{"id":2643},"更好的方案批量查询-映射回填",[2645],{"type":33,"value":2646},"更好的方案：批量查询 + 映射回填",{"type":27,"tag":459,"props":2648,"children":2650},{"className":461,"code":2649,"language":463,"meta":7,"style":7},"const products = await db.query\u003CProduct>(\n  'SELECT id, title FROM products WHERE category_id = ?',\n  [categoryId],\n)\n\nconst productIds = products.map(item => item.id)\n\nconst inventories = await db.query\u003C{ product_id: number; available: number }>(\n  `SELECT product_id, available\n   FROM inventory\n   WHERE product_id IN (?)`,\n  [productIds],\n)\n\nconst inventoryMap = new Map(\n  inventories.map(item => [item.product_id, item.available]),\n)\n\nreturn products.map(product => ({\n  ...product,\n  stock: inventoryMap.get(product.id) ?? 0,\n}))\n",[2651],{"type":27,"tag":125,"props":2652,"children":2653},{"__ignoreMap":7},[2654,2693,2704,2711,2718,2725,2770,2777,2847,2855,2863,2875,2883,2890,2897,2928,2957,2964,2971,3005,3018,3050],{"type":27,"tag":469,"props":2655,"children":2656},{"class":471,"line":472},[2657,2661,2665,2669,2673,2677,2681,2685,2689],{"type":27,"tag":469,"props":2658,"children":2659},{"style":486},[2660],{"type":33,"value":2460},{"type":27,"tag":469,"props":2662,"children":2663},{"style":538},[2664],{"type":33,"value":2465},{"type":27,"tag":469,"props":2666,"children":2667},{"style":486},[2668],{"type":33,"value":661},{"type":27,"tag":469,"props":2670,"children":2671},{"style":486},[2672],{"type":33,"value":722},{"type":27,"tag":469,"props":2674,"children":2675},{"style":508},[2676],{"type":33,"value":727},{"type":27,"tag":469,"props":2678,"children":2679},{"style":502},[2680],{"type":33,"value":732},{"type":27,"tag":469,"props":2682,"children":2683},{"style":508},[2684],{"type":33,"value":511},{"type":27,"tag":469,"props":2686,"children":2687},{"style":502},[2688],{"type":33,"value":2490},{"type":27,"tag":469,"props":2690,"children":2691},{"style":508},[2692],{"type":33,"value":521},{"type":27,"tag":469,"props":2694,"children":2695},{"class":471,"line":482},[2696,2700],{"type":27,"tag":469,"props":2697,"children":2698},{"style":775},[2699],{"type":33,"value":2502},{"type":27,"tag":469,"props":2701,"children":2702},{"style":508},[2703],{"type":33,"value":546},{"type":27,"tag":469,"props":2705,"children":2706},{"class":471,"line":20},[2707],{"type":27,"tag":469,"props":2708,"children":2709},{"style":508},[2710],{"type":33,"value":2514},{"type":27,"tag":469,"props":2712,"children":2713},{"class":471,"line":549},[2714],{"type":27,"tag":469,"props":2715,"children":2716},{"style":508},[2717],{"type":33,"value":2522},{"type":27,"tag":469,"props":2719,"children":2720},{"class":471,"line":570},[2721],{"type":27,"tag":469,"props":2722,"children":2723},{"emptyLinePlaceholder":18},[2724],{"type":33,"value":685},{"type":27,"tag":469,"props":2726,"children":2727},{"class":471,"line":593},[2728,2732,2737,2741,2746,2751,2755,2760,2765],{"type":27,"tag":469,"props":2729,"children":2730},{"style":486},[2731],{"type":33,"value":2460},{"type":27,"tag":469,"props":2733,"children":2734},{"style":538},[2735],{"type":33,"value":2736}," productIds",{"type":27,"tag":469,"props":2738,"children":2739},{"style":486},[2740],{"type":33,"value":661},{"type":27,"tag":469,"props":2742,"children":2743},{"style":508},[2744],{"type":33,"value":2745}," products.",{"type":27,"tag":469,"props":2747,"children":2748},{"style":502},[2749],{"type":33,"value":2750},"map",{"type":27,"tag":469,"props":2752,"children":2753},{"style":508},[2754],{"type":33,"value":772},{"type":27,"tag":469,"props":2756,"children":2757},{"style":527},[2758],{"type":33,"value":2759},"item",{"type":27,"tag":469,"props":2761,"children":2762},{"style":486},[2763],{"type":33,"value":2764}," =>",{"type":27,"tag":469,"props":2766,"children":2767},{"style":508},[2768],{"type":33,"value":2769}," item.id)\n",{"type":27,"tag":469,"props":2771,"children":2772},{"class":471,"line":614},[2773],{"type":27,"tag":469,"props":2774,"children":2775},{"emptyLinePlaceholder":18},[2776],{"type":33,"value":685},{"type":27,"tag":469,"props":2778,"children":2779},{"class":471,"line":645},[2780,2784,2789,2793,2797,2801,2805,2810,2815,2819,2824,2829,2834,2838,2842],{"type":27,"tag":469,"props":2781,"children":2782},{"style":486},[2783],{"type":33,"value":2460},{"type":27,"tag":469,"props":2785,"children":2786},{"style":538},[2787],{"type":33,"value":2788}," inventories",{"type":27,"tag":469,"props":2790,"children":2791},{"style":486},[2792],{"type":33,"value":661},{"type":27,"tag":469,"props":2794,"children":2795},{"style":486},[2796],{"type":33,"value":722},{"type":27,"tag":469,"props":2798,"children":2799},{"style":508},[2800],{"type":33,"value":727},{"type":27,"tag":469,"props":2802,"children":2803},{"style":502},[2804],{"type":33,"value":732},{"type":27,"tag":469,"props":2806,"children":2807},{"style":508},[2808],{"type":33,"value":2809},"\u003C{ ",{"type":27,"tag":469,"props":2811,"children":2812},{"style":527},[2813],{"type":33,"value":2814},"product_id",{"type":27,"tag":469,"props":2816,"children":2817},{"style":486},[2818],{"type":33,"value":535},{"type":27,"tag":469,"props":2820,"children":2821},{"style":538},[2822],{"type":33,"value":2823}," number",{"type":27,"tag":469,"props":2825,"children":2826},{"style":508},[2827],{"type":33,"value":2828},"; ",{"type":27,"tag":469,"props":2830,"children":2831},{"style":527},[2832],{"type":33,"value":2833},"available",{"type":27,"tag":469,"props":2835,"children":2836},{"style":486},[2837],{"type":33,"value":535},{"type":27,"tag":469,"props":2839,"children":2840},{"style":538},[2841],{"type":33,"value":2823},{"type":27,"tag":469,"props":2843,"children":2844},{"style":508},[2845],{"type":33,"value":2846}," }>(\n",{"type":27,"tag":469,"props":2848,"children":2849},{"class":471,"line":679},[2850],{"type":27,"tag":469,"props":2851,"children":2852},{"style":775},[2853],{"type":33,"value":2854},"  `SELECT product_id, available\n",{"type":27,"tag":469,"props":2856,"children":2857},{"class":471,"line":688},[2858],{"type":27,"tag":469,"props":2859,"children":2860},{"style":775},[2861],{"type":33,"value":2862},"   FROM inventory\n",{"type":27,"tag":469,"props":2864,"children":2865},{"class":471,"line":702},[2866,2871],{"type":27,"tag":469,"props":2867,"children":2868},{"style":775},[2869],{"type":33,"value":2870},"   WHERE product_id IN (?)`",{"type":27,"tag":469,"props":2872,"children":2873},{"style":508},[2874],{"type":33,"value":546},{"type":27,"tag":469,"props":2876,"children":2877},{"class":471,"line":748},[2878],{"type":27,"tag":469,"props":2879,"children":2880},{"style":508},[2881],{"type":33,"value":2882},"  [productIds],\n",{"type":27,"tag":469,"props":2884,"children":2885},{"class":471,"line":756},[2886],{"type":27,"tag":469,"props":2887,"children":2888},{"style":508},[2889],{"type":33,"value":2522},{"type":27,"tag":469,"props":2891,"children":2892},{"class":471,"line":786},[2893],{"type":27,"tag":469,"props":2894,"children":2895},{"emptyLinePlaceholder":18},[2896],{"type":33,"value":685},{"type":27,"tag":469,"props":2898,"children":2899},{"class":471,"line":795},[2900,2904,2909,2913,2918,2923],{"type":27,"tag":469,"props":2901,"children":2902},{"style":486},[2903],{"type":33,"value":2460},{"type":27,"tag":469,"props":2905,"children":2906},{"style":538},[2907],{"type":33,"value":2908}," inventoryMap",{"type":27,"tag":469,"props":2910,"children":2911},{"style":486},[2912],{"type":33,"value":661},{"type":27,"tag":469,"props":2914,"children":2915},{"style":486},[2916],{"type":33,"value":2917}," new",{"type":27,"tag":469,"props":2919,"children":2920},{"style":502},[2921],{"type":33,"value":2922}," Map",{"type":27,"tag":469,"props":2924,"children":2925},{"style":508},[2926],{"type":33,"value":2927},"(\n",{"type":27,"tag":469,"props":2929,"children":2930},{"class":471,"line":804},[2931,2936,2940,2944,2948,2952],{"type":27,"tag":469,"props":2932,"children":2933},{"style":508},[2934],{"type":33,"value":2935},"  inventories.",{"type":27,"tag":469,"props":2937,"children":2938},{"style":502},[2939],{"type":33,"value":2750},{"type":27,"tag":469,"props":2941,"children":2942},{"style":508},[2943],{"type":33,"value":772},{"type":27,"tag":469,"props":2945,"children":2946},{"style":527},[2947],{"type":33,"value":2759},{"type":27,"tag":469,"props":2949,"children":2950},{"style":486},[2951],{"type":33,"value":2764},{"type":27,"tag":469,"props":2953,"children":2954},{"style":508},[2955],{"type":33,"value":2956}," [item.product_id, item.available]),\n",{"type":27,"tag":469,"props":2958,"children":2959},{"class":471,"line":813},[2960],{"type":27,"tag":469,"props":2961,"children":2962},{"style":508},[2963],{"type":33,"value":2522},{"type":27,"tag":469,"props":2965,"children":2966},{"class":471,"line":21},[2967],{"type":27,"tag":469,"props":2968,"children":2969},{"emptyLinePlaceholder":18},[2970],{"type":33,"value":685},{"type":27,"tag":469,"props":2972,"children":2973},{"class":471,"line":858},[2974,2979,2983,2987,2991,2996,3000],{"type":27,"tag":469,"props":2975,"children":2976},{"style":486},[2977],{"type":33,"value":2978},"return",{"type":27,"tag":469,"props":2980,"children":2981},{"style":508},[2982],{"type":33,"value":2745},{"type":27,"tag":469,"props":2984,"children":2985},{"style":502},[2986],{"type":33,"value":2750},{"type":27,"tag":469,"props":2988,"children":2989},{"style":508},[2990],{"type":33,"value":772},{"type":27,"tag":469,"props":2992,"children":2993},{"style":527},[2994],{"type":33,"value":2995},"product",{"type":27,"tag":469,"props":2997,"children":2998},{"style":486},[2999],{"type":33,"value":2764},{"type":27,"tag":469,"props":3001,"children":3002},{"style":508},[3003],{"type":33,"value":3004}," ({\n",{"type":27,"tag":469,"props":3006,"children":3007},{"class":471,"line":867},[3008,3013],{"type":27,"tag":469,"props":3009,"children":3010},{"style":486},[3011],{"type":33,"value":3012},"  ...",{"type":27,"tag":469,"props":3014,"children":3015},{"style":508},[3016],{"type":33,"value":3017},"product,\n",{"type":27,"tag":469,"props":3019,"children":3020},{"class":471,"line":875},[3021,3026,3031,3036,3041,3046],{"type":27,"tag":469,"props":3022,"children":3023},{"style":508},[3024],{"type":33,"value":3025},"  stock: inventoryMap.",{"type":27,"tag":469,"props":3027,"children":3028},{"style":502},[3029],{"type":33,"value":3030},"get",{"type":27,"tag":469,"props":3032,"children":3033},{"style":508},[3034],{"type":33,"value":3035},"(product.id) ",{"type":27,"tag":469,"props":3037,"children":3038},{"style":486},[3039],{"type":33,"value":3040},"??",{"type":27,"tag":469,"props":3042,"children":3043},{"style":538},[3044],{"type":33,"value":3045}," 0",{"type":27,"tag":469,"props":3047,"children":3048},{"style":508},[3049],{"type":33,"value":546},{"type":27,"tag":469,"props":3051,"children":3052},{"class":471,"line":889},[3053],{"type":27,"tag":469,"props":3054,"children":3055},{"style":508},[3056],{"type":33,"value":3057},"}))\n",{"type":27,"tag":35,"props":3059,"children":3060},{},[3061,3063,3069],{"type":33,"value":3062},"这时候，",{"type":27,"tag":125,"props":3064,"children":3066},{"className":3065},[],[3067],{"type":33,"value":3068},"inventory",{"type":33,"value":3070}," 表至少要有：",{"type":27,"tag":459,"props":3072,"children":3074},{"className":1391,"code":3073,"language":1393,"meta":7,"style":7},"CREATE INDEX idx_inventory_product_id ON inventory (product_id);\n",[3075],{"type":27,"tag":125,"props":3076,"children":3077},{"__ignoreMap":7},[3078],{"type":27,"tag":469,"props":3079,"children":3080},{"class":471,"line":472},[3081],{"type":27,"tag":469,"props":3082,"children":3083},{},[3084],{"type":33,"value":3073},{"type":27,"tag":35,"props":3086,"children":3087},{},[3088,3090,3095],{"type":33,"value":3089},"这里最关键的不是“有没有索引”，而是",{"type":27,"tag":96,"props":3091,"children":3092},{},[3093],{"type":33,"value":3094},"先把查询次数打下来",{"type":33,"value":1294},{"type":27,"tag":35,"props":3097,"children":3098},{},[3099],{"type":33,"value":3100},"在一个推荐接口里，我们曾把 54 次查询压到 6 次，接口耗时直接从 1.4s 降到 210ms。单条 SQL 优化反而只是次要收益。",{"type":27,"tag":253,"props":3102,"children":3104},{"id":3103},"场景-3财务对账页为什么老是卡死",[3105],{"type":33,"value":3106},"场景 3：财务对账页为什么老是卡死",{"type":27,"tag":35,"props":3108,"children":3109},{},[3110],{"type":33,"value":3111},"对账页常见写法：",{"type":27,"tag":459,"props":3113,"children":3115},{"className":1391,"code":3114,"language":1393,"meta":7,"style":7},"SELECT *\nFROM settlement_records\nWHERE DATE(created_at) = '2026-03-01'\n  AND merchant_id = 8899;\n",[3116],{"type":27,"tag":125,"props":3117,"children":3118},{"__ignoreMap":7},[3119,3127,3135,3143],{"type":27,"tag":469,"props":3120,"children":3121},{"class":471,"line":472},[3122],{"type":27,"tag":469,"props":3123,"children":3124},{},[3125],{"type":33,"value":3126},"SELECT *\n",{"type":27,"tag":469,"props":3128,"children":3129},{"class":471,"line":482},[3130],{"type":27,"tag":469,"props":3131,"children":3132},{},[3133],{"type":33,"value":3134},"FROM settlement_records\n",{"type":27,"tag":469,"props":3136,"children":3137},{"class":471,"line":20},[3138],{"type":27,"tag":469,"props":3139,"children":3140},{},[3141],{"type":33,"value":3142},"WHERE DATE(created_at) = '2026-03-01'\n",{"type":27,"tag":469,"props":3144,"children":3145},{"class":471,"line":549},[3146],{"type":27,"tag":469,"props":3147,"children":3148},{},[3149],{"type":33,"value":3150},"  AND merchant_id = 8899;\n",{"type":27,"tag":35,"props":3152,"children":3153},{},[3154],{"type":33,"value":3155},"这个写法有两个问题：",{"type":27,"tag":162,"props":3157,"children":3158},{},[3159,3169],{"type":27,"tag":65,"props":3160,"children":3161},{},[3162,3167],{"type":27,"tag":125,"props":3163,"children":3165},{"className":3164},[],[3166],{"type":33,"value":130},{"type":33,"value":3168}," 很宽",{"type":27,"tag":65,"props":3170,"children":3171},{},[3172,3174],{"type":33,"value":3173},"在索引列上用了函数 ",{"type":27,"tag":125,"props":3175,"children":3177},{"className":3176},[],[3178],{"type":33,"value":3179},"DATE(created_at)",{"type":27,"tag":35,"props":3181,"children":3182},{},[3183],{"type":33,"value":3184},"更合理的改法：",{"type":27,"tag":459,"props":3186,"children":3188},{"className":1391,"code":3187,"language":1393,"meta":7,"style":7},"SELECT id, merchant_id, amount, status, created_at\nFROM settlement_records\nWHERE merchant_id = 8899\n  AND created_at >= '2026-03-01 00:00:00'\n  AND created_at \u003C '2026-03-02 00:00:00';\n",[3189],{"type":27,"tag":125,"props":3190,"children":3191},{"__ignoreMap":7},[3192,3200,3207,3215,3223],{"type":27,"tag":469,"props":3193,"children":3194},{"class":471,"line":472},[3195],{"type":27,"tag":469,"props":3196,"children":3197},{},[3198],{"type":33,"value":3199},"SELECT id, merchant_id, amount, status, created_at\n",{"type":27,"tag":469,"props":3201,"children":3202},{"class":471,"line":482},[3203],{"type":27,"tag":469,"props":3204,"children":3205},{},[3206],{"type":33,"value":3134},{"type":27,"tag":469,"props":3208,"children":3209},{"class":471,"line":20},[3210],{"type":27,"tag":469,"props":3211,"children":3212},{},[3213],{"type":33,"value":3214},"WHERE merchant_id = 8899\n",{"type":27,"tag":469,"props":3216,"children":3217},{"class":471,"line":549},[3218],{"type":27,"tag":469,"props":3219,"children":3220},{},[3221],{"type":33,"value":3222},"  AND created_at >= '2026-03-01 00:00:00'\n",{"type":27,"tag":469,"props":3224,"children":3225},{"class":471,"line":570},[3226],{"type":27,"tag":469,"props":3227,"children":3228},{},[3229],{"type":33,"value":3230},"  AND created_at \u003C '2026-03-02 00:00:00';\n",{"type":27,"tag":35,"props":3232,"children":3233},{},[3234],{"type":33,"value":2371},{"type":27,"tag":459,"props":3236,"children":3238},{"className":1391,"code":3237,"language":1393,"meta":7,"style":7},"CREATE INDEX idx_settlement_merchant_created_at\nON settlement_records (merchant_id, created_at);\n",[3239],{"type":27,"tag":125,"props":3240,"children":3241},{"__ignoreMap":7},[3242,3250],{"type":27,"tag":469,"props":3243,"children":3244},{"class":471,"line":472},[3245],{"type":27,"tag":469,"props":3246,"children":3247},{},[3248],{"type":33,"value":3249},"CREATE INDEX idx_settlement_merchant_created_at\n",{"type":27,"tag":469,"props":3251,"children":3252},{"class":471,"line":482},[3253],{"type":27,"tag":469,"props":3254,"children":3255},{},[3256],{"type":33,"value":3257},"ON settlement_records (merchant_id, created_at);\n",{"type":27,"tag":35,"props":3259,"children":3260},{},[3261,3263,3268],{"type":33,"value":3262},"优化后，这类查询最明显的收益不是“平均快一点”，而是",{"type":27,"tag":96,"props":3264,"children":3265},{},[3266],{"type":33,"value":3267},"峰值更稳",{"type":33,"value":3269},"。财务系统讨厌的从来不是 120ms 和 80ms 的差距，而是偶发 8 秒。",{"type":27,"tag":204,"props":3271,"children":3272},{},[],{"type":27,"tag":208,"props":3274,"children":3276},{"id":3275},"七覆盖索引为什么它经常比再加机器更划算",[3277],{"type":33,"value":3278},"七、覆盖索引：为什么它经常比“再加机器”更划算",{"type":27,"tag":253,"props":3280,"children":3282},{"id":3281},"_71-什么叫回表",[3283],{"type":33,"value":3284},"7.1 什么叫回表",{"type":27,"tag":35,"props":3286,"children":3287},{},[3288],{"type":33,"value":3289},"如果索引里只存了筛选列和主键，而你查询时还要拿别的字段，数据库通常要：",{"type":27,"tag":162,"props":3291,"children":3292},{},[3293,3298],{"type":27,"tag":65,"props":3294,"children":3295},{},[3296],{"type":33,"value":3297},"先走索引定位到主键",{"type":27,"tag":65,"props":3299,"children":3300},{},[3301],{"type":33,"value":3302},"再回到数据页把完整行取出来",{"type":27,"tag":35,"props":3304,"children":3305},{},[3306],{"type":33,"value":3307},"这一步就叫回表。",{"type":27,"tag":35,"props":3309,"children":3310},{},[3311],{"type":33,"value":3312},"回表不是罪恶，但当返回记录多、访问频繁、缓存命中又一般时，它会成为稳定性问题。",{"type":27,"tag":253,"props":3314,"children":3316},{"id":3315},"_72-一个典型优化",[3317],{"type":33,"value":3318},"7.2 一个典型优化",{"type":27,"tag":35,"props":3320,"children":3321},{},[3322],{"type":33,"value":3323},"原查询：",{"type":27,"tag":459,"props":3325,"children":3327},{"className":1391,"code":3326,"language":1393,"meta":7,"style":7},"SELECT id, title, status, created_at\nFROM articles\nWHERE tenant_id = 1024\n  AND status = 'published'\nORDER BY created_at DESC\nLIMIT 20;\n",[3328],{"type":27,"tag":125,"props":3329,"children":3330},{"__ignoreMap":7},[3331,3339,3347,3354,3362,3369],{"type":27,"tag":469,"props":3332,"children":3333},{"class":471,"line":472},[3334],{"type":27,"tag":469,"props":3335,"children":3336},{},[3337],{"type":33,"value":3338},"SELECT id, title, status, created_at\n",{"type":27,"tag":469,"props":3340,"children":3341},{"class":471,"line":482},[3342],{"type":27,"tag":469,"props":3343,"children":3344},{},[3345],{"type":33,"value":3346},"FROM articles\n",{"type":27,"tag":469,"props":3348,"children":3349},{"class":471,"line":20},[3350],{"type":27,"tag":469,"props":3351,"children":3352},{},[3353],{"type":33,"value":1421},{"type":27,"tag":469,"props":3355,"children":3356},{"class":471,"line":549},[3357],{"type":27,"tag":469,"props":3358,"children":3359},{},[3360],{"type":33,"value":3361},"  AND status = 'published'\n",{"type":27,"tag":469,"props":3363,"children":3364},{"class":471,"line":570},[3365],{"type":27,"tag":469,"props":3366,"children":3367},{},[3368],{"type":33,"value":1445},{"type":27,"tag":469,"props":3370,"children":3371},{"class":471,"line":593},[3372],{"type":27,"tag":469,"props":3373,"children":3374},{},[3375],{"type":33,"value":2007},{"type":27,"tag":35,"props":3377,"children":3378},{},[3379],{"type":33,"value":3380},"如果索引只有：",{"type":27,"tag":459,"props":3382,"children":3383},{"className":1391,"code":2103,"language":1393,"meta":7,"style":7},[3384],{"type":27,"tag":125,"props":3385,"children":3386},{"__ignoreMap":7},[3387],{"type":27,"tag":469,"props":3388,"children":3389},{"class":471,"line":472},[3390],{"type":27,"tag":469,"props":3391,"children":3392},{},[3393],{"type":33,"value":2103},{"type":27,"tag":35,"props":3395,"children":3396},{},[3397,3399,3405],{"type":33,"value":3398},"那 ",{"type":27,"tag":125,"props":3400,"children":3402},{"className":3401},[],[3403],{"type":33,"value":3404},"title",{"type":33,"value":3406}," 可能仍需要回表。",{"type":27,"tag":35,"props":3408,"children":3409},{},[3410],{"type":33,"value":3411},"在读多写少的场景里，可以考虑把查询需要的轻量字段也纳入索引：",{"type":27,"tag":459,"props":3413,"children":3415},{"className":1391,"code":3414,"language":1393,"meta":7,"style":7},"CREATE INDEX idx_articles_tenant_status_created_title\nON articles (tenant_id, status, created_at DESC, title);\n",[3416],{"type":27,"tag":125,"props":3417,"children":3418},{"__ignoreMap":7},[3419,3427],{"type":27,"tag":469,"props":3420,"children":3421},{"class":471,"line":472},[3422],{"type":27,"tag":469,"props":3423,"children":3424},{},[3425],{"type":33,"value":3426},"CREATE INDEX idx_articles_tenant_status_created_title\n",{"type":27,"tag":469,"props":3428,"children":3429},{"class":471,"line":482},[3430],{"type":27,"tag":469,"props":3431,"children":3432},{},[3433],{"type":33,"value":3434},"ON articles (tenant_id, status, created_at DESC, title);\n",{"type":27,"tag":35,"props":3436,"children":3437},{},[3438],{"type":33,"value":3439},"这样做的代价：",{"type":27,"tag":61,"props":3441,"children":3442},{},[3443,3448,3453],{"type":27,"tag":65,"props":3444,"children":3445},{},[3446],{"type":33,"value":3447},"索引更大",{"type":27,"tag":65,"props":3449,"children":3450},{},[3451],{"type":33,"value":3452},"写入更慢",{"type":27,"tag":65,"props":3454,"children":3455},{},[3456],{"type":33,"value":3457},"内存占用更高",{"type":27,"tag":35,"props":3459,"children":3460},{},[3461],{"type":33,"value":3462},"但收益也很直接：",{"type":27,"tag":61,"props":3464,"children":3465},{},[3466,3471,3476],{"type":27,"tag":65,"props":3467,"children":3468},{},[3469],{"type":33,"value":3470},"读取更稳",{"type":27,"tag":65,"props":3472,"children":3473},{},[3474],{"type":33,"value":3475},"回表更少",{"type":27,"tag":65,"props":3477,"children":3478},{},[3479],{"type":33,"value":3480},"热门列表页性能更可控",{"type":27,"tag":253,"props":3482,"children":3484},{"id":3483},"_73-什么时候值得做覆盖索引",[3485],{"type":33,"value":3486},"7.3 什么时候值得做覆盖索引",{"type":27,"tag":35,"props":3488,"children":3489},{},[3490],{"type":33,"value":3491},"适合覆盖索引的通常是：",{"type":27,"tag":61,"props":3493,"children":3494},{},[3495,3500,3505,3510],{"type":27,"tag":65,"props":3496,"children":3497},{},[3498],{"type":33,"value":3499},"读多写少",{"type":27,"tag":65,"props":3501,"children":3502},{},[3503],{"type":33,"value":3504},"返回列比较少",{"type":27,"tag":65,"props":3506,"children":3507},{},[3508],{"type":33,"value":3509},"高频列表页或推荐流",{"type":27,"tag":65,"props":3511,"children":3512},{},[3513],{"type":33,"value":3514},"业务对尾延迟敏感",{"type":27,"tag":35,"props":3516,"children":3517},{},[3518],{"type":33,"value":3519},"不适合的通常是：",{"type":27,"tag":61,"props":3521,"children":3522},{},[3523,3528,3533,3538],{"type":27,"tag":65,"props":3524,"children":3525},{},[3526],{"type":33,"value":3527},"宽字段很多",{"type":27,"tag":65,"props":3529,"children":3530},{},[3531],{"type":33,"value":3532},"写入非常频繁",{"type":27,"tag":65,"props":3534,"children":3535},{},[3536],{"type":33,"value":3537},"查询模式变化大",{"type":27,"tag":65,"props":3539,"children":3540},{},[3541],{"type":33,"value":3542},"只是偶发报表，不值得为它长期维护大索引",{"type":27,"tag":204,"props":3544,"children":3545},{},[],{"type":27,"tag":208,"props":3547,"children":3549},{"id":3548},"八排序范围查询分页一起出现时为什么最容易翻车",[3550],{"type":33,"value":3551},"八、排序、范围查询、分页，一起出现时为什么最容易翻车",{"type":27,"tag":35,"props":3553,"children":3554},{},[3555],{"type":33,"value":3556},"这是业务里最常见的组合，也是索引设计最容易出错的地方。",{"type":27,"tag":253,"props":3558,"children":3560},{"id":3559},"_81-为什么-order-by-经常把索引打废",[3561,3563,3569],{"type":33,"value":3562},"8.1 为什么 ",{"type":27,"tag":125,"props":3564,"children":3566},{"className":3565},[],[3567],{"type":33,"value":3568},"ORDER BY",{"type":33,"value":3570}," 经常把索引打废",{"type":27,"tag":35,"props":3572,"children":3573},{},[3574,3576,3582,3584,3590],{"type":33,"value":3575},"很多人只为 ",{"type":27,"tag":125,"props":3577,"children":3579},{"className":3578},[],[3580],{"type":33,"value":3581},"WHERE",{"type":33,"value":3583}," 建索引，忘了排序也会决定数据库是否需要额外做 ",{"type":27,"tag":125,"props":3585,"children":3587},{"className":3586},[],[3588],{"type":33,"value":3589},"filesort",{"type":33,"value":1294},{"type":27,"tag":35,"props":3592,"children":3593},{},[3594],{"type":33,"value":3595},"例如：",{"type":27,"tag":459,"props":3597,"children":3599},{"className":1391,"code":3598,"language":1393,"meta":7,"style":7},"SELECT id, created_at, status\nFROM orders\nWHERE tenant_id = 1024\n  AND status = 'paid'\nORDER BY updated_at DESC\nLIMIT 20;\n",[3600],{"type":27,"tag":125,"props":3601,"children":3602},{"__ignoreMap":7},[3603,3611,3618,3625,3632,3640],{"type":27,"tag":469,"props":3604,"children":3605},{"class":471,"line":472},[3606],{"type":27,"tag":469,"props":3607,"children":3608},{},[3609],{"type":33,"value":3610},"SELECT id, created_at, status\n",{"type":27,"tag":469,"props":3612,"children":3613},{"class":471,"line":482},[3614],{"type":27,"tag":469,"props":3615,"children":3616},{},[3617],{"type":33,"value":1413},{"type":27,"tag":469,"props":3619,"children":3620},{"class":471,"line":20},[3621],{"type":27,"tag":469,"props":3622,"children":3623},{},[3624],{"type":33,"value":1421},{"type":27,"tag":469,"props":3626,"children":3627},{"class":471,"line":549},[3628],{"type":27,"tag":469,"props":3629,"children":3630},{},[3631],{"type":33,"value":1429},{"type":27,"tag":469,"props":3633,"children":3634},{"class":471,"line":570},[3635],{"type":27,"tag":469,"props":3636,"children":3637},{},[3638],{"type":33,"value":3639},"ORDER BY updated_at DESC\n",{"type":27,"tag":469,"props":3641,"children":3642},{"class":471,"line":593},[3643],{"type":27,"tag":469,"props":3644,"children":3645},{},[3646],{"type":33,"value":2007},{"type":27,"tag":35,"props":3648,"children":3649},{},[3650],{"type":33,"value":3651},"如果你的索引是：",{"type":27,"tag":459,"props":3653,"children":3654},{"className":1391,"code":2103,"language":1393,"meta":7,"style":7},[3655],{"type":27,"tag":125,"props":3656,"children":3657},{"__ignoreMap":7},[3658],{"type":27,"tag":469,"props":3659,"children":3660},{"class":471,"line":472},[3661],{"type":27,"tag":469,"props":3662,"children":3663},{},[3664],{"type":33,"value":2103},{"type":27,"tag":35,"props":3666,"children":3667},{},[3668,3670,3676],{"type":33,"value":3669},"那过滤可能不错，但排序列是 ",{"type":27,"tag":125,"props":3671,"children":3673},{"className":3672},[],[3674],{"type":33,"value":3675},"updated_at",{"type":33,"value":3677},"，数据库大概率还得额外排序。",{"type":27,"tag":253,"props":3679,"children":3681},{"id":3680},"_82-设计时要问自己我到底在优化过滤还是在优化排序",[3682],{"type":33,"value":3683},"8.2 设计时要问自己：我到底在优化过滤，还是在优化排序",{"type":27,"tag":35,"props":3685,"children":3686},{},[3687],{"type":33,"value":3688},"这不是一句废话，而是一个取舍问题。",{"type":27,"tag":35,"props":3690,"children":3691},{},[3692],{"type":33,"value":3693},"如果一个列表页：",{"type":27,"tag":61,"props":3695,"children":3696},{},[3697,3702,3707],{"type":27,"tag":65,"props":3698,"children":3699},{},[3700],{"type":33,"value":3701},"过滤条件非常稳定",{"type":27,"tag":65,"props":3703,"children":3704},{},[3705],{"type":33,"value":3706},"排序字段也很固定",{"type":27,"tag":65,"props":3708,"children":3709},{},[3710],{"type":33,"value":3711},"访问量很大",{"type":27,"tag":35,"props":3713,"children":3714},{},[3715],{"type":33,"value":3716},"那就值得为它单独设计一条索引路径。",{"type":27,"tag":35,"props":3718,"children":3719},{},[3720],{"type":33,"value":3721},"如果查询形态千变万化，指望一条万能索引全部搞定，通常不现实。",{"type":27,"tag":253,"props":3723,"children":3725},{"id":3724},"_83-范围条件为什么会截断后续索引收益",[3726],{"type":33,"value":3727},"8.3 范围条件为什么会“截断”后续索引收益",{"type":27,"tag":35,"props":3729,"children":3730},{},[3731],{"type":33,"value":2100},{"type":27,"tag":459,"props":3733,"children":3735},{"className":1391,"code":3734,"language":1393,"meta":7,"style":7},"(tenant_id, created_at, status)\n",[3736],{"type":27,"tag":125,"props":3737,"children":3738},{"__ignoreMap":7},[3739],{"type":27,"tag":469,"props":3740,"children":3741},{"class":471,"line":472},[3742],{"type":27,"tag":469,"props":3743,"children":3744},{},[3745],{"type":33,"value":3734},{"type":27,"tag":35,"props":3747,"children":3748},{},[3749],{"type":33,"value":3750},"查询：",{"type":27,"tag":459,"props":3752,"children":3754},{"className":1391,"code":3753,"language":1393,"meta":7,"style":7},"WHERE tenant_id = 1024\n  AND created_at >= '2026-03-01'\n  AND status = 'paid'\n",[3755],{"type":27,"tag":125,"props":3756,"children":3757},{"__ignoreMap":7},[3758,3765,3773],{"type":27,"tag":469,"props":3759,"children":3760},{"class":471,"line":472},[3761],{"type":27,"tag":469,"props":3762,"children":3763},{},[3764],{"type":33,"value":1421},{"type":27,"tag":469,"props":3766,"children":3767},{"class":471,"line":482},[3768],{"type":27,"tag":469,"props":3769,"children":3770},{},[3771],{"type":33,"value":3772},"  AND created_at >= '2026-03-01'\n",{"type":27,"tag":469,"props":3774,"children":3775},{"class":471,"line":20},[3776],{"type":27,"tag":469,"props":3777,"children":3778},{},[3779],{"type":33,"value":1429},{"type":27,"tag":35,"props":3781,"children":3782},{},[3783,3785,3790,3792,3797],{"type":33,"value":3784},"一旦 ",{"type":27,"tag":125,"props":3786,"children":3788},{"className":3787},[],[3789],{"type":33,"value":2074},{"type":33,"value":3791}," 这里进入范围扫描，后面的 ",{"type":27,"tag":125,"props":3793,"children":3795},{"className":3794},[],[3796],{"type":33,"value":2063},{"type":33,"value":3798}," 能否继续充分利用，要看数据库实现和具体执行计划。工程上不要想当然，必须看实际计划。",{"type":27,"tag":35,"props":3800,"children":3801},{},[3802,3807],{"type":27,"tag":96,"props":3803,"children":3804},{},[3805],{"type":33,"value":3806},"经验上",{"type":33,"value":3808},"：范围列通常放在联合索引靠后位置，更利于前面的等值过滤先缩小范围。",{"type":27,"tag":204,"props":3810,"children":3811},{},[],{"type":27,"tag":208,"props":3813,"children":3815},{"id":3814},"九别只盯-sql很多性能问题其实是数据模型在反噬你",[3816],{"type":33,"value":3817},"九、别只盯 SQL，很多性能问题其实是“数据模型在反噬你”",{"type":27,"tag":253,"props":3819,"children":3821},{"id":3820},"_91-一个表既服务在线查询又服务分析报表",[3822],{"type":33,"value":3823},"9.1 一个表既服务在线查询，又服务分析报表",{"type":27,"tag":35,"props":3825,"children":3826},{},[3827],{"type":33,"value":3828},"这是很常见的历史包袱。",{"type":27,"tag":35,"props":3830,"children":3831},{},[3832],{"type":33,"value":3833},"订单主表既给用户查列表，又给财务跑对账，又给 BI 做周报。最后结果是：",{"type":27,"tag":61,"props":3835,"children":3836},{},[3837,3842,3847,3852],{"type":27,"tag":65,"props":3838,"children":3839},{},[3840],{"type":33,"value":3841},"字段越来越多",{"type":27,"tag":65,"props":3843,"children":3844},{},[3845],{"type":33,"value":3846},"索引越来越多",{"type":27,"tag":65,"props":3848,"children":3849},{},[3850],{"type":33,"value":3851},"写入越来越慢",{"type":27,"tag":65,"props":3853,"children":3854},{},[3855],{"type":33,"value":3856},"查询模式互相打架",{"type":27,"tag":35,"props":3858,"children":3859},{},[3860],{"type":33,"value":3861},"这种时候继续调单条 SQL，收益有限。更应该考虑：",{"type":27,"tag":61,"props":3863,"children":3864},{},[3865,3870,3875,3880],{"type":27,"tag":65,"props":3866,"children":3867},{},[3868],{"type":33,"value":3869},"读写分离",{"type":27,"tag":65,"props":3871,"children":3872},{},[3873],{"type":33,"value":3874},"冷热数据归档",{"type":27,"tag":65,"props":3876,"children":3877},{},[3878],{"type":33,"value":3879},"宽表拆分",{"type":27,"tag":65,"props":3881,"children":3882},{},[3883],{"type":33,"value":3884},"明细表与汇总表分开",{"type":27,"tag":253,"props":3886,"children":3888},{"id":3887},"_92-索引不是越多越好",[3889],{"type":33,"value":3890},"9.2 索引不是越多越好",{"type":27,"tag":35,"props":3892,"children":3893},{},[3894],{"type":33,"value":3895},"每加一个索引，都意味着：",{"type":27,"tag":61,"props":3897,"children":3898},{},[3899,3904,3909,3914],{"type":27,"tag":65,"props":3900,"children":3901},{},[3902],{"type":33,"value":3903},"插入、更新、删除成本增加",{"type":27,"tag":65,"props":3905,"children":3906},{},[3907],{"type":33,"value":3908},"磁盘和内存占用增加",{"type":27,"tag":65,"props":3910,"children":3911},{},[3912],{"type":33,"value":3913},"优化器选择空间变复杂",{"type":27,"tag":65,"props":3915,"children":3916},{},[3917],{"type":33,"value":3918},"维护成本增加",{"type":27,"tag":35,"props":3920,"children":3921},{},[3922],{"type":33,"value":3923},"我见过一个表只有 14 个核心字段，却挂了 19 个索引。最后写入 TPS 不断抖，原因不是机器差，而是每次写入都在替一堆历史查询还债。",{"type":27,"tag":253,"props":3925,"children":3927},{"id":3926},"_93-用业务生命周期管理数据比盲目优化更有效",[3928],{"type":33,"value":3929},"9.3 用业务生命周期管理数据，比盲目优化更有效",{"type":27,"tag":35,"props":3931,"children":3932},{},[3933],{"type":33,"value":3934},"例如交易流水：",{"type":27,"tag":61,"props":3936,"children":3937},{},[3938,3943,3948],{"type":27,"tag":65,"props":3939,"children":3940},{},[3941],{"type":33,"value":3942},"近 90 天：高频在线查询，保留在热表",{"type":27,"tag":65,"props":3944,"children":3945},{},[3946],{"type":33,"value":3947},"90 天到 1 年：归档表，偶发运营查询",{"type":27,"tag":65,"props":3949,"children":3950},{},[3951],{"type":33,"value":3952},"1 年以上：冷存储或离线仓库",{"type":27,"tag":35,"props":3954,"children":3955},{},[3956],{"type":33,"value":3957},"这类治理的收益，往往大于你把一条 SQL 从 180ms 优化到 120ms。",{"type":27,"tag":204,"props":3959,"children":3960},{},[],{"type":27,"tag":208,"props":3962,"children":3964},{"id":3963},"十什么时候该上缓存什么时候不该",[3965],{"type":33,"value":3966},"十、什么时候该上缓存，什么时候不该",{"type":27,"tag":35,"props":3968,"children":3969},{},[3970],{"type":33,"value":3971},"数据库优化做到一半，团队常会问：",{"type":27,"tag":148,"props":3973,"children":3974},{},[3975],{"type":27,"tag":35,"props":3976,"children":3977},{},[3978],{"type":33,"value":3979},"要不直接上 Redis？",{"type":27,"tag":35,"props":3981,"children":3982},{},[3983,3985,3990],{"type":33,"value":3984},"缓存当然有用，但它解决的是",{"type":27,"tag":96,"props":3986,"children":3987},{},[3988],{"type":33,"value":3989},"重复读取",{"type":33,"value":3991},"问题，不是糟糕查询设计的免责卡。",{"type":27,"tag":253,"props":3993,"children":3995},{"id":3994},"_101-适合先上缓存的场景",[3996],{"type":33,"value":3997},"10.1 适合先上缓存的场景",{"type":27,"tag":61,"props":3999,"children":4000},{},[4001,4006,4011],{"type":27,"tag":65,"props":4002,"children":4003},{},[4004],{"type":33,"value":4005},"首页榜单、推荐位、标签云这类读多写少场景",{"type":27,"tag":65,"props":4007,"children":4008},{},[4009],{"type":33,"value":4010},"热点极高、可接受短时间不一致",{"type":27,"tag":65,"props":4012,"children":4013},{},[4014],{"type":33,"value":4015},"查询结果计算成本高，但更新频率低",{"type":27,"tag":253,"props":4017,"children":4019},{"id":4018},"_102-不适合拿缓存硬扛的场景",[4020],{"type":33,"value":4021},"10.2 不适合拿缓存硬扛的场景",{"type":27,"tag":61,"props":4023,"children":4024},{},[4025,4030,4035],{"type":27,"tag":65,"props":4026,"children":4027},{},[4028],{"type":33,"value":4029},"实时库存、余额、优惠资格这类强一致场景",{"type":27,"tag":65,"props":4031,"children":4032},{},[4033],{"type":33,"value":4034},"查询条件组合很多，缓存 key 爆炸",{"type":27,"tag":65,"props":4036,"children":4037},{},[4038],{"type":33,"value":4039},"底层 SQL 已经很烂，缓存只是遮羞布",{"type":27,"tag":253,"props":4041,"children":4043},{"id":4042},"_103-更实用的判断标准",[4044],{"type":33,"value":4045},"10.3 更实用的判断标准",{"type":27,"tag":35,"props":4047,"children":4048},{},[4049],{"type":33,"value":4050},"你可以先问两句：",{"type":27,"tag":162,"props":4052,"children":4053},{},[4054,4059],{"type":27,"tag":65,"props":4055,"children":4056},{},[4057],{"type":33,"value":4058},"这类请求是否高度重复？",{"type":27,"tag":65,"props":4060,"children":4061},{},[4062],{"type":33,"value":4063},"即使缓存 miss，数据库查询本身是否仍在可接受范围？",{"type":27,"tag":35,"props":4065,"children":4066},{},[4067],{"type":33,"value":4068},"如果第二个问题答案是否定的，那你应该先把数据库路径修到健康，再考虑缓存放大收益。",{"type":27,"tag":204,"props":4070,"children":4071},{},[],{"type":27,"tag":208,"props":4073,"children":4075},{"id":4074},"十一一套可执行的慢-sql-优化流程",[4076],{"type":33,"value":4077},"十一、一套可执行的慢 SQL 优化流程",{"type":27,"tag":35,"props":4079,"children":4080},{},[4081],{"type":33,"value":4082},"下面这套流程，是我更推荐团队内部固化成规范的。",{"type":27,"tag":253,"props":4084,"children":4086},{"id":4085},"第-1-步确认问题边界",[4087],{"type":33,"value":4088},"第 1 步：确认问题边界",{"type":27,"tag":61,"props":4090,"children":4091},{},[4092,4097,4102,4107],{"type":27,"tag":65,"props":4093,"children":4094},{},[4095],{"type":33,"value":4096},"影响哪些接口",{"type":27,"tag":65,"props":4098,"children":4099},{},[4100],{"type":33,"value":4101},"影响哪些用户群体",{"type":27,"tag":65,"props":4103,"children":4104},{},[4105],{"type":33,"value":4106},"峰值时段是否更明显",{"type":27,"tag":65,"props":4108,"children":4109},{},[4110],{"type":33,"value":4111},"是稳定慢还是偶发抖动",{"type":27,"tag":253,"props":4113,"children":4115},{"id":4114},"第-2-步抓到-top-sql",[4116],{"type":33,"value":4117},"第 2 步：抓到 Top SQL",{"type":27,"tag":35,"props":4119,"children":4120},{},[4121],{"type":33,"value":4122},"按接口、按模板、按总耗时排序，先抓贡献最大的。",{"type":27,"tag":35,"props":4124,"children":4125},{},[4126],{"type":33,"value":4127},"不要从“最丑的 SQL”开始，而要从“最影响业务的 SQL”开始。",{"type":27,"tag":253,"props":4129,"children":4131},{"id":4130},"第-3-步看真实调用方式",[4132],{"type":33,"value":4133},"第 3 步：看真实调用方式",{"type":27,"tag":61,"props":4135,"children":4136},{},[4137,4142,4152],{"type":27,"tag":65,"props":4138,"children":4139},{},[4140],{"type":33,"value":4141},"单次慢，还是次数多",{"type":27,"tag":65,"props":4143,"children":4144},{},[4145,4147],{"type":33,"value":4146},"同一个接口里是否存在 ",{"type":27,"tag":125,"props":4148,"children":4150},{"className":4149},[],[4151],{"type":33,"value":190},{"type":27,"tag":65,"props":4153,"children":4154},{},[4155],{"type":33,"value":4156},"查询是不是在循环中触发",{"type":27,"tag":253,"props":4158,"children":4160},{"id":4159},"第-4-步看数据分布和执行计划",[4161],{"type":33,"value":4162},"第 4 步：看数据分布和执行计划",{"type":27,"tag":61,"props":4164,"children":4165},{},[4166,4171,4176,4184,4189],{"type":27,"tag":65,"props":4167,"children":4168},{},[4169],{"type":33,"value":4170},"表总行数",{"type":27,"tag":65,"props":4172,"children":4173},{},[4174],{"type":33,"value":4175},"条件命中比例",{"type":27,"tag":65,"props":4177,"children":4178},{},[4179],{"type":27,"tag":125,"props":4180,"children":4182},{"className":4181},[],[4183],{"type":33,"value":1363},{"type":27,"tag":65,"props":4185,"children":4186},{},[4187],{"type":33,"value":4188},"扫描行数 / 返回行数",{"type":27,"tag":65,"props":4190,"children":4191},{},[4192],{"type":33,"value":4193},"是否有排序、临时表、回表",{"type":27,"tag":253,"props":4195,"children":4197},{"id":4196},"第-5-步选择优化手段",[4198],{"type":33,"value":4199},"第 5 步：选择优化手段",{"type":27,"tag":35,"props":4201,"children":4202},{},[4203],{"type":33,"value":4204},"按优先级通常是：",{"type":27,"tag":162,"props":4206,"children":4207},{},[4208,4213,4218,4223,4228,4233],{"type":27,"tag":65,"props":4209,"children":4210},{},[4211],{"type":33,"value":4212},"减少不必要查询次数",{"type":27,"tag":65,"props":4214,"children":4215},{},[4216],{"type":33,"value":4217},"改 SQL 写法",{"type":27,"tag":65,"props":4219,"children":4220},{},[4221],{"type":33,"value":4222},"调整索引结构",{"type":27,"tag":65,"props":4224,"children":4225},{},[4226],{"type":33,"value":4227},"调整分页方式",{"type":27,"tag":65,"props":4229,"children":4230},{},[4231],{"type":33,"value":4232},"加缓存或汇总层",{"type":27,"tag":65,"props":4234,"children":4235},{},[4236],{"type":33,"value":4237},"做数据归档 / 拆分",{"type":27,"tag":253,"props":4239,"children":4241},{"id":4240},"第-6-步验证收益而不是凭感觉上线",[4242],{"type":33,"value":4243},"第 6 步：验证收益，而不是凭感觉上线",{"type":27,"tag":35,"props":4245,"children":4246},{},[4247],{"type":33,"value":4248},"至少记录：",{"type":27,"tag":305,"props":4250,"children":4251},{},[4252,4274],{"type":27,"tag":309,"props":4253,"children":4254},{},[4255],{"type":27,"tag":313,"props":4256,"children":4257},{},[4258,4263,4269],{"type":27,"tag":317,"props":4259,"children":4260},{},[4261],{"type":33,"value":4262},"指标",{"type":27,"tag":317,"props":4264,"children":4266},{"align":4265},"right",[4267],{"type":33,"value":4268},"优化前",{"type":27,"tag":317,"props":4270,"children":4271},{"align":4265},[4272],{"type":33,"value":4273},"优化后",{"type":27,"tag":333,"props":4275,"children":4276},{},[4277,4295,4313,4330,4348],{"type":27,"tag":313,"props":4278,"children":4279},{},[4280,4285,4290],{"type":27,"tag":340,"props":4281,"children":4282},{},[4283],{"type":33,"value":4284},"P95 耗时",{"type":27,"tag":340,"props":4286,"children":4287},{"align":4265},[4288],{"type":33,"value":4289},"860ms",{"type":27,"tag":340,"props":4291,"children":4292},{"align":4265},[4293],{"type":33,"value":4294},"110ms",{"type":27,"tag":313,"props":4296,"children":4297},{},[4298,4303,4308],{"type":27,"tag":340,"props":4299,"children":4300},{},[4301],{"type":33,"value":4302},"扫描行数",{"type":27,"tag":340,"props":4304,"children":4305},{"align":4265},[4306],{"type":33,"value":4307},"320000",{"type":27,"tag":340,"props":4309,"children":4310},{"align":4265},[4311],{"type":33,"value":4312},"40",{"type":27,"tag":313,"props":4314,"children":4315},{},[4316,4321,4326],{"type":27,"tag":340,"props":4317,"children":4318},{},[4319],{"type":33,"value":4320},"QPS 峰值",{"type":27,"tag":340,"props":4322,"children":4323},{"align":4265},[4324],{"type":33,"value":4325},"280",{"type":27,"tag":340,"props":4327,"children":4328},{"align":4265},[4329],{"type":33,"value":4325},{"type":27,"tag":313,"props":4331,"children":4332},{},[4333,4338,4343],{"type":27,"tag":340,"props":4334,"children":4335},{},[4336],{"type":33,"value":4337},"数据库 CPU",{"type":27,"tag":340,"props":4339,"children":4340},{"align":4265},[4341],{"type":33,"value":4342},"78%",{"type":27,"tag":340,"props":4344,"children":4345},{"align":4265},[4346],{"type":33,"value":4347},"51%",{"type":27,"tag":313,"props":4349,"children":4350},{},[4351,4356,4361],{"type":27,"tag":340,"props":4352,"children":4353},{},[4354],{"type":33,"value":4355},"超时率",{"type":27,"tag":340,"props":4357,"children":4358},{"align":4265},[4359],{"type":33,"value":4360},"3.8%",{"type":27,"tag":340,"props":4362,"children":4363},{"align":4265},[4364],{"type":33,"value":4365},"0.2%",{"type":27,"tag":253,"props":4367,"children":4369},{"id":4368},"第-7-步补防回退机制",[4370],{"type":33,"value":4371},"第 7 步：补防回退机制",{"type":27,"tag":35,"props":4373,"children":4374},{},[4375],{"type":33,"value":4376},"如果这条 SQL 很关键，建议把检查写进工程体系：",{"type":27,"tag":61,"props":4378,"children":4379},{},[4380,4385,4390,4395],{"type":27,"tag":65,"props":4381,"children":4382},{},[4383],{"type":33,"value":4384},"慢查询告警阈值",{"type":27,"tag":65,"props":4386,"children":4387},{},[4388],{"type":33,"value":4389},"大表变更 review 清单",{"type":27,"tag":65,"props":4391,"children":4392},{},[4393],{"type":33,"value":4394},"新增索引评审",{"type":27,"tag":65,"props":4396,"children":4397},{},[4398],{"type":33,"value":4399},"ORM 查询次数审计",{"type":27,"tag":35,"props":4401,"children":4402},{},[4403],{"type":33,"value":4404},"否则团队很容易在 2 个月后把旧坑重新挖出来。",{"type":27,"tag":204,"props":4406,"children":4407},{},[],{"type":27,"tag":208,"props":4409,"children":4411},{"id":4410},"十二三个常见误区值得提前避坑",[4412],{"type":33,"value":4413},"十二、三个常见误区，值得提前避坑",{"type":27,"tag":253,"props":4415,"children":4417},{"id":4416},"误区-1看到慢就加索引",[4418],{"type":33,"value":4419},"误区 1：看到慢就加索引",{"type":27,"tag":35,"props":4421,"children":4422},{},[4423],{"type":33,"value":4424},"加索引之前先判断：",{"type":27,"tag":61,"props":4426,"children":4427},{},[4428,4433,4438,4443],{"type":27,"tag":65,"props":4429,"children":4430},{},[4431],{"type":33,"value":4432},"问题是不是查询次数太多",{"type":27,"tag":65,"props":4434,"children":4435},{},[4436],{"type":33,"value":4437},"原来的索引是不是顺序不对",{"type":27,"tag":65,"props":4439,"children":4440},{},[4441],{"type":33,"value":4442},"是否只是偶发锁等待",{"type":27,"tag":65,"props":4444,"children":4445},{},[4446],{"type":33,"value":4447},"这个查询是否真的值得长期维护一条索引",{"type":27,"tag":253,"props":4449,"children":4451},{"id":4450},"误区-2把测试库结果当生产结论",[4452],{"type":33,"value":4453},"误区 2：把测试库结果当生产结论",{"type":27,"tag":35,"props":4455,"children":4456},{},[4457],{"type":33,"value":4458},"测试环境几万行数据跑得快，不代表生产几千万行也一样。尤其是：",{"type":27,"tag":61,"props":4460,"children":4461},{},[4462,4467,4472,4477],{"type":27,"tag":65,"props":4463,"children":4464},{},[4465],{"type":33,"value":4466},"数据倾斜",{"type":27,"tag":65,"props":4468,"children":4469},{},[4470],{"type":33,"value":4471},"热点租户",{"type":27,"tag":65,"props":4473,"children":4474},{},[4475],{"type":33,"value":4476},"大量历史脏数据",{"type":27,"tag":65,"props":4478,"children":4479},{},[4480],{"type":33,"value":4481},"统计信息过期",{"type":27,"tag":35,"props":4483,"children":4484},{},[4485],{"type":33,"value":4486},"都可能让执行计划完全不同。",{"type":27,"tag":253,"props":4488,"children":4490},{"id":4489},"误区-3优化到-80-分却没有治理手段守住-80-分",[4491],{"type":33,"value":4492},"误区 3：优化到 80 分，却没有治理手段守住 80 分",{"type":27,"tag":35,"props":4494,"children":4495},{},[4496],{"type":33,"value":4497},"性能不是一次性项目，而是持续博弈。",{"type":27,"tag":35,"props":4499,"children":4500},{},[4501],{"type":33,"value":4502},"如果团队没有：",{"type":27,"tag":61,"props":4504,"children":4505},{},[4506,4511,4516,4521],{"type":27,"tag":65,"props":4507,"children":4508},{},[4509],{"type":33,"value":4510},"SQL review 基线",{"type":27,"tag":65,"props":4512,"children":4513},{},[4514],{"type":33,"value":4515},"慢查询巡检",{"type":27,"tag":65,"props":4517,"children":4518},{},[4519],{"type":33,"value":4520},"接口与 SQL 的链路指标",{"type":27,"tag":65,"props":4522,"children":4523},{},[4524],{"type":33,"value":4525},"大表变更约束",{"type":27,"tag":35,"props":4527,"children":4528},{},[4529],{"type":33,"value":4530},"那你今天优化完，后天又会被新需求推回原点。",{"type":27,"tag":204,"props":4532,"children":4533},{},[],{"type":27,"tag":208,"props":4535,"children":4537},{"id":4536},"十三什么时候该停下不要过度优化",[4538],{"type":33,"value":4539},"十三、什么时候该停下，不要过度优化",{"type":27,"tag":35,"props":4541,"children":4542},{},[4543],{"type":33,"value":4544},"这一点很重要。",{"type":27,"tag":35,"props":4546,"children":4547},{},[4548],{"type":33,"value":4549},"不是所有 300ms 的查询都要压到 30ms。真正的工程优化，不是“把所有数都做到极致”，而是：",{"type":27,"tag":148,"props":4551,"children":4552},{},[4553],{"type":27,"tag":35,"props":4554,"children":4555},{},[4556],{"type":33,"value":4557},"把资源花在最影响业务体验和系统稳定性的地方。",{"type":27,"tag":35,"props":4559,"children":4560},{},[4561],{"type":33,"value":4562},"下面几种情况，不建议过度投入：",{"type":27,"tag":61,"props":4564,"children":4565},{},[4566,4571,4576,4581],{"type":27,"tag":65,"props":4567,"children":4568},{},[4569],{"type":33,"value":4570},"低频后台报表，一周只用几次",{"type":27,"tag":65,"props":4572,"children":4573},{},[4574],{"type":33,"value":4575},"查询已经 80ms，但调用链其他部分要 600ms",{"type":27,"tag":65,"props":4577,"children":4578},{},[4579],{"type":33,"value":4580},"新功能还在快速变化，索引策略大概率会改",{"type":27,"tag":65,"props":4582,"children":4583},{},[4584],{"type":33,"value":4585},"优化收益小于维护成本",{"type":27,"tag":35,"props":4587,"children":4588},{},[4589],{"type":33,"value":4590},"一个成熟团队会问：",{"type":27,"tag":61,"props":4592,"children":4593},{},[4594,4599,4604,4609],{"type":27,"tag":65,"props":4595,"children":4596},{},[4597],{"type":33,"value":4598},"这条优化能省多少机器成本？",{"type":27,"tag":65,"props":4600,"children":4601},{},[4602],{"type":33,"value":4603},"能减少多少超时和投诉？",{"type":27,"tag":65,"props":4605,"children":4606},{},[4607],{"type":33,"value":4608},"会不会增加写入负担？",{"type":27,"tag":65,"props":4610,"children":4611},{},[4612],{"type":33,"value":4613},"未来 6 个月查询形态会不会变？",{"type":27,"tag":35,"props":4615,"children":4616},{},[4617],{"type":33,"value":4618},"优化不是竞赛，是投资。",{"type":27,"tag":204,"props":4620,"children":4621},{},[],{"type":27,"tag":208,"props":4623,"children":4625},{"id":4624},"十四给团队的数据库优化检查清单",[4626],{"type":33,"value":4627},"十四、给团队的数据库优化检查清单",{"type":27,"tag":35,"props":4629,"children":4630},{},[4631],{"type":33,"value":4632},"如果你想把今天的内容落成团队规范，可以直接用这份检查清单：",{"type":27,"tag":253,"props":4634,"children":4636},{"id":4635},"查询设计层",[4637],{"type":33,"value":4635},{"type":27,"tag":61,"props":4639,"children":4642},{"className":4640},[4641],"contains-task-list",[4643,4655,4664,4673,4682],{"type":27,"tag":65,"props":4644,"children":4647},{"className":4645},[4646],"task-list-item",[4648,4653],{"type":27,"tag":4649,"props":4650,"children":4652},"input",{"disabled":18,"type":4651},"checkbox",[],{"type":33,"value":4654}," 是否明确知道这条查询服务哪个业务场景",{"type":27,"tag":65,"props":4656,"children":4658},{"className":4657},[4646],[4659,4662],{"type":27,"tag":4649,"props":4660,"children":4661},{"disabled":18,"type":4651},[],{"type":33,"value":4663}," 是否只查真正需要的字段",{"type":27,"tag":65,"props":4665,"children":4667},{"className":4666},[4646],[4668,4671],{"type":27,"tag":4649,"props":4669,"children":4670},{"disabled":18,"type":4651},[],{"type":33,"value":4672}," 是否避免在索引列上做函数或隐式转换",{"type":27,"tag":65,"props":4674,"children":4676},{"className":4675},[4646],[4677,4680],{"type":27,"tag":4649,"props":4678,"children":4679},{"disabled":18,"type":4651},[],{"type":33,"value":4681}," 是否避免无意义深分页",{"type":27,"tag":65,"props":4683,"children":4685},{"className":4684},[4646],[4686,4689],{"type":27,"tag":4649,"props":4687,"children":4688},{"disabled":18,"type":4651},[],{"type":33,"value":4690}," 是否避免在循环里发 SQL",{"type":27,"tag":253,"props":4692,"children":4694},{"id":4693},"索引设计层",[4695],{"type":33,"value":4693},{"type":27,"tag":61,"props":4697,"children":4699},{"className":4698},[4641],[4700,4709,4718,4727],{"type":27,"tag":65,"props":4701,"children":4703},{"className":4702},[4646],[4704,4707],{"type":27,"tag":4649,"props":4705,"children":4706},{"disabled":18,"type":4651},[],{"type":33,"value":4708}," 是否根据高频查询模式设计，而不是拍脑袋加索引",{"type":27,"tag":65,"props":4710,"children":4712},{"className":4711},[4646],[4713,4716],{"type":27,"tag":4649,"props":4714,"children":4715},{"disabled":18,"type":4651},[],{"type":33,"value":4717}," 联合索引顺序是否匹配过滤与排序路径",{"type":27,"tag":65,"props":4719,"children":4721},{"className":4720},[4646],[4722,4725],{"type":27,"tag":4649,"props":4723,"children":4724},{"disabled":18,"type":4651},[],{"type":33,"value":4726}," 是否评估了覆盖索引的读写成本",{"type":27,"tag":65,"props":4728,"children":4730},{"className":4729},[4646],[4731,4734],{"type":27,"tag":4649,"props":4732,"children":4733},{"disabled":18,"type":4651},[],{"type":33,"value":4735}," 是否清理了长期无用索引",{"type":27,"tag":253,"props":4737,"children":4739},{"id":4738},"数据治理层",[4740],{"type":33,"value":4738},{"type":27,"tag":61,"props":4742,"children":4744},{"className":4743},[4641],[4745,4754,4763,4772],{"type":27,"tag":65,"props":4746,"children":4748},{"className":4747},[4646],[4749,4752],{"type":27,"tag":4649,"props":4750,"children":4751},{"disabled":18,"type":4651},[],{"type":33,"value":4753}," 热数据和冷数据是否分层",{"type":27,"tag":65,"props":4755,"children":4757},{"className":4756},[4646],[4758,4761],{"type":27,"tag":4649,"props":4759,"children":4760},{"disabled":18,"type":4651},[],{"type":33,"value":4762}," 在线查询与分析报表是否适当隔离",{"type":27,"tag":65,"props":4764,"children":4766},{"className":4765},[4646],[4767,4770],{"type":27,"tag":4649,"props":4768,"children":4769},{"disabled":18,"type":4651},[],{"type":33,"value":4771}," 大表是否有归档策略",{"type":27,"tag":65,"props":4773,"children":4775},{"className":4774},[4646],[4776,4779],{"type":27,"tag":4649,"props":4777,"children":4778},{"disabled":18,"type":4651},[],{"type":33,"value":4780}," 高峰流量是否有缓存或降级兜底",{"type":27,"tag":253,"props":4782,"children":4784},{"id":4783},"工程治理层",[4785],{"type":33,"value":4783},{"type":27,"tag":61,"props":4787,"children":4789},{"className":4788},[4641],[4790,4799,4808,4817],{"type":27,"tag":65,"props":4791,"children":4793},{"className":4792},[4646],[4794,4797],{"type":27,"tag":4649,"props":4795,"children":4796},{"disabled":18,"type":4651},[],{"type":33,"value":4798}," 是否能从接口追踪到 SQL 模板和耗时",{"type":27,"tag":65,"props":4800,"children":4802},{"className":4801},[4646],[4803,4806],{"type":27,"tag":4649,"props":4804,"children":4805},{"disabled":18,"type":4651},[],{"type":33,"value":4807}," 是否有慢查询巡检机制",{"type":27,"tag":65,"props":4809,"children":4811},{"className":4810},[4646],[4812,4815],{"type":27,"tag":4649,"props":4813,"children":4814},{"disabled":18,"type":4651},[],{"type":33,"value":4816}," 是否对核心表变更加了 review 清单",{"type":27,"tag":65,"props":4818,"children":4820},{"className":4819},[4646],[4821,4824],{"type":27,"tag":4649,"props":4822,"children":4823},{"disabled":18,"type":4651},[],{"type":33,"value":4825}," 是否有优化前后指标对比记录",{"type":27,"tag":204,"props":4827,"children":4828},{},[],{"type":27,"tag":208,"props":4830,"children":4832},{"id":4831},"总结",[4833],{"type":33,"value":4831},{"type":27,"tag":35,"props":4835,"children":4836},{},[4837],{"type":33,"value":4838},"把数据库优化这件事讲透，其实可以收敛成 5 句话：",{"type":27,"tag":162,"props":4840,"children":4841},{},[4842,4859,4869,4879,4889],{"type":27,"tag":65,"props":4843,"children":4844},{},[4845,4850,4852,4857],{"type":27,"tag":96,"props":4846,"children":4847},{},[4848],{"type":33,"value":4849},"先定位，再优化。",{"type":33,"value":4851}," 别把锁等待、连接池、",{"type":27,"tag":125,"props":4853,"children":4855},{"className":4854},[],[4856],{"type":33,"value":190},{"type":33,"value":4858}," 和慢 SQL 混在一起。",{"type":27,"tag":65,"props":4860,"children":4861},{},[4862,4867],{"type":27,"tag":96,"props":4863,"children":4864},{},[4865],{"type":33,"value":4866},"索引是为访问模式服务，不是为字段服务。",{"type":33,"value":4868}," 联合索引设计永远围绕真实查询。",{"type":27,"tag":65,"props":4870,"children":4871},{},[4872,4877],{"type":27,"tag":96,"props":4873,"children":4874},{},[4875],{"type":33,"value":4876},"真正的大收益，常常来自减少扫描和减少查询次数。",{"type":33,"value":4878}," 不只是“再加一个索引”。",{"type":27,"tag":65,"props":4880,"children":4881},{},[4882,4887],{"type":27,"tag":96,"props":4883,"children":4884},{},[4885],{"type":33,"value":4886},"排序、分页、范围查询放在一起时最危险。",{"type":33,"value":4888}," 这时更要结合执行计划和数据分布。",{"type":27,"tag":65,"props":4890,"children":4891},{},[4892,4897],{"type":27,"tag":96,"props":4893,"children":4894},{},[4895],{"type":33,"value":4896},"性能优化要能守住。",{"type":33,"value":4898}," 没有治理机制的优化，很快会被新需求冲掉。",{"type":27,"tag":35,"props":4900,"children":4901},{},[4902],{"type":33,"value":4903},"如果你只记住一句话，我希望是这一句：",{"type":27,"tag":148,"props":4905,"children":4906},{},[4907],{"type":27,"tag":35,"props":4908,"children":4909},{},[4910],{"type":33,"value":4911},"好的数据库优化，不是让 SQL 看起来更聪明，而是让系统在数据长大之后，依然保持可预测。",{"type":27,"tag":35,"props":4913,"children":4914},{},[4915],{"type":33,"value":4916},"毕竟线上最让人崩溃的，不是“慢”，而是——",{"type":27,"tag":35,"props":4918,"children":4919},{},[4920],{"type":27,"tag":96,"props":4921,"children":4922},{},[4923],{"type":33,"value":4924},"它昨天还快，今天为什么突然不快了。",{"type":27,"tag":4926,"props":4927,"children":4928},"style",{},[4929],{"type":33,"value":4930},"html .default .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}",{"title":7,"searchDepth":20,"depth":20,"links":4932},[4933,4941,4950,4954,4961,4967,4977,4982,4988,4993,4998,5007,5012,5013,5019],{"id":210,"depth":482,"text":213,"children":4934},[4935,4940],{"id":255,"depth":20,"text":258,"children":4936},[4937,4938,4939],{"id":262,"depth":549,"text":265},{"id":273,"depth":549,"text":276},{"id":284,"depth":549,"text":287},{"id":295,"depth":20,"text":298},{"id":438,"depth":482,"text":441,"children":4942},[4943,4944,4949],{"id":444,"depth":20,"text":447},{"id":1096,"depth":20,"text":1099,"children":4945},[4946,4947,4948],{"id":1107,"depth":549,"text":1110},{"id":1118,"depth":549,"text":1121},{"id":1129,"depth":549,"text":1132},{"id":1140,"depth":20,"text":1143},{"id":1212,"depth":482,"text":1215,"children":4951},[4952,4953],{"id":1228,"depth":20,"text":1231},{"id":1297,"depth":20,"text":1300},{"id":1354,"depth":482,"text":4955,"children":4956},"四、先学会看 EXPLAIN，但别把它当成占卜",[4957,4958,4959],{"id":1385,"depth":20,"text":1388},{"id":1489,"depth":20,"text":1492},{"id":1781,"depth":20,"text":4960},"4.3 EXPLAIN 的最大误区",{"id":1856,"depth":482,"text":1859,"children":4962},[4963,4964,4965,4966],{"id":1862,"depth":20,"text":1865},{"id":1914,"depth":20,"text":1917},{"id":2079,"depth":20,"text":2082},{"id":2183,"depth":20,"text":2186},{"id":2248,"depth":482,"text":2251,"children":4968},[4969,4972,4976],{"id":2254,"depth":20,"text":2257,"children":4970},[4971],{"id":2313,"depth":549,"text":2316},{"id":2430,"depth":20,"text":4973,"children":4974},"场景 2：商品详情页的 N+1 查询",[4975],{"id":2643,"depth":549,"text":2646},{"id":3103,"depth":20,"text":3106},{"id":3275,"depth":482,"text":3278,"children":4978},[4979,4980,4981],{"id":3281,"depth":20,"text":3284},{"id":3315,"depth":20,"text":3318},{"id":3483,"depth":20,"text":3486},{"id":3548,"depth":482,"text":3551,"children":4983},[4984,4986,4987],{"id":3559,"depth":20,"text":4985},"8.1 为什么 ORDER BY 经常把索引打废",{"id":3680,"depth":20,"text":3683},{"id":3724,"depth":20,"text":3727},{"id":3814,"depth":482,"text":3817,"children":4989},[4990,4991,4992],{"id":3820,"depth":20,"text":3823},{"id":3887,"depth":20,"text":3890},{"id":3926,"depth":20,"text":3929},{"id":3963,"depth":482,"text":3966,"children":4994},[4995,4996,4997],{"id":3994,"depth":20,"text":3997},{"id":4018,"depth":20,"text":4021},{"id":4042,"depth":20,"text":4045},{"id":4074,"depth":482,"text":4077,"children":4999},[5000,5001,5002,5003,5004,5005,5006],{"id":4085,"depth":20,"text":4088},{"id":4114,"depth":20,"text":4117},{"id":4130,"depth":20,"text":4133},{"id":4159,"depth":20,"text":4162},{"id":4196,"depth":20,"text":4199},{"id":4240,"depth":20,"text":4243},{"id":4368,"depth":20,"text":4371},{"id":4410,"depth":482,"text":4413,"children":5008},[5009,5010,5011],{"id":4416,"depth":20,"text":4419},{"id":4450,"depth":20,"text":4453},{"id":4489,"depth":20,"text":4492},{"id":4536,"depth":482,"text":4539},{"id":4624,"depth":482,"text":4627,"children":5014},[5015,5016,5017,5018],{"id":4635,"depth":20,"text":4635},{"id":4693,"depth":20,"text":4693},{"id":4738,"depth":20,"text":4738},{"id":4783,"depth":20,"text":4783},{"id":4831,"depth":482,"text":4831},"markdown","content:topics:engineering:database-index-query-optimization-guide.md","content","topics/engineering/database-index-query-optimization-guide.md","topics/engineering/database-index-query-optimization-guide","md",1777109948398]