由于 InnoDB 在 B+ 树中组织其数据。树的高度影响 IO 次数,这可能是 DB 变慢的主要原因之一。
所以我的问题是如何断言或计算B+树的高度(例如根据可以通过行大小、页面大小和行号计算的页数),从而决定是否进行分区数据到不同的主人。
最佳答案
https://www.percona.com/blog/2009/04/28/the_depth_of_a_b_tree/
设 N 为表中的行数。
令 B 为适合一个 B 树节点的键数。
树的深度是(log N) / (log B)
.
来自博客:
Let’s put some numbers in there. Say you have a billion rows, and you can currently fit 64 keys in a node. Then the depth of the tree is (log 109)/ log 64 ≈ 30/6 = 5. Now you rebuild the tree with keys half the size and you get log 109 / log 128 ≈ 30/7 = 4.3. Assuming the top 3 levels of the tree are in memory, then you go from 2 disk seeks on average to 1.3 disk seeks on average, for a 35% speedup.
我还要补充一点,通常您不必针对 I/O 成本进行优化,因为您经常使用的数据应该在 InnoDB 缓冲池中,因此读取它不会产生任何 I/O 成本。您应该调整缓冲池的大小,以使大多数读取都能实现这一点。
关于innodb - 如何预测mysql查询的IO计数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60716446/