Skip to content

Instantly share code, notes, and snippets.

@camelcaseblog
Last active January 29, 2019 07:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save camelcaseblog/0dccdc985caaf6223371a6df88455057 to your computer and use it in GitHub Desktop.
Save camelcaseblog/0dccdc985caaf6223371a6df88455057 to your computer and use it in GitHub Desktop.
example of query with joins that affect performance
class Reporter(models.Model):
id = models.AutoField(primary_key=True, null=False)
name = models.TextField
class Article(models.Model):
id = models.AutoField(primary_key=True, null=False)
content = models.TextField(null=True)
reporter = models.ForeignKey(Reporter, on_delete=models.CASCADE)
ids = [a.reporter.id for a in Article.objects.filter(content__contains="camelCase")] # 1
ids = [a.reporter_id for a in Article.objects.filter(content__contains="camelCase")] # 2
query = '''
SELECT R.id
FROM article AS A
INNER JOIN reporter AS R
ON A.reporter_id = R.id
WHERE content like '%camelCase%'
'''
query = '''
SELECT A.reporter_id
FROM article AS A
WHERE content LIKE '%camelCase%'
'''
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment