Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/285.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 为什么Django ORM这么慢?_Python_Sql_Django_Performance_Orm - Fatal编程技术网

Python 为什么Django ORM这么慢?

Python 为什么Django ORM这么慢?,python,sql,django,performance,orm,Python,Sql,Django,Performance,Orm,1。此代码-原始SQL-需要2.6秒: * all_feeds = Feed.objects.all() for feed in all_feeds: q_sku = MainData.objects.raw(f'SELECT id as id, COUNT(DISTINCT sku) as "count" FROM imports_maindata WHERE feed_id={feed.id}') q_loc = Ma

1。此代码-原始SQL-需要2.6秒:

  *
    all_feeds = Feed.objects.all()

    for feed in all_feeds:
        q_sku = MainData.objects.raw(f'SELECT id as id, COUNT(DISTINCT sku) as "count" FROM imports_maindata WHERE feed_id={feed.id}')
        q_loc = MainData.objects.raw(
        f'SELECT id as id, COUNT(DISTINCT locale) AS "count" FROM imports_maindata WHERE feed_id={feed.id}')
        q_spec = MapSpecs.objects.raw(
        f'SELECT id as id, COUNT(DISTINCT f_feat_id) AS "count" FROM imports_mapspecs WHERE feed_id={feed.id}')
        q_mapped = MapSpecs.objects.raw(
        f'SELECT id as id, COUNT(DISTINCT ic_feat_id) AS "count" FROM imports_mapspecs WHERE feed_id={feed.id} AND ic_feat_id IS NOT NULL')
    
        q_date = MainData.objects.raw(
        f'SELECT id as id, MAX(last_update) as "last_date" FROM imports_maindata WHERE feed_id={feed.id}')
        print(q_sku[0].count, q_loc[0].count, q_spec[0].count, q_mapped[0].count, q_date[0].last_date)*

    *f = Feed.objects.all()
    for feed in f:
        prods_count = f.filter(maindata__feed_id=feed.id).values('maindata__sku').distinct().count()
        
        locales_count = f.filter(maindata__feed_id=feed.id).values_list('maindata__locale', flat=True).distinct()
    
        total_specs = f.filter(mapspecs__feed_id=feed.id).count()
        mapped_specs = f.filter(mapspecs__feed_id=feed.id, mapspecs__ic_feat_id__isnull=False).all().count()
    
        try:
            last_update = f.filter(maindata__feed_id=feed.id).values('maindata__last_update').distinct().order_by('-maindata__last_update').first()['maindata__last_update']
        except TypeError:
            pass*
    *

    f = Feed.objects.all()
    prods = f.annotate(num_prods=Count('maindata__sku', distinct=True))
    locs = f.annotate(num_locs=Count('maindata__locale', distinct=True))
    total_sp_count = f.annotate(num_books=Count('mapspecs__f_feat_id', distinct=True))
    total_sp_mapped = f.filter(mapspecs__ic_feat_id__isnull=False).annotate(
            num_books=Count('mapspecs__ic_feat_id', distinct=True))
    dates = f.annotate(num_books=Max('maindata__last_update'))

