acf icon indicating copy to clipboard operation
acf copied to clipboard

Flexible Content Field speed

Open njbarrett opened this issue 7 years ago • 18 comments

Hey,

Thanks for developing this amazing package. I'm testing the flexible content field as follows:

        $page = Page::find(17);
        $modules = $page->acf->flexibleContent('modules');

This alone takes 8+ seconds, which I'm assuming is due to the number of queries it has to make. I have 2 layouts, both have repeaters in them, but they contain a few text and image fields. It seems it is returning all of the field data, but doing each field individually and probably generating a lot of queries to get to this.

njbarrett avatar Mar 17 '17 02:03 njbarrett

Hi @njbarrett thanks for the comment. Both FlexibleContent and Repeater makes a lot of SQL, even in the original plugin. Our goal is to decrease that, but we need help ;-)

Anyway, 8 seconds is a lot. I don't think this is because of that, of you have a lot of repeater in the same page, for example. If you're using Laravel can you monitor the number of SQL queries using for example Laravel Debugbar?

Cheers, JG.

jgrossi avatar Mar 17 '17 13:03 jgrossi

Hey @jgrossi

Thanks for the suggestion. I've run the page in debugbar and found this:

screen shot 2017-03-20 at 10 34 21 am

So 449 statements is a lot, but 348 duplicates is more concerning.

I had a brief look through the queries and the most duplicated query seems to be:

select * from bao_postmetawherepost_id in ('17') . 17 is the post id of the page I am loading.

This query appears many times, so if we can come up with a way to cache this im sure it would speed it up a lot.

I will look at your libraries code when I get some more time - for now I have chosen to cache the returns from the acf functions using Laravels cache drivers.

njbarrett avatar Mar 20 '17 03:03 njbarrett

Hey @njbarrett thanks for that!

Yep, we have to remove those duplicated queries. I'm gonna take a look on the code and check where I can "cache" that to void those queries.

Thanks for reporting that. JG

jgrossi avatar Mar 20 '17 13:03 jgrossi

Hey @njbarrett,

is there a current status to cache or prevent so many SQL queries? I ran into the same issue, see below. 🙈

image

Something I saw:

If you are using the Repeater, there is one query executed like:

select * from wp_postmeta where post_id = '216' and (meta_key like 'ingredients_0_%' or meta_key like 'ingredients_1_%' or meta_key like 'ingredients_2_%' or meta_key like 'ingredients_3_%' or meta_key like 'ingredients_4_%' or meta_key like 'ingredients_5_%' or meta_key like 'ingredients_6_%' or meta_key like 'ingredients_7_%')

If I manually execute this query, I will retrieve all data in the meta rows. Now, corcel/acf maybe has only to create a mapping to the 'metakey' which is also known at this time. Am I wrong?

floflock avatar May 13 '17 05:05 floflock

Hi @florianflock @njbarrett yep! That should be improved a lot. We're doing extra queries, but I think "caching" is not our responsibility here, but better SQL queries. I'm sure we can reduce 4, 5 queries into just one and improve a lot the performance we have.

This is related to #37 too.

jgrossi avatar May 25 '17 15:05 jgrossi

Hi @jgrossi,

right, we should concentrate on continuous performance, not only on caching. I cannot help till summer, unfortunately. But I will try to create a pull request on that.

Stay tuned!

floflock avatar May 25 '17 16:05 floflock

Hi @florianflock sorry for the late response. I'm gonna try to work on this in the next weeks, and if you can help you're welcome!

jgrossi avatar Jun 20 '17 12:06 jgrossi

What is the official way to get contents from a flexible? Can't find any ref to that even though it seems implemented.

I tried the @njbarrett solution ($modules = $page->acf->flexibleContent('modules');) as my flexible is named Modules but it keeps returning an empty array/object... Any hints? Is there a proper documentation I didn't find yet? Thanks!

DanDvoracek avatar Oct 25 '17 10:10 DanDvoracek

@DanDvoracek there's no official documentation about all fields for now. The code itself is very simple and easy to understand. You're welcome to send a PR ;-)

jgrossi avatar Oct 25 '17 11:10 jgrossi

@DanDvoracek and about getting the flexibleContent you're doing it right. If the call is returning an empty result is a good opportunity to debug your code and maybe fix a bug in the code :-) thanks!

jgrossi avatar Oct 25 '17 11:10 jgrossi

Hmmm... @jgrossi I'm simply testing the whole thing for now so I don't have ANY complexity on the Laravel side.

Only thing that I do is the following:

// PagesController
$pages = Page::type('page')->status('publish')->get();
return view('frontend.pages', ['pages' => $pages]);

Here is the model I use... Simply extending the Corcel\Model\Page .

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

use Corcel\Model\Page as Corcel;

class Page extends Corcel
{

}

Anything I'm doing wrong at that level?

DanDvoracek avatar Oct 25 '17 11:10 DanDvoracek

@DanDvoracek no, everything is correct. You can simplify:

$pages = Page::published()->get();
return view('frontend.pages', compact('pages'));

If you have published pages in the WP database this must work.

jgrossi avatar Oct 25 '17 11:10 jgrossi

Right I changed the way I query and return the data in the view.

Unfortunately,no luck and still nothing :/ I'm gonna dive into that a bit more.

DanDvoracek avatar Oct 25 '17 11:10 DanDvoracek

@DanDvoracek DId you configure the Laravel database connection? Do you have any data in the database?

jgrossi avatar Oct 25 '17 11:10 jgrossi

@jgrossi Of course I did ;) Everything seems fine on the connection side as I was able to output the data I needed. I could even work with repeaters but the flexible content is not working at all for me.

DanDvoracek avatar Oct 25 '17 11:10 DanDvoracek

@jgrossi Ok I got it. Nothing wrong with your plugin. I was using clones inside my flexible.. I suppose that has broken smth in the transaction (I can see this type isn't supported). Is there any plan to support that type of content from acf too ?

Thanks anyway ;)

EDIT: clones work using @drosendo solution here: https://github.com/corcel/acf/issues/49 . Not really Corcel plugin related but I am able to get all the data no matter what config I have in my Wp backend.. And I even do less queries in my frontend, so I guess I'm happy with that, for now.

Thanks!

DanDvoracek avatar Oct 25 '17 12:10 DanDvoracek

Hi @DanDvoracek maybe also look at my old PR for clone fields https://github.com/corcel/acf/pull/25

njbarrett avatar Oct 26 '17 00:10 njbarrett