Why does it take so long for django to perform a simple database operation through queryset?

clipboard.png

as shown in the figure, a simple add and query operation takes up to 1 second [environment: windows + python3 + django2]. (the amount of data in PS: is not much, and todo_list has only 2 rows of data, which is still so slow.)

def get(request):
    -sharp /todo/list 
    todo_list = list( Todo.objects.all().values("name") )

    return JsonResponse({
        "code": 0,
        "data":   todo_list
    })

@post_method
def add(request):
    -sharp /todo/add 
    item = Todo.objects.create(name=request.POST["name"])
    return JsonResponse({
        "code": 0,
        "msg": "add successfully."
    })

the mysql, used in the database will not be so slow if the default sqlite3 is used.

-sharp settings.py

DATABASES = {
    "default": {
        "ENGINE": "django.db.backends.mysql",
        "NAME": "ops_chart",
        "USER": "root",
        "PASSWORD": "xxxxxx",
        "HOST": "localhost",
        "PORT": "3306",

        "OPTIONS": {
            "sql_mode": "STRICT_TRANS_TABLES"
        }
    }
}

it is said that the operation of the database is slow, don't you even explain what database you are using?

I just saw that it is a direct visit by mysql; to see how the comparison is.


debugged and found that the sql statement didn't take long to execute.
later discovered that

the mysql service of windows will be slow (about 1s), and
the mysql service of linux will be within the normal category (100ms or so).

the problem is solved, but the reason is not clear. Is it slower to establish a connection to the local mysql service of windows?


is generally similar to django's hibernate and java's, and it doesn't take shortcuts. In the real world, writing your own sql, can simplify sql. Instead, he needs to sql, the object back to the sql, just like the computer, it's a good student, but it's not smart, it's just that you teach it what to do, it repeats what you teach it, and it won't if you don't teach it.
by the way, put two groups, check the method of performing sql, read more official documents, it will tell you why

print Province.objects.all().query
from core.models import Province
from django.db import connection
p = Province(name=u'', code='0371')
p.save()

Open debug bar

Menu