@@ -446,6 +446,24 @@ def fetch_information_schema(env: Env, target_dbname: str) -> Dict[str, dict]:
446446 tmp_ = env .execute (sql , params = None )
447447 global_var_dict [key ] = int (tmp_ [0 ][1 ])
448448
449+ # part 0: analyze all base tables before fetching statistic data.
450+ failed = 0
451+ all_tables = env .execute (
452+ "SELECT table_name "
453+ "FROM information_schema.tables "
454+ f"WHERE table_schema = '{ target_dbname } ' AND table_type = 'BASE TABLE';"
455+ )
456+ for (table_name ,) in all_tables :
457+ logging .info (f"analyze table `{ target_dbname } `.`{ table_name } `" )
458+ try :
459+ env .execute (f"ANALYZE TABLE `{ target_dbname } `.`{ table_name } `;" )
460+ except Exception as e :
461+ failed += 1
462+ logging .warning (f"analyze table `{ target_dbname } `.`{ table_name } ` failed: { e } " )
463+
464+ if failed :
465+ logging .warning (f"ANALYZE TABLE completed with { failed } failures; stats may be inaccurate." )
466+
449467 # part 1: basic
450468 sql = """
451469 SELECT TABLE_CATALOG, TABLE_SCHEMA, TABLE_NAME, TABLE_TYPE, ENGINE,
@@ -490,6 +508,9 @@ def fetch_information_schema(env: Env, target_dbname: str) -> Dict[str, dict]:
490508 print (table_name , 'not found in res_dict' )
491509 else :
492510 res_dict [table_name ].update (row )
511+ logging .debug (f"- stats { table_name = } { row } "
512+ f"N_ROWS={ res_dict [table_name ].get ('N_ROWS' )} "
513+ f"TABLE_ROWS={ res_dict [table_name ].get ('TABLE_ROWS' )} " )
493514
494515 # part 3: table_in_mem_estimate
495516 sql = """
0 commit comments