小编JCJ*_*CJS的帖子

Django Sites Framework:初始数据迁移位置

在Django 1.7之前,当使用Django Sites Framework时,可以/应该使用Initial Fixtures定义初始数据.

的myproject /装置/ initial_data.json

[
{
    "pk": 1, 
    "model": "sites.site", 
    "fields": {
        "domain": "domain1", 
        "name": "name1"
    }
},
{
    "pk": 2, 
    "model": "sites.site", 
    "fields": {
        "domain": "domain2", 
        "name": "name2"
    }
},
{
    "pk": 3, 
    "model": "sites.site", 
    "fields": {
        "domain": "domain3", 
        "name": "name3"
    }
}
]
Run Code Online (Sandbox Code Playgroud)

由于它是一个全局项目设置,我在项目根目录中添加了一个"fixtures"文件夹,并将其添加到FIXTURE_DIRS.

# Used to search fixture files directories.
# Fixture files are files that provide initial data to be
# inserted in the database. (>python manage.py loaddata) …
Run Code Online (Sandbox Code Playgroud)

django-sites django-fixtures django-migrations

14
推荐指数
2
解决办法
3096
查看次数

Django模型选择:IntegerField与CharField

TL; DR:我有一个包含数百万个实例的表,我想知道应该如何编制索引.

我有一个使用SQL Server作为数据库后端的Django项目.

在生产环境中拥有大约1400万个实例的模型之后,我意识到我遇到了性能问题:

class UserEvent(models.Model)

    A_EVENT = 'A'
    B_EVENT = 'B'

    types = (
        (A_EVENT, 'Event A'),
        (B_EVENT, 'Event B')
    )

    event_type = models.CharField(max_length=1, choices=types)

    contract = models.ForeignKey(Contract)

    # field_x = (...)
    # field_y = (...)
Run Code Online (Sandbox Code Playgroud)

我使用了很多基于此字段的查询,并且效率非常低,因为该字段未编入索引.仅使用此字段过滤模型大约需要7秒,而使用索引外键查询则不会出现性能问题:

UserEvent.objects.filter(event_type=UserEvent.B_EVENT).count()
# elapsed time: 0:00:06.921287

UserEvent.objects.filter(contract_id=62).count()
# elapsed time: 0:00:00.344261
Run Code Online (Sandbox Code Playgroud)

当我意识到这一点,我也做了一个问题对自己说:"不应该在该领域是一个SmallIntegerField因为我只有一小部分的选择,以及基于在整数字段查询比基于文本/ VARCHAR查询更有效率?".

所以,根据我的理解,我有两个选择*:

*我意识到可能存在第三种选择,因为具有低基数的索引字段可能不会导致严重的改进,但由于我的值具有[1%-99%]分布(并且我正在寻找1%的部分),所以索引这个领域似乎是一个有效的选择.

  • A)只需索引此字段,并将其保留为CharField.

    A_EVENT = 'A'
    B_EVENT = 'B'
    
    types = (
        (A_EVENT, 'Event A'),
        (B_EVENT, 'Event B')
    )
    
    event_type = models.CharField(max_length=1, choices=types, db_index=True)
    
    Run Code Online (Sandbox Code Playgroud)
  • B) …

sql-server django indexing django-models

10
推荐指数
1
解决办法
2102
查看次数

在弹性搜索上查询多级嵌套字段

我是Elastic Search和非SQL范例的新手.我一直在关注ES教程,但有一件事我无法工作.

在下面的代码中(我使用PyES与ES交互)我创建了一个带有嵌套字段(主题)的单个文档,其中包含另一个嵌套字段(概念).

from pyes import *

conn = ES('127.0.0.1:9200')  # Use HTTP

# Delete and Create a new index.
conn.indices.delete_index("documents-index")
conn.create_index("documents-index")

# Create a single document.
document = {
    "docid": 123456789,
    "title": "This is the doc title.",
    "description": "This is the doc description.",
    "datepublished": 2005,
    "author": ["Joe", "John", "Charles"],
    "subjects": [{
                    "subjectname": 'subject1',
                    "subjectid": [210, 311, 1012, 784, 568],
                    "subjectkey": 2,
                    "concepts": [
                                    {"name": "concept1", "score": 75},
                                    {"name": "concept2", "score": 55}
                                  ]
                },
                {
                    "subjectname": 'subject2',
                    "subjectid": …
Run Code Online (Sandbox Code Playgroud)

nested nosql elasticsearch pyes

8
推荐指数
1
解决办法
8543
查看次数