Hi,

i want to start a discussion about the book module and some SQL query that can be really huge.

Step to reproduce :
Use Drupal 8.7 with book module enable.
Add 2000 node content, no need to be allowed book content type.
Create a view on the homepage to display all the 2000.
You will see a SQL query : SELECT * FROM book b where b.nid IN (
)

This request is slow and not really usefull, because we are just looking of the content that can be books.

I would imagine that we can improve this query in 2 differents ways:
- limit to the node that are from the allowed book type (/admin/structure/book/settings)
- limit to the existing book node (/admin/structure/book)

I don't know which is the best option. But in my case i patch the module with the first option.
This is suffisant in my use case.

Past issue (for drupal 7) with the same problem and same solution: https://www.drupal.org/project/drupal/issues/2070807

Issue fork book-3096844

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

Tomefa created an issue. See original summary.

tomefa’s picture

Here the patch

Version: 8.7.x-dev » 8.9.x-dev

Drupal 8.8.7 was released on June 3, 2020 and is the final full bugfix release for the Drupal 8.8.x series. Branches prior to 8.8.x are not supported, and Drupal 8.8.x will not receive any further development aside from security fixes. Sites should prepare to update to Drupal 8.9.0 or Drupal 9.0.0 for ongoing support.

Bug reports should be targeted against the 8.9.x-dev branch from now on, and new development or disruptive changes should be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.9.x-dev » 9.2.x-dev

Drupal 8 is end-of-life as of November 17, 2021. There will not be further changes made to Drupal 8. Bugfixes are now made to the 9.3.x and higher branches only. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.2.x-dev » 9.3.x-dev

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.15 was released on June 1st, 2022 and is the final full bugfix release for the Drupal 9.3.x series. Drupal 9.3.x will not receive any further development aside from security fixes. Drupal 9 bug reports should be targeted for the 9.4.x-dev branch from now on, and new development or disruptive changes should be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

poker10’s picture

Status: Active » Needs review
Issue tags: -SQL queries
StatusFileSize
new1.44 KB
new1.04 KB

The patch does not applied anymore. Uploading a reroll with some cosmetic changes (like that we do not need to create a second array of nodes if all we need are NIDs).

Status: Needs review » Needs work

The last submitted patch, 7: 3096844_7.patch, failed testing. View results

poker10’s picture

Hmm, it seems like that the failures are not a problem of this patch, but are related to the allowed_types settings.

In those failing migration tests this settings returns NULL. Then it will throw PHP 8 typerror:

TypeError: in_array(): Argument #2 ($haystack) must be of type array, null given

So I think we need to either fix those migration tests, or change the book_type_is_allowed() function to count on the possibility, that NULL can be returned.

function book_type_is_allowed($type) {
  return in_array($type, \Drupal::config('book.settings')->get('allowed_types'));
}

Either way it seems to me that a new issue should be created for this, as this is out of scope of this. Any thoughts?

mcdruid’s picture

Added a related issue where D7 was able to fix this relatively simply as hook_node_load() is passed a list of $types.

pwolanin’s picture

It looks like the function is just sloppy, and yes, we should make sure getting the config gives you and array (and/or cast it to array).

Also, if you are possibly loading many nodes here, it would be faster (IDK if enough to matter?) to inline the check to reduce the number of confg get() calls you have to make?

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.9 was released on December 7, 2022 and is the final full bugfix release for the Drupal 9.4.x series. Drupal 9.4.x will not receive any further development aside from security fixes. Drupal 9 bug reports should be targeted for the 9.5.x-dev branch from now on, and new development or disruptive changes should be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

poker10’s picture

Status: Needs work » Postponed

This turned out to be more complicated, since the allowed_types variable cannot be relied upon. Users with "Administer book outlines" permission can add all types to book outlines. Therefore if we restrict book_node_load for such nodes, it will make these books unusable.

See:
#2862291: [Duplicate] Book outline not limited to allowed content types
#502430: Allow the book outline functionality on non-book-enabled node types to be hidden from users with the "Administer book outlines" permission

If this behavior is a feature, as explained in those linked issues, this needs to be at least postponed until mentioned issues are resolved. Second option is to close this as Won't fix, but the performance issues are there and better to think how we can solve them.

quietone’s picture

This extension is being deprecated, see #3376070: [Meta] Tasks to deprecate Book module. It will be removed from core and moved to a contrib project, #3376101: [11.x] [Meta] Tasks to remove Book.

This is now Postponed. The status is set according to two policies. The Remove a core extension and move it to a contributed project and the Extensions approved for removal policies.

This issue may be re-opened if it can be considered critical, If unsure, re-open the issue and ask in a comment.

Version: 9.5.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

smustgrave’s picture

Project: Drupal core » Book
Version: 11.x-dev » 1.0.x-dev
Component: book.module » Code
smustgrave’s picture

Version: 1.0.x-dev » 2.0.x-dev
Status: Postponed » Postponed (maintainer needs more info)

  • smustgrave committed 0d355688 on 2.0.x
    Issue #3096844 by poker10, tomefa, smustgrave, mcdruid, pwolanin: Book...
smustgrave’s picture

Status: Postponed (maintainer needs more info) » Fixed

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.