MySQL Python taking too long to query large database
===

I have a database with over 30,000 tables and ~40-100 rows in each table. I want to retrieve a list of table names which contain a string under a specific column.

So for example:

I want to retrieve the names of all tables which contain 'foo'...

    Database
        Table_1
            ID: 1, STR: bar
            ID: 2, STR: foo
            ID: 3, STR: bar
        Table_2
            ID: 1, STR: bar
            ID: 2, STR: bar
            ID: 3, STR: bar
        Table_3
            ID: 1, STR: bar
            ID: 2, STR: bar
            ID: 3, STR: foo

So in this case the function should return ['Table_1', 'Table_3']

So far I have this, it works fine but takes over 2 minutes to execute, which is way too long for the application I have in mind.

    self.m('SHOW TABLES')
    result = self.db.store_result()
    tablelist = result.fetch_row(0, 1)
    for table in tablelist:
        table_name = table['Tables_in_definitions']
        self.m("""SELECT `def` FROM `""" + table_name + """` WHERE `def` = '""" + str + """'""")
        result = self.db.store_result()
        r = result.fetch_row(1, 1)
        if len(r) > 0:
            results.append(table_name)

I'm not smart enough to come up with a way to speed this up so if anyone has any suggestions it would be greatly appreciated, thanks!