Support Forum
Support › MB Relationships › Relationship field performanceResolved
Hi Anh,
We have been using the MB Relationships extension on the platform we have built and everything was going well, but on our last site with more data than I have handled so far we have encountered performance problems in the loading of the relationship fields.
What I have seen and please correct me if I am wrong, is that the selects on the relationship fields are builded with Post fields and in each of them query all posts of that post type and this is killing the database.
My thought is to set a pagination here and then get a lazy loading going so that when a person opens a select field and scrolls to the bottom of the list or types in a name into the search field, a query is then made to populate the next 50 or 100 posts or the proper search result. This will probably happen via Ajax.
Some of our posts have 700 relationships and more than 4,000 possible options within individual relationship fields.
Do you think my analysis is correct? And do you think that implement paging and lazy loading to solve it is a good approach?
This is very important for the site I am building, and time sensitive because the admin remains unusable until it is fixed. Thank you for your time.
Hi,
You're right about the query for the post field. Currently, it queries all posts, which causes the performance issue. I'll work on making it works with ajax to reduce the load on websites that have many posts.
FYI: the number of relationships doesn't affect the performance, since the plugin queries only once.
Thank you for your reply Anh and for working on this.
You're right about the number of relationships, now I've seen it.
I will stay expectant
Anh, do you have any ETA when you can have this? We really appreciate your help.
Hi, I'll start working on this from tomorrow. I've been busy updating the extensions. Hopefully can get it done next week.
Hi Anh,
We found that recently Advanced Custom Fields is using Ajax to populate the posts on the relationship field as the field comes into the browser.
https://www.advancedcustomfields.com/blog/acf-5-8-4-release/
I share this in case it helps. Thanks
I read the news, too. Thanks for sharing.
Hi Anh, just to know if you think you'll get it soon or if it can get complicated. I appreciate your time on this. Thanks
Hi,
I almost finish it. You can track the progress here: https://github.com/wpmetabox/meta-box/commits/ajax-post.
What I've done so far:
post
field (which is used in relationships plugin) by adding 'ajax' => true
to the field.posts_per_page
param in the query_args
)I'm working on clone. If everything goes well, I can give you an example by the end of today.
That would be great Anh, thanks
Hi again,
I've done the relationships for post
. Please follow this guide to use it:
Now try it again and you'll see the ajax requests are sent when open the dropdown.
If you want to customize the ajax request, in your code to register relationships, use this code:
add_action( 'mb_relationships_init', function () {
MB_Relationships_API::register( array(
'id' => 'users_to_posts',
'from' => array(
'object_type' => 'post',
'meta_box' => array(
'title' => 'Manages',
),
'field' => array(
'ajax' => true, // Note this
'query_args' => array(
'posts_per_page' => 10, // This is for pagination in Ajax
),
'js_options' => array(
'minimumInputLength' => 1, // Send ajax requests when users enter at least 1 character
),
),
),
'to' => array(
'object_type' => 'post',
'post_type' => 'page',
'meta_box' => array(
'title' => 'Managed By',
'context' => 'side',
'empty_message' => 'No users',
),
'field' => array(
'ajax' => true, // Note this
'query_args' => array(
'posts_per_page' => 10, // This is for pagination in Ajax
),
),
),
) );
} );
Some notes:
field
settings, which you can pass full settings array for the field. It's similar to post
, taxonomy
or user
field.ajax
for the dropdown by adding 'ajax' => true
to the fieldposts_per_page
parameter.Currently, it works for posts only. I'll make it works for terms and users by tomorrow.
Update: the ajax + pagination is enabled by default and works for terms and users. Please use the latest version on ajax-post branch on Github:
https://github.com/wpmetabox/meta-box/tree/ajax-post
Please try it before I release a new version.
Thanks so much for the solution Anh, we have noticed that the time to editing the relationship fields is greatly improved. The customization of parameters also worked well. However, we still find two more moments where the load becomes really heavy in post with many relationships.
One of those moments happens when the user open a closed relationship field. We understand that there is not much to do, since all relationships are loaded only once.
The other moment is in the initial charge. We are not sure that this happens there, but it's possible not to query any relationships for the relationship fields closed? In this way, a lazy load could be added that loads only the relationship field requested by the user.
Please correct me if I am wrong or if you find something else that can improve the initial load of the editor that is still very heavy.
Glad you like it.
Do you want to remove an ajax query when users open a closed relationships field? I understand "closed relationship field" is a field that users already chose items for the relationship, right?
Well, what if users want to change a selected item to something else? We still need to make ajax query to fetch other items for users to change.
FYI, if a field already has some selected items, only once query is made when page loads (not via ajax). It makes sure the selected items is visible in the dropdown.
If field has no selected items, no queries are created when page loads. Queries are created only via ajax.
My thought is to remove the queries that are made when the page loads, only for the relationship fields that are closed closed => true
So we use the ajax query to fetch the items when the user open the field.
If the field is set open, those queries cannot be omitted in the page load since the selected items must be displayed from the start.
Although when the page load the queries are done only once, having several relationship fields and many elements selected in each one, all queries are made even if the user does not need to edit the fields.
This is just my guess, you can tell me if it's feasible. Thanks again.