I\'m using SQLAlchemy with a MySQL database and I\'d like to count the rows in a table (roughly 300k). The SQLAlchemy count function takes about 50 times as long to run as w
It took me a long time to find this as the solution to my problem. I was getting the following error:
sqlalchemy.exc.DatabaseError: (mysql.connector.errors.DatabaseError) 126 (HY000): Incorrect key file for table '/tmp/#sql_40ab_0.MYI'; try to repair it
The problem was resolved when I changed this:
query = session.query(rumorClass).filter(rumorClass.exchangeDataState == state)
return query.count()
to this:
query = session.query(func.count(rumorClass.id)).filter(rumorClass.exchangeDataState == state)
return query.scalar()