From 86386135897485bbada6c50ec9a2743626111433 Mon Sep 17 00:00:00 2001 From: Galen Suen Date: Sat, 26 Oct 2024 14:13:52 +0800 Subject: [PATCH] perf: refactor SelectQuery.ScanAndCount to optimize performance when there is no limit and offset (#1035) --- query_select.go | 33 ++++++++++++++++++++++++--------- 1 file changed, 24 insertions(+), 9 deletions(-) diff --git a/query_select.go b/query_select.go index 932cd48be..0c5e9974f 100644 --- a/query_select.go +++ b/query_select.go @@ -856,52 +856,57 @@ func (q *SelectQuery) Exec(ctx context.Context, dest ...interface{}) (res sql.Re } func (q *SelectQuery) Scan(ctx context.Context, dest ...interface{}) error { + _, err := q.scanResult(ctx, dest...) + return err +} + +func (q *SelectQuery) scanResult(ctx context.Context, dest ...interface{}) (sql.Result, error) { if q.err != nil { - return q.err + return nil, q.err } model, err := q.getModel(dest) if err != nil { - return err + return nil, err } if q.table != nil { if err := q.beforeSelectHook(ctx); err != nil { - return err + return nil, err } } if err := q.beforeAppendModel(ctx, q); err != nil { - return err + return nil, err } queryBytes, err := q.AppendQuery(q.db.fmter, q.db.makeQueryBytes()) if err != nil { - return err + return nil, err } query := internal.String(queryBytes) res, err := q.scan(ctx, q, query, model, true) if err != nil { - return err + return nil, err } if n, _ := res.RowsAffected(); n > 0 { if tableModel, ok := model.(TableModel); ok { if err := q.selectJoins(ctx, tableModel.getJoins()); err != nil { - return err + return nil, err } } } if q.table != nil { if err := q.afterSelectHook(ctx); err != nil { - return err + return nil, err } } - return nil + return res, nil } func (q *SelectQuery) beforeSelectHook(ctx context.Context) error { @@ -946,6 +951,16 @@ func (q *SelectQuery) Count(ctx context.Context) (int, error) { } func (q *SelectQuery) ScanAndCount(ctx context.Context, dest ...interface{}) (int, error) { + if q.offset == 0 && q.limit == 0 { + // If there is no limit and offset, we can use a single query to get the count and scan + if res, err := q.scanResult(ctx, dest...); err != nil { + return 0, err + } else if n, err := res.RowsAffected(); err != nil { + return 0, err + } else { + return int(n), nil + } + } if _, ok := q.conn.(*DB); ok { return q.scanAndCountConc(ctx, dest...) }