Commit 1167dba
Improve count(*) performance and fix index merge bug
Summary:
Today in `SELECT count(*)` MyRocks would still decode every single column due to this check, despite the readset being empty:
```
// bitmap is cleared on index merge, but it still needs to decode columns
bool field_requested =
decode_all_fields || m_verify_row_debug_checksums ||
bitmap_is_set(field_map, m_table->field[i]->field_index);
```
As a result MyRocks is significantly slower than InnoDB in this particular scenario.
Turns out in index merge, when it tries to reset, it calls ha_index_init with an empty column_bitmap, so our field decoders didn't know it needs to decode anything, so the entire query would return nothing. This is discussed in [this commit](facebook@70f2bcd), and [issue 624](facebook#624) and [PR 626](facebook#626). So the workaround we had at that time is to simply treat empty map as implicitly everything, and the side effect is massively slowed down count(*).
We have a few options to address this:
1. Fix index merge optimizer - looking at the code in QUICK_RANGE_SELECT::init_ror_merged_scan, it actually fixes up the column_bitmap properly, but after init/reset, so the fix would simply be moving the bitmap set code up. For secondary keys, prepare_for_position will automatically call `mark_columns_used_by_index_no_reset(s->primary_key, read_set)` if HA_PRIMARY_KEY_REQUIRED_FOR_POSITION is set (true for both InnoDB and MyRocks), so we would know correctly that we need to unpack PK when walking SK during index merge.
2. Overriding `column_bitmaps_signal` and setup decoders whenever the bitmap changes - however this doesn't work by itself. Because no storage engine today actually use handler::column_bitmaps_signal this path haven't been tested properly in index merge. In this case, QUICK_RANGE_SELECT::init_ror_merged_scan should call set_column_bitmaps_no_signal to avoid resetting the correct read/write set of head since head is used as first handler (reuses_handler=true) and subsequent place holders for read/write set updates (reuse_handler=false).
3. Follow InnoDB's solution - InnoDB delays it actually initialize its template again in index_read for the 2nd time (relying on `prebuilt->sql_stat_start`), and during index_read `QUICK_RANGE_SELECT::column_bitmap` is already fixed up and the table read/write set is switched to it, so the new template would be built correctly.
In order to make it easier to maintain and port, after discussing with Manuel, I'm going with a simplified version of facebook#3 that delays decoder creation until the first read operation (index_*, rnd_*, range_read_*, multi_range_read_*), and setting the delay flag in index_init / rnd_init / multi_range_read_init.
Also, I ran into a bug with truncation_partition where Rdb_converter's tbl_def is stale (we only update ha_rocksdb::m_tbl_def), but it is fine because it is not being used after table open. But my change moves the lookup_bitmap initialization into Rdb_converter which takes a dependency on Rdb_converter::m_tbl_def so now we need to reset it properly.
Reference Patch: facebook@44d6a8d
---------
Porting Note: Due to 8.0's new counting infra (handler::record & handler::record_with_index), this only helps PK counting. Will send out a better fix that works better with 8.0 new counting infra.
Reviewed By: Pushapgl
Differential Revision: D262654701 parent e4e5295 commit 1167dba
File tree
5 files changed
+84
-38
lines changed- storage/rocksdb
5 files changed
+84
-38
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
7330 | 7330 | | |
7331 | 7331 | | |
7332 | 7332 | | |
7333 | | - | |
| 7333 | + | |
| 7334 | + | |
7334 | 7335 | | |
7335 | 7336 | | |
7336 | 7337 | | |
| |||
8803 | 8804 | | |
8804 | 8805 | | |
8805 | 8806 | | |
8806 | | - | |
8807 | | - | |
| 8807 | + | |
8808 | 8808 | | |
8809 | 8809 | | |
8810 | 8810 | | |
| |||
8892 | 8892 | | |
8893 | 8893 | | |
8894 | 8894 | | |
| 8895 | + | |
8895 | 8896 | | |
8896 | 8897 | | |
8897 | 8898 | | |
| |||
9187 | 9188 | | |
9188 | 9189 | | |
9189 | 9190 | | |
9190 | | - | |
9191 | | - | |
| 9191 | + | |
| 9192 | + | |
| 9193 | + | |
9192 | 9194 | | |
9193 | 9195 | | |
9194 | 9196 | | |
| |||
9318 | 9320 | | |
9319 | 9321 | | |
9320 | 9322 | | |
9321 | | - | |
| 9323 | + | |
| 9324 | + | |
9322 | 9325 | | |
9323 | 9326 | | |
9324 | 9327 | | |
| |||
9363 | 9366 | | |
9364 | 9367 | | |
9365 | 9368 | | |
| 9369 | + | |
| 9370 | + | |
9366 | 9371 | | |
9367 | 9372 | | |
9368 | 9373 | | |
| |||
10005 | 10010 | | |
10006 | 10011 | | |
10007 | 10012 | | |
| 10013 | + | |
| 10014 | + | |
10008 | 10015 | | |
10009 | 10016 | | |
10010 | 10017 | | |
| |||
10025 | 10032 | | |
10026 | 10033 | | |
10027 | 10034 | | |
| 10035 | + | |
| 10036 | + | |
10028 | 10037 | | |
10029 | 10038 | | |
10030 | 10039 | | |
| |||
10085 | 10094 | | |
10086 | 10095 | | |
10087 | 10096 | | |
| 10097 | + | |
| 10098 | + | |
10088 | 10099 | | |
10089 | 10100 | | |
10090 | 10101 | | |
| |||
10101 | 10112 | | |
10102 | 10113 | | |
10103 | 10114 | | |
| 10115 | + | |
| 10116 | + | |
10104 | 10117 | | |
10105 | 10118 | | |
10106 | 10119 | | |
| |||
11472 | 11485 | | |
11473 | 11486 | | |
11474 | 11487 | | |
| 11488 | + | |
| 11489 | + | |
| 11490 | + | |
11475 | 11491 | | |
11476 | 11492 | | |
11477 | 11493 | | |
11478 | 11494 | | |
11479 | 11495 | | |
11480 | 11496 | | |
11481 | 11497 | | |
11482 | | - | |
11483 | | - | |
11484 | | - | |
11485 | | - | |
11486 | 11498 | | |
11487 | 11499 | | |
11488 | 11500 | | |
| |||
11506 | 11518 | | |
11507 | 11519 | | |
11508 | 11520 | | |
| 11521 | + | |
| 11522 | + | |
11509 | 11523 | | |
11510 | 11524 | | |
11511 | 11525 | | |
| |||
11651 | 11665 | | |
11652 | 11666 | | |
11653 | 11667 | | |
| 11668 | + | |
| 11669 | + | |
11654 | 11670 | | |
11655 | 11671 | | |
11656 | 11672 | | |
11657 | 11673 | | |
11658 | 11674 | | |
| 11675 | + | |
| 11676 | + | |
| 11677 | + | |
| 11678 | + | |
| 11679 | + | |
| 11680 | + | |
| 11681 | + | |
| 11682 | + | |
| 11683 | + | |
| 11684 | + | |
| 11685 | + | |
| 11686 | + | |
| 11687 | + | |
11659 | 11688 | | |
11660 | 11689 | | |
11661 | 11690 | | |
| |||
11664 | 11693 | | |
11665 | 11694 | | |
11666 | 11695 | | |
| 11696 | + | |
| 11697 | + | |
| 11698 | + | |
11667 | 11699 | | |
11668 | 11700 | | |
11669 | 11701 | | |
| |||
11672 | 11704 | | |
11673 | 11705 | | |
11674 | 11706 | | |
11675 | | - | |
11676 | | - | |
11677 | | - | |
11678 | | - | |
11679 | | - | |
11680 | | - | |
11681 | | - | |
11682 | | - | |
11683 | 11707 | | |
11684 | 11708 | | |
11685 | 11709 | | |
11686 | 11710 | | |
11687 | 11711 | | |
11688 | | - | |
11689 | | - | |
11690 | 11712 | | |
11691 | 11713 | | |
11692 | 11714 | | |
| |||
11697 | 11719 | | |
11698 | 11720 | | |
11699 | 11721 | | |
11700 | | - | |
| 11722 | + | |
11701 | 11723 | | |
11702 | | - | |
| 11724 | + | |
11703 | 11725 | | |
11704 | 11726 | | |
11705 | 11727 | | |
| |||
12052 | 12074 | | |
12053 | 12075 | | |
12054 | 12076 | | |
| 12077 | + | |
| 12078 | + | |
12055 | 12079 | | |
12056 | 12080 | | |
12057 | 12081 | | |
| |||
16868 | 16892 | | |
16869 | 16893 | | |
16870 | 16894 | | |
| 16895 | + | |
| 16896 | + | |
16871 | 16897 | | |
16872 | 16898 | | |
16873 | 16899 | | |
| |||
17114 | 17140 | | |
17115 | 17141 | | |
17116 | 17142 | | |
| 17143 | + | |
| 17144 | + | |
17117 | 17145 | | |
17118 | 17146 | | |
17119 | 17147 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
383 | 383 | | |
384 | 384 | | |
385 | 385 | | |
386 | | - | |
387 | | - | |
388 | | - | |
389 | | - | |
390 | | - | |
391 | | - | |
392 | | - | |
393 | 386 | | |
394 | 387 | | |
395 | 388 | | |
| |||
956 | 949 | | |
957 | 950 | | |
958 | 951 | | |
959 | | - | |
| 952 | + | |
960 | 953 | | |
961 | 954 | | |
962 | 955 | | |
| |||
1012 | 1005 | | |
1013 | 1006 | | |
1014 | 1007 | | |
| 1008 | + | |
| 1009 | + | |
| 1010 | + | |
1015 | 1011 | | |
1016 | 1012 | | |
1017 | 1013 | | |
| |||
1048 | 1044 | | |
1049 | 1045 | | |
1050 | 1046 | | |
| 1047 | + | |
| 1048 | + | |
| 1049 | + | |
1051 | 1050 | | |
1052 | 1051 | | |
1053 | 1052 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1306 | 1306 | | |
1307 | 1307 | | |
1308 | 1308 | | |
1309 | | - | |
| 1309 | + | |
| 1310 | + | |
1310 | 1311 | | |
1311 | 1312 | | |
1312 | 1313 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
313 | 313 | | |
314 | 314 | | |
315 | 315 | | |
| 316 | + | |
316 | 317 | | |
317 | 318 | | |
318 | 319 | | |
| |||
337 | 338 | | |
338 | 339 | | |
339 | 340 | | |
340 | | - | |
| 341 | + | |
341 | 342 | | |
342 | 343 | | |
343 | 344 | | |
344 | 345 | | |
345 | 346 | | |
346 | 347 | | |
347 | | - | |
348 | 348 | | |
349 | 349 | | |
350 | 350 | | |
351 | 351 | | |
352 | 352 | | |
353 | 353 | | |
| 354 | + | |
354 | 355 | | |
355 | 356 | | |
356 | 357 | | |
| 358 | + | |
357 | 359 | | |
358 | 360 | | |
359 | 361 | | |
360 | 362 | | |
361 | | - | |
362 | 363 | | |
363 | 364 | | |
364 | | - | |
365 | 365 | | |
366 | 366 | | |
367 | 367 | | |
| |||
397 | 397 | | |
398 | 398 | | |
399 | 399 | | |
| 400 | + | |
| 401 | + | |
| 402 | + | |
| 403 | + | |
| 404 | + | |
400 | 405 | | |
401 | 406 | | |
402 | 407 | | |
| |||
584 | 589 | | |
585 | 590 | | |
586 | 591 | | |
| 592 | + | |
| 593 | + | |
| 594 | + | |
| 595 | + | |
| 596 | + | |
587 | 597 | | |
588 | 598 | | |
589 | 599 | | |
| |||
605 | 615 | | |
606 | 616 | | |
607 | 617 | | |
608 | | - | |
| 618 | + | |
609 | 619 | | |
610 | 620 | | |
611 | 621 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
134 | 134 | | |
135 | 135 | | |
136 | 136 | | |
137 | | - | |
138 | | - | |
| 137 | + | |
| 138 | + | |
139 | 139 | | |
140 | 140 | | |
141 | 141 | | |
| |||
172 | 172 | | |
173 | 173 | | |
174 | 174 | | |
| 175 | + | |
| 176 | + | |
175 | 177 | | |
176 | 178 | | |
177 | 179 | | |
| |||
241 | 243 | | |
242 | 244 | | |
243 | 245 | | |
| 246 | + | |
| 247 | + | |
| 248 | + | |
| 249 | + | |
| 250 | + | |
| 251 | + | |
244 | 252 | | |
245 | 253 | | |
0 commit comments