ClickHouse/src
Ivan Babrou d9d8d0242e Optimize PK lookup for queries that match exact PK range
Existing code that looks up marks that match the query has a pathological
case, when most of the part does in fact match the query.

The code works by recursively splitting a part into ranges and then discarding
the ranges that definitely do not match the query, based on primary key.

The problem is that it requires visiting every mark that matches the query,
making the complexity of this sort of look up O(n).

For queries that match exact range on the primary key, we can find
both left and right parts of the range with O(log 2) complexity.

This change implements exactly that.

To engage this optimization, the query must:

* Have a prefix list of the primary key.
* Have only range or single set element constraints for columns.
* Have only AND as a boolean operator.

Consider a table with `(service, timestamp)` as the primary key.

The following conditions will be optimized:

* `service = 'foo'`
* `service = 'foo' and timestamp >= now() - 3600`
* `service in ('foo')`
* `service in ('foo') and timestamp >= now() - 3600 and timestamp <= now`

The following will fall back to previous lookup algorithm:

* `timestamp >= now() - 3600`
* `service in ('foo', 'bar') and timestamp >= now() - 3600`
* `service = 'foo'`

Note that the optimization won't engage when PK has a range expression
followed by a point expression, since in that case the range is not continuous.

Trace query logging provides the following messages types of messages,
each representing a different kind of PK usage for a part:

```
Used optimized inclusion search over index for part 20200711_5710108_5710108_0 with 9 steps
Used generic exclusion search over index for part 20200711_5710118_5710228_5 with 1495 steps
Not using index on part 20200710_5710473_5710473_0
```

Number of steps translates to computational complexity.

Here's a comparison for before and after for a query over 24h of data:

```
Read 4562944 rows, 148.05 MiB in 45.19249672 sec.,   100966 rows/sec.,   3.28 MiB/sec.
Read 4183040 rows, 135.78 MiB in 0.196279627 sec., 21311636 rows/sec., 691.75 MiB/sec.
```

This is especially useful for queries that read data in order
and terminate early to return "last X things" matching a query.

See #11564 for more thoughts on this.
2020-07-11 12:26:54 -07:00
..
Access Fix calculating implicit access rights. 2020-07-10 17:16:43 +03:00
AggregateFunctions Fix error 2020-07-10 08:30:54 +03:00
Client Merge remote-tracking branch 'origin/master' into HEAD 2020-07-07 15:42:11 +03:00
Columns Add comment 2020-07-10 08:42:09 +03:00
Common Merge pull request #12341 from ClickHouse/aku/perfect-visitor 2020-07-11 13:36:37 +03:00
Compression Remove useless code 2020-07-10 04:58:27 +03:00
Core add setting output_format_pretty_grid_charset 2020-07-10 22:25:49 +03:00
Databases Make code clearer: use enum instead of bool internal. 2020-07-05 17:14:29 +03:00
DataStreams Merge pull request #12042 from ClickHouse/fix-constraints 2020-07-05 19:01:33 +03:00
DataTypes Update DataTypeNullable.cpp 2020-07-11 00:29:07 +03:00
Dictionaries Show error after TrieDictionary failed to load. 2020-07-08 13:55:39 +03:00
Disks Implemented single part uploads for DiskS3 (#12026) 2020-07-09 17:09:17 +03:00
Formats add setting output_format_pretty_grid_charset 2020-07-10 22:25:49 +03:00
Functions Merge pull request #12400 from vitlibar/fix-bad_typeid 2020-07-11 05:40:44 +03:00
Interpreters RIGHT and FULL JOIN for MergeJoin (#12118) 2020-07-10 21:10:06 +03:00
IO Implemented single part uploads for DiskS3 (#12026) 2020-07-09 17:09:17 +03:00
Parsers Merge remote-tracking branch 'origin/master' into HEAD 2020-07-08 13:18:09 +03:00
Processors Merge pull request #12372 from s-mx/issue-10429-add_setting_for_ascii_grid_symbols 2020-07-11 05:31:41 +03:00
Server Add integration test for mysql replacement query 2020-07-09 22:20:54 +08:00
Storages Optimize PK lookup for queries that match exact PK range 2020-07-11 12:26:54 -07:00
TableFunctions Fix error 2020-07-06 04:25:45 +03:00
CMakeLists.txt Fix ORC build (#12258) 2020-07-09 03:49:21 +04:00
NOTICE dbms/ → src/ 2020-04-03 18:14:31 +03:00
ya.make Split programs/server into actual program and library (#11186) 2020-05-27 20:52:52 +03:00