After we migrate to new the newest releases of ClickHouse, we started to notice a considerable performance degradation in almost all of our queries.

Specifically, we migrated from 20.12 to 21.1.

### Query
Here is one of our simplest queries that got affected:

```sql
SELECT
    ProjectId,
    UserId,
    SessionId,
    Url,
    EntryUrl,
    ExitUrl,
    Browser,
    BrowserVersion,
    Device,
    OS,
    OSVersion,
    Country
FROM clarity.page_data_all
WHERE (ProjectId = <id>) AND ((SessionStart >= '2021/03/03 00:00:00') AND (SessionStart <= '2021/03/08 00:00:00')) AND (Playback = 1)
ORDER BY SessionStart DESC
LIMIT 100
```

In `clickhouse-server` version `20.12`:

```
100 rows in set. Elapsed: 0.074 sec. Processed 10.88 million rows, 304.95 MB (147.38 million rows/s., 4.13 GB/s.)
```

In `clickhouse-server` version `21.1`:

```
100 rows in set. Elapsed: 0.181 sec. Processed 10.88 million rows, 3.16 GB (59.98 million rows/s., 17.39 GB/s.)
```

There is a 100 ms increase in latency in the newest version, and this increase is consistent when running the query several times.

**Note:** both versions were running on the exact same data.

### Table Schema
This is a simplified version of our table schema:

```sql
CREATE TABLE IF NOT EXISTS clarity.page_data_local
(
    Timestamp                       DateTime,
    ProjectId                       UInt64,
    UserId                          UInt32,
    SessionId                       UInt32,
    PageNum                         UInt8,
    Device                          UInt8,
    OS                              UInt8,
    OSVersion                       Nullable(String),
    Browser                         UInt8,
    BrowserVersion                  Nullable(String),
    Country                         UInt8,
    Url                             Nullable(String),
    EntryUrl                        Nullable(String),
    ExitUrl                         Nullable(String),
    SessionStart                    DateTime,
    Playback                        UInt8,
)
ENGINE = ReplicatedMergeTree('/clickhouse/tables/clarity/page_data_local/{shard}', '{replica}')
PARTITION BY toYYYYMM(Timestamp)
ORDER BY (ProjectId, Date, intHash32(UserId), SessionId, PageNum)
SAMPLE BY intHash32(UserId)
```

### Profile Events
And here are the profile events of the two versions:

| Event | Old (v20.12) |	New (v21.21)	| Diff |
| ------|-----|-----|------|
| Query |	1.00 |	1.00 |	0.00 |
| SelectQuery |	1.00 |	1.00 |	0.00 |
| FileOpen |	741.00 |	703.00 |	38.00 |
| Seek |	166.00 |	417.00 |	-251.00 |
| ReadBufferFromFileDescriptorRead |	212.00 |	790.00 |	-578.00 |
| ReadBufferFromFileDescriptorReadBytes |	56633195.00 |	225970859.00 |	-169337664.00 |
| ReadCompressedBytes |	40778823.00 |	173846053.00 |	-133067230.00 |
| CompressedReadBufferBlocks |	956.00 |	6810.00 |	-5854.00 |
| CompressedReadBufferBytes |	151034072.00 |	1499238564.00 |	-1348204492.00 |
| IOBufferAllocs |	942.00 |	1410.00 |	-468.00 |
| IOBufferAllocBytes |	171385503.00 |	218698263.00 |	-47312760.00 |
| FunctionExecute |	5173.00 |	7155.00 |	-1982.00 |
| MarkCacheHits |	741.00 |	703.00 |	38.00 |
| CreatedReadBufferOrdinary |	741.00 |	703.00 |	38.00 |
| DiskReadElapsedMicroseconds |	24000.00 |	88000.00 |	-64000.00 |
| NetworkReceiveElapsedMicroseconds |	56534.00 |	150364.00 |	-93830.00 |
| NetworkSendElapsedMicroseconds |	266.00 |	407.00 |	-141.00 |
| SelectedParts |	29.00 |	29.00 |	0.00 |
| SelectedRanges |	29.00 |	29.00 |	0.00 |
| SelectedMarks |	1155.00 |	1155.00 |	0.00 |
| SelectedRows |	10881313.00 |	10881313.00 |	0.00 |
| SelectedBytes |	305029428.00 |	3155152423.00 |	-2850122995.00 |
| ContextLock |	159.00 |	161.00 |	-2.00 |
| RWLockAcquiredReadLocks |	3.00 |	3.00 |	0.00 |
| RealTimeMicroseconds |	658422.00 |	1856185.00 |	-1197763.00 |
| UserTimeMicroseconds |	163011.00 |	1213301.00 |	-1050290.00 |
| SystemTimeMicroseconds |	65631.00 |	169707.00 |	-104076.00 |
| SoftPageFaults |	18305.00 |	8868.00 |	9437.00 |
| OSCPUWaitMicroseconds |	37.00 |	193.00 |	-156.00 |
| OSCPUVirtualTimeMicroseconds |	228617.00 |	99.00 |	228518.00 |
| OSWriteBytes |	24576.00 |	1382946.00 |	-1358370.00 |
| OSReadChars |	56662016.00 |	24576.00 |	56637440.00 |
| OSWriteChars |	76800.00 |	225998848.00 |	-225922048.00 |
{
    if (const auto * func = ast->as<ASTFunction>())
    {
        if (!KeyCondition::atom_map.count(func->name))
            return false;
        const auto & args = func->arguments->children;
        if (args.size() != 2)
            return false;
        const auto & first_arg_name = args.front()->getColumnName();
        const auto & second_arg_name = args.back()->getColumnName();
        if ((first_primary_key_column == first_arg_name && isConstant(args[1]))
            || (first_primary_key_column == second_arg_name && isConstant(args[0]))
            || (first_primary_key_column == first_arg_name && functionIsInOrGlobalInOperator(func->name)))
            return true;
    }
    return false;
}
bool MergeTreeWhereOptimizer::isSortingKey(const String & column_name) const