Created
August 28, 2018 16:41
-
-
Save hobbsh/fba439e36b4e2b6881823306c2731c30 to your computer and use it in GitHub Desktop.
Analyze postgres tables in parallel
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import multiprocessing | |
import psycopg2 | |
import time | |
""" | |
Run analyze on multiple tables in parallel | |
Author: Wylie Hobbs - 2018 | |
""" | |
TABLES = [ | |
'some_schema.some_table' | |
] | |
USER = "dbuser" | |
HOST = "dbhost" | |
DBPW = "dbpw" | |
DBNAME = "dbname" | |
def run_query(query): | |
start = int(time.time()) | |
connect_string = "dbname='%s' user='%s' host=%s port=%s password='%s'" % (DBNAME, USER, HOST, 5432, DBPW) | |
con = psycopg2.connect(connect_string) | |
cur = con.cursor() | |
cur.execute(query) | |
end = time.time() - start | |
print(f'Finished {query} in {end} seconds - {con.notices}') | |
con.commit() | |
con.close() | |
def main(): | |
queries = [] | |
pool = multiprocessing.Pool(6) | |
for table in TABLES: | |
query = "ANALYZE VERBOSE %s" % (table) | |
pool.apply_async(run_query, args=(query,)) | |
pool.close() | |
pool.join() | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment