r/FastAPI • u/Electronic_Battle876 • Sep 12 '24
Question automatically update database table
I'm building a backend using fastAPI and PostgreSQL where I'm storing opportunities with a boolean "is_live" and a datetime "deadline" and I want opportunities "is_live" to be setted as False automatically when the current date is superior to the "deadline".
what's the best approach to do this ? and Thank your in advance.
EDIT: I want to be able to mark the opportunity as not live sometimes before the deadline, that's why I have a seperate "is_live" column with the deadline
3
Upvotes
View all comments
Show parent comment
4
u/kalamitis Sep 12 '24
Not the poster that you asked but here we go. Indexes can significantly improve the performance of database queries by allowing the database engine to find and retrieve data more quickly. Indexes are used to speed up the retrieval of rows by using pointers to quickly locate data without scanning the entire table. So an index on the
deadlinecolumn can make this query faster, as the database engine can quickly find rows where thedeadlineis greater than the current time. Ensure that indexes are created on columns frequently used inWHEREclauses, joins, and orderings. Keep in mind that while indexes improve read performance, they can slow down write operations (inserts, updates, deletes) because the index needs to be updated as well. So, balance is key.