Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/347.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 为什么Django管理员搜索字段需要花费太多时间来响应?_Python_Django_Django Models_Django Rest Framework - Fatal编程技术网

Python 为什么Django管理员搜索字段需要花费太多时间来响应?

Python 为什么Django管理员搜索字段需要花费太多时间来响应?,python,django,django-models,django-rest-framework,Python,Django,Django Models,Django Rest Framework,这是我第一次用大数据处理项目。我有1600万的数据。当我在django shell中查询时,它会在几秒钟内给出结果。但在django管理网站上,当我注册我的模型并应用搜索字段时。查询时间太长了。我尝试了django调试工具栏,下面是它显示给我的结果 models.py: class TrademarkAvailability(models.Model): serial_number = models.IntegerField(unique=True) filing_da

这是我第一次用大数据处理项目。我有1600万的数据。当我在django shell中查询时,它会在几秒钟内给出结果。但在django管理网站上,当我注册我的模型并应用搜索字段时。查询时间太长了。我尝试了django调试工具栏,下面是它显示给我的结果

  • models.py:

     class TrademarkAvailability(models.Model):
         serial_number = models.IntegerField(unique=True)
         filing_date = models.IntegerField(null=True)
         status_code = models.IntegerField(null=True)
         status_date = models.IntegerField(null=True)
         mark_identification = models.CharField(
             max_length=5000, null=True, blank=True, db_index=True)
         mark_drawing_code = models.CharField(
             max_length=5000, null=True, blank=True)
         attorney_name = models.CharField(max_length=5000, null=True, blank=True)
         current_location = models.CharField(max_length=5000, null=True, blank=True)
         employee_name = models.CharField(max_length=5000, null=True, blank=True)
         correspondent = models.JSONField()
         classifications = models.JSONField(null=True)
         case_file_owners = models.JSONField(null=True)
         transaction_date = models.JSONField(null=True)
         registration_number = models.JSONField(null=True)
         case_file_statements = models.JSONField(null=True)
         case_file_event_statements = models.JSONField(null=True)
    
  • 管理员

     @admin.register(TrademarkAvailability)
     class TrademarkAdmin(admin.ModelAdmin):
         list_display = ("mark_identification",)
         search_fields = ["=mark_identification"]
         paginator = LargeTablePaginator
         show_full_result_count = False
    

可能会有一些问题,但一个好的起点是使用
上限的索引,因此索引不区分大小写

如果您运行的是Django<3.2,那么就可以在
mark_identification
上创建不区分大小写的索引

CREATE INDEX mark_identification_insensitive_idx ON app_trademarkavailability (upper(mark_identification));
如果Django>=3.2

 class TrademarkAvailability(models.Model):
     serial_number = models.IntegerField(unique=True)
     filing_date = models.IntegerField(null=True)
     status_code = models.IntegerField(null=True)
     status_date = models.IntegerField(null=True)
     mark_identification = models.CharField(
         max_length=5000, null=True, blank=True, db_index=True)
     mark_drawing_code = models.CharField(
         max_length=5000, null=True, blank=True)
     attorney_name = models.CharField(max_length=5000, null=True, blank=True)
     current_location = models.CharField(max_length=5000, null=True, blank=True)
     employee_name = models.CharField(max_length=5000, null=True, blank=True)
     correspondent = models.JSONField()
     classifications = models.JSONField(null=True)
     case_file_owners = models.JSONField(null=True)
     transaction_date = models.JSONField(null=True)
     registration_number = models.JSONField(null=True)
     case_file_statements = models.JSONField(null=True)
     case_file_event_statements = models.JSONField(null=True)

    class Meta:
        indexes = [
            Index(
                Upper('mark_identification'),
                name='mark_identification_insensitive_idx',
            ),
        ]

请记住,您始终可以使用SQL解释

编辑:


在shell中,您是否使用
iexact
TrademarkAvailability.objects.filter(mark\u identification\uu iexact='Something')
)进行查询?因为管理员站点使用
iexact
查找
=
前缀。在这种情况下,您可能需要在
mark_identification
字段上建立一个函数索引,以加快速度。使用_iexact需要10到15秒您使用什么数据库(及其版本),Django的哪个版本?Django 3.1.7和postgres 12我应该在哪里运行该查询?@TayyabKhan run
python manage.py dbshell
并在那里运行该查询,或者直接在数据库中运行该查询。您可能还需要从上载的映像中看到的降序索引。由于这是数据库状态更改,请在迁移中运行它。示例添加了编辑。非常感谢。
class Migration(migrations.Migration):

    dependencies = [
        ('app', 'previous_mig'),
    ]

    operations = [
        migrations.RunSQL(
            sql=r'CREATE INDEX "mark_identification_insensitive_idx" ON "app_trademarkavailability" (upper("mark_identification"));',
            reverse_sql=r'DROP INDEX "mark_identification_insensitive_idx";'
        ),
    ]