开发者

Is there any point to using Any() linq expression for optimisation purposes?

开发者 https://www.devze.com 2023-04-09 05:26 出处:网络
I have a MVC application which returns 2 types of Json responses from 2 controller methods; AnyRemindersExis开发者_Python百科t() and GetAllUserReminders(). The first returns a boolean, 2nd returns an

I have a MVC application which returns 2 types of Json responses from 2 controller methods; AnyRemindersExis开发者_Python百科t() and GetAllUserReminders(). The first returns a boolean, 2nd returns an array, both wrapped as Json.

I have a JavaScript timer checking for calendar reminders against a user. It makes the first call (AnyRemindersExist) to check whether reminders exist and whether the client should then make the 2nd call.

For example, if the result of the Json response is false from the Any() query, it doesn't then make the 2nd controller action which makes a LINQ select call. If there are reminders that exist, it then goes further and then requests them (making use of the LINQ SELECT).

Imagine a system ramped up where 100-1000s users use the system and on the client, every 30-60 seconds a request comes in to load in the reminders. Does this Any() call help in anyway in reducing load on the server?


If you're always going to get the actual values afterwards, then no - it would make more sense to have fewer requests, and just always give the full results. I very much doubt that returning no results is slower than returning an indication that there are no results.

EDIT: tvanfosson's latest comment (at the time of this writing) is worth promoting:

You can really only tell by measuring and I'd only resort to it IFF the performance of the select only option didn't meet the requirements.

That's the most important thing about performance: the value of a guess is much less than the value of test data.


I would say that it depends on how the underlying queries are translated. If the any call is translated into an indexed lookup when the select (perhaps due to a join to get related data) must do some sort of table scan, then it will save some work in the case when there are no reminders to be found. It will cause a little extra work when there are reminders. It might be useful if the majority of the calls don't result in any results.

In the general case, though, I would just select the data and only try to optimize IF that turns out to not be fast enough. The conditions under which it will actually save effort on the server are pretty narrow and might only apply if you hand-craft the SQL rather than depend on your ORM.


Any only checks to see if there is at least one item in the Collection that is being returned. Versus using something like Count > 0 which counts the total amount of items in the collection then yes this is more optimal.

If your AnyRemindersExist method is operating on a similar principle then not calling a second call to the server would reduce your load.


So you are asking if not doing work the application doesn't need to do would reduce the workload on the server?

Of course. How would this answer every be "yes, doing extra work for no reason won't effect the server load".


It ultimately depends on how much faster the Any check is compared to getting the results and how often it will be false.

  • If the Any call takes near as long as the select then it pretty much never makes sense.

  • If the Any call is much faster than the select but 90% of the time it's true, then it probably isn't worth it (best case you get 10% improvement, worst case it's actually more work).

  • If the Any call is much faster than the select and 90% of the time it's false, then it probably makes sense to check if there are any before actually getting results.

So the answer is it depends on your specific scenario. Ultimately you're going to need to measure both the relative performance (on different typical loads, maybe some queries are more intensive than others) as well as the frequency that there are no results to return.

Actually it should almost never make sense to check Any in this case.

  • If Any returns false then you don't need to grab the results. However this means it would have returned no results anyway, so unless your Any check is significantly faster than a select returning 0 results, there's no added benefit here.

  • On the other hand, if Any returns true, then you'll need to get the results anyway, so in this case Any is purely additional work done.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号