*
3。而这一次(仅限ORM)需要3.1秒:

  *
    all_feeds = Feed.objects.all()

    for feed in all_feeds:
        q_sku = MainData.objects.raw(f'SELECT id as id, COUNT(DISTINCT sku) as "count" FROM imports_maindata WHERE feed_id={feed.id}')
        q_loc = MainData.objects.raw(
        f'SELECT id as id, COUNT(DISTINCT locale) AS "count" FROM imports_maindata WHERE feed_id={feed.id}')
        q_spec = MapSpecs.objects.raw(
        f'SELECT id as id, COUNT(DISTINCT f_feat_id) AS "count" FROM imports_mapspecs WHERE feed_id={feed.id}')
        q_mapped = MapSpecs.objects.raw(
        f'SELECT id as id, COUNT(DISTINCT ic_feat_id) AS "count" FROM imports_mapspecs WHERE feed_id={feed.id} AND ic_feat_id IS NOT NULL')
    
        q_date = MainData.objects.raw(
        f'SELECT id as id, MAX(last_update) as "last_date" FROM imports_maindata WHERE feed_id={feed.id}')
        print(q_sku[0].count, q_loc[0].count, q_spec[0].count, q_mapped[0].count, q_date[0].last_date)*

    *f = Feed.objects.all()
    for feed in f:
        prods_count = f.filter(maindata__feed_id=feed.id).values('maindata__sku').distinct().count()
        
        locales_count = f.filter(maindata__feed_id=feed.id).values_list('maindata__locale', flat=True).distinct()
    
        total_specs = f.filter(mapspecs__feed_id=feed.id).count()
        mapped_specs = f.filter(mapspecs__feed_id=feed.id, mapspecs__ic_feat_id__isnull=False).all().count()
    
        try:
            last_update = f.filter(maindata__feed_id=feed.id).values('maindata__last_update').distinct().order_by('-maindata__last_update').first()['maindata__last_update']
        except TypeError:
            pass*
    *

    f = Feed.objects.all()
    prods = f.annotate(num_prods=Count('maindata__sku', distinct=True))
    locs = f.annotate(num_locs=Count('maindata__locale', distinct=True))
    total_sp_count = f.annotate(num_books=Count('mapspecs__f_feat_id', distinct=True))
    total_sp_mapped = f.filter(mapspecs__ic_feat_id__isnull=False).annotate(
            num_books=Count('mapspecs__ic_feat_id', distinct=True))
    dates = f.annotate(num_books=Max('maindata__last_update'))

*
3。使用ORM但方法不同的这个,在3.1-3.2秒内返回:

  *
    all_feeds = Feed.objects.all()

    for feed in all_feeds:
        q_sku = MainData.objects.raw(f'SELECT id as id, COUNT(DISTINCT sku) as "count" FROM imports_maindata WHERE feed_id={feed.id}')
        q_loc = MainData.objects.raw(
        f'SELECT id as id, COUNT(DISTINCT locale) AS "count" FROM imports_maindata WHERE feed_id={feed.id}')
        q_spec = MapSpecs.objects.raw(
        f'SELECT id as id, COUNT(DISTINCT f_feat_id) AS "count" FROM imports_mapspecs WHERE feed_id={feed.id}')
        q_mapped = MapSpecs.objects.raw(
        f'SELECT id as id, COUNT(DISTINCT ic_feat_id) AS "count" FROM imports_mapspecs WHERE feed_id={feed.id} AND ic_feat_id IS NOT NULL')
    
        q_date = MainData.objects.raw(
        f'SELECT id as id, MAX(last_update) as "last_date" FROM imports_maindata WHERE feed_id={feed.id}')
        print(q_sku[0].count, q_loc[0].count, q_spec[0].count, q_mapped[0].count, q_date[0].last_date)*

    *f = Feed.objects.all()
    for feed in f:
        prods_count = f.filter(maindata__feed_id=feed.id).values('maindata__sku').distinct().count()
        
        locales_count = f.filter(maindata__feed_id=feed.id).values_list('maindata__locale', flat=True).distinct()
    
        total_specs = f.filter(mapspecs__feed_id=feed.id).count()
        mapped_specs = f.filter(mapspecs__feed_id=feed.id, mapspecs__ic_feat_id__isnull=False).all().count()
    
        try:
            last_update = f.filter(maindata__feed_id=feed.id).values('maindata__last_update').distinct().order_by('-maindata__last_update').first()['maindata__last_update']
        except TypeError:
            pass*
    *

    f = Feed.objects.all()
    prods = f.annotate(num_prods=Count('maindata__sku', distinct=True))
    locs = f.annotate(num_locs=Count('maindata__locale', distinct=True))
    total_sp_count = f.annotate(num_books=Count('mapspecs__f_feat_id', distinct=True))
    total_sp_mapped = f.filter(mapspecs__ic_feat_id__isnull=False).annotate(
            num_books=Count('mapspecs__ic_feat_id', distinct=True))
    dates = f.annotate(num_books=Max('maindata__last_update'))

*

那么为什么Django ORM如此低效和缓慢呢?计时适用于以DB为单位的少量行(低于50K)。。。因此,它不仅比原始SQL慢,而且语法更加混乱(有时过于模糊)。我想应该考虑其他一些Python框架…

您能提供您从哪里开始测量代码中的时间吗?您能提供您从哪里开始测量代码中的时间吗?