Skip to content

Instantly share code, notes, and snippets.

@hobbsh
Created August 28, 2018 16:41
Show Gist options
  • Save hobbsh/fba439e36b4e2b6881823306c2731c30 to your computer and use it in GitHub Desktop.
Save hobbsh/fba439e36b4e2b6881823306c2731c30 to your computer and use it in GitHub Desktop.
Analyze postgres tables in parallel
#!/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