Junior — Middle
What methods do you know for speeding up data retrieval from a database?
sobes.tech AI
Answer from AI
Several methods can be used in Python to speed up data retrieval from a database:
- Query optimization: use only necessary fields, avoid redundant JOINs, apply filters at the query level.
- Database indexes: ensure indexes are created on frequently used fields.
- Using ORM with lazy loading: to avoid loading unnecessary data.
- Caching: store frequently requested data in memory (e.g., using Redis).
- Batch loading: load data in chunks to prevent memory overload.
- Asynchronous queries: use libraries like
asynciofor parallel execution of queries.
Example using indexes and optimized query in SQLAlchemy:
from sqlalchemy import create_engine, select
from models import User
engine = create_engine('postgresql://user:pass@localhost/db')
with engine.connect() as conn:
stmt = select(User.id, User.name).where(User.active == True)
result = conn.execute(stmt)
users = result.fetchall()
Here, only the necessary fields are selected, and active users are filtered, which speeds up retrieval.