mysql随机查询的时候,为什么>=的速度要比=快得多?

WBOY
Release: 2016-06-06 20:28:06
Original
1342 people have browsed it

<code>SELECT * FROM `table` 
  WHERE id >= (SELECT floor(RAND() * (SELECT MAX(id) FROM `table`)))  
  ORDER BY id LIMIT 1;</code>
Copy after login
Copy after login

以上例子来自:http://www.2cto.com/database/201310/249698.html

这里的 WHERE id >= 如果换成 WHERE id = 的速度要慢得多。
为什么呢?
测试后发现只有=的时候速度会很慢,,=的速度都挺快的。

不知为什么?

回复内容:

<code>SELECT * FROM `table` 
  WHERE id >= (SELECT floor(RAND() * (SELECT MAX(id) FROM `table`)))  
  ORDER BY id LIMIT 1;</code>
Copy after login
Copy after login

以上例子来自:http://www.2cto.com/database/201310/249698.html

这里的 WHERE id >= 如果换成 WHERE id = 的速度要慢得多。
为什么呢?
测试后发现只有=的时候速度会很慢,,=的速度都挺快的。

不知为什么?

原理或许与匹配机制有关,=类似字符串的搜索,

=类似模糊搜索,范围比较大导致查询速度问题。或者你试验下

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template