Does adding #39;LIMIT 1#39; to MySQL queries make them faster when you know there will only be 1 result?(当您知道只有 1 个结果时,将“LIMIT 1添加到 MySQL 查询是否会使它们更快?)
问题描述
当我将 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 查询是否会使它们更快?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:当您知道只有 1 个结果时,将“LIMIT 1"添加到 MySQL 查询是否会使它们更快?


- 如何使用 pip 安装 Python MySQLdb 模块? 2021-01-01
- 如何将 SonarQube 6.7 从 MySQL 迁移到 postgresql 2022-01-01
- 使用 Oracle PL/SQL developer 生成测试数据 2021-01-01
- 如何将 Byte[] 插入 SQL Server VARBINARY 列 2021-01-01
- 远程 mySQL 连接抛出“无法使用旧的不安全身份验证连接到 MySQL 4.1+"来自 XAMPP 的错误 2022-01-01
- 更改自动增量起始编号? 2021-01-01
- 导入具有可变标题的 Excel 文件 2021-01-01
- 在SQL中,如何为每个组选择前2行 2021-01-01
- 以一个值为轴心,但将一行上的数据按另一行分组? 2022-01-01
- SQL 临时表问题 2022-01-01