I have not been able to get to the root cause of this, but I have a database that returns some unusual results for very simple queries:

SELECT COUNT(nid) FROM node; 
# returns 21019

SELECT COUNT(DISTINCT nid) FROM node; 
# returns 0

explain SELECT COUNT(DISTINCT nid) FROM node; 
# returns 1	SIMPLE	node	index	NULL	node_changed	4	NULL	20276	Using index

SELECT COUNT( vid) FROM node; 
# returns 21019

SELECT COUNT(DISTINCT vid) FROM node; 
# returns 21019

SELECT DISTINCT nid  FROM node; 
#  returns a list of 21019 nids (starting at 10000)

SELECT COUNT( nid), COUNT( vid) FROM node; 
# returns 21019	21019

SELECT COUNT(DISTINCT nid), COUNT(vid) FROM node; 
# returns 0	21019

SELECT COUNT(DISTINCT vid) FROM node; 
# returns 21019

SELECT COUNT(DISTINCT nid), COUNT(DISTINCT vid) FROM node; 
# returns 0	0

The query SELECT COUNT(DISTINCT nid) FROM node; is of particular interest, because it is used in _node_access_rebuild_batch_operation(), which is the batch operation for rebuilding node access permissions. Since this query may return 0 here, and the batch process uses this for the max value, the batch gets stuck in an infinite loop because it tries to compute the completion percentage, but it's dividing by zero instead.

Since nid is the primary key in the node table , it should already be unique, so why does DISTINCT need to be used here at all? I suggest removing it. Otherwise the workaround is to use hook_batch_alter() to replace the _node_access_rebuild_batch_operation() with your own re-implementation with a corrected query. Patch fixing the query to follow.

I have not been able to replicate this in a fresh install. The database I'm working with is from a site that's been around for a while, which previously migrated from D6 by another team of developers who I don't know. So I don't rule out any oddities in database quality, but I haven't encountered any issues in collation etc., This operation has been completely re-written in D8, so presumably it's not an issue there, but I have not tested it.

Misc details:

  • Drupal 7.39
  • PHP 5.4.45
  • MySQL Server version: 5.5.45-37.4-log Percona Server (GPL), Release 37.4, Revision 042e02b
CommentFileSizeAuthor
#2 node_access_rebuild_distinct-2596899-2.patch596 bytesbburg

Comments

bburg created an issue. See original summary.

bburg’s picture

Status: Active » Needs review
StatusFileSize
new596 bytes

Patch removes the DISTINCT qualifier from the query.

David_Rothstein’s picture

Yeah, the DISTINCT doesn't seem like it should be necessary. However it also seems like it should be harmless, and:

SELECT COUNT(nid) FROM node; 
# returns 21019

SELECT COUNT(DISTINCT nid) FROM node; 
# returns 0

explain SELECT COUNT(DISTINCT nid) FROM node; 
# returns 1	SIMPLE	node	index	NULL	node_changed	4	NULL	20276	Using index

These results definitely look crazy :) When I run it on my own site all three give the same number of rows. I really don't get how those could give three different results unless there's something really weird going on with the database, but I can't even imagine what...

bburg’s picture

Possibly a database encoding issue? I didn't get that deep when debugging the problem, and have since moved on.

bburg’s picture

I was discussing this bug with some of my colleagues, and we found this bug report against MySQL: http://bugs.mysql.com/bug.php?id=30402.

So that's a good explanation of what might be driving this issue. The bug seems to have been verified in versions 5.6.12, and the issue itself is still just a "verified" state.

David_Rothstein’s picture

Status: Needs review » Reviewed & tested by the community

Hm, interesting. Well, the patch is harmless (and node access rebuilds still work correctly with it) so I guess we can just do it, even if it is working around someone else's bug.

Looks from https://api.drupal.org/api/drupal/core!modules!node!node.module/function... like Drupal 8 won't need this, only Drupal 7.

David_Rothstein’s picture

Status: Reviewed & tested by the community » Fixed

Committed to 7.x - thanks!

  • David_Rothstein committed 7dfe657 on 7.x
    Issue #2596899 by bburg: Remove unnecessary DISTINCT from node access...

Status: Fixed » Closed (fixed)

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