Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[perf] improve delete from table without where & limit #13120

Merged
merged 25 commits into from
Dec 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
0ba1555
[perf] optimizing unconditional delete through truncate
noorall Nov 30, 2023
688bee7
[fix] the issue of being unable to delete special tables
noorall Nov 30, 2023
682d31f
[mod] remove useless codes
noorall Nov 30, 2023
3855d65
[refactor] truncate table operation
noorall Nov 30, 2023
05f9f2f
[refactor] move truncate to deletion operator
noorall Dec 1, 2023
9e13b11
[mod] revert dml.go
noorall Dec 1, 2023
9fcc82a
[mod] add License
noorall Dec 1, 2023
4096cda
[mod] remove useless code
noorall Dec 3, 2023
2fca1dd
Merge branch 'main' into teach-12845
noorall Dec 3, 2023
f761fc4
Revert "[mod] remove useless code"
noorall Dec 5, 2023
8d70fb7
Revert "[mod] add License"
noorall Dec 5, 2023
873ffdf
Revert "[mod] revert dml.go"
noorall Dec 5, 2023
5038a6c
Revert "[refactor] move truncate to deletion operator"
noorall Dec 5, 2023
5751f4b
Revert "[refactor] truncate table operation"
noorall Dec 5, 2023
6937cec
Revert "[mod] remove useless codes"
noorall Dec 5, 2023
bd06f24
Revert "[fix] the issue of being unable to delete special tables"
noorall Dec 5, 2023
46b6731
Revert "[perf] optimizing unconditional delete through truncate"
noorall Dec 5, 2023
42eae4a
[perf] optimizing unconditional delete through truncate
noorall Dec 5, 2023
8a2348c
[fix] explain error case by delete by truncate node
noorall Dec 5, 2023
bb6e9c6
[mod] remove delete by truncate node
noorall Dec 7, 2023
5199899
[mod] add truncate table for deleteCtx
noorall Dec 7, 2023
988f324
[mod] remove useless codes
noorall Dec 7, 2023
50b6a6b
[mod] add judgments for truncate type
noorall Dec 7, 2023
b5198b3
Merge branch 'main' into teach-12845
ouyuanning Dec 7, 2023
e3703f6
Merge branch 'main' into teach-12845
mergify[bot] Dec 10, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions pkg/pb/plan/plan.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 18 additions & 1 deletion pkg/sql/compile/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -557,7 +557,9 @@ func (c *Compile) compileScope(ctx context.Context, pn *plan.Plan) ([]*Scope, er
return nil, err
}
for _, s := range scopes {
s.Plan = pn
if s.Plan == nil {
s.Plan = pn
}
}
return scopes, nil
case *plan.Plan_Ddl:
Expand Down Expand Up @@ -1128,6 +1130,21 @@ func (c *Compile) compilePlanScope(ctx context.Context, step int32, curNodeIdx i
c.setAnalyzeCurrent(right, curr)
return c.compileSort(n, c.compileUnionAll(left, right)), nil
case plan.Node_DELETE:
if n.DeleteCtx.CanTruncate {
return []*Scope{{
Magic: TruncateTable,
Plan: &plan.Plan{
Plan: &plan.Plan_Ddl{
Ddl: &plan.DataDefinition{
DdlType: plan.DataDefinition_TRUNCATE_TABLE,
Definition: &plan.DataDefinition_TruncateTable{
TruncateTable: n.DeleteCtx.TruncateTable,
},
},
},
},
}}, nil
}
c.appendMetaTables(n.DeleteCtx.Ref)
curr := c.anal.curr
c.setAnalyzeCurrent(nil, int(n.Children[0]))
Expand Down
13 changes: 12 additions & 1 deletion pkg/sql/compile/ddl.go
Original file line number Diff line number Diff line change
Expand Up @@ -1264,6 +1264,8 @@ func (s *Scope) TruncateTable(c *Compile) error {
dbName := tqry.GetDatabase()
tblName := tqry.GetTable()
oldId := tqry.GetTableId()
keepAutoIncrement := false
affectedRows := int64(0)

dbSource, err = c.e.Database(c.ctx, dbName, c.proc.TxnOperator)
if err != nil {
Expand Down Expand Up @@ -1305,6 +1307,14 @@ func (s *Scope) TruncateTable(c *Compile) error {
}
}

if tqry.IsDelete {
keepAutoIncrement = true
affectedRows, err = rel.Rows(c.ctx)
if err != nil {
return err
}
}

if isTemp {
// memoryengine truncate always return 0, so for temporary table, just use origin tableId as newId
_, err = dbSource.Truncate(c.ctx, engine.GetTempTableName(dbName, tblName))
Expand Down Expand Up @@ -1388,7 +1398,7 @@ func (s *Scope) TruncateTable(c *Compile) error {
c.ctx,
oldId,
newId,
false,
keepAutoIncrement,
c.proc.TxnOperator)
if err != nil {
return err
Expand All @@ -1400,6 +1410,7 @@ func (s *Scope) TruncateTable(c *Compile) error {
if err != nil {
return err
}
c.addAffectedRows(uint64(affectedRows))
return nil
}

Expand Down
50 changes: 44 additions & 6 deletions pkg/sql/plan/build_dml_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@ type deleteNodeInfo struct {
partTableIDs []uint64 // Align array index with the partition number
partTableNames []string // Align array index with the partition number
partitionIdx int // The array index position of the partition expression column
indexTableNames []string
foreignTbl []uint64
addAffectedRows bool
pkPos int
pkTyp *plan.Type
Expand Down Expand Up @@ -268,7 +270,16 @@ func buildDeletePlans(ctx CompilerContext, builder *QueryBuilder, bindCtx *BindC
// both UK and SK. To handle SK case, we will have flags to indicate if it's UK or SK.
hasUniqueKey := haveUniqueKey(delCtx.tableDef)
hasSecondaryKey := haveSecondaryKey(delCtx.tableDef)
if hasUniqueKey || hasSecondaryKey {
canTruncate := delCtx.isDeleteWithoutFilters

if len(delCtx.tableDef.RefChildTbls) > 0 ||
delCtx.tableDef.ViewSql != nil ||
(util.TableIsClusterTable(delCtx.tableDef.GetTableType()) && ctx.GetAccountId() != catalog.System_Account) ||
delCtx.objRef.PubInfo != nil {
canTruncate = false
}

if (hasUniqueKey || hasSecondaryKey) && !canTruncate {
typMap := make(map[string]*plan.Type)
posMap := make(map[string]int)
for idx, col := range delCtx.tableDef.Cols {
Expand Down Expand Up @@ -330,7 +341,7 @@ func buildDeletePlans(ctx CompilerContext, builder *QueryBuilder, bindCtx *BindC
//sink_scan -> lock -> delete
lastNodeId = appendSinkScanNode(builder, bindCtx, newSourceStep)
delNodeInfo := makeDeleteNodeInfo(builder.compCtx, uniqueObjRef, uniqueTableDef, uniqueDeleteIdx, -1, false, uniqueTblPkPos, uniqueTblPkTyp, delCtx.lockTable, delCtx.partitionInfos)
lastNodeId, err = makeOneDeletePlan(builder, bindCtx, lastNodeId, delNodeInfo, isUk, isSK)
lastNodeId, err = makeOneDeletePlan(builder, bindCtx, lastNodeId, delNodeInfo, isUk, isSK, false)
putDeleteNodeInfo(delNodeInfo)
if err != nil {
return err
Expand Down Expand Up @@ -380,7 +391,7 @@ func buildDeletePlans(ctx CompilerContext, builder *QueryBuilder, bindCtx *BindC
} else {
// it's more simple for delete hidden unique table .so we append nodes after the plan. not recursive call buildDeletePlans
delNodeInfo := makeDeleteNodeInfo(builder.compCtx, uniqueObjRef, uniqueTableDef, uniqueDeleteIdx, -1, false, uniqueTblPkPos, uniqueTblPkTyp, delCtx.lockTable, delCtx.partitionInfos)
lastNodeId, err = makeOneDeletePlan(builder, bindCtx, lastNodeId, delNodeInfo, isUk, isSK)
lastNodeId, err = makeOneDeletePlan(builder, bindCtx, lastNodeId, delNodeInfo, isUk, isSK, false)
putDeleteNodeInfo(delNodeInfo)
if err != nil {
return err
Expand All @@ -400,7 +411,7 @@ func buildDeletePlans(ctx CompilerContext, builder *QueryBuilder, bindCtx *BindC
}
pkPos, pkTyp := getPkPos(delCtx.tableDef, false)
delNodeInfo := makeDeleteNodeInfo(ctx, delCtx.objRef, delCtx.tableDef, delCtx.rowIdPos, partExprIdx, true, pkPos, pkTyp, delCtx.lockTable, delCtx.partitionInfos)
lastNodeId, err := makeOneDeletePlan(builder, bindCtx, lastNodeId, delNodeInfo, false, false)
lastNodeId, err := makeOneDeletePlan(builder, bindCtx, lastNodeId, delNodeInfo, false, false, canTruncate)
putDeleteNodeInfo(delNodeInfo)
if err != nil {
return err
Expand Down Expand Up @@ -1528,6 +1539,7 @@ func makeOneDeletePlan(
delNodeInfo *deleteNodeInfo,
isUK bool, // is delete unique key hidden table
isSK bool,
canTruncate bool,
) (int32, error) {
if isUK || isSK {
// append lock
Expand All @@ -1550,7 +1562,20 @@ func makeOneDeletePlan(
}
lastNodeId = builder.appendNode(lockNode, bindCtx)
}

truncateTable := &plan.TruncateTable{}
if canTruncate {
tableDef := delNodeInfo.tableDef
truncateTable.Table = tableDef.Name
truncateTable.TableId = tableDef.TblId
truncateTable.Database = delNodeInfo.objRef.SchemaName
truncateTable.IndexTableNames = delNodeInfo.indexTableNames
truncateTable.PartitionTableNames = delNodeInfo.partTableNames
truncateTable.ForeignTbl = delNodeInfo.foreignTbl
truncateTable.ClusterTable = &plan.ClusterTable{
IsClusterTable: util.TableIsClusterTable(tableDef.GetTableType()),
}
truncateTable.IsDelete = true
}
// append delete node
deleteNode := &Node{
NodeType: plan.Node_DELETE,
Expand All @@ -1559,13 +1584,14 @@ func makeOneDeletePlan(
DeleteCtx: &plan.DeleteCtx{
RowIdIdx: int32(delNodeInfo.deleteIndex),
Ref: delNodeInfo.objRef,
CanTruncate: false,
CanTruncate: canTruncate,
AddAffectedRows: delNodeInfo.addAffectedRows,
IsClusterTable: delNodeInfo.IsClusterTable,
PartitionTableIds: delNodeInfo.partTableIDs,
PartitionTableNames: delNodeInfo.partTableNames,
PartitionIdx: int32(delNodeInfo.partitionIdx),
PrimaryKeyIdx: int32(delNodeInfo.pkPos),
TruncateTable: truncateTable,
},
}
lastNodeId = builder.appendNode(deleteNode, bindCtx)
Expand Down Expand Up @@ -1678,6 +1704,18 @@ func makeDeleteNodeInfo(ctx CompilerContext, objRef *ObjectRef, tableDef *TableD
}

}
if tableDef.Fkeys != nil {
for _, fk := range tableDef.Fkeys {
delNodeInfo.foreignTbl = append(delNodeInfo.foreignTbl, fk.ForeignTbl)
}
}
if tableDef.Indexes != nil {
for _, indexdef := range tableDef.Indexes {
if indexdef.TableExist {
delNodeInfo.indexTableNames = append(delNodeInfo.indexTableNames, indexdef.IndexTableName)
}
}
}
return delNodeInfo
}

Expand Down
2 changes: 2 additions & 0 deletions proto/plan.proto
Original file line number Diff line number Diff line change
Expand Up @@ -872,6 +872,7 @@ message DeleteCtx {
repeated string partition_table_names = 7;
int32 partition_idx = 8;
int32 primary_key_idx = 9;
TruncateTable truncate_table = 10;
}

message Query {
Expand Down Expand Up @@ -1253,6 +1254,7 @@ message TruncateTable {
ClusterTable cluster_table = 5;
uint64 table_id = 6;
repeated uint64 foreign_tbl = 7;
bool is_delete = 8;
}

message ClusterTable{
Expand Down
Loading