Hi,
This is a pretty basic question, just wanted to confirm that my understanding is correct.
I have 2 types of post on my site - Books & Videos.
Some books may have a video that is related to them, but the vast majority will not. If aim to create a many-to-many post relationship between the two post types, and a view to display content of the related post in the post being displayed.
I wanted to understand what happens when there is no relationship in a given post. Does the view still get fired and consume server resources even when there is no relationship, or does the view only get fired when there is a relationship to display.
Hello,
Yes, the view will be fired even there isn't any result.
But there is a built-in cache within Views plugin, by default Views will use cache to display the results, so it will not consume a lots of your server resources, see our document:
https://toolset.com/documentation/user-guides/views-shortcodes/#wpv-view
cached (opt):
off
Turns off the caching system for the View.
And here is a document about "How to Use Views Custom Search on Large Sites":
https://toolset.com/documentation/user-guides/front-page-filters/how-to-use-views-parametric-search-on-large-sites/
For your reference.
As per my understanding, a view generated via a post-relationship will not get cached due to the way the views caching works.
I had asked this specific question on whether post relationships views can get cached in a previous support thread: https://toolset.com/documentation/user-guides/front-page-filters/how-to-use-views-parametric-search-on-large-sites/ and providing a snippet of the answer below
"these criteria are important:
- The View shortcode has any attribute other than name, id or target_id....
- The View is nested or has a filter or an option that depends on the current page, current user, or current archive data.
- Things that also change the content or the status of posts, terms, or users will invalidate cache automatically.
This View of the author bio must either include a shortcode attribute which sets the context for the author bio, or it must be based on a relationship to the current page. In either case, Views caching is disabled."
Since the view is not cached, and the view will have to fire even if there is no post relationship, my sense was that this could lead to a material increase in load.
I would ideally have thought that if there was no content to display, the view would not get fired at all.
I had a couple of follow-up questions:
(a) Is it possible to assess what is the performance hit of a non-cached view with no data being fired?
(b) For a site, which has a large number of posts without a relationship, and only some posts with a relationship, is it advisable to not use post relationships to avoid recurring firing of non-cached views with no results.
I had another follow up to the previous 2 raised:
(c) I am not sure if there is really that large a performance hit of many empty views constantly firing, but is a better solution to show related content for the few posts that have related content via a view that uses post ids as an argument to show the related content information ... this is much more manual work, but may be a workaround that can work
There is a logic problem, since view's result isn't cached, if you don't query database, you will not know if there is any result.
And as the document I mentioned above, if your view's settings satisfy those criteria, then view's result will be cached.
a) If there is no result found, even it is a non-cached view, it should not conduct the performance problem than other normal query.
b) And the post type relationship should not conduct performance problem than other normal query.
I suggest you test it in your website, check if there is any performance problem, then feedback the problem.