views:

178

answers:

2

What we have:

3 MySQL DB tables: user, text, friend

user: username, password, email, etc.

text: username, text, date, etc.

friend: username, friend_username, etc.

Task:

Write an algorithm (in Java) to show 10 latest texts from your friends.

Ultimate target is to have running time within O(n log n).

DB tables can be modified (added new ones) as required.

Data volume: 200 000 users, ~50 text per user.

I would appreciate any ideas, examples, points, etc. Thank you!

(Not a homework. Pure problem, looking for performance improvement)

+3  A: 

Are you sure this has to be done in Java? Is this not an SQL query?

SELECT text
  FROM TEXT
 WHERE username IN
         (
           SELECT friend_username FROM FRIEND WHERE username = 'YOUR_USERNAME'
         )
 ORDER BY date DESC
 LIMIT 10
Adam Paynter
Ack! It's been labeled as homework! What have I done?!
Adam Paynter
If the assignment (and what else could it be?) was to do it in Java, then you didn't do the homework. ;)
Michael Myers
IN can be very slow with large volumes of data as far as I know?
Artemij
@Artemij: It depends on which database management software you are using. Which software are you using? Does it offer a tool to explain queries? If so, I would recommend having it explain the query to you. This should help clarify any performance bottlenecks.
Adam Paynter
@Artemij: Sorry, I didn't realize you had edited the question to specify MySQL as your DBMS. If I remember correctly, the MySQL Query Browser supports the "Explain" functionality.
Adam Paynter
@Adam: MySQL Server 5.1 + NetBeans 6.5
Artemij
@Artemij: Would you be able to have the MySQL Query Browser explain the query to you and post the results?
Adam Paynter
+1  A: 

You didn't say what database, so I'll just make an assumption (Postgres syntax below):

select t.* from text t
    inner join friend f ON f.friend_username = t.username
    where f.username = 'myusername'
    order by t.date desc
    LIMIT 10
jsight
It is a homework, he must do it in Java. :)
fbinder
I'm leaving a pure Java version up to the questioner as a new homework assignment. :)
jsight
@jsight: Thank you, that really helped!
Artemij