Hello,

I'm pretty new on Drupal, and I was working on a site that it uses Drupal 7 (7.50). I've made a view for the home page but then I needed to filter some content comparing some field of the author with the same field of the current user who was browsing the site. I did it by using PHP filter and actually works. But the problem is the site is horribly slow since then. I'd tried to enhance it by adding caching tools to manage workloads but I can't get it faster enough again. It takes up to 40 seconds to load most of the time.
It seems that it applies the filter to ALL the content before it shows the first page, even when only 10 items are loaded per page. Additionally I'm using Views Infinite Scroll.
I would really appreciate any guidance or advice, or maybe how to approach my problem with some other workaround

Thank you in advance,

Comments

RoloDMonkey’s picture

Probably, the problem is that you have moved the "limit" out of the SQL query and into PHP. Now, the view has to load all of the entities in order and filter out the first ten that meet your criteria. This can take a lot more time and a lot more memory, and it will get worse as the number of results grows.

You could try a few things:

1. Reconsider if you really need this.
2. Get rid of paging, at least on the home page, and hard code a link at the bottom to another variation of the view that does do paging.
3. Move the processing out of PHP and back to SQL by re-writing the query in an alter hook.

--

Read more at iRolo.net

educacionaldia’s picture

Ok, I'm going to try to do use an alter_hook or maybe re design my solution
Just in case if someone has an idea, let me go deep inside on what was I trying to achieve,
Let's say the nodes I was loading in the home page were called articles. This articles have a field to identify whether their scope is national or international. And I identify their country by it's author's country field. So in order to validate to show/hide the node I was comparing the author's country to the current user's country.
I needed to show only the nodes in the home page conditioned to scopes but in the same time to be filtered (hide/show) (if the scope was "national") by the country of the current user.
I just needed to hide nodes identified to be from a different country only if their scope was "national".
What I was doing,
I created a page view where I was calling published and latests articles but I needed to filter them to give a customized navigation to each user. So I choosed the PHP Filter criteria to do it. First of all I was checking some node field called scope in order to identify whether the article is national or international. Then only those were identified to has a national scope I was figuring out their origin country. In order to do that I was comparing the current user country field to the author (also a user) country field.
The problem was, when I put the code or whatever, as a PHP Filter criteria seems to be applied to all the nodes that exists, and not only to the every 10 nodes I'd selected to appear per page. I tried to use cache and actually works a lot faster but pagination don't work, whether If I use Infinite Scroll or paginator when I load more content, the actual content is loaded again, making to look as a constant loop of content.