Last active
January 29, 2019 07:33
-
-
Save camelcaseblog/0dccdc985caaf6223371a6df88455057 to your computer and use it in GitHub Desktop.
example of query with joins that affect performance
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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