0

I am using the code below to extract table names on a database at a GET call in a Flask app.:

session = db.session()
qry = session.query(models.BaseTableModel) 
results = session.execute(qry)
table_names = []
for row in results:
    for column, value in row.items():
        #this seems like a bit of a hack
        if column == "tables_table_name":
            table_names.append(value)
print('{0}: '.format(table_names))

Given that tables in the database may added/deleted regularly, is the code above an efficient and reliable way to get the names of tables in a database?

7
  • Use row["tables_table_name"] instead of second loop? Commented Dec 31, 2016 at 12:03
  • Generally when trying to optimize something you should follow this flow: 1. measure performance, 2. if not satisfied with performance go to (3) else exit. 3. find and apply optimizations and go to (1). Doing (3) without doing (1) and (2) is bad, since you will very likely end up with antipattern called premature optimization. Commented Dec 31, 2016 at 12:08
  • @YaroslavAdmin Thanks, that will save a few lines of code and probably a bit of execution time. I was wondering if the sqlalchemy methodology used is best practice for my goal? Commented Dec 31, 2016 at 12:10
  • 1
    BaseTableModel is not a SQLAlchemy thing, right? It's just a table you created? If so, doing plain select is probably the fastest approach you can get. Commented Dec 31, 2016 at 12:15
  • @YaroslavAdmin BaseTableModel inherits from the Model object Flask Appbuilder which in turn inherits from SqlAlchemy but I am not clear on exactly what is going on with FAB behind the scenes. Commented Dec 31, 2016 at 12:52

1 Answer 1

1

One obvious optimization is to use row["tables_table_name"] instead of second loop.

Assuming that BaseTableModel is a table, which contains names of all other tables, than you're using the fastest approach to get this data.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.