LIMIT 1 有任何性能提升吗?

发布于 2024-12-10 03:16:42 字数 488 浏览 0 评论 0原文

查询后附加的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

維他命╮ 2024-12-17 03:16:42

这取决于你是否在列上有索引,

DELETE FROM `unicorns` WHERE `id` = 123 LIMIT 1

如果 id 是主键,则毫无意义,但

DELETE FROM `unicorns` WHERE `noindexoclumn` = 123 LIMIT 1

会给你带来性能提升

it depends do you have index on column or not

DELETE FROM `unicorns` WHERE `id` = 123 LIMIT 1

is pointless if id is Primary Key, but

DELETE FROM `unicorns` WHERE `noindexoclumn` = 123 LIMIT 1

will give u perfomance boost

零度° 2024-12-17 03:16:42

它将提高性能,因为数据库只会启动满足查询条件的行的获取。但更新有什么意义呢?特别是如果您对主键进行精确匹配 - 一开始您只会匹配一行,因此没有必要说“更新表中的这一行,但只更新一行”最多”。

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".

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文