Problem/Motivation

Apache/2.4.18 (Ubuntu)
PHP Version 7.0.8 ubuntu 16.04.2 x64

1. Create content type
2. Add some records
3. Add a comment field to the content type
4. Create view from that content type
5. Once I add the field Comment count to the view, the records disappear

Records should not disappear. Looks like it is SQL problem it is with INNER join to table "comment_entity_statistics", should be with LEFT join.

SQL from my environment

SELECT node_field_data.langcode AS node_field_data_langcode, comment_entity_statistics.comment_count AS comment_entity_statistics_comment_count, node_field_data.created AS node_field_data_created, node_field_data.nid AS nid
FROM 
{node_field_data} node_field_data
INNER JOIN {comment_entity_statistics} comment_entity_statistics ON node_field_data.nid = comment_entity_statistics.entity_id AND comment_entity_statistics.entity_type = 'node'
WHERE (( (node_field_data.status = '1') AND (node_field_data.type IN  ('raksts')) ))
ORDER BY node_field_data_created DESC
LIMIT 10 OFFSET 0

I fixed it by adding records in table "comment_entity_statistics" manually.

Proposed resolution

Change the INNER join to a LEFT join. This effectively reverts #375342: Unnecessary LEFT JOIN on {node_comment_statistics} causing bad query performance which was done in the days of D6 when comments weren't a field yet. That commit may have given performance and made no harm then, but together with other changes now it is a performance optimisation that does harm.

Remaining tasks

Review

User interface changes

None

API changes

None

Data model changes

None

Release notes snippet

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

kristaps.vitolins created an issue. See original summary.

Sutharsan’s picture

Title: View loses records by adding comment count » View loses records after adding comment count field
Issue summary: View changes

I helped @kristaps.vitolins to investigate this issue and come to the same conclusion. Records in the 'comment_entity_statistics' table are only added after a comment field was added to the entity.

Sutharsan’s picture

This patches fixes it. But it considering the comments in the code about multiple comment fields in one entity, I expect some complexities and this fix may have side effects.

Sutharsan’s picture

Status: Active » Needs review
dawehner’s picture

One thing I'd certainly check with git blame in core and the old views repo, whether in the past this used to be LEFT and went to INNER to fix some bug.

Sutharsan’s picture

It has been 'INNER' since the early days of Views 7.x-3.x (31dd0540) November 2009. That is as far as I traced it back, did not look any further (yet).

Version: 8.2.x-dev » 8.3.x-dev

Drupal 8.2.6 was released on February 1, 2017 and is the final full bugfix release for the Drupal 8.2.x series. Drupal 8.2.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.3.0 on April 5, 2017. (Drupal 8.3.0-alpha1 is available for testing.)

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

Version: 8.3.x-dev » 8.4.x-dev

Drupal 8.3.6 was released on August 2, 2017 and is the final full bugfix release for the Drupal 8.3.x series. Drupal 8.3.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.4.0 on October 4, 2017. (Drupal 8.4.0-alpha1 is available for testing.)

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

Version: 8.4.x-dev » 8.5.x-dev

Drupal 8.4.4 was released on January 3, 2018 and is the final full bugfix release for the Drupal 8.4.x series. Drupal 8.4.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.5.0 on March 7, 2018. (Drupal 8.5.0-alpha1 is available for testing.)

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

andypost’s picture

dawehner’s picture

Issue tags: +Needs tests

I just had a look, this would effectively revert #375342: Unnecessary LEFT JOIN on {node_comment_statistics} causing bad query performance. It was just used for performance reasons originally, but well, the original assumption was wrong. It is not a 1to1 relationship.

Is there a way how we could test it?

gilles.koffmann’s picture

Version: 8.5.x-dev » 8.5.3
FileSize
710 bytes

Here is the patch for 8.5.3... But it does not seem to resolve the problem on this drupal version...

andypost’s picture

Version: 8.5.3 » 8.6.x-dev

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

Drupal 8.6.0-alpha1 will be released the week of July 16, 2018, which means new developments and disruptive changes should now be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

reszli’s picture

Status: Needs review » Reviewed & tested by the community

tested on 8.5.5 and it works as expected

andypost’s picture

Status: Reviewed & tested by the community » Needs work
nashkrammer’s picture

Tested patch on #12 8.5.8 core and works as expected.

DamienGR’s picture

