简体   繁体   English

MySQL预处理器优化

[英]MySQL preprocessor optimization

Does anybody know if MySQL has any pre-processor optimizations involved before launching a query? 在启动查询之前,有没有人知道MySQL是否涉及任何预处理器优化?

Consider following query: 考虑以下查询:

string query = "SELECT giveaway_id FROM giveaways WHERE status >= @minStatus AND status <= @maxStatus AND type >= @minType AND type <= @maxType";

If @minStatus and @maxStatus are equal, as well as @minType and @maxType , we can write above query in more optimized form: 如果@minStatus@maxStatus相等,以及@minType@maxType ,我们可以用更优化的形式写上面的查询:

string query = "SELECT giveaway_id FROM giveaways WHERE status = @minStatus AND type = @minType";

I'm wondering if MySQL is smart enough to do above itself, or I should help it in writing proper optimized statements prior to launching them in my C# application. 我想知道MySQL是否足够智能,或者在我的C#应用​​程序中启动它之前我应该​​帮助它编写适当的优化语句。

Thanks. 谢谢。

It depends on the index you have. 这取决于你拥有的索引。 INDEX(status), INDEX(type) is pretty useless. INDEX(status), INDEX(type)很没用。 INDEX(status, type) (or the opposite order) may work well. INDEX(status, type) (或相反的顺序) 可能效果很好。

It depends on what version you are using. 这取决于您使用的是什么版本。 Newer versions use "Index Condition Pushdown", which may be smarter in this case. 较新版本使用“索引条件下推”,在这种情况下可能更智能。

Older version may see the inequalities and decide it is a "range". 旧版本可能会看到不平等并决定它是一个“范围”。 Then it will say "2 ranges, so I can't make use of both parts of INDEX(status, type) ". 然后它会说“2个范围,所以我不能使用INDEX(status, type)的两个部分INDEX(status, type) ”。

EXPLAIN FORMAT=JSON SELECT ... (5.6 or newer) will give you some clues of what is going on. EXPLAIN FORMAT=JSON SELECT ... (5.6或更新版本)将为您提供一些线索。

FLUSH STATUS;
SELECT ...;
SHOW SESSION STATUS LIKE 'Handler%';

(in any version) will give you a stronger clue. (在任何版本中)将为您提供更强的线索。 If Handler_read_next has a value approximately equal to the number of rows returned, then it is running as efficiently as possible. 如果Handler_read_next的值大约等于返回的行数,则它尽可能高效地运行。

Another possible wrinkle... @ variables don't always work like literals, so also test with literal values. 另一种可能的皱纹......变量并不总是像文字一样工作,所以也要用文字值进行测试。

This is too long for a comment. 这个评论太长了。

The step that you are referring to is not "pre-processing" but "compiling and optimizing". 您所指的步骤不是“预处理”,而是“编译和优化”。 And, yes, MySQL does do various optimizations, described here . 而且,是的,MySQL的做各种优化,描述在这里

However, the implication of your question is that a single equality comparison = is going to be much faster than two inequalities. 然而,你的问题的含义是,单个相等比较=将比两个不等式快得多。 That is generally not the case. 通常情况并非如此。 The bigger issue is whether an index is available on status and type . 更大的问题是索引是否可用于statustype The use of the index will be the major factor in performance -- and multiple inequalities could prevent MySQL from finding the best index. 索引的使用将是性能的主要因素 - 多个不等式可能会阻止MySQL找到最佳索引。

My advice would be to create the correct indexes for your queries. 我的建议是为您的查询创建正确的索引。 Then write the queries to be sure they take advantage of the indexes. 然后编写查询以确保它们利用索引。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM