Artemij
Artemij

Reputation: 159

Efficiently sorting data from DB using Java?

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)

Upvotes: 0

Views: 1377

Answers (2)

Adam Paynter
Adam Paynter

Reputation: 46908

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

Upvotes: 3

jsight
jsight

Reputation: 28419

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

Upvotes: 1

Related Questions