I would probably do it in mysql, because I like having as much number crunching as possible done on the db side, and php pretty much outputting it, though it often makes the query more complex. If you're using really large amounts of data, mysql will probably do the calculations faster than bringing in a lot of data an looping through. Of course you can also do it largely in php. In this case there's not really a should.
An example query - compare followers since the last check
Code:
SELECT tw1.Twitter_handle, MAX(tw1.last_update) AS last_update,
(
SELECT tw2.followers
FROM twitter_followers tw2
WHERE
tw2.Twitter_handle=tw1.twitter_handle AND
tw2.last_update = MAX(tw1.last_update)
) AS last_followers,
(
SELECT MAX(tw3.last_update)
FROM twitter_followers tw3
WHERE
tw3.Twitter_handle=tw1.twitter_handle AND
tw3.last_update<MAX(tw1.last_update)
) AS prev_update,
(
SELECT tw4.followers
FROM twitter_followers tw4
WHERE
tw4.Twitter_handle=tw1.twitter_handle AND
tw4.last_update=(
SELECT MAX(tw3.last_update)
FROM twitter_followers tw3
WHERE
tw3.Twitter_handle=tw1.twitter_handle
AND tw3.last_update<MAX(tw1.last_update)
)
) AS prev_followers
FROM twitter_followers tw1 GROUP BY tw1.Twitter_handle
Output:
Code:
Twitter_handle last_update last_followers prev_update prev_followers
handle1 2012-02-20 1100 2012-02-15 900
handle2 2012-02-20 1300 2012-02-15 1125
This looks a bit messy, what with all the subqueries, and I'm sure there's a more elegant/efficient way of doing it keeping within the database layer.
Bookmarks