Using LIMIT 1 in MySQL

后端 未结 5 2131
猫巷女王i
猫巷女王i 2021-02-01 02:54

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

5条回答
  •  南笙
    南笙 (楼主)
    2021-02-01 03:28

    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.

提交回复
热议问题