Tested on 8.6.7 and works great

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

Drupal 8.7.0-alpha1 will be released the week of March 11, 2019, which means new developments and disruptive changes should now be targeted against the 8.8.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

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

Drupal 8.8.0-alpha1 will be released the week of October 14th, 2019, which means new developments and disruptive changes should now be targeted against the 8.9.x-dev branch. (Any changes to 8.9.x will also be committed to 9.0.x in preparation for Drupal 9’s release, but some changes like significant feature additions will be deferred to 9.1.x.). For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

geek-merlin’s picture

Closed #2654986: A views may not display all content if we use the "comment_count" field and the field was added after some content exists as dup of this.

As of #16:
> It still needs ... approval why it reverts #375342: Unnecessary LEFT JOIN on {node_comment_statistics} causing bad query performance

This is answered by @dawehner #11 (and who should know better than them): This was originally intented to be a performance optimization with no result change based on the assumption "{node_comment_statistics} is always 1:1 on {node}" (IS). That turned out to be wrong and results do change. So that commit was wrong.

Additionally, #2330627-8: Create CommentStatistics entries when new comment field is created. does not provide any reason that comment statistics are to be created mandatorily

So NW for tests only.

Lendude’s picture

That turned out to be wrong and results do change. So that commit was wrong.

The original fix was for D6 and I think this was true then since comments weren't a field back in those days.

geek-merlin’s picture

> The original fix was for D6 and I think this was true then

I suppose you are right. So let's re-state: That commit may have given performance and made no harm then, but together with other changes now it is a performance optimization that does harm.

Lendude’s picture

Issue summary: View changes

#23 sounds spot on, updated the IS to reflect the direction of the change and the discussion about reverting an old optimisation.

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

Drupal 8.9.0-beta1 was released on March 20, 2020. 8.9.x is the final, long-term support (LTS) minor release of Drupal 8, which means new developments and disruptive changes should now 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.

wombatbuddy’s picture

Tested patch #12 on 8.8.4 core and works as expected.

Lendude’s picture

Status: Needs work » Needs review
Issue tags: -Needs tests
FileSize
8.84 KB
9.54 KB

Here is a test for this.

Interdiff is the test only file.

Status: Needs review » Needs work

The last submitted patch, 27: 2801929-27.patch, failed testing. View results

ridhimaabrol24’s picture

Assigned: Unassigned » ridhimaabrol24
ridhimaabrol24’s picture

Assigned: ridhimaabrol24 » Unassigned
Status: Needs work » Needs review
FileSize
9.57 KB
1.23 KB

Fixed the deprecated function.

geek-merlin’s picture

@ridhimaabrol24 Can you post the new test-only to validate it is red so provokes the issue?

Lendude’s picture

Lendude’s picture

Ah sorry forgot to add the original patch, we want de last patch to be the one with the fix

The last submitted patch, 32: 2801929-30-TEST-ONLY.patch, failed testing. View results

geek-merlin’s picture

Status: Needs review » Reviewed & tested by the community

Code looks good. Test provokes the issue and that oneliner fixes it.

JordiK’s picture

Applied cleanly and worked for me on D8.9.

Rohit Tiwari’s picture

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 33: 2801929-30.patch, failed testing. View results

andypost’s picture

Status: Needs work » Reviewed & tested by the community

bot flux

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 33: 2801929-30.patch, failed testing. View results

Lendude’s picture

Status: Needs work » Reviewed & tested by the community

Unrelated fail

alexpott’s picture

Version: 9.1.x-dev » 8.9.x-dev
Status: Reviewed & tested by the community » Fixed

Committed and pushed a76a7586a7 to 9.1.x and 08b33b5731 to 9.0.x and c68ebfbc41 to 8.9.x. Thanks!

Ran the test locally on 8.9.x and it's green so all good for a backport.

  • alexpott committed a76a758 on 9.1.x
    Issue #2801929 by Lendude, ridhimaabrol24, Sutharsan, gilles.koffmann,...

  • alexpott committed 08b33b5 on 9.0.x
    Issue #2801929 by Lendude, ridhimaabrol24, Sutharsan, gilles.koffmann,...

  • alexpott committed c68ebfb on 8.9.x
    Issue #2801929 by Lendude, ridhimaabrol24, Sutharsan, gilles.koffmann,...

Status: Fixed » Closed (fixed)

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