From 295033f59b021e8dce4967ddfcffedf8ceb99837 Mon Sep 17 00:00:00 2001 From: jiyfhust Date: Fri, 23 Aug 2019 17:06:18 +0800 Subject: [PATCH] ddl: tiny refine the `checkForNullValue` function. (#11833) change "select count(*) from `%s`.`%s` where `%s` is null limit 1" to "select 1 from `%s`.`%s` where `%s` is null limit 1" The later would return when the first row meets the condition, thus scanning the whole table is avoided --- ddl/column.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ddl/column.go b/ddl/column.go index 9308254a80748..579d11f25460c 100644 --- a/ddl/column.go +++ b/ddl/column.go @@ -473,12 +473,12 @@ func (w *worker) doModifyColumn(t *meta.Meta, job *model.Job, newCol *model.Colu // checkForNullValue ensure there are no null values of the column of this table. // `isDataTruncated` indicates whether the new field and the old field type are the same, in order to be compatible with mysql. func checkForNullValue(ctx sessionctx.Context, isDataTruncated bool, schema, table, oldCol, newCol model.CIStr) error { - sql := fmt.Sprintf("select count(*) from `%s`.`%s` where `%s` is null limit 1;", schema.L, table.L, oldCol.L) + sql := fmt.Sprintf("select 1 from `%s`.`%s` where `%s` is null limit 1;", schema.L, table.L, oldCol.L) rows, _, err := ctx.(sqlexec.RestrictedSQLExecutor).ExecRestrictedSQL(ctx, sql) if err != nil { return errors.Trace(err) } - rowCount := rows[0].GetInt64(0) + rowCount := len(rows) if rowCount != 0 { if isDataTruncated { return errInvalidUseOfNull