> ## Documentation Index
> Fetch the complete documentation index at: https://docs.cockroachlabs.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Technical Advisory 97090

export const InternalLink = ({version, path = "", children, ...props}) => {
  let detectedVersion = version || "stable";
  if (typeof window !== 'undefined' && !version) {
    const match = window.location.pathname.match(/\/docs\/([^/]+)/);
    if (match) {
      detectedVersion = match[1];
    }
  }
  const normalizedPath = path.startsWith("/") ? path.slice(1) : path;
  return <a href={`/docs/${detectedVersion}/${normalizedPath}`} {...props}>
      {children}
    </a>;
};

Publication date: March 6, 2023

## Description

In all versions of CockroachDB from <InternalLink version="releases" path="v19.1">v19.1</InternalLink> to <InternalLink version="releases" path="v22.1">v22.1.15</InternalLink> and <InternalLink version="releases" path="v22.2">v22.2.0</InternalLink> to <InternalLink version="releases" path="v22.2">v22.2.5</InternalLink>, queries that used a [zigzag join](https://www.cockroachlabs.com/docs/v22.2/cost-based-optimizer#zigzag-joins") could return incorrect results in some cases. Zigzag joins can be planned when at least two columns from the same table are fixed to constant values by the query predicate, and [indexes](https://www.cockroachlabs.com/docs/v22.2/indexes) exist on both of those columns. The bug could occur when the two indexes used had a matching suffix of key columns where the direction of at least one column differed between the two indexes.

For example, if a table `t` contained indexes `INDEX (a ASC, c DESC)` and `INDEX (b ASC, c ASC)`, a query such as `SELECT * FROM t WHERE a = 1 AND b = 2;` could return incorrect results, since the direction of column `c` was `DESC` in one index and `ASC` in the other.

## Statement

This is resolved in CockroachDB by a fix that prevents the [optimizer](https://www.cockroachlabs.com/docs/v22.2/cost-based-optimizer) from planning zigzag joins when any columns in the matching suffix between two candidate indexes have different directions.

The fix has been applied to maintenance releases of CockroachDB <InternalLink version="releases" path="v22.1">v22.1.16</InternalLink> and <InternalLink version="releases" path="v22.2">v22.2.6</InternalLink>.

## Mitigation

Users of CockroachDB <InternalLink version="releases" path="v19.1">v19.1</InternalLink> to <InternalLink version="releases" path="v22.1">v22.1.15</InternalLink> and <InternalLink version="releases" path="v22.2">v22.2.0</InternalLink> to <InternalLink version="releases" path="v22.2">v22.2.5</InternalLink> are encouraged to upgrade to <InternalLink version="releases" path="v22.1">v22.1.16</InternalLink>, <InternalLink version="releases" path="v22.2">v22.2.6</InternalLink>, or a later version.

To determine whether your queries may be affected by this issue on prior versions, you should examine your query plans. If the output of [`EXPLAIN`](https://www.cockroachlabs.com/docs/v22.2/explain) shows that the query is using a zigzag join, it may be affected by this issue. You can mitigate the problem by setting the [session setting](https://www.cockroachlabs.com/docs/v22.2/set-vars) `enable_zigzag_join` to `false`. On v21.2 or later, you can also force the optimizer to avoid zigzag joins with a [`NO_ZIGZAG_JOIN` hint](https://www.cockroachlabs.com/docs/v22.2/cost-based-optimizer#prevent-or-force-a-zigzag-join).

The best mitigation, however, is to upgrade to <InternalLink version="releases" path="v22.1">v22.1.16</InternalLink> or <InternalLink version="releases" path="v22.2">v22.2.6</InternalLink> as soon as possible.

## Impact

Some queries planned with a zigzag join could produce incorrect results if the two indexes used for the zigzag join had a matching suffix of index key columns where the direction (`ASC`/`DESC`) was different between the two indexes. Versions affected include all versions of CockroachDB <InternalLink version="releases" path="v19.1">v19.1</InternalLink> to <InternalLink version="releases" path="v22.1">v22.1.15</InternalLink> and <InternalLink version="releases" path="v22.2">v22.2.0</InternalLink> to <InternalLink version="releases" path="v22.2">v22.2.5</InternalLink>.

Questions about any technical alert can be directed to our [support team](https://support.cockroachlabs.com/).
