The query of Daniel Verite was really helpfull. Unfortunatelly after aprox 20sec query runtime I had timeout performance issues. Due I'm not allowed to adjust the config I wrote a looped script to workaround the problem.
This fitted my purposes best:
Solution as follows:
-> create a conf file to provide all paramters of course
-> usage of psycopg2 for database connects via postgres and python
-> numpy for handling I/O files
import psycopg2
from psycopg2 import Error
import numpy as np
from configparser import SafeConfigParser
def get_config_parameter(conf_file,section, parameter):
config_object = SafeConfigParser()
config_object.read(conf_file)
return str(config_object.get(section ,parameter))
dbpassword = get_config_parameter('db.conf', 'PROD ENV', 'dbusername')
dbusername = get_config_parameter('db.conf', 'PROD ENV', 'dbusername')
dbhost = get_config_parameter('db.conf', 'PROD ENV', 'dbhost')
dbname = get_config_parameter('db.conf', 'PROD ENV', 'dbname')
input_file_name = get_config_parameter('db.conf', 'PROD ENV', 'input_file_name')
output_result_file = get_config_parameter('db.conf', 'PROD ENV', 'output_result_file')
chunksize = 500
eculist = np.loadtxt(input_file_name, dtype="str")
ecu_chunks = ""
ecu_chunk_list = []
for ecu in eculist:
ecu_chunk_list.append(f"('{ecu}'), ")
def query(dbusername, dbpassword, dbhost, dbname, query_input):
try:
connection = psycopg2.connect(user=dbusername, password=dbpassword, host=dbhost, database=dbname)
# create a cursor to perform database operations
cursor = connection.cursor()
print("CONNECTED:", connection.get_dsn_parameters(), ": \n")
try:
query = f"WITH list(pattern) as (values {query_input}) SELECT pattern, EXISTS (select 1 FROM mule.transactions WHERE transaction_id LIKE '%'||pattern||'%') FROM list;"
print('query in progress... please wait')
cursor.execute(query)
result = str(cursor.fetchall())
return result
except (Exception, Error) as error:
print("SQL: Error while executing query:", error)
except (Exception, Error) as error:
print("NOT CONNECTED: Error while connection to Database:", error)
finally:
if connection:
cursor.close()
connection.close()
print("Database connection closed!")
result = ""
out_text = ""
for i in range(0,len(ecu_chunk_list),chunksize):
test = str(ecu_chunk_list[i:i+chunksize]).replace('", "','').replace('"','')
length = len(test)
test = test[1:length -3]
result = query(dbusername,dbpassword,dbhost,dbname, test)
out_text = out_text + result
save_file = open(output_result_file, 'w+')
save_file.write(out_text)
save_file.close()
Of course you can get rid of all the fail-safe but I think it's usefull.
SELECT trans_id FROM schema.table WHERE trans_id IN (<list of ids>
- if you're just checking for existence, I'm not sure you'd want to useLIKE
or any other fuzzy operator.