是否为"LIMIT 1"建议在哪里条件基于PK的情况下查询? [英] Is "LIMIT 1" recommended for query where WHERE condition is based on PK?

查看:75
本文介绍了是否为"LIMIT 1"建议在哪里条件基于PK的情况下查询?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在查询mySQL数据库以从1个特定行中检索数据.我正在使用表主键作为WHERE约束参数.

I am querying a mySQL database to retrieve the data from 1 particular row. I'm using the table primary key as the WHERE constraint parameter.

例如

SELECT name FROM users WHERE userid = 4

userid列是表的主键.在该mySQL语句的末尾使用LIMIT 1是一种好习惯吗?还是有速度优势?

The userid column is the primary key of the table. Is it good practice to use LIMIT 1 on the end of that mySQL statement? Or are there any speed benefits?

推荐答案

我认为这是一种不好的做法,因为当涉及到userid之类的东西时,它通常是唯一的,而且最多只能有一个.因此,使用LIMIT 1似乎是很矛盾的,以后来维护您的代码的人可能不得不重新猜测您的设计.

I would call that a bad practice as when it comes to something like a userid it's generally unique and you won't have more than one. Therefore, having LIMIT 1 seems pretty contradictory and someone who comes to maintain your code later may have to second-guess your design.

此外,我认为它根本没有任何速度优势.您可以查看mySQL的说明,以获取一种简单的工具来分析查询.

Also, I don't think it has any speed benefit at all. You can check out mySQL's Explain for a simple tool to analyze a query.

请注意,如注释中所述.在其他情况下,LIMIT #确实具有速度和一般优势,而不仅仅是这种情况.

Note, as mentioned in the comments. LIMIT # does have speed and general benefits in other cases, just not this one.

这篇关于是否为"LIMIT 1"建议在哪里条件基于PK的情况下查询?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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