在MySQL中使用LIMIT 1 [英] Using LIMIT 1 in MySQL

查看:312
本文介绍了在MySQL中使用LIMIT 1的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在MySQL中,在查询末尾添加LIMIT 1的适当时间是合适的.我通常在DELETE中添加它,但是我已经看到它与INSERT a甚至UPDATE一起使用.是矫kill过正还是好的做法?

When is it appropriate to add LIMIT 1 at the end of the query in MySQL. I normally add it in DELETE but I've seen it being used with INSERT a and even UPDATE. Is it an overkill or a good practice?

推荐答案

好吧,如果您使用EXPLAIN,您会发现它加快了速度,一旦发现一个结果,它就会停止.

Well, if you use EXPLAIN you'll see that it speeds things up, as once it's found one result, it stops.

这也是一种故障保护–如果您知道更新的插入只会影响一行,通过指定它,可以保证它不会出错并弄乱多行.

It's also a failsafe – if you know your insert of update should only ever affect one row, by specifying it, you are guaranteeing that it won't ever go wrong and mess up multiple rows.

这篇关于在MySQL中使用LIMIT 1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