Which of the following notations is better?
下面哪个表示法更好?
SELECT id,name,data FROM table WHERE id = X
OR
或
SELECT id,name,data FROM table WHERE id = X LIMIT 1
I think it should not have "LIMIT".
我认为它不应该有“极限”。
Thanks!
谢谢!
2 个解决方案
#1
2
If there is a unique constraint on id
then they will be exactly the same.
如果id上有唯一的约束,那么它们将完全相同。
If there isn't a unique constraint (which I would find highly surprising on a column called id
) then which is better depends on what you want to do:
如果没有唯一的约束(我在一个名为id的列上发现这一点非常令人惊讶),那么哪个更好取决于您想做什么:
- If you want to find all rows matching the condition, don't use
LIMIT
. - 如果您想查找与条件匹配的所有行,请不要使用LIMIT。
- If you want to find any row matching the condition (and you don't care which), use
LIMIT 1
. - 如果您想找到任何与条件匹配的行(您不关心哪个),请使用LIMIT 1。
#2
1
Always use LIMIT with select statement even if you are fetching 1 record because it will speed up your query. So use :
即使要获取一条记录,也要对select语句使用LIMIT,因为这会加快查询速度。所以使用:
SELECT id,name,data FROM table WHERE id = X LIMIT 1
For example : If there are 1000 records in your table than if you using
例如:如果您的表中有1000条记录,而不是您使用的记录
SELECT id,name,data FROM table WHERE id = X
than it will traverse through 1000 records even if finds that id But if you using LIMIT like this
它会遍历1000条记录,即使找到了那个id,但是如果你使用这样的限制
SELECT id,name,data FROM table WHERE id = X LIMIT 1
than it will stop executing when finds first record.
当发现第一个记录时,它将停止执行。
#1
2
If there is a unique constraint on id
then they will be exactly the same.
如果id上有唯一的约束,那么它们将完全相同。
If there isn't a unique constraint (which I would find highly surprising on a column called id
) then which is better depends on what you want to do:
如果没有唯一的约束(我在一个名为id的列上发现这一点非常令人惊讶),那么哪个更好取决于您想做什么:
- If you want to find all rows matching the condition, don't use
LIMIT
. - 如果您想查找与条件匹配的所有行,请不要使用LIMIT。
- If you want to find any row matching the condition (and you don't care which), use
LIMIT 1
. - 如果您想找到任何与条件匹配的行(您不关心哪个),请使用LIMIT 1。
#2
1
Always use LIMIT with select statement even if you are fetching 1 record because it will speed up your query. So use :
即使要获取一条记录,也要对select语句使用LIMIT,因为这会加快查询速度。所以使用:
SELECT id,name,data FROM table WHERE id = X LIMIT 1
For example : If there are 1000 records in your table than if you using
例如:如果您的表中有1000条记录,而不是您使用的记录
SELECT id,name,data FROM table WHERE id = X
than it will traverse through 1000 records even if finds that id But if you using LIMIT like this
它会遍历1000条记录,即使找到了那个id,但是如果你使用这样的限制
SELECT id,name,data FROM table WHERE id = X LIMIT 1
than it will stop executing when finds first record.
当发现第一个记录时,它将停止执行。