From 6d60c444dd094c00d4a9265870ae334944e742aa Mon Sep 17 00:00:00 2001 From: Jason Song Date: Fri, 30 Dec 2022 14:03:41 +0800 Subject: [PATCH 1/2] feat: support estimate count for multiple schemas --- models/db/context.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/models/db/context.go b/models/db/context.go index c8ad0c1aa2739..0127cc6ee124c 100644 --- a/models/db/context.go +++ b/models/db/context.go @@ -188,7 +188,9 @@ func EstimateCount(ctx context.Context, bean interface{}) (int64, error) { case schemas.MYSQL: _, err = e.Context(ctx).SQL("SELECT table_rows FROM information_schema.tables WHERE tables.table_name = ? AND tables.table_schema = ?;", tablename, x.Dialect().URI().DBName).Get(&rows) case schemas.POSTGRES: - _, err = e.Context(ctx).SQL("SELECT reltuples AS estimate FROM pg_class WHERE relname = ?;", tablename).Get(&rows) + // the table can live in multiple schemas of a postgres database + tablename = x.TableName(bean, true) + _, err = e.Context(ctx).SQL("SELECT reltuples::bigint AS estimate FROM pg_class WHERE oid = ?::regclass;", tablename).Get(&rows) case schemas.MSSQL: _, err = e.Context(ctx).SQL("sp_spaceused ?;", tablename).Get(&rows) default: From decf1c98aa2c654a13a099310dea8be44cae5fcf Mon Sep 17 00:00:00 2001 From: Jason Song Date: Fri, 30 Dec 2022 22:22:31 +0800 Subject: [PATCH 2/2] Update models/db/context.go Co-authored-by: delvh --- models/db/context.go | 1 + 1 file changed, 1 insertion(+) diff --git a/models/db/context.go b/models/db/context.go index 0127cc6ee124c..3db8b16528da4 100644 --- a/models/db/context.go +++ b/models/db/context.go @@ -189,6 +189,7 @@ func EstimateCount(ctx context.Context, bean interface{}) (int64, error) { _, err = e.Context(ctx).SQL("SELECT table_rows FROM information_schema.tables WHERE tables.table_name = ? AND tables.table_schema = ?;", tablename, x.Dialect().URI().DBName).Get(&rows) case schemas.POSTGRES: // the table can live in multiple schemas of a postgres database + // See https://wiki.postgresql.org/wiki/Count_estimate tablename = x.TableName(bean, true) _, err = e.Context(ctx).SQL("SELECT reltuples::bigint AS estimate FROM pg_class WHERE oid = ?::regclass;", tablename).Get(&rows) case schemas.MSSQL: