Performance: Why ORM simplifies database access
This concept affects the speed of database queries and the efficiency of data handling in the app, impacting how fast pages load and respond.
user = User.query.get(user_id)
# Using Flask-SQLAlchemy ORM to fetch user by idcursor.execute('SELECT * FROM users WHERE id = %s', (user_id,)) user = cursor.fetchone() # Manual SQL queries everywhere
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Manual SQL queries with repeated calls | N/A | N/A | N/A | [X] Bad |
| ORM with optimized queries and caching | N/A | N/A | N/A | [OK] Good |
user = User(name='Alice') db.session.add(user) db.session.commit() print(user.id)
print(user.id) output after commit?user = User(name='Bob') db.session.add(user) print(user.id)
user = User.query.filter_by(id=5).first() user.email = 'new@example.com' db.session.commit()
Pass the test and this pattern moves unknown