Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

leaderboard: delegate calculations and sorting to database #34

Open
auscompgeek opened this issue Oct 1, 2015 · 2 comments
Open

leaderboard: delegate calculations and sorting to database #34

auscompgeek opened this issue Oct 1, 2015 · 2 comments

Comments

@auscompgeek
Copy link
Contributor

Currently, the leaderboard won't work very well if we end up with a lot of users, since all the processing is done on the Python side rather than the database side.

Here's a couple of ways this could work:

SELECT user_id, SUM(num_correct) * 1.0 / SUM(num_answered) AS total_ratio
FROM scores
GROUP BY user_id
ORDER BY total_ratio DESC
LIMIT 10;
SELECT username, SUM(num_correct) * 1.0 / SUM(num_answered) AS total_ratio
FROM scores
NATURAL JOIN users
GROUP BY user_id
ORDER BY total_ratio DESC
LIMIT 10;

The first query would be easiest to get working with the database API. I'm not sure how we could get joins working nicely with Model._query().

NB: Right now, I have an uncommitted version of the database API where Model._query() takes a few more optional arguments: _group_by: str, _sort_by: str, limit: int.

@auscompgeek auscompgeek self-assigned this Oct 1, 2015
@auscompgeek auscompgeek changed the title leaderboard: delegate summation and sorting to database leaderboard: delegate calculations and sorting to database Oct 1, 2015
@auscompgeek
Copy link
Contributor Author

Maybe I'll add in a _join: str or something to Model._query() which only does NATURAL JOIN. Hmm.

@auscompgeek
Copy link
Contributor Author

Hmm. This is what the query currently looks like:

SELECT user_id, username, SUM(num_correct) AS total_correct, SUM(num_answered) AS total_answered, total_correct * 1.0 / total_answered AS total_ratio
FROM scores NATURAL JOIN users
GROUP BY user_id
ORDER BY total_ratio DESC
LIMIT 10;

Except, uhh, apparently I can't use custom columns I define in later custom columns?

So, one option I see to solve this is to not return a total_ratio column. I'm not sure whether this is the best idea though. There could be other solutions though, and I'd like to hear people's thoughts on this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant