WalzoneInterview Prep
📞 Interviewing soon? Practice with a realistic AI mock phone interview — it calls you, then scores you. First 15 min FREE →

Python · Expert · question 75 of 100

What are some advanced techniques for working with databases in Python, including database sharding, connection pooling, and optimizing query performance?

📕 Buy this interview preparation book: 100 Python questions & answers — PDF + EPUB for $5

Python has several database libraries for interacting with various databases, including PostgreSQL, MySQL, SQLite, and Oracle. When working with databases, it is important to consider performance, scalability, and security. In this answer, we will discuss some advanced techniques for working with databases in Python.

Database sharding

Database sharding is a technique used to horizontally partition data across multiple servers or nodes. The purpose of sharding is to distribute the data to multiple servers, so that the data is more easily managed and queryable, and can handle more users and requests. Each node contains a portion of the data, and queries are sent to all nodes and the results are combined. In Python, the SQLAlchemy library supports database sharding for relational databases.

Here is an example of how to shard a PostgreSQL database using SQLAlchemy:

    from sqlalchemy import create_engine, Table, Column, Integer, String, MetaData
    from sqlalchemy.pool import QueuePool
    from sqlalchemy.ext.horizontal_shard import ShardedSession
    
    metadata = MetaData()
    user = Table('user', metadata,
        Column('id', Integer, primary_key=True),
        Column('name', String),
    )
    
    # create multiple database engines, one for each shard
    engines = {}
    for i in range(4):
    engines[i] = create_engine('postgresql://user:password@localhost/db%d' % i,
        poolclass=QueuePool, max_overflow=10, pool_size=5)
    
    # create a sharded session
    sharded_session = ShardedSession(create_engine('postgresql://user:password@localhost',
        poolclass=QueuePool, max_overflow=10, pool_size=5),
        engines, lambda shard_id: shard_id % 4)
    
    # create a new user on shard 2
    with sharded_session() as session:
        session.execute(user.insert().values(name='Alice'))

Connection pooling

Connection pooling is a technique used to reuse database connections instead of creating new connections for each request. Connection pooling can improve performance and reduce the overhead of creating and tearing down database connections. In Python, the psycopg2 and SQLAlchemy libraries support connection pooling for PostgreSQL.

Here is an example of how to use connection pooling with SQLAlchemy:

    from sqlalchemy import create_engine, Table, Column, Integer, String, MetaData
    from sqlalchemy.pool import QueuePool
    
    metadata = MetaData()
        user = Table('user', metadata,
        Column('id', Integer, primary_key=True),
        Column('name', String),
    )
    
    # create a connection pool
    pool = QueuePool(create_engine('postgresql://user:password@localhost'),
        max_overflow=10, pool_size=5)
    
    # create a new user
    with pool.connect() as conn:
        conn.execute(user.insert().values(name='Alice'))

Optimizing query performance

Query performance can be improved by creating indexes on frequently used columns, using efficient query patterns, and reducing the number of queries. In Python, the SQLAlchemy library provides a query interface that supports complex queries and can be used to optimize query performance.

Here is an example of how to optimize query performance using SQLAlchemy:

    from sqlalchemy import create_engine, Table, Column, Integer, String, MetaData
    from sqlalchemy.orm import sessionmaker
    
    metadata = MetaData()
    user = Table('user', metadata,
        Column('id', Integer, primary_key=True),
        Column('name', String),
        Column('age', Integer),
    )
    
    engine = create_engine('postgresql://user:password@localhost')
    Session = sessionmaker(bind=engine)
    
    # create an index on the age column
    engine.execute('CREATE INDEX age_index ON user (age)')
    
    # query for users over the age of 30
    with Session() as session:
        users = session.query(user).filter(user.age > )
Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic Python interview — then scores it.
📞 Practice Python — free 15 min
📕 Buy this interview preparation book: 100 Python questions & answers — PDF + EPUB for $5

All 100 Python questions · All topics