문제

What is better in terms of speed…

I am trying to determine whether or not a user has added a certain URL to their list of shortcuts. If they have added the URL there will be a link on the page to remove the page from the shortcuts otherwise they can add it to their shortcuts for quick access via a dropdown menu. Unfortunately I need to make this check at every page load so the code is in my AppController. I would like to do whatever I can to speed this up. I don't want this cached.

Would it be faster to do a find('first') while limiting the "fields" to just “id”, a find('count'), or a field('id') where the conditions of either statement would be 'URL' => $this->here. Only 1 or 0 results should be returned.

도움이 되었습니까?

해결책

Assuming your table is indexed correctly you will likely not see a difference. Per @mark's comment, use whichever one suites your needs.

The logic of which one to use should be your main concern.

If you're only trying to see IF there is one, then using field makes the most sense, since it's limit 1 and only returns a single field.

If you want to know how many there are, then you'll need count.

And if you want to know IF there is one, and retrieve it's data, then first or exists is the way to go.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top