简体   繁体   English

Mysql等于vs大于(小于)性能差异

[英]Mysql equal vs greater than(less than) performance difference

Is there a performance difference in Mysql for big dataset while using equals or greater than in where clause. 在使用等于或大于where子句时,大数据集的Mysql是否存在性能差异。

I can run following two queries for same purpose. 为了相同的目的,我可以运行以下两个查询。

select * from My_Table where MY_Field > 100; // 100 is old value // 100是旧值

or 要么

select * from MY_Table where MY_Field = 101; 

It is known value of My_Field if greater than 100 will 101. 如果大于100,则知道My_Field的值为101。

Any Suggestions? 有什么建议?

If MY_Field is covered with index then the difference in searching values in index is not big. 如果MY_Field被索引覆盖,则索引中搜索值的差异不大。 This is true until the result set is about 30% of the total number rows in a table. 在结果集大约占表中行总数的30%之前,这是正确的。

But, for each record found in index mysql need to perform seek on data file to find the appropriate row (since you have SELECT * ). 但是,对于在索引mysql中找到的每条记录,需要在数据文件上执行搜索以找到相应的行(因为你有SELECT * )。

So - in your case if you know the specific field value it must be better to use = . 所以 - 在你的情况下,如果你知道具体的字段值,最好使用=

Better use MY_Field = 101 because mysql will perform ref access type which means index will be compared with reference value, which is faster that range in case MY_Field > 100 which means mysql will compare range values in index. 更好地使用MY_Field = 101因为mysql将执行ref访问类型,这意味着索引将与参考值进行比较,如果MY_Field > 100这意味着mysql将比较索引中的范围值,则该range更快。

You can check it in type field in explain for your query. 您可以在explain中的type字段中查看您的查询。

取决于此特定列上的索引类型:当您搜索范围时BTREE很好,当您搜索等式时HASH很好

You can optimize query if you know only one result is going to be fetched use LIMIT 1 如果您只知道将使用LIMIT 1获取一个结果,则可以优化查询

select * from MY_Table where MY_Field = 101 LIMIT 1;

which is better as while executing if it finds a single row matching result mysql will stop execution 哪个更好,因为在执行时如果找到单行匹配结果,mysql将停止执行

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

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