Part 1: MySQL
From a MySQL database how can I join the following two counts
SELECT COUNT(activity_id) as activities, user_id FROM Activity
WHERE activity_completed_time IS NOT NULL
AND group = somevalue
GROUP BY user_id;
SELECT COUNT(id) as comments, author_id FROM Comment
WHERE group = somevalue
GROUP BY author_id;
These queries return two separate tables but I should be able to join them on Activity.user_id = Comment.author_id.
The first query gives:
activities | user_id
The second query gives:
comments | author_id
What I want to return is:
user_id | activities | comments
I also need 0 values for activities and comments where they only appear in 1 table (NULL would work fine).
Part 2: Django
Is it possible to then do this in a single query in Django without using raw queries?
Would it be quicker to join 2 separate queries in Python with 2 Django querysets or SQL?
I have activity and comments for users stored in separate models:
class Activity(models.Model):
user_id = models.CharField(max_length=50, primary_key=True)
activity_id = models.CharField(max_length=5)
activity_completed_time = models.DateTimeField()
group = models.CharField(max_length=70, primary_key=True)
class Comment(models.Model):
id = models.IntegerField(primary_key=True)
author_id = models.CharField(max_length=50)
group = models.CharField(max_length=70, primary_key=True)
The two separate queries with conditions that return the necessary data are:
Activity.objects.values('user_id')
.filter(group='value')
.filter(activity_completed_time__isnull = false)
.annotate('total_completed' = Count('activity_id'))
Comment.objects.values('author_id')
.filter(group='value')
.annotate('total_comments' = Count('id'))
It is obviously possible to do this in several different ways, I am trying to find the quickest way to do it as I am processing over 5.5 million records in the Activity table and over 790k records in the Comment table.