Bug #109786 ha_innopart::external_lock is hot with point select via pk on partition table
Submitted: 26 Jan 6:59 Modified: 26 Jan 14:17
Reporter: zhai weixiang (OCA) Email Updates:
Status: Verified Impact on me:
None 
Category:MySQL Server: InnoDB storage engine Severity:S3 (Non-critical)
Version:8.0 OS:Any
Assigned to: CPU Architecture:Any

[26 Jan 6:59] zhai weixiang
Description:
while testing point select on partition table, I found ha_innopart::external_lock  is hotest which cost 4.62% cpu time
+    4.62%  mysqld   mysqld                      [.] ha_innopart::external_lock                                                                                ▒
+    3.05%  mysqld   mysqld                      [.] cmp_dtuple_rec_with_match_low                                                                             ▒
+    1.82%  mysqld   mysqld                      [.] page_cur_search_with_match                                                                                ▒
+    1.51%  mysqld   libjemalloc.so.1            [.] malloc                                                                                                    ▒
+    1.50%  mysqld   mysqld                      [.] ha_innopart::clear_blob_heaps                                                                             ▒
+    1.30%  mysqld   mysqld                      [.] btr_cur_search_to_nth_level                                                                               ▒
+    1.30%  mysqld   libc-2.17.so                [.] __memcpy_ssse3_back                                                                                       ▒
+    1.27%  mysqld   mysqld                      [.] buf_page_hash_get_low                                                                                     ▒
+    1.25%  mysqld   mysqld                      [.] row_search_mvcc                                                                                           ▒
+    1.17%  mysqld   mysqld                      [.] dispatch_command                                                                                          ▒
+    1.03%  mysqld   libjemalloc.so.1            [.] free                                                                                                      ▒
+    1.03%  mysqld   mysqld                      [.] rw_lock_s_lock                                                                                            ▒
+    0.97%  mysqld   mysqld                      [.] PolicyMutex<TTASEventMutex<GenericPolicy> >::enter                                                        ▒
+    0.93%  mysqld   mysqld                      [.] JOIN::optimize                                

The root cause is it iterates all partitions(256 parts in my test) in function ha_innopart::external_lock which is unnessesary. I slightly changed the function(take out conditions outside for-loop) and the hot function is gone from perf report. tps from 94.9w to 100w.

How to repeat:
create table and load data with sysbench:
./sysbench --mysql-port=13306 --mysql-host=127.0.0.1 --mysql-user=xx --mysql-db=sb2 --table-size=30000000 --tables=1 --time=1800 --threads=1 --report-interval=1   lua/oltp_point_select.lua  prepare

alter table to partition table:
alter table sbtest1 partition by hash(id) partitions 256;

run sysbench:
./sysbench --mysql-port=13306 --mysql-host=127.0.0.1 --mysql-user=xx --mysql-db=sb2 --table-size=30000000 --tables=1 --time=1800 --threads=256 --report-interval=1   lua/oltp_point_select.lua run

Suggested fix:
take condition out of for-loop in function ha_innopart::external_lock
[26 Jan 7:01] zhai weixiang
for testing

(*) I confirm the code being submitted is offered under the terms of the OCA, and that I am authorized to contribute it.

Contribution: part_external_lock.diff (application/octet-stream, text), 3.68 KiB.

[26 Jan 14:17] MySQL Verification Team
Hello zhai,

Thank you for the report and contribution.

regards,
Umesh