返回计数比返回对象更好/更快吗?堆栈溢出!

发布于 2024-10-09 06:59:12 字数 106 浏览 3 评论 0原文

我正在 EF4 中创建一个存储库。其中一种方法使用密码和用户名来验证用户。该方法返回用户计数,因此 0 表示用户不存在,1 表示用户存在。如果我只返回一个用户对象并检查它是否为空,会有很大的不同吗?

I'm creating a repository in EF4. For one of the methods a password and username is used to verify a user. The method returns a count of users so a 0 means they don't exist and a 1 they do. Would it make much of a difference if I just returned a user object and checked it for null?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

揽月 2024-10-16 06:59:12

从技术上讲,最有效的方法可能是使用 Any() 扩展方法。如果您返回一个对象,则会产生填充该对象的成本。如果返回计数,则需要遍历每条记录(在应用 where 子句之后)并对它们进行计数。 Any()应该在sql中使用Exists,这样SQL Server只要找到第一条记录就可以停止。

但最终,我同意其他人的观点,这不是您想要立即开始优化的地方。唐纳德·高德纳 (Donald Knuth) 对此可能有最好的引用:
“我们应该忘记小的效率,大约 97% 的时候说:过早的优化是万恶之源”。

例如,假设您让此方法返回 bool 并且使用 Any() 方法。稍后在请求中,您可能需要从数据库中提取用户对象(这可能是您最终要做的很多事情)。现在,通过尽早优化,您实际上增加了对数据库的调用次数。

华泰

Technically the most efficient way would probably be to use the Any() extension method. If you return an object there is the cost of filling that object. If you return a count, then there is the cost of going through every record (after the where clause has been applied) and counting them. Any() should use Exists in sql, and therefore, SQL server can stop as soon as it finds the first record.

Ultimately though, I agree with the others, this isn't a place you want to start optimizing right away. Donald Knuth probably has the best quote about this:
"We should forget about small efficiencies, say about 97% of the time: premature optimization is the root of all evil".

For instance, let's say you have this method return a bool and you use the Any() method. Later in the request, you might need to pull the user object out of the database (this could be something you end up doing a lot). Now, by optimizing early, you've actually increased the number of calls to the database.

HTH

月棠 2024-10-16 06:59:12

Any 选项会更好,因为 EF 对象的物化和更改跟踪成本很高,如果该对象碰巧有很多属性,那么您绝对应该考虑使用 Any。

well the option with Any is going to be better because EF has a high cost of materialization and change tracking for an object and if that object happens to have lot of properties, you should definitely consider using Any.

不寐倦长更 2024-10-16 06:59:12

第二个版本在设计方面会更好。就微效率而言,这并不重要

The second version would be better - in terms of design. In terms of microefficiency it shouldn't matter

╰つ倒转 2024-10-16 06:59:12

如果你想知道真相的话,胡基。这两种方法都可以忽略不计,因此您选择哪一种都无关紧要。选择使您的代码更易于理解和阅读的方法。人们常常担心在所有错误的地方的表现。

我同意 Armen 的观点,返回对象并检查是否为 null。非常简单,很容易理解发生了什么。

Hoakey if you want to know the truth. Both methods are going to be so negligible that it wont matter which one you choose. Choose whichever method makes your code easier to understand and read. Often times people get worried about performance in all the wrong places.

I agree with Armen, return the object and check for null. Very simple and is easy to understand what is going on.

送舟行 2024-10-16 06:59:12

如果在验证存在有效的用户/密码组合后不需要“用户”表中的任何数据,则任一方法都可以工作(并且性能并不重要)。

另一方面,如果一旦您验证了有效的用户名/密码,您计划进行第二次调用来获取用户详细信息,那么首先明确返回对象(并检查 null 以验证是否存在)是一种更有效的策略我的意见。

If you don't need any data from the 'user' table after you verify that a valid user/password combo exists, then either method will work (and performance won't matter).

On the other hand, if once you verify valid username/password you plan on making a second call to get the user details, then clearly returning the object in the first place(and checking for null to verify existence) is a more efficient strategy in my opinion.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文