Efficient way to combine results of two database queries.
        Posted  
        
            by ensnare
        on Stack Overflow
        
        See other posts from Stack Overflow
        
            or by ensnare
        
        
        
        Published on 2010-05-11T04:46:50Z
        Indexed on 
            2010/05/11
            4:54 UTC
        
        
        Read the original article
        Hit count: 259
        
I have two tables on different servers, and I'd like some help finding an efficient way to combine and match the datasets. Here's an example:
From server 1, which holds our stories, I perform a query like:
query = """SELECT author_id, title, text
           FROM stories
           ORDER BY timestamp_created DESC
           LIMIT 10
           """
results = DB.getAll(query)
for i in range(len(results)):
    #Build a string of author_ids, e.g. '1314,4134,2624,2342'
But, I'd like to fetch some info about each author_id from server 2:
query = """SELECT id, avatar_url
           FROM members
           WHERE id IN (%s)
           """
values = (uid_list)
results = DB.getAll(query, values)
Now I need some way to combine these two queries so I have a dict that has the story as well as avatar_url and member_id.
If this data were on one server, it would be a simple join that would look like:
SELECT *
FROM members, stories
WHERE members.id = stories.author_id
But since we store the data on multiple servers, this is not possible.
What is the most efficient way to do this?
Thanks.
© Stack Overflow or respective owner