ページの読み込みがかなり遅くなるコードが少しあります (128 ミリ秒で 49 クエリ)。これは私のサイトのランディング ページなので、すばやく読み込む必要があります。
以下は、サイトの最新の更新のフィードを作成し、[デバッグ] ツールバーに表示されるものから最も遅い読み込み時間を引き起こしている私の views.py です。
def product_feed(request):
""" Return all site activity from friends, etc. """
latestparts = Part.objects.all().prefetch_related('uniparts').order_by('-added')
latestdesigns = Design.objects.all().order_by('-added')
latest = list(latestparts) + list(latestdesigns)
latestupdates = sorted (latest, key = lambda x: x.added, reverse = True)
latestupdates = latestupdates [0:8]
# only get the unique avatars that we need to put on the page so we're not pinging for images for each update
uniqueusers = User.objects.filter(id__in = Part.objects.values_list('adder', flat=True))
return render_to_response("homepage.html", {
"uniqueusers": uniqueusers,
"latestupdates": latestupdates
}, context_instance=RequestContext(request))
最も時間がかかるクエリは次のようです。
latest = list(latestparts) + list(latestdesigns) (25ms)
それぞれ 17 ミリ秒 (サイト全体のアナウンス) と 25 ミリ秒 (各製品フィード アイテムにタグ付きアイテムを追加) の別のものもあり、これも調査中です。
アクティビティ フィードの読み込みを最適化する方法を知っている人はいますか?