pingcap / tidb

TiDB is an open-source, cloud-native, distributed, MySQL-Compatible database for elastic scale and real-time analytics. Try AI-powered Chat2Query free at : https://www.pingcap.com/tidb-serverless/
https://pingcap.com
Apache License 2.0
36.8k stars 5.8k forks source link

[enhance] Try to reduce the reallocation of insertPlan in the buildValuesListOfInsert #19629

Open markjenny opened 3 years ago

markjenny commented 3 years ago

topology

running the tiup playground on my laptop. 1 TiKV, 1 TiDB and 1 PD.

workload

using sysbench to create one database consist of 32 tables. There're 10000 records in each table.

sysbench oltp_update_non_index --config-file=config --threads=32 --tables=32 --table-size=10000 prepare

profile

generate the profile using the method below

curl http://{TiDBIP}:10080/debug/zip?seconds=60 --output debug.zip

the heap profile is here The most memory cost is in the fuction buildValueListOfInsert, So try to find the memory optimization.

In the file planbuilder.go, the variable insertPlan.Lists is zero value. We can init the Lists as below:

insertPlan.Lists = make([][]expression.Expression, 0, len(insert.Lists)) // my modify
insertPlan.AllAssignmentsAreConstant = true
totalTableCols := insertPlan.Table.Cols()
for i, valuesItem := range insert.Lists {
    //...
    insertPlan.Lists = append(insertPlan.Lists, exprList)
}

Can I init the insertPlan to avoid the reallocation of the slice grow?

ZenoTan commented 3 years ago

/label sig/planner

wjhuang2016 commented 3 years ago

@ZenoTan
Yes, you can take a try.