LIMIT 1 有任何性能提升吗?
查询后附加的 LIMIT 1
是否有任何性能提升?
...如果只能有一个可能的条目匹配(主键的 WHERE
子句)?
SELECT `x`
FROM `unicorns`
WHERE `id` = 123
LIMIT 1
...相同,但现在是 DELETE
:
DELETE FROM `unicorns`
WHERE `id` = 123
LIMIT 1
...和 UPDATE
:
UPDATE `unicorns`
SET `rainbows` = `rainbows` + 1
WHERE `id` = 123
LIMIT 1
PS 列 id
是主键,因此它是唯一的。
谢谢指教!
Does appended LIMIT 1
after query have any performance boost?
...if there could be only one possible entry that matched (WHERE
clause for primary key)?
SELECT `x`
FROM `unicorns`
WHERE `id` = 123
LIMIT 1
...the same, but now it's DELETE
:
DELETE FROM `unicorns`
WHERE `id` = 123
LIMIT 1
...and UPDATE
:
UPDATE `unicorns`
SET `rainbows` = `rainbows` + 1
WHERE `id` = 123
LIMIT 1
P.S. Column id
is primary key so it's unique.
Thanks in an advice!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这取决于你是否在列上有索引,
如果 id 是主键,则毫无意义,但
会给你带来性能提升
it depends do you have index on column or not
is pointless if id is Primary Key, but
will give u perfomance boost
它将提高性能,因为数据库只会启动满足查询条件的行的获取。但更新有什么意义呢?特别是如果您对主键进行精确匹配 - 一开始您只会匹配一行,因此没有必要说“更新表中的这一行,但只更新一行”最多”。
It'll boost performance in that the DB will only initiate a fetch of the rows that satisfied the conditions of the query. But on updates, what's the point? Especially if you're doing an exact match on a primary key - you'd only ever match one row to begin with, so there's no point in saying what amounts to "update this one row in the table, but only update one row at most".