且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

为什么MySQL并不总是在这里使用索引合并?

更新时间:2023-01-29 21:32:32

哇!这是我见过的最复杂的索引合并。

Wow! That's the most complicated "index merge" I have seen.

通常(也许总是),你可以制作一个'复合'索引来代替索引合并交叉,并执行更好的。将 key2 (固定)更改为(固定,DeviceId)。这个可以摆脱'相交'并加快它的速度。

Usually (perhaps always), you can make a 'composite' index to replace an index-merge-intersect, and perform better. Change key2 from just (pinned) to (pinned, DeviceId). This may get rid of the 'intersect' and speed it up.

一般来说,优化器只在绝望中使用索引合并。 (我认为这是标题问题的答案。)对查询或所涉及的值进行任何细微更改,优化程序将执行查询而不进行索引合并。

In general, the Optimizer uses index merge only in desperation. (I think this is the answer to the title question.) Any slight changes to the query or the values involved, and the Optimizer will perform the query without index merge.

临时表 __代码的改进是构建具有大范围值的永久表,然后使用Proc中该表的一系列值。如果您使用的是MariaDB,则使用动态构建的序列表。例如,'table' seq_1_to_100 有效地一列,数字为1..100。无需声明或填充它。

An improvement on the temp table __codes is to build a permanent table with a large range of values, then use a range of values from that table inside your Proc. If you are using MariaDB, then use the dynamically built "sequence" table. For example the 'table' seq_1_to_100 is effectively a table of one column with numbers 1..100. No need to declare it or populate it.

您可以通过摆脱其他 REPEAT 循环计算时间代码

避免 LOOPs 将是最大的性能优势。

Avoiding LOOPs will be the biggest performance benefit.

完成所有工作,然后我可能会有其他提示。

Get all that done, then I may have other tips.