gpt4 book ai didi

SQL 查询 : Which one should i use? 计数 ("columnname") 或计数 (1)

转载 作者:行者123 更新时间:2023-12-05 00:42:34 25 4
gpt4 key购买 nike

在我的 SQL 查询中,我只需要检查特定用户 ID 的数据是否存在。

我总是只希望在数据存在时返回一行。

我有两个选择

1. select count(columnname) from table where userid=:userid

2. select count(1) from tablename where userid=:userid

我认为第二个是我应该使用的,因为与第一个相比,它可能具有更好的响应时间。

最佳答案

count(*) 之间可能存在差异和 count(column) . count(*)由于某些原因,速度通常最快 discussed here .基本上,与 count(column)数据库必须检查是否 column每行是否为空。与 count(column)它只返回表中可能有的总行数。确切的细节可能取决于数据库和数据库的版本。

简短回答:使用 count(*)count(1) . hell ,忘记计数和select userid .

您还应该确保 where子句表现良好,并且它使用了索引。调查EXPLAIN .

关于SQL 查询 : Which one should i use? 计数 ("columnname") 或计数 (1),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2004807/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com