问题描述
当我向MySQL查询中添加LIMIT 1时,它会在找到1个结果(从而使其速度更快)之后停止搜索吗?还是仍然获取所有结果并在最后截断?
When I add LIMIT 1 to a MySQL query, does it stop the search after it finds 1 result (thus making it faster) or does it still fetch all of the results and truncate at the end?
推荐答案
根据查询,添加limit子句可能会对性能产生巨大影响.如果您只需要一行(或者知道只有一行可以满足查询的事实),并且不确定内部优化器将如何执行它(例如,WHERE子句未命中索引等等),那么您绝对应该添加LIMIT子句.
Depending on the query, adding a limit clause can have a huge effect on performance. If you want only one row (or know for a fact that only one row can satisfy the query), and are not sure about how the internal optimizer will execute it (for example, WHERE clause not hitting an index and so forth), then you should definitely add a LIMIT clause.
对于优化的查询(在小型表上使用索引),性能可能并不重要,但还是要重申一下-如果您只对一行感兴趣,而不是添加LIMIT子句.
As for optimized queries (using indexes on small tables) it probably won't matter much in performance, but again - if you are only interested in one row than add a LIMIT clause regardless.
这篇关于当您知道只有1个结果时,是否在MySQL查询中添加"LIMIT 1"会使它们更快?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!