Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8219009
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T13:00:11+00:00 2026-06-07T13:00:11+00:00

I was expecting postgres to take some time, but wasn’t expecting for it to

  • 0

I was expecting postgres to take some time, but wasn’t expecting for it to not be the bottleneck here, how can i solve this?

2772856 function calls (2578490 primitive calls) in 32.361 seconds

Ordered by: internal time

ncalls  tottime  percall  cumtime  percall filename:lineno(function)
127921/19340    3.670    0.000   11.486    0.001 /usr/lib/python2.7/copy.py:145(deepcopy)
 2240    3.031    0.001    3.244    0.001 {method 'execute' of 'psycopg2._psycopg.cursor' objects}
114402    1.541    0.000    2.348    0.000 /usr/lib/python2.7/copy.py:267(_keep_alive)

———————————————————————————-

    suppliers = models.Supplier.objects.all().order_by('company')
    for supplier in suppliers :
        sup = {}
        sup['company'] = supplier.company
        sup['supplies'] = get_supplies(1, supplier.uuid)
        sup['category'] = 'Supplier'
        if isocode == None :
            addresses = models.Address.objects.filter(company = supplier.company)
        else :
            addresses = models.Address.objects.filter(company = supplier.company, country_iso = isocode)
        sup['contacts'] = models.Contact.objects.filter(address__in=addresses)
        company_list.append(sup)

———————————————————————————-

def get_supplies (bought_in_controlpanel_id, supplier_uuid) :

    supplier    = None
    activenode  = None

    if supplier_uuid is not None :
        supplier = models.Supplier.objects.get(uuid = supplier_uuid)

    try :
        activenode = boughtin.BoughtInControlPanel.objects.get (pk = 1)
    except :
        pass

    supplies = boughtin.BoughtInControlPanel.objects.filter (parent = activenode)
    for supply in supplies :
        supply.checked  = 0
        supply.disabled = ""
        supply.open     = 0

        if supplier_uuid is not None :
            try    :
                models.Supplies.objects.get(supplier = supplier, bought_in_control_panel = supply)
                supply.checked = 1
            except :
                supply.open    = 1

    return supplies
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T13:00:14+00:00Added an answer on June 7, 2026 at 1:00 pm

    Performance is usually bad when you query DB in a loop. Try avoiding that.

    Why not continue using relations and __in query? =)

    I think this should work with less SQL queries for the first example:

    company_list = models.Supplier.objects.values_list('company', flat=True)
    
    filter_kwargs = dict(address__company__in=company_list)
    
    if isocode is not None:
        filter_kwargs.update(dict(address__company__isocode=isocode))
    
    sup['contacts'] = models.Contact.objects.filter(**filter_kwargs)
    

    Regarding get_supplies(), I’d avoid Supplies.objects.get() in loop as well, unless you’re sure it will be hit just a couple of times. It may be better to gather a list of supplies with one larger query before the loop, and then simply check if required item is present in that list. Although you should profile both variants and select the faster one.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My IDE is expecting a semicolon but I'm not sure where! It is this
I'm expecting a string NOT to match a regular expression, but it is! >>>
I am expecting the string which has i but getting empty results. Can you
I am expecting this to return the value of 'C', but it is returning
I'm expecting this output: output:xyz But if I type the following: a = [x,
This is something I wasn't expecting. I'm using anemone to count links in pages
I wasn't expecting to come across this error. I imagine I'm doing something wrong
This might not be quite the question you're expecting! I don't want a regex
I am not expecting a definite yes or no. Any knowledge you might have
By design I'm expecting a DuplicateKeyExeception from time to time. Is it possible to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.