View preview executes the SQL query twice

Created on 26 March 2024, 6 months ago
Updated 27 March 2024, 5 months ago

Problem/Motivation

When editing a view and updating a view's results using the "Filter" button, the SQL query is executed twice.

Steps to reproduce

  1. Enable Views, Views UI, Node
  2. Set a breakpoint in StatementWrapperIterator::execute, such that it only is hit if the condition is TRUE:
  3. str_contains($this->clientStatement->queryString, '"node_field_data"')
  4. Go to /admin/structure/views/view/content
  5. Click to update preview (the breakpoint is hit once)
  6. Clear the Drupal cache
  7. Click "Filter" (the breakpoint is hit twice)

This is extremely noticeable if it's a long-running query. I'm troubleshooting performance on a site, and a long-running view has it's execution time doubled when I'm testing it in the admin UI like this:

When the view is shown on a page, rather than viewed in the admin UI, the SQL is only executed once, as it should be.

First execution of SQL stack trace for Drupal 10.2.4:

Drupal\Core\Database\StatementWrapperIterator->execute (...\web\core\lib\Drupal\Core\Database\StatementWrapperIterator.php:116)
Drupal\Core\Database\Connection->query (...\web\core\lib\Drupal\Core\Database\Connection.php:851)
Drupal\Core\Database\Query\Select->execute (...\web\core\lib\Drupal\Core\Database\Query\Select.php:525)
Drupal\views\Plugin\views\query\Sql->execute (...\web\core\modules\views\src\Plugin\views\query\Sql.php:1541)
Drupal\views\ViewExecutable->execute (...\web\core\modules\views\src\ViewExecutable.php:1444)
Drupal\views\ViewExecutable->render (...\web\core\modules\views\src\ViewExecutable.php:1472)
Drupal\views\Plugin\views\display\DisplayPluginBase->preview (...\web\core\modules\views\src\Plugin\views\display\DisplayPluginBase.php:2464)
Drupal\views\ViewExecutable->preview (...\web\core\modules\views\src\ViewExecutable.php:1680)
Drupal\views_ui\ViewUI->renderPreview (...\web\core\modules\views_ui\src\ViewUI.php:612)
Drupal\views_ui\ViewPreviewForm->form (...\web\core\modules\views_ui\src\ViewPreviewForm.php:62)
Drupal\Core\Entity\EntityForm->buildForm (...\web\core\lib\Drupal\Core\Entity\EntityForm.php:107)
Drupal\views_ui\ViewFormBase->buildForm (...\web\core\modules\views_ui\src\ViewFormBase.php:41)
call_user_func_array:{/var/www/html/web/core/lib/Drupal/Core/Form/FormBuilder.php:536} (...\web\core\lib\Drupal\Core\Form\FormBuilder.php:536)
Drupal\Core\Form\FormBuilder->retrieveForm (...\web\core\lib\Drupal\Core\Form\FormBuilder.php:536)
Drupal\Core\Form\FormBuilder->buildForm (...\web\core\lib\Drupal\Core\Form\FormBuilder.php:283)
Drupal\Core\Controller\FormController->getContentResult (...\web\core\lib\Drupal\Core\Controller\FormController.php:73)
Drupal\layout_builder\Controller\LayoutBuilderHtmlEntityFormController->getContentResult (...\web\core\modules\layout_builder\src\Controller\LayoutBuilderHtmlEntityFormController.php:39)
call_user_func_array:{/var/www/html/web/core/lib/Drupal/Core/EventSubscriber/EarlyRenderingControllerWrapperSubscriber.php:123} (...\web\core\lib\Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber.php:123)
Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure:/var/www/html/web/core/lib/Drupal/Core/EventSubscriber/EarlyRenderingControllerWrapperSubscriber.php:121-124} (...\web\core\lib\Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber.php:123)
Drupal\Core\Render\Renderer->executeInRenderContext (...\web\core\lib\Drupal\Core\Render\Renderer.php:627)
Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->wrapControllerExecutionInRenderContext (...\web\core\lib\Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber.php:121)
Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure:/var/www/html/web/core/lib/Drupal/Core/EventSubscriber/EarlyRenderingControllerWrapperSubscriber.php:96-98} (...\web\core\lib\Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber.php:97)
Symfony\Component\HttpKernel\HttpKernel->handleRaw (...\vendor\symfony\http-kernel\HttpKernel.php:181)
Symfony\Component\HttpKernel\HttpKernel->handle (...\vendor\symfony\http-kernel\HttpKernel.php:76)
Drupal\Core\StackMiddleware\Session->handle (...\web\core\lib\Drupal\Core\StackMiddleware\Session.php:58)
Drupal\Core\StackMiddleware\KernelPreHandle->handle (...\web\core\lib\Drupal\Core\StackMiddleware\KernelPreHandle.php:48)
Drupal\Core\StackMiddleware\ContentLength->handle (...\web\core\lib\Drupal\Core\StackMiddleware\ContentLength.php:28)
Drupal\big_pipe\StackMiddleware\ContentLength->handle (...\web\core\modules\big_pipe\src\StackMiddleware\ContentLength.php:32)
Drupal\page_cache\StackMiddleware\PageCache->pass (...\web\core\modules\page_cache\src\StackMiddleware\PageCache.php:106)
Drupal\page_cache\StackMiddleware\PageCache->handle (...\web\core\modules\page_cache\src\StackMiddleware\PageCache.php:85)
Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle (...\web\core\lib\Drupal\Core\StackMiddleware\ReverseProxyMiddleware.php:48)
Drupal\Core\StackMiddleware\NegotiationMiddleware->handle (...\web\core\lib\Drupal\Core\StackMiddleware\NegotiationMiddleware.php:51)
Drupal\Core\StackMiddleware\AjaxPageState->handle (...\web\core\lib\Drupal\Core\StackMiddleware\AjaxPageState.php:36)
Drupal\Core\StackMiddleware\StackedHttpKernel->handle (...\web\core\lib\Drupal\Core\StackMiddleware\StackedHttpKernel.php:51)
Drupal\Core\DrupalKernel->handle (...\web\core\lib\Drupal\Core\DrupalKernel.php:704)
{main} (...\web\index.php:19)

