From c7eae459f2b789bd9852c9976e5c5c0b99721a9a Mon Sep 17 00:00:00 2001 From: Eukanj827 Date: Thu, 11 Aug 2022 17:31:27 +0800 Subject: [PATCH 1/8] Update driver_execute.py --- .../tools/components/index_advisor/dao/driver_execute.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/gausskernel/dbmind/tools/components/index_advisor/dao/driver_execute.py b/src/gausskernel/dbmind/tools/components/index_advisor/dao/driver_execute.py index 62fe9bf9b..38951401e 100644 --- a/src/gausskernel/dbmind/tools/components/index_advisor/dao/driver_execute.py +++ b/src/gausskernel/dbmind/tools/components/index_advisor/dao/driver_execute.py @@ -18,6 +18,12 @@ import psycopg2 from .execute_factory import ExecuteFactory from .execute_factory import IndexInfo +#class name: +#description: +#methods: +#note: +#date: 2022/8/10 +#contact: 1865997821 class DriverExecute(ExecuteFactory): def __init__(self, *arg): From 62e6f3a028e4c3c6fa36e729281c4bf83b208be4 Mon Sep 17 00:00:00 2001 From: Eukanj827 Date: Thu, 11 Aug 2022 17:32:10 +0800 Subject: [PATCH 2/8] Update execute_factory.py --- .../components/index_advisor/dao/execute_factory.py | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/src/gausskernel/dbmind/tools/components/index_advisor/dao/execute_factory.py b/src/gausskernel/dbmind/tools/components/index_advisor/dao/execute_factory.py index 8517972a0..88ffade7f 100644 --- a/src/gausskernel/dbmind/tools/components/index_advisor/dao/execute_factory.py +++ b/src/gausskernel/dbmind/tools/components/index_advisor/dao/execute_factory.py @@ -13,6 +13,12 @@ import re +#class name: +#description: +#methods: +#note: +#date: 2022/8/10 +#contact: 1865997821 class IndexInfo: def __init__(self, schema, table, indexname, columns, indexdef): @@ -24,7 +30,12 @@ class IndexInfo: self.primary_key = False self.redundant_obj = [] - +#class name: +#description: +#methods: +#note: +#date: 2022/8/10 +#contact: 1865997821 class ExecuteFactory: def __init__(self, dbname, user, password, host, port, schema, multi_node, max_index_storage): self.dbname = dbname From 4c345b1d31eab61a5d3c87110373ce9319ac6151 Mon Sep 17 00:00:00 2001 From: Eukanj827 Date: Thu, 11 Aug 2022 17:32:33 +0800 Subject: [PATCH 3/8] Update gsql_execute.py --- .../tools/components/index_advisor/dao/gsql_execute.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/gausskernel/dbmind/tools/components/index_advisor/dao/gsql_execute.py b/src/gausskernel/dbmind/tools/components/index_advisor/dao/gsql_execute.py index 074a721ea..dd8aa6610 100644 --- a/src/gausskernel/dbmind/tools/components/index_advisor/dao/gsql_execute.py +++ b/src/gausskernel/dbmind/tools/components/index_advisor/dao/gsql_execute.py @@ -23,7 +23,12 @@ from .execute_factory import IndexInfo BASE_CMD = None - +#class name: +#description: +#methods: +#note: +#date: 2022/8/11 +#contact: 1865997821 class GSqlExecute(ExecuteFactory): def __init__(self, *args): super(GSqlExecute, self).__init__(*args) From eb4c1bd953222d213b094bec7ca568d77a2fac97 Mon Sep 17 00:00:00 2001 From: Eukanj827 Date: Mon, 15 Aug 2022 09:58:17 +0800 Subject: [PATCH 4/8] Update driver_execute.py --- .../components/index_advisor/dao/driver_execute.py | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/gausskernel/dbmind/tools/components/index_advisor/dao/driver_execute.py b/src/gausskernel/dbmind/tools/components/index_advisor/dao/driver_execute.py index 38951401e..7f69748a6 100644 --- a/src/gausskernel/dbmind/tools/components/index_advisor/dao/driver_execute.py +++ b/src/gausskernel/dbmind/tools/components/index_advisor/dao/driver_execute.py @@ -18,19 +18,19 @@ import psycopg2 from .execute_factory import ExecuteFactory from .execute_factory import IndexInfo -#class name: -#description: -#methods: -#note: +#class name: DriverExecute (Inherits from the parent class ExecuteFactory) +#description: The SQL statement performs the operations associated with the call #date: 2022/8/10 #contact: 1865997821 class DriverExecute(ExecuteFactory): def __init__(self, *arg): + #Call the arguments of the parent class __init__ method super(DriverExecute, self).__init__(*arg) self.conn = None self.cur = None + #Connecting to the database def init_conn_handle(self): self.conn = psycopg2.connect(dbname=self.dbname, user=self.user, @@ -39,6 +39,7 @@ class DriverExecute(ExecuteFactory): port=self.port) self.cur = self.conn.cursor() +#If an error occurs after the SQL statement is executed, the error information is reported to the user def execute(self, sql): try: self.cur.execute(sql) @@ -47,11 +48,13 @@ class DriverExecute(ExecuteFactory): except Exception: self.conn.commit() +#Disconnecting from the database def close_conn(self): if self.conn and self.cur: self.cur.close() self.conn.close() +#Check whether multiple nodes exist def is_multi_node(self): self.init_conn_handle() try: From 91a7306a8857cc832e944c1bf060a3c85759717c Mon Sep 17 00:00:00 2001 From: Eukanj827 Date: Mon, 15 Aug 2022 10:08:06 +0800 Subject: [PATCH 5/8] Update execute_factory.py --- .../index_advisor/dao/execute_factory.py | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/src/gausskernel/dbmind/tools/components/index_advisor/dao/execute_factory.py b/src/gausskernel/dbmind/tools/components/index_advisor/dao/execute_factory.py index 88ffade7f..287ef86e6 100644 --- a/src/gausskernel/dbmind/tools/components/index_advisor/dao/execute_factory.py +++ b/src/gausskernel/dbmind/tools/components/index_advisor/dao/execute_factory.py @@ -13,10 +13,9 @@ import re -#class name: -#description: -#methods: -#note: +#class name: IndexInfo +#description: Define information about table indexes +#methods: __init__ #date: 2022/8/10 #contact: 1865997821 @@ -30,7 +29,7 @@ class IndexInfo: self.primary_key = False self.redundant_obj = [] -#class name: +#class name: ExecuteFactory #description: #methods: #note: @@ -47,11 +46,11 @@ class ExecuteFactory: self.max_index_storage = max_index_storage self.multi_node = multi_node +# Record redundant indexes @staticmethod def record_redundant_indexes(cur_table_indexes, redundant_indexes): cur_table_indexes = sorted(cur_table_indexes, key=lambda index_obj: len(index_obj.columns.split(','))) - # record redundant indexes for pos, index in enumerate(cur_table_indexes[:-1]): is_redundant = False for candidate_index in cur_table_indexes[pos + 1:]: @@ -63,6 +62,7 @@ class ExecuteFactory: if is_redundant: redundant_indexes.append(index) +#Match the name of the table against the index of the query @staticmethod def match_table_name(table_name, query_index_dict): for elem in query_index_dict.keys(): @@ -77,6 +77,7 @@ class ExecuteFactory: return False, table_name return True, table_name +#Retrieves a valid index based on the regular expression, adding the corresponding index and empty element if none exists @staticmethod def get_valid_indexes(record, hypoid_table_column, valid_indexes): tokens = record.split(' ') @@ -99,6 +100,7 @@ class ExecuteFactory: if columns not in valid_indexes[table_name]: valid_indexes[table_name].append((columns, index_type)) +#Record invalid SQL statements and returns the corresponding help information that matches the corresponding SQL statement @staticmethod def record_ineffective_negative_sql(candidate_index, obj, ind): cur_table = candidate_index.table @@ -136,6 +138,7 @@ class ExecuteFactory: candidate_index.ineffective_pos.append(ind) candidate_index.total_sql_num += obj.frequency +#Returns the last input and the corresponding result @staticmethod def match_last_result(table_name, index_column, history_indexes, history_invalid_indexes): for column in history_indexes.get(table_name, dict()): @@ -153,6 +156,7 @@ class ExecuteFactory: if not history_indexes[table_name]: del history_indexes[table_name] +#Correcting SQL statements @staticmethod def make_single_advisor_sql(ori_sql): sql = 'select gs_index_advise(\'' From d9cde8c89b9a5464c89c282932fbf26d4cacbe36 Mon Sep 17 00:00:00 2001 From: Eukanj827 Date: Mon, 15 Aug 2022 10:08:36 +0800 Subject: [PATCH 6/8] Update execute_factory.py --- .../tools/components/index_advisor/dao/execute_factory.py | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/gausskernel/dbmind/tools/components/index_advisor/dao/execute_factory.py b/src/gausskernel/dbmind/tools/components/index_advisor/dao/execute_factory.py index 287ef86e6..cf92f09b2 100644 --- a/src/gausskernel/dbmind/tools/components/index_advisor/dao/execute_factory.py +++ b/src/gausskernel/dbmind/tools/components/index_advisor/dao/execute_factory.py @@ -30,9 +30,6 @@ class IndexInfo: self.redundant_obj = [] #class name: ExecuteFactory -#description: -#methods: -#note: #date: 2022/8/10 #contact: 1865997821 class ExecuteFactory: From 43c3001415f75171a7f942db8e6b282cc0a0c84c Mon Sep 17 00:00:00 2001 From: Eukanj827 Date: Mon, 15 Aug 2022 10:21:41 +0800 Subject: [PATCH 7/8] Update gsql_execute.py --- .../components/index_advisor/dao/gsql_execute.py | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/src/gausskernel/dbmind/tools/components/index_advisor/dao/gsql_execute.py b/src/gausskernel/dbmind/tools/components/index_advisor/dao/gsql_execute.py index dd8aa6610..7d44f8914 100644 --- a/src/gausskernel/dbmind/tools/components/index_advisor/dao/gsql_execute.py +++ b/src/gausskernel/dbmind/tools/components/index_advisor/dao/gsql_execute.py @@ -23,10 +23,8 @@ from .execute_factory import IndexInfo BASE_CMD = None -#class name: -#description: -#methods: -#note: +#class name: GSqlExecute +#description: Solve the optimization problem of GSQL statement execution #date: 2022/8/11 #contact: 1865997821 class GSqlExecute(ExecuteFactory): @@ -34,6 +32,7 @@ class GSqlExecute(ExecuteFactory): super(GSqlExecute, self).__init__(*args) def init_conn_handle(self): + #define a global variable BASE_CMD,it is a connection command statement global BASE_CMD BASE_CMD = 'gsql -p ' + str(self.port) + ' -d ' + self.dbname if self.host: @@ -43,6 +42,7 @@ class GSqlExecute(ExecuteFactory): if self.password: BASE_CMD += ' -W ' + self.password +#Run the shell command in BASE_CMD def run_shell_cmd(self, target_sql_list): cmd = BASE_CMD + ' -c \"' if self.schema: @@ -52,6 +52,7 @@ class GSqlExecute(ExecuteFactory): cmd += '\"' proc = subprocess.Popen( cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True) + #Read data from stdout and stderr,If an error message is displayed, an error message is displayed (stdout, stderr) = proc.communicate() stdout, stderr = stdout.decode(), stderr.decode() if 'gsql: FATAL:' in stderr or 'failed to connect' in stderr: @@ -79,6 +80,7 @@ class GSqlExecute(ExecuteFactory): print(e.output.decode(), file=sys.stderr) return int(ret.decode().strip().split()[2]) > 0 +#Parse the recommended result returned @staticmethod def parse_single_advisor_result(res, table_index_dict): if len(res) > 2 and res[0:2] == ' (': @@ -182,6 +184,7 @@ class GSqlExecute(ExecuteFactory): return cost_total +# def parse_explain_plan(self, workload, index_config, res, ori_indexes_name, select_sql_pos): i = 0 hypo_index_num = 0 @@ -227,6 +230,7 @@ class GSqlExecute(ExecuteFactory): i += 1 return total_cost +#Production workflows consume report files def estimate_workload_cost_file(self, workload, index_config=None, ori_indexes_name=None): sql_file = str(time.time()) + '.sql' is_computed = False @@ -269,6 +273,7 @@ class GSqlExecute(ExecuteFactory): return total_cost +#Check for empty indexes and note them to optimize the table structure def check_useless_index(self, history_indexes, history_invalid_indexes): schemas = [elem.lower() for elem in filter(None, self.schema.split(','))] From 313e3c4e002ecae335e699b1c70a51b9872b97cb Mon Sep 17 00:00:00 2001 From: Eukanj827 Date: Mon, 15 Aug 2022 10:26:07 +0800 Subject: [PATCH 8/8] Update gsql_execute.py --- .../dbmind/tools/components/index_advisor/dao/gsql_execute.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/gausskernel/dbmind/tools/components/index_advisor/dao/gsql_execute.py b/src/gausskernel/dbmind/tools/components/index_advisor/dao/gsql_execute.py index 7d44f8914..852a0d503 100644 --- a/src/gausskernel/dbmind/tools/components/index_advisor/dao/gsql_execute.py +++ b/src/gausskernel/dbmind/tools/components/index_advisor/dao/gsql_execute.py @@ -184,13 +184,13 @@ class GSqlExecute(ExecuteFactory): return cost_total -# def parse_explain_plan(self, workload, index_config, res, ori_indexes_name, select_sql_pos): i = 0 hypo_index_num = 0 total_cost = 0 found_plan = False hypo_index = False + # create hypo-indexes for line in res: if 'QUERY PLAN' in line: found_plan = True