This commit is contained in:
Minh VU
2024-12-11 13:08:46 +01:00
parent 6b20a03bb2
commit 676057fde8
2 changed files with 9 additions and 27 deletions

View File

@@ -66,14 +66,16 @@ def create_tables_postgres():
try: try:
with connect_postgres() as conn: with connect_postgres() as conn:
with conn.cursor() as cursor: with conn.cursor() as cursor:
start_time = time.time()
for command in sql_command_create_table: for command in sql_command_create_table:
cursor.execute(command) cursor.execute(command)
conn.commit() conn.commit()
end_time = time.time()
cursor.close() cursor.close()
conn.close() conn.close()
except (psycopg2.DatabaseError, Exception) as error: except (psycopg2.DatabaseError, Exception) as error:
print(error) print(error)
return end_time-start_time
def create_data_postgres(): def create_data_postgres():
sql_command_copy =( sql_command_copy =(
@@ -83,14 +85,16 @@ def create_data_postgres():
try: try:
with connect_postgres() as conn: with connect_postgres() as conn:
with conn.cursor() as cursor: with conn.cursor() as cursor:
start_time = time.time()
for command in sql_command_copy: for command in sql_command_copy:
cursor.execute(command) cursor.execute(command)
conn.commit() conn.commit()
end_time = time.time()
cursor.close() cursor.close()
conn.close() conn.close()
except (psycopg2.DatabaseError, Exception) as error: except (psycopg2.DatabaseError, Exception) as error:
print(error) print(error)
return end_time - start_time
def requete_postgres(str): def requete_postgres(str):
try: try:
@@ -99,12 +103,12 @@ def requete_postgres(str):
start = time.time() start = time.time()
cursor.execute(str) cursor.execute(str)
conn.commit() conn.commit()
temps = time.time() -start end = time.time() -start
print("Cette requête prends:", temps, "seconde")
cursor.close() cursor.close()
conn.close() conn.close()
except (psycopg2.DatabaseError, Exception) as error: except (psycopg2.DatabaseError, Exception) as error:
print(error) print(error)
return end-start
def reset_database_postgres(): def reset_database_postgres():
try: try:

24
main.py
View File

@@ -2,8 +2,6 @@ from MonetDB import *
from PostgreSQL import * from PostgreSQL import *
import time import time
data =[]
header=["Nom de requete","Temps"]
#Monet DB #Monet DB
requeteSelect1MonetDB = """SELECT * FROM flights WHERE "AIRLINE" = 'AA';""" requeteSelect1MonetDB = """SELECT * FROM flights WHERE "AIRLINE" = 'AA';"""
requeteSelect2MonetDB = """SELECT * FROM flights;""" requeteSelect2MonetDB = """SELECT * FROM flights;"""
@@ -27,17 +25,6 @@ requeteDeletePostgreSQL = """DELETE FROM flights WHERE "DAY" > 10"""
requeteInsertPostgreSQL = """INSERT INTO flights SELECT * FROM flights10""" requeteInsertPostgreSQL = """INSERT INTO flights SELECT * FROM flights10"""
requeteDropflight10MonetSQL = """DROP TABLE flights10""" requeteDropflight10MonetSQL = """DROP TABLE flights10"""
def display_table(data, headers):
column_lengths = [len(header) for header in headers]
for row in data:
for idx, cell in enumerate(row):
column_lengths[idx] = max(column_lengths[idx], len(str(cell)))
row_format = " | ".join(["{{:<{}}}".format(length) for length in column_lengths])
print(row_format.format(*headers))
print("-|-".join(['-' * length for length in column_lengths]))
for row in data:
print(row_format.format(*[str(cell) for cell in row])) # Ensure cells are strings
def initMonetDB(): def initMonetDB():
reset_data_monetdb() reset_data_monetdb()
time = create_table_monetdb() time = create_table_monetdb()
@@ -69,8 +56,6 @@ def execForMonetDB(n, tabReq):
t[j] = t[j]/n t[j] = t[j]/n
print("[MonetDB] requete (moyenne de",n,") :", tabReq[j] , "\n time = ", t[j], "s\n") print("[MonetDB] requete (moyenne de",n,") :", tabReq[j] , "\n time = ", t[j], "s\n")
def initPostgreSQL(): def initPostgreSQL():
reset_database_postgres() reset_database_postgres()
start_time = time.time() start_time = time.time()
@@ -107,18 +92,11 @@ def execForPostgreSQL(n, tabReq):
t[j] = t[j] / n t[j] = t[j] / n
print("[PostgreSQL] requete (moyenne de", n, ") :", tabReq[j], "\n time = ", t[j], "s\n") print("[PostgreSQL] requete (moyenne de", n, ") :", tabReq[j], "\n time = ", t[j], "s\n")
def runPostgreSQL():
data.append([requeteSelect1PostgreSQL,execPostgreSQL(requeteSelect1PostgreSQL)])
data.append([requeteSelect2PostgreSQL,execPostgreSQL(requeteSelect2PostgreSQL)])
data.append([requeteSelect3PostgreSQL,execPostgreSQL(requeteSelect3PostgreSQL)])
data.append([requeteSelect4PostgreSQL,execPostgreSQL(requeteSelect4PostgreSQL)])
if __name__ == '__main__': if __name__ == '__main__':
initMonetDB() initMonetDB()
runMonetDB() runMonetDB()
#initPostgreSQL() #initPostgreSQL()
runPostgreSQL() #runPostgreSQL()
display_table(data, header)