私は、教育的な質問記録のリストからいくつかの一般的なタグの組み合わせをふるいにかけようとしています。
この例では、2タグの例(tag-tag)のみを見ており、「point」+「curve」(65エントリ)「add」+「subtract」(40エントリ)のような結果の例を取得する必要があります。 )..。
これは、SQLステートメントで望ましい結果です。
SELECT a.tag, b.tag, count(*)
FROM examquestions.dbmanagement_tag as a
INNER JOIN examquestions.dbmanagement_tag as b on a.question_id_id = b.question_id_id
where a.tag != b.tag
group by a.tag, b.tag
基本的に、一般的な質問を含むさまざまなタグをリストに識別し、それらを同じ一致するタグの組み合わせ内にグループ化します。
私はdjangoクエリセットを使用して同様のクエリを実行しようとしました:
twotaglist = [] #final set of results
alphatags = tag.objects.all().values('tag', 'type').annotate().order_by('tag')
betatags = tag.objects.all().values('tag', 'type').annotate().order_by('tag')
startindex = 0 #startindex reduced by 1 to shorten betatag range each time the atag changes. this is to reduce the double count of comparison of similar matches of tags
for atag in alphatags:
for btag in betatags[startindex:]:
if (atag['tag'] != btag['tag']):
commonQns = [] #to check how many common qns
atagQns = tag.objects.filter(tag=atag['tag'], question_id__in=qnlist).values('question_id').annotate()
btagQns = tag.objects.filter(tag=btag['tag'], question_id__in=qnlist).values('question_id').annotate()
for atagQ in atagQns:
for btagQ in btagQns:
if (atagQ['question_id'] == btagQ['question_id']):
commonQns.append(atagQ['question_id'])
if (len(commonQns) > 0):
twotaglist.append({'atag': atag['tag'],
'btag': btag['tag'],
'count': len(commonQns)})
startindex=startindex+1
ロジックは正常に機能しますが、このプラットフォームはかなり新しいので、効率を上げるために、より短い回避策があるかどうかはわかりません。
現在、クエリは約5KX5Kタグの比較で約45秒必要でした:(
アドオン:タグクラス
class tag(models.Model):
id = models.IntegerField('id',primary_key=True,null=False)
question_id = models.ForeignKey(question,null=False)
tag = models.TextField('tag',null=True)
type = models.CharField('type',max_length=1)
def __str__(self):
return str(self.tag)