Second execution of SQL:

Drupal\Core\Database\StatementWrapperIterator->execute (...\web\core\lib\Drupal\Core\Database\StatementWrapperIterator.php:116)
Drupal\Core\Database\Connection->query (...\web\core\lib\Drupal\Core\Database\Connection.php:851)
Drupal\Core\Database\Query\Select->execute (...\web\core\lib\Drupal\Core\Database\Query\Select.php:525)
Drupal\views\Plugin\views\query\Sql->execute (...\web\core\modules\views\src\Plugin\views\query\Sql.php:1541)
Drupal\views\ViewExecutable->execute (...\web\core\modules\views\src\ViewExecutable.php:1444)
Drupal\views\ViewExecutable->render (...\web\core\modules\views\src\ViewExecutable.php:1472)
Drupal\views\Plugin\views\display\DisplayPluginBase->preview (...\web\core\modules\views\src\Plugin\views\display\DisplayPluginBase.php:2464)
Drupal\views\ViewExecutable->preview (...\web\core\modules\views\src\ViewExecutable.php:1680)
Drupal\views_ui\ViewUI->renderPreview (...\web\core\modules\views_ui\src\ViewUI.php:612)
Drupal\views_ui\ViewPreviewForm->form (...\web\core\modules\views_ui\src\ViewPreviewForm.php:62)
Drupal\Core\Entity\EntityForm->buildForm (...\web\core\lib\Drupal\Core\Entity\EntityForm.php:107)
Drupal\views_ui\ViewFormBase->buildForm (...\web\core\modules\views_ui\src\ViewFormBase.php:41)
call_user_func_array:{/var/www/html/web/core/lib/Drupal/Core/Form/FormBuilder.php:536} (...\web\core\lib\Drupal\Core\Form\FormBuilder.php:536)
Drupal\Core\Form\FormBuilder->retrieveForm (...\web\core\lib\Drupal\Core\Form\FormBuilder.php:536)
Drupal\Core\Form\FormBuilder->rebuildForm (...\web\core\lib\Drupal\Core\Form\FormBuilder.php:375)
Drupal\Core\Form\FormBuilder->processForm (...\web\core\lib\Drupal\Core\Form\FormBuilder.php:645)
Drupal\Core\Form\FormBuilder->buildForm (...\web\core\lib\Drupal\Core\Form\FormBuilder.php:325)
Drupal\Core\Controller\FormController->getContentResult (...\web\core\lib\Drupal\Core\Controller\FormController.php:73)
Drupal\layout_builder\Controller\LayoutBuilderHtmlEntityFormController->getContentResult (...\web\core\modules\layout_builder\src\Controller\LayoutBuilderHtmlEntityFormController.php:39)
call_user_func_array:{/var/www/html/web/core/lib/Drupal/Core/EventSubscriber/EarlyRenderingControllerWrapperSubscriber.php:123} (...\web\core\lib\Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber.php:123)
Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure:/var/www/html/web/core/lib/Drupal/Core/EventSubscriber/EarlyRenderingControllerWrapperSubscriber.php:121-124} (...\web\core\lib\Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber.php:123)
Drupal\Core\Render\Renderer->executeInRenderContext (...\web\core\lib\Drupal\Core\Render\Renderer.php:627)
Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->wrapControllerExecutionInRenderContext (...\web\core\lib\Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber.php:121)
Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure:/var/www/html/web/core/lib/Drupal/Core/EventSubscriber/EarlyRenderingControllerWrapperSubscriber.php:96-98} (...\web\core\lib\Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber.php:97)
Symfony\Component\HttpKernel\HttpKernel->handleRaw (...\vendor\symfony\http-kernel\HttpKernel.php:181)
Symfony\Component\HttpKernel\HttpKernel->handle (...\vendor\symfony\http-kernel\HttpKernel.php:76)
Drupal\Core\StackMiddleware\Session->handle (...\web\core\lib\Drupal\Core\StackMiddleware\Session.php:58)
Drupal\Core\StackMiddleware\KernelPreHandle->handle (...\web\core\lib\Drupal\Core\StackMiddleware\KernelPreHandle.php:48)
Drupal\Core\StackMiddleware\ContentLength->handle (...\web\core\lib\Drupal\Core\StackMiddleware\ContentLength.php:28)
Drupal\big_pipe\StackMiddleware\ContentLength->handle (...\web\core\modules\big_pipe\src\StackMiddleware\ContentLength.php:32)
Drupal\page_cache\StackMiddleware\PageCache->pass (...\web\core\modules\page_cache\src\StackMiddleware\PageCache.php:106)
Drupal\page_cache\StackMiddleware\PageCache->handle (...\web\core\modules\page_cache\src\StackMiddleware\PageCache.php:85)
Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle (...\web\core\lib\Drupal\Core\StackMiddleware\ReverseProxyMiddleware.php:48)
Drupal\Core\StackMiddleware\NegotiationMiddleware->handle (...\web\core\lib\Drupal\Core\StackMiddleware\NegotiationMiddleware.php:51)
Drupal\Core\StackMiddleware\AjaxPageState->handle (...\web\core\lib\Drupal\Core\StackMiddleware\AjaxPageState.php:36)
Drupal\Core\StackMiddleware\StackedHttpKernel->handle (...\web\core\lib\Drupal\Core\StackMiddleware\StackedHttpKernel.php:51)
Drupal\Core\DrupalKernel->handle (...\web\core\lib\Drupal\Core\DrupalKernel.php:704)
{main} (...\web\index.php:19)

Proposed resolution

Remaining tasks

User interface changes

API changes

Data model changes

Release notes snippet

πŸ› Bug report
Status

Active

Version

11.0 πŸ”₯

Component
Views UIΒ  β†’

Last updated 9 days ago

Created by

Live updates comments and jobs are added and updated live.
  • Performance

    It affects performance. It is often combined with the Needs profiling tag.

Sign in to follow issues

Comments & Activities

Production build 0.71.5 2024