Thank you chetan I will try it out and let you know

On Tue, Jan 26, 2021 at 7:58 PM Chetan Ganji <[email protected]> wrote:

>
> Hi Salima,
>
> Couple of things you can change in the code to make it run faster!
> Do these changes and performance will definitely improve.
>
> 1.Queryset
>
> Problem:
> classifieds =
> vk_classifieds.objects.filter(class_status='1').order_by('-added_date')
> This will fetch all the entries in the table; if there are million records
> in the table,
> all of them will be fetched, which is not required at all!
>
> Processing them will consume resources on the server which is unnecessary
> calculation!
> Avoid it!
>
> Solution:
> classifieds =
> vk_classifieds.objects.filter(class_status='1').order_by('-added_date')[:page_size]
>
> Only fetch what you need! As your page size is 40 actual query will look
> like below.
> classifieds =
> vk_classifieds.objects.filter(class_status='1').order_by('-added_date')[:40]
>
> 2. Calculation on every request
>
> 2.1 classifieds_dist
> Distance between two zipcodes is not going to be changed! You dont have to
> calculate on every request.
> Calculate it only once when the new classified is created and store it in
> a separate table.
> This calculation must be done using a celery task.
>
> Every time zipcode of classified is updated or user updates his zipcode,
> this distance has to be updated.
> You have to put restrictions on how many times a user can change his
> zipcode.
> Otherwise you could end up paying hugh money to the api providers!
> This updation must be done using a celery task.
>
>
> 2.2 diff_time
> I dont know the code and complexity of it.
> However as it is going to be required on every request find a way to
> calculate it once and store in a separate table.
>
> Let me know if they are helpful or not!
> Cheers!
>
>
> On Thu, Jan 21, 2021, 9:35 AM Salima Begum <[email protected]>
> wrote:
>
>> Hi all,
>>
>> We are building website, Here I have written functionality for
>> classifieds page.
>> It is loading to slow because of We are prompting distance calculation in
>> classifieds page. By using distance API matrix based on logged in user zip
>> code and individual Ad zip codes from query set(database).
>>
>> ```
>>     def classifieds(request):
>>         global dict_time
>>         try:
>>             dict_time = {}
>>             email = request.session.get('email')
>>
>>             # Here we are displaying the classified ads "order by date".
>> The ads will be sorted by latest date.
>>             classifieds =
>> vk_classifieds.objects.filter(class_status='1').order_by('-added_date')
>>
>>             count =
>> vk_classifieds.objects.filter(class_status='1').order_by('-added_date').count()
>>             for i in classifieds:
>>                 # diff_time is a child method. By passing 'i' as object
>> in diff_time.
>>                 difrnc_date = diff_time(i)
>>                 dict_time[i.id] = difrnc_date
>>
>>             # Pagination for classifieds page.
>>             # classified = random.sample(list(classifieds), k=count)
>>             # print("classified = ", str(classified))
>>             # By default first page
>>             page = request.GET.get('page', 1)
>>             # print("page = ", str(page))
>>             # Per page setting 40 objects.
>>             paginator = Paginator(list(classifieds), 40)
>>             # print("paginator = ", str(paginator))
>>             classified_p = paginator.page(page)
>>             # print(classified_p)
>>         except PageNotAnInteger:
>>             classified_p = paginator.page(1)
>>         except EmptyPage:
>>             classified_p = paginator.page(paginator.num_pages)
>>         except Exception as e:
>>             logging.error(e)
>>             return render(request, "classifieds.html",
>>                           {"Classifieds": classified_p,
>>  # distance calculation
>>                            "distance": classifieds_dist(email),
>>                            'some_date': dict_time,
>>                            })
>>         return render(request, "classifieds.html", {"Classifieds":
>> classified_p,
>>                                                     "distance":
>> classifieds_dist(email),
>>                                                     'some_date':
>> dict_time,
>>                                                     })
>> ```
>>
>> ```
>>
>> def classifieds_dist(email):
>>     global frm, km
>>     dict_distance = {}
>>
>>     qury = vk_customer.objects.filter(email=email).first()
>>
>>     # From above qury variable we are getting zip of customer.
>>     frm = qury.Zip
>>     # importing json package to calculate the distance
>>     url = "
>> https://maps.googleapis.com/maps/api/distancematrix/json?units=imperial";
>>     headers = {
>>         'Authorization': "Bearer somevalue",
>>         'User-Agent': "some value",
>>         'Accept': "*/*",
>>         'Cache-Control': "no-cache",
>>         'Postman-Token': "some value",
>>         'Host': "maps.googleapis.com",
>>         'Accept-Encoding': "gzip, deflate",
>>         'Connection': "keep-alive",
>>         'cache-control': "no-cache"
>>     }
>>     classifieds =
>> vk_classifieds.objects.filter(class_status='1').order_by('-added_date')
>>     for i in classifieds:
>>         # while a user login through his login email we capture his
>> complete detail into an variable which we given as "qury"
>>         # and after the details are stored into the variable from there
>> we will filter his starting and destination point as zipcode
>>
>>         # After login his/her based on email we are filtering in
>> vk_customer table. Then storing in qury variable.
>>
>>         # This zip is getting from vk_classifieds(model in models.py)
>> table.
>>         # 'i' attribute is getting from classifieds() function.
>>         to = i.zip
>>
>>         origin_list = [to]
>>         desination_list = [frm]
>>         # here we used api for calculating the source and destination
>> point
>>         querystring = {"origins": origin_list, "destinations":
>> desination_list, "departure_time": "now",
>>                        "key": "AIzaSyDhlCiMAEEfoYhkPcOyP0PLqpHsVMmYEXM"}
>>         # here we are passing these headers to the api
>>
>>         # we are capturing the response in variable called response
>>         response = requests.request("GET", url, headers=headers,
>> params=querystring)
>>         jsondata = response.text
>>         obj = json.loads(jsondata)
>>         list = obj['rows']
>>         if list:
>>             a = list[0].get('elements')
>>             obj2 = a[0].get("distance")
>>             if obj2 is None:
>>                 km = "None"
>>             else:
>>                 km = obj2["text"]
>>             dict_distance[i.id] = km
>>             l1.append(i.id)
>>             print("id = ", str(i.id))
>>             print("km = ", str(km))
>>     return dict_distance
>>
>>
>> ```
>>
>> Because of this loading time of classifieds page is to slow. Please help
>> me to solve this issue.
>>
>> Thanks
>> ~Salima
>>
>> --
>> You received this message because you are subscribed to the Google Groups
>> "Django users" group.
>> To unsubscribe from this group and stop receiving emails from it, send an
>> email to [email protected].
>> To view this discussion on the web visit
>> https://groups.google.com/d/msgid/django-users/CAMSz6bn7j5vsyya9%3Dg4wQBQSyiAEkc8%3DSqm_5QOtthUBWnmZFA%40mail.gmail.com
>> <https://groups.google.com/d/msgid/django-users/CAMSz6bn7j5vsyya9%3Dg4wQBQSyiAEkc8%3DSqm_5QOtthUBWnmZFA%40mail.gmail.com?utm_medium=email&utm_source=footer>
>> .
>>
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/CAMKMUjvYZLDwu0eVSTupFcJHgOiHXF_FuTw09fhyvhBFXgMTPA%40mail.gmail.com
> <https://groups.google.com/d/msgid/django-users/CAMKMUjvYZLDwu0eVSTupFcJHgOiHXF_FuTw09fhyvhBFXgMTPA%40mail.gmail.com?utm_medium=email&utm_source=footer>
> .
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CABscGkSvZGVYf4hh9E344tEVtMPN2wPrvOv4CbwiuJGGyfjF4Q%40mail.gmail.com.

Reply via email to