我有一个构建API,该API应该从-大查询集返回10个随机选择的结果。
我有以下4种型号:
class ScrapingOperation(models.Model):
completed = models.BooleanField(default=False)
(...)
indexes = [
models.Index(fields=['completed'], name='completed_idx'),
models.Index(fields=['trusted'], name='trusted_idx'),
]
@property
def ads(self):
"""returns all ads linked to the searches of this operation"""
return Ad.objects.filter(searches__in=self.searches.all())
class Search(models.Model):
completed = models.BooleanField(default=False)
scraping_operation = models.ForeignKey(
ScrapingOperation,
on_delete=models.CASCADE,
related_name='searches'
)
(...)
class Ad(models.Model):
searches = models.ManyToManyField('scraper.Search', related_name='ads')
(...)
class Label(models.Model):
value = models.Integerfield()
linked_ad = models.OneToOneField(
Ad, on_delete=models.CASCADE, related_name='labels'
)
数据库中当前有400.000 + Ad
个对象,但是平均ScrapingOperation
有14000个Ad
对象链接到该对象。我希望API从这些+/- 14000中返回10个随机结果,这些结果还没有链接的Label
对象(每个操作最多只能有数百个对象)
因此必须从包含14.000个对象的查询中返回10个随机结果。
早期版本只能返回1个结果,但是使用了慢得多的sort_by('?')
方法。当我不得不按比例放大以随机返回10个Ad
对象时,我使用了一种部分基于this stackoverflow answer
以下是选择(并返回)10个随机对象的代码:
# Get all ads linked to the last completed operation
last_op_ads = ScrapingOperation.objects.filter(completed=True).last().ads
# Get all ads that don't have an label yet
random_ads = last_op_ads.filter(labels__isnull=True)
# Get list ids of all potential ads
id_list = random_ads.values_list('id', flat=True)
id_list = list(id_list)
# Select a random sample of 10, get objects with PK matches
samples = rd.sample(id_list, min(len(id_list), 10))
selected_samples = random_ads.filter(id__in=samples)
return selected_samples
但是,尽管进行了优化,但此查询仍需要10秒钟以上的时间才能完成,从而创建了非常慢的API。
这么长的延迟是随机查询固有的吗? (如果是这样,其他程序员如何处理此限制?)还是我缺少的代码中存在错误/效率低下?
编辑:基于响应,我在下面包括了原始sql查询 (注意:这些文件在我的本地环境中运行,该环境仅包含生产环境中包含的数据的5%)
{'sql': 'SELECT "scraper_scrapingoperation"."id",
"scraper_scrapingoperation"."date_started",
"scraper_scrapingoperation"."date_completed",
"scraper_scrapingoperation"."completed",
"scraper_scrapingoperation"."round",
"scraper_scrapingoperation"."trusted" FROM "scraper_scrapingoperation"
WHERE "scraper_scrapingoperation"."completed" = true ORDER BY
"scraper_scrapingoperation"."id" DESC LIMIT 1', 'time': '0.001'}
{'sql': 'SELECT "database_ad"."id" FROM "database_ad" INNER JOIN
"database_ad_searches" ON ("database_ad"."id" =
"database_ad_searches"."ad_id") LEFT OUTER JOIN "classifier_label" ON
("database_ad"."id" = "classifier_label"."ad_id") WHERE
("database_ad_searches"."search_id" IN (SELECT U0."id" FROM
"scraper_search" U0 WHERE U0."scraping_operation_id" = 6) AND
"classifier_label"."id" IS NULL)', 'time': '1.677'}
编辑2:我尝试了一种更深的select_related
参数
random_ads = ScrapingOperation.objects.prefetch_related(
'searches__ads__labels',
).filter(completed=True).last().ads.exclude(
labels__isnull=True
)
id_list = random_ads.values_list('id', flat=True)
id_list = list(id_list)
samples = rd.sample(id_list, min(
len(id_list), 10))
selected_samples = random_ads.filter(
id__in=samples)
return selected_samples
产生以下SQL查询:
{'time': '0.008', 'sql': 'SELECT "scraper_search"."id",
"scraper_search"."item_id", "scraper_search"."date_started",
"scraper_search"."date_completed", "scraper_search"."completed",
"scraper_search"."round", "scraper_search"."scraping_operation_id",
"scraper_search"."trusted" FROM "scraper_search" WHERE
"scraper_search"."scraping_operation_id" IN (6)'}
{'time': '0.113', 'sql': 'SELECT ("database_ad_searches"."search_id")
AS "_prefetch_related_val_search_id", "database_ad"."id",
"database_ad"."item_id", "database_ad"."item_state",
"database_ad"."title", "database_ad"."seller_id",
"database_ad"."url", "database_ad"."price",
"database_ad"."transaction_type", "database_ad"."transaction_method",
"database_ad"."first_seen", "database_ad"."last_seen",
"database_ad"."promoted" FROM "database_ad" INNER JOIN
"database_ad_searches" ON ("database_ad"."id" =
"database_ad_searches"."ad_id") WHERE
"database_ad_searches"."search_id" IN (130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160)'}
{'time': '0.041', 'sql': 'SELECT "classifier_label"."id",
"classifier_label"."set_by_id", "classifier_label"."ad_id",
"classifier_label"."date", "classifier_label"."phone_type",
"classifier_label"."seller_type", "classifier_label"."sale_type" FROM
"classifier_label" WHERE "classifier_label"."ad_id" IN (1, 3, 6, 10, 20, 29, 30, 35, 43, (and MANY more of these numbers) ....'}
{'time': '1.498', 'sql': 'SELECT "database_ad"."id" FROM "database_ad"
INNER JOIN "database_ad_searches" ON ("database_ad"."id" = "database_ad_searches"."ad_id") LEFT OUTER JOIN "classifier_label" ON
("database_ad"."id" = "classifier_label"."ad_id") WHERE
("database_ad_searches"."search_id" IN (SELECT U0."id" FROM
"scraper_search" U0 WHERE U0."scraping_operation_id" = 6) AND NOT
("classifier_label"."id" IS NOT NULL))'}
每个ScrapingOperation
“仅”具有+/- 14000个链接广告,但实际生产的广告总数为400.000(并且还在不断增长)。上面的所有代码在我的本地环境(仅包含5%的数据)上返回有效结果,但在生产中的API上返回502错误。
答案 0 :(得分:1)
我会尝试首先隔离链接的广告,然后使用生成的随机列的顺序从中随机抽取10个广告。我不确定生成的sql如何有效。可以肯定的是,我希望完全在任务上创建一个存储过程,因为这显然是对随机样本进行数据挖掘的操作。