如何确定过滤器中字段顺序的优先级
How can I prioritize the order of the fields in the filter
当我得到对象时,
objs = Page.objects.get(slug="some-slug.html", web_id=1)
生成的 SQL 查询是
select * from cms_page WHERE ("cms_page"."web_id" = '1' AND "cms_page"."slug" = '''some-slug''')
我想将 SQL 更改为
select * from cms_page WHERE ("cms_page"."slug" = '''some-slug''' and "cms_page"."web_id" = '1' )
第一个slug字段,因为slug字段是一个索引(该字段存在索引)
class Page(models.Model):
title = models.CharField(max_length=128)
body = models.TextField(blank=True)
slug = models.CharField(max_length=200, db_index=True)
web = models.ForeignKey(Web, editable=False)
def __unicode__(self):
return self.title
def save(self, *args, **kwargs):
if not self.slug:
self.slug = "{0}{1}".format(slugify(self.title),".html")
super(Page,self).save(*args,**kwargs)
class Meta:
unique_together = ("web", "slug")
index_together = ["web", "slug"]
对于大多数数据库,此查询与
完全相同
select * from cms_page WHERE ("cms_page"."web_id" = '1' AND "cms_page"."slug" = '''some-slug''')
这个
select * from cms_page WHERE ("cms_page"."slug" = '''some-slug''' and "cms_page"."web_id" = '1' )
You might be tempted to rewrite your queries to make arithmetic
operations faster, while sacrificing readability. Because MySQL does
similar optimizations automatically, you can often avoid this work,
and leave the query in a more understandable and maintainable form.
Some of the optimizations performed by MySQL follow:
http://dev.mysql.com/doc/refman/5.7/en/where-optimizations.html
现在,mysql 不像 postgresql 那样热衷于使用索引,所以如果 mysql 这样做,postgresql 也会这样做!
如果索引可用,RDBMS 查询解析器将决定首先对索引覆盖的那些列执行比较。
当我得到对象时,
objs = Page.objects.get(slug="some-slug.html", web_id=1)
生成的 SQL 查询是
select * from cms_page WHERE ("cms_page"."web_id" = '1' AND "cms_page"."slug" = '''some-slug''')
我想将 SQL 更改为
select * from cms_page WHERE ("cms_page"."slug" = '''some-slug''' and "cms_page"."web_id" = '1' )
第一个slug字段,因为slug字段是一个索引(该字段存在索引)
class Page(models.Model):
title = models.CharField(max_length=128)
body = models.TextField(blank=True)
slug = models.CharField(max_length=200, db_index=True)
web = models.ForeignKey(Web, editable=False)
def __unicode__(self):
return self.title
def save(self, *args, **kwargs):
if not self.slug:
self.slug = "{0}{1}".format(slugify(self.title),".html")
super(Page,self).save(*args,**kwargs)
class Meta:
unique_together = ("web", "slug")
index_together = ["web", "slug"]
对于大多数数据库,此查询与
完全相同select * from cms_page WHERE ("cms_page"."web_id" = '1' AND "cms_page"."slug" = '''some-slug''')
这个
select * from cms_page WHERE ("cms_page"."slug" = '''some-slug''' and "cms_page"."web_id" = '1' )
You might be tempted to rewrite your queries to make arithmetic operations faster, while sacrificing readability. Because MySQL does similar optimizations automatically, you can often avoid this work, and leave the query in a more understandable and maintainable form. Some of the optimizations performed by MySQL follow:
http://dev.mysql.com/doc/refman/5.7/en/where-optimizations.html
现在,mysql 不像 postgresql 那样热衷于使用索引,所以如果 mysql 这样做,postgresql 也会这样做!
如果索引可用,RDBMS 查询解析器将决定首先对索引覆盖的那些列执行比较。