Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
Would it be possible to cache the results of the _sections_in_section() function? On some of my pages this is getting 100s of times and querying the DB with the same query over and over again, accounting for a high percentage of my queries per page.
Could I fix this with some configuration change?
Comment | File | Size | Author |
---|---|---|---|
#21 | sections_repeated_queries-D6.patch | 3.21 KB | hass |
#12 | sections_repeated_queries.patch | 1.61 KB | recidive |
#10 | sections-too-many-select-785554-10.patch | 4.07 KB | sdelbosc |
Comments
Comment #1
KingMoore CreditAttribution: KingMoore commentedI am not set up to roll a patch, but here is a modified version of the above function with very basic caching implemented by me:
On the specific page I am working on it reduces my number of queries by over 200.
Comment #2
hass CreditAttribution: hass commentedYes caching would be great and was planned!
Comment #3
hass CreditAttribution: hass commentedAside it's not normal that you have 100s... Schould be much less (2-3). Could you take a look where the 100s are comming from, please?
Comment #4
KingMoore CreditAttribution: KingMoore commentedIt does this query:
repeatedly.
Comment #5
hass CreditAttribution: hass commentedBut - why - 100 times!?
Comment #6
KingMoore CreditAttribution: KingMoore commentedYeah I have no idea. It was alarming to me. I have 4 sections defined like so:
1. Show on every page except the listed pages.
(none, so effectively the default)
2. Only on listed pages:
admin
admin/*
3. Only on the listed pages:
order-form
4. Only on the listed pages:
order-form-sell
I actually applied the code/patch I posted above in my working copy of sections module, so I can't recreate. It has worked for me so far without any negative effects that I can tell. Executing the following query only once:
Comment #7
hass CreditAttribution: hass commentedWe need to understand first. It may be your default section that make no sense as your site defaults to a theme. You only need sections for other paths.
Please remove the patch and disable your first sectionand set your default theme to the section #1 theme. This is the recommended config.
Never create a section like your section #1
Comment #8
C-LogemannHello Hass,
I currently working on a client project which was started by another company.
In this project we have up to 421 of these DB requests (_sections_in_section()) on <front> by using sections.modul.
There was no rule for default theme.
The snippet above don't change this situation.
On a small test system I have at least 26 DB requests on <front> by using sections.module.
With disabling flag.module I have only 17 requests. On the customer project flag is also active.
Perhaps in this way you will find a solution.
For my customer I have solved this problem by disabling sections and control the theme-switch in a custom module:
http://drupal.org/node/68932 and http://api.drupal.org/api/function/drupal_match_path/6
kind regards
Carsten Logemann
(paratio.com e.K.)
Comment #9
hass CreditAttribution: hass commentedAfter more thinking I do have an idea... it's possible that Paratio have - let's say 10 node on the home and maybe a few boxes. This makes a sum of 17 reasonable... I'm aware about this, but haven't found the time to implement before the last release.
The changes from KingMore should work - without testing myself yet. Can you verify if you really have the DB requests with his changes or only the function calls?
Comment #10
sdelbosc CreditAttribution: sdelbosc commentedI have a composite page with lots of menu, nodes, views, blocks, ... and I get more than 300 requests related to sections module per page.
I think it is due to hook_preprocess implementation which is probably called for many themable elements.
I do not understand the goal behind hook_preprocess implementation. But if it is not possible to do what is intended in another manner, I would suggest the patch attached to reduce the number of queries.
Comment #11
KingMoore CreditAttribution: KingMoore commentedHass,
Sorry I have abandoned this thread. This is the really busy time of year for me. I will try to do/test what you asked in the next week sometime.
Comment #12
recidive CreditAttribution: recidive commentedHere is a simpler patch. It just adds _sections_roles() wrapper around the query and cache its results using role ids as key.
This patch solves the problem in a project I'm working on, without this patch the query runs 119 times, with the patch it runs only once.
Comment #13
sauloamui CreditAttribution: sauloamui commentedWork to me (#12)!
Really left only one query.
Thank´s recidive!
Comment #14
hass CreditAttribution: hass commentedSorry, but this cannot be the right way... sections_data table is the basis... not roles. In may work in your special case, but if other modules call the sections module for different keys via
_sections_in_section($section = NULL)
we need to cache the results per $section.Comment #15
recidive CreditAttribution: recidive commented@hass, I may be missing something obvious. But what the patch does is just avoid this query to run a bunch of times when it can run only once per request.
That query only filters by role ids. So unless there's data going in and out of those tables in the same request, this query will always return the same result set for a given set of role ids.
I'm not aware of the inner workings of sections module. But, yes, caching per sections may be more efficient. And maybe this shouldn't even be a static cache but a database one.
This patch was an immediate solution for a project suffering from performance problems related to mysql overhead.
Comment #16
sdelbosc CreditAttribution: sdelbosc commentedHello hass. Could we have your opinion about the patch given in post #10 ?
Comment #18
hass CreditAttribution: hass commentedNeed to review and test it... sorry i was busy with google analytics.
Comment #19
KingMoore CreditAttribution: KingMoore commentedJust want to confirm that deleting my "default section" did not make any difference in the # of queries being performed.
Comment #20
hass CreditAttribution: hass commentedAfter more thinking and testing I came to the same result as recidive in #12. My first assumption that this is wrong - was incorrect. A user object could theoretically change at run time by other modules... so the rids must be the key. I'm doing some tests, but it looks very good.
Comment #21
hass CreditAttribution: hass commentedUpdated the patch from #12 with some documentation and changed naming. Commited code. THX.
Could the others in here please test and confirm, before we create a new release?
Comment #22
hass CreditAttribution: hass commentedComment #24
pixelpreview@gmail.com CreditAttribution: pixelpreview@gmail.com commentedok simply to say that the patch in #21 resolve the problem and remove a lot of "_sections_in_section()" in my site too