Skip to content

Instantly share code, notes, and snippets.

@violet4
Created August 29, 2018 13:54
Show Gist options
  • Save violet4/818ce027bbe617336cd373163e48fd27 to your computer and use it in GitHub Desktop.
Save violet4/818ce027bbe617336cd373163e48fd27 to your computer and use it in GitHub Desktop.
detect_functional_dependencies.py
#!/usr/bin/env python
import os
import subprocess
from itertools import combinations
from mysql import connector
conn = connector.connect(
option_files=os.path.expanduser('~/.my.cnf'),
# note that this only works on linux-based OSes like ubuntu and mac. doesn't work on windows.
password=subprocess.check_output('grep password ~/.my.cnf | head -1 | sed s/.*=//', shell=True).decode().strip()[1:-1],
port='3306',
database='db1',
)
cursor = conn.cursor()
cursor.execute('describe companies')
colnames = [c[0] for c in cursor.fetchall()]
for c1, c2 in combinations(colnames, 2):
cursor.execute("""SELECT DISTINCT COUNT
FROM (
SELECT
{c1}, count(distinct {c2}) AS count
FROM FIRST1a
GROUP BY snum
) t1
""".format(c1=c1, c2=c2))
counts = cursor.fetchall()
if counts == [(1,)]:
print('there is probably FD between {} and {}'.format(c1, c2))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment