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 查询是否会使它们更快?
基础教程推荐
- 是否可以执行按位分组功能? 2021-01-01
- SQL:使用来自具有相同列名的两个表中的数据... 2021-01-01
- 需要 MySQL 5.1 中的抽象触发器来更新审计日志 2021-01-01
- SQL 效率:WHERE IN 子查询 vs. JOIN 然后 GROUP 2021-01-01
- SSMS 中的权限问题:“对象 'extended_properties'、数据库 'mssqlsystem_resource'、... 错误 229)上的 SELECT 权限被拒绝" 2022-01-01
- 将 SQL Server DateTime 列迁移到 DateTimeOffset 2021-01-01
- SQL Server 实例在登录协商期间返回无效或不受支持的协议版本 2021-01-01
- 如何使用 mysql.connector 禁用查询缓存 2022-01-01
- 无法解决整理冲突 2021-01-01
- 在 SQL 中连接多个表 2021-01-01
