diff --git a/core/modules/taxonomy/src/Plugin/views/relationship/NodeTermData.php b/core/modules/taxonomy/src/Plugin/views/relationship/NodeTermData.php
index 5a88acea4d8bdbbf7538ef9454771fe99ed07738..621f43d04e3dda6c959a2d6424976d9125c3b591 100644
--- a/core/modules/taxonomy/src/Plugin/views/relationship/NodeTermData.php
+++ b/core/modules/taxonomy/src/Plugin/views/relationship/NodeTermData.php
@@ -133,7 +133,9 @@ public function query() {
       $query = Database::getConnection()->select('taxonomy_term_field_data', 'td');
       $query->addJoin($def['type'], 'taxonomy_index', 'tn', 'tn.tid = td.tid');
       $query->condition('td.vid', array_filter($this->options['vids']), 'IN');
-      $query->addTag('taxonomy_term_access');
+      if (empty($this->query->options['disable_sql_rewrite'])) {
+        $query->addTag('taxonomy_term_access');
+      }
       $query->fields('td');
       $query->fields('tn', ['nid']);
       $def['table formula'] = $query;
diff --git a/core/modules/taxonomy/tests/src/Functional/Views/RelationshipNodeTermDataTest.php b/core/modules/taxonomy/tests/src/Functional/Views/RelationshipNodeTermDataTest.php
index c78a511b3c76e44a908296107ee69d90e6768385..5de4e0a7e174d6837aed13c27a82b2d3d5098fb6 100644
--- a/core/modules/taxonomy/tests/src/Functional/Views/RelationshipNodeTermDataTest.php
+++ b/core/modules/taxonomy/tests/src/Functional/Views/RelationshipNodeTermDataTest.php
@@ -3,6 +3,7 @@
 namespace Drupal\Tests\taxonomy\Functional\Views;
 
 use Drupal\views\Views;
+use Drupal\views\ViewExecutable;
 
 /**
  * Tests the taxonomy term on node relationship handler.
@@ -60,4 +61,52 @@ public function testViewsHandlerRelationshipNodeTermData() {
     $this->assertIdenticalResultset($view, $expected_result, $column_map);
   }
 
+  /**
+   * Tests that the 'taxonomy_term_access' tag is added to the Views query.
+   */
+  public function testTag() {
+    // Change the view to test relation limited by vocabulary.
+    $this->config('views.view.test_taxonomy_node_term_data')
+      ->set('display.default.display_options.relationships.term_node_tid.vids', ['views_testing_tags'])
+      ->save();
+    $view = Views::getView('test_taxonomy_node_term_data');
+    $this->executeView($view, [$this->term1->id()]);
+
+    // By default, view has taxonomy_term_access tag.
+    $this->assertQueriesTermAccessTag($view, TRUE);
+
+    // The term_access tag is not set if disable_sql_rewrite is set.
+    $view = Views::getView('test_taxonomy_node_term_data');
+    $display = $view->getDisplay();
+    $display_options = $display->getOption('query');
+    $display_options['options']['disable_sql_rewrite'] = TRUE;
+    $display->setOption('query', $display_options);
+    $view->save();
+    $this->executeView($view, [$this->term1->id()]);
+
+    $this->assertQueriesTermAccessTag($view, FALSE);
+  }
+
+  /**
+   * Assert views queries have taxonomy_term_access tag.
+   *
+   * @param \Drupal\views\ViewExecutable $view
+   *   The View to check for the term access tag.
+   * @param bool $hasTag
+   *   The expected existence of taxonomy_term_access tag.
+   */
+  protected function assertQueriesTermAccessTag(ViewExecutable $view, $hasTag) {
+    $main_query = $view->build_info['query'];
+    $count_query = $view->build_info['count_query'];
+
+    foreach ([$main_query, $count_query] as $query) {
+      $tables = $query->getTables();
+      foreach ($tables as $join_table) {
+        if (is_object($join_table['table'])) {
+          $this->assertSame($join_table['table']->hasTag('taxonomy_term_access'), $hasTag);
+        }
+      }
+    }
+  }
+
 }