python中的counter函数_python的counter()函数太慢

我使用的是django rest框架,在使用counter()函数的行中,有一个post api面临速度问题。

@api_view(["POST"])

def calculate_stuff(request):

t1 = time.time()

machine_type = request.data['machine_type']

machine_nos = Machine.objects.filter(machine_type=machine_type).values_list('machine_no', flat=True)

query = Performance.objects.filter(Q(power=100) | Q(power=192),machine_no__in=machine_nos,

).values_list("machine_no", "power")

t2 = time.time()

print t2 - t1 # is around 0.2 seconds

count_192_100 = Counter(query)

t3 = time.time()

print t3 - t2 # is around 1.3 seconds

模型如下:

class Machine(models.Model):

machine_type = models.CharField(null=True, max_length=10)

machine_no = models.IntegerField(null=True)

store_code = models.IntegerField(null=True)

created = models.DateTimeField(auto_now_add=True)

updated = models.DateTimeField(auto_now = True)

class Performance(models.Model):

machine_no = models.IntegerField(null=True)

power = models.IntegerField(null=True)

store_code = models.IntegerField(null=True)

created = models.DateTimeField(auto_now_add=True)

updated = models.DateTimeField(auto_now=True)

此项目正在生产中运行,由于某些原因,两个模型之间没有使用foreignkey。我想在这里实现的是首先:从某个机器类型的“机器”模型中获取机器编号。第二:我想知道在“性能”模型中,每台机器的功率为100,功率为192的次数。

其他信息:我正在使用django 1.11、python 2.7.10、postgresql。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值