[MDEV-16402] Support Index Condition Pushdown for clustered PK scans Created: 2018-06-05 Updated: 2024-01-23 |
|
| Status: | Confirmed |
| Project: | MariaDB Server |
| Component/s: | Optimizer, Storage Engine - InnoDB, Storage Engine - XtraDB |
| Fix Version/s: | 11.2 |
| Type: | Task | Priority: | Major |
| Reporter: | Marko Mäkelä | Assignee: | Sergei Petrunia |
| Resolution: | Unresolved | Votes: | 1 |
| Labels: | lock, performance | ||
| Issue Links: |
|
||||||||||||||||||||||||
| Description |
|
The following test will lock 3 records and their preceding gaps inside InnoDB, even though only the 2 first records match the condition id<=20:
InnoDB cannot be blamed for this, because the SQL layer is not invoking ha_innobase::idx_cond_push(), so prebuilt->idx_cond will be NULL and InnoDB cannot know that the id=30 record does not match the WHERE condition. To confirm this:
There will be 3 calls to lock_rec_lock() before the DROP TABLE. There should be only 2 calls, with type=LOCK_ORDINARY to lock the keys id=10,id=20 and the gaps preceding them. Another reason to enable index condition pushdown (ICP) for table scan (or PRIMARY KEY scan) is that if the query would return BLOBs, it would be much more efficient to avoid copying and throwing away the BLOBs of non-matching rows. |
| Comments |
| Comment by Marko Mäkelä [ 2018-06-05 ] | |||||||||||||
|
For the record, I also tried the following single-key lookups:
The first one would correctly only acquire a LOCK_REC_NOT_GAP lock on the only matching record id=20, and the second one would only acquire a LOCK_GAP on the gap that protects the non-matching key, attached to the successor record id=30. I believe that ICP mostly helps with range or table scans. With the following test (UNIQUE KEY without NOT NULL), index condition pushdown will be used, and only the secondary index records (id=10,DB_ROW_ID=512),(id=20,DB_ROW_ID=513) (and preceding gaps) will be locked, interleaved with requests to lock the clustered index records (but not preceding gaps) (DB_ROW_ID=512),(DB_ROW_ID=513). There is no request to lock id=30, so the ICP is working as it should.
After the last clustered index record was locked, there is a redundant call to lock (id=20,DB_ROW_ID=513) and the preceding gap (the lock already exists). That is a minor InnoDB bug. | |||||||||||||
| Comment by Sergei Petrunia [ 2018-06-15 ] | |||||||||||||
SQL layer informs InnoDB that the record with id=30 does not match the WHERE by calling ::unlock_row. This is how it is called for the row with id=30:
(I am debugging the SELECT * FROM t1 WHERE id<=20 FOR UPDATE; query ) | |||||||||||||
| Comment by Sergei Petrunia [ 2018-06-15 ] | |||||||||||||
|
It is not difficult to get the SQL layer to call cond_push for Clustered PK, too. However, if InnoDB acknowledges that index condition pushdown has happened, it must make sure that it is not returning records that do not match the index condition. marko, will this happen automatically or one needs to make some changes inside storage/innodb for this to happen? | |||||||||||||
| Comment by Marko Mäkelä [ 2018-06-15 ] | |||||||||||||
|
Yes, unlock_row() would help a little, but it would be even better to not lock the record at all in the first place.
There are 3 calls to row_search_idx_cond_check() for different code paths: looking up a primary key (after using adaptive hash index), searching in the secondary index, and the last one (above) for validating a primary key record in the general case. This last call should/could be moved before lock acquisition in a follow-up InnoDB fix to improve performance further. | |||||||||||||
| Comment by Marko Mäkelä [ 2018-06-21 ] | |||||||||||||
|
The index condition pushdown was initially enabled on all indexes in MySQL, but it was disabled on PRIMARY KEY due to a performance regression in sysbench.
Unfortunately, InnoDB does not currently make use of the range limits, so it is unnecessarily locking (and possibly later unlocking) too many records. | |||||||||||||
| Comment by Marko Mäkelä [ 2022-06-07 ] | |||||||||||||
|
In The commit also mentions "several of the queries in sysbench". Which queries might those be? Would it be possible to avoid pushing down WHERE conditions that never actually filter anything? Could | |||||||||||||
| Comment by Marko Mäkelä [ 2022-06-10 ] | |||||||||||||
|
The code change that was mentioned in a
| |||||||||||||
| Comment by Marko Mäkelä [ 2023-04-25 ] | |||||||||||||
|
It makes sense to implement MDEV-16232 first. After that, the only remaining value of this optimization might be that we can avoid fetching BLOBs for rows that do not match the condition. |