DB queries at import time

8 views
Skip to first unread message

Thomas Guettler

unread,
Apr 12, 2012, 8:09:54 AM4/12/12
to Django users
Hi,

sometimes it happens, that db queries get executed at import time (during importing the file by the interpreter).
That's waste of time a resources.

Is there a way to test how many queries get executing during import? I want some automated way to detect these db queries.

Example:

def mychoices():
for obj in MyModel.objects.all(): # this hits the db during import. That's not good.
....

class MyForm(forms.Form):
foo=forms.ChoiceField(choices=mychoices())


--
Thomas Guettler, http://www.thomas-guettler.de/
E-Mail: guettli (*) thomas-guettler + de

Jani Tiainen

unread,
Apr 12, 2012, 9:25:34 AM4/12/12
to django...@googlegroups.com
12.4.2012 15:09, Thomas Guettler kirjoitti:
> Hi,
>
> sometimes it happens, that db queries get executed at import time
> (during importing the file by the interpreter).
> That's waste of time a resources.
>
> Is there a way to test how many queries get executing during import? I
> want some automated way to detect these db queries.
>
> Example:
>
> def mychoices():
> for obj in MyModel.objects.all(): # this hits the db during import.
> That's not good.
> ....
>
> class MyForm(forms.Form):
> foo=forms.ChoiceField(choices=mychoices())
>
>

That is because you pass parameter as a function call, so of course
function must be evaluated. So in your code reads:

Declare class attribute "foo" as a new instance of "forms.ChoiceField"
with parameter of "choices" with values returned from "mychoices".

So to get rid of that part, you can pass method or a function as a
parameter and it will be evaluated at the runtime:

class MyForm(forms.Form):
foo=forms.ChoiceField(choices=mychoices)

--

Jani Tiainen


Thomas Guettler

unread,
Apr 12, 2012, 10:33:42 AM4/12/12
to django...@googlegroups.com

Am 12.04.2012 15:25, schrieb Jani Tiainen:
> 12.4.2012 15:09, Thomas Guettler kirjoitti:
>> Hi,
>>
>> sometimes it happens, that db queries get executed at import time
>> (during importing the file by the interpreter).
>> That's waste of time a resources.
>>
>> Is there a way to test how many queries get executing during import? I
>> want some automated way to detect these db queries.
>>
>> Example:
>>
>> def mychoices():
>> for obj in MyModel.objects.all(): # this hits the db during import.
>> That's not good.
>> ....
>>
>> class MyForm(forms.Form):
>> foo=forms.ChoiceField(choices=mychoices())
>>
>>
>
> That is because you pass parameter as a function call, so of course function must be evaluated. So in your code reads:

> ...

Yes, I know this. But I work in a team where not everybody is a django expert. I search a way to test for queries which
happen at import time.

.... django debug toolbar does some tricky wrapping of cursor.execute(). I guess this could be a solution.

Thomas

Daniel Roseman

unread,
Apr 12, 2012, 10:53:11 AM4/12/12
to django...@googlegroups.com
On Thursday, 12 April 2012 13:09:54 UTC+1, guettli wrote:
Hi,

sometimes it happens, that db queries get executed at import time (during importing the file by the interpreter).
That's waste of time a resources.

 
Why? Imports only happen the first time a process accesses a module. A process lasts for many requests.

 

Is there a way to test how many queries get executing during import? I want some automated way to detect these db queries.

Example:

def mychoices():
    for obj in MyModel.objects.all(): # this hits the db during import. That's not good.
        ....

class MyForm(forms.Form):
     foo=forms.ChoiceField(choices=mychoices())


The problem caused by this being executed at import time is not a "waste of time and resources", but the fact that if you add objects to MyModel, they won't appear in the MyForm.foo choices, until the process is restarted - which could be several days.
--
DR.

Matt Schinckel

unread,
Apr 12, 2012, 5:33:32 PM4/12/12
to django...@googlegroups.com
If you install django-devserver, and enable SQL queries in the console, then
every query will be displayed as it happens, and you should be able to track them
down.

Matt.

Thomas Guettler

unread,
Apr 13, 2012, 5:18:32 AM4/13/12
to django...@googlegroups.com

thank you, django-devserver seems to have a lot of other nice debugging features.

Thomas

Reply all
Reply to author
Forward
0 new messages