问题描述
当我将 LIMIT 1 添加到 MySQL 查询时,它是在找到 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?
推荐答案
根据查询,添加限制子句会对性能产生巨大影响.如果您只需要一行(或者知道只有一行可以满足查询的事实),并且不确定内部优化器将如何执行它(例如,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 个结果时,将“LIMIT 1"添加到 MySQL 查询是否会使它们更快?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!