85 lines
3.8 KiB
C++
85 lines
3.8 KiB
C++
/**
|
|
* Copyright (c) 2021 OceanBase
|
|
* OceanBase CE is licensed under Mulan PubL v2.
|
|
* You can use this software according to the terms and conditions of the Mulan PubL v2.
|
|
* You may obtain a copy of Mulan PubL v2 at:
|
|
* http://license.coscl.org.cn/MulanPubL-2.0
|
|
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
|
|
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
|
|
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
|
|
* See the Mulan PubL v2 for more details.
|
|
*/
|
|
|
|
#define USING_LOG_PREFIX SQL_ENG
|
|
#include "share/stat/ob_stat_define.h"
|
|
#include "share/stat/ob_dbms_stats_executor.h"
|
|
#include "pl/sys_package/ob_dbms_stats.h"
|
|
#include "sql/engine/cmd/ob_analyze_executor.h"
|
|
#include "sql/engine/ob_exec_context.h"
|
|
#include "share/stat/ob_dbms_stats_lock_unlock.h"
|
|
|
|
//#define COMPUTE_FREQUENCY_HISTOGRAM
|
|
// "SELECT /*+NO_USE_PX*/ col, sum(val) over (order by col rows between unbounded preceding and current row) "
|
|
// "FROM (SELECT %.*s as col, count(*) as val FROM %s WHERE %.*s IS NOT NULL GROUP BY %.*s) temp;"
|
|
//
|
|
//#define COMPUTE_TOP_FREQUENCY_HISTOGRAM
|
|
// "SELECT /*+NO_USE_PX*/ col, sum(val) over (order by col rows between unbounded preceding and current row) "
|
|
// "FROM (SELECT * FROM (SELECT %.*s as col, count(*) as val FROM %s WHERE %.*s IS NOT NULL GROUP BY %.*s ORDER BY val LIMIT %ld) temp) temp2;"
|
|
//
|
|
//#define COMPUTE_HEIGHT_BASED_HISTOGRAM
|
|
// "SELECT /*+NO_USE_PX*/ endpoint_value, sum(endpoint_num) over (order by endpoint_value rows between unbounded preceding and current row) "
|
|
// "FROM (SELECT endpoint_value, count(*) as endpoint_num "
|
|
// "FROM (SELECT MAX(col) as endpoint_value "
|
|
// "FROM (SELECT %.*s as col, ntile(%ld) over (order by %.*s) as bucket FROM %s WHERE %.*s IS NOT NULL) temp GROUP BY bucket) temp2 group by endpoint_value) temp3;"
|
|
|
|
namespace oceanbase
|
|
{
|
|
using namespace common;
|
|
namespace sql
|
|
{
|
|
|
|
int ObAnalyzeExecutor::execute(ObExecContext &ctx, ObAnalyzeStmt &stmt)
|
|
{
|
|
int ret = OB_SUCCESS;
|
|
obrpc::ObCommonRpcProxy *proxy = NULL;
|
|
ObTableStatParam param;
|
|
share::schema::ObSchemaGetterGuard *schema_guard = ctx.get_virtual_table_ctx().schema_guard_;
|
|
ObSQLSessionInfo *session = ctx.get_my_session();
|
|
bool in_restore = false;
|
|
if (OB_ISNULL(schema_guard) || OB_ISNULL(session)) {
|
|
ret = OB_ERR_UNEXPECTED;
|
|
LOG_WARN("get unexpected null", K(ret), K(schema_guard), K(session));
|
|
} else if (OB_FAIL(schema_guard->check_tenant_is_restore(session->get_effective_tenant_id(),
|
|
in_restore))) {
|
|
LOG_WARN("failed to check tenant is restore", K(ret));
|
|
} else if (OB_UNLIKELY(in_restore) ||
|
|
GCTX.is_standby_cluster()) {
|
|
ret = OB_NOT_SUPPORTED;
|
|
LOG_USER_ERROR(OB_NOT_SUPPORTED, "analyze table during restore or standby cluster");
|
|
} else if (OB_FAIL(stmt.fill_table_stat_param(ctx, param))) {
|
|
LOG_WARN("failed to fill table stat param", K(ret));
|
|
} else if (!stmt.is_delete_histogram()) {
|
|
if (OB_FAIL(ctx.get_task_executor_ctx()->get_common_rpc(proxy))) {
|
|
LOG_WARN("failed to get common rpc", K(ret));
|
|
} else if (OB_FAIL(ObDbmsStatsLockUnlock::check_stat_locked(ctx, param))) {
|
|
LOG_WARN("failed check stat locked", K(ret));
|
|
} else if (OB_FAIL(ObDbmsStatsExecutor::gather_table_stats(ctx, param))) {
|
|
LOG_WARN("failed to gather table stats", K(ret));
|
|
} else if (OB_FAIL(pl::ObDbmsStats::update_stat_cache(proxy, param))) {
|
|
LOG_WARN("failed to update stat cache", K(ret));
|
|
} else {
|
|
LOG_TRACE("succeed to gather table stats", K(param));
|
|
}
|
|
} else {
|
|
if (OB_FAIL(ObDbmsStatsExecutor::delete_table_stats(ctx, param, true))) {
|
|
LOG_WARN("failed to drop table stats", K(ret));
|
|
} else {
|
|
LOG_TRACE("succeed to drop table stats", K(param));
|
|
}
|
|
}
|
|
return ret;
|
|
}
|
|
|
|
} // end of SQL
|
|
} // end of OceanBase
|