如何在不循环的情况下返回昂贵的查询的一年中每天的每天记录数?

时间:2019-06-13 11:00:55

标签: python database sqlite peewee

在数据库表中,我们找到具有文件路径的记录,处理文件的员工以及处理文件的时间戳。 表“日志”每位员工仅包含几千条记录。每位员工每天最多都有几条带有时间戳的记录(尽管不是唯一的时间戳)。 我想提取一个元组列表,其中包含一个日期和在该日期创建的表条目的数量。

我提供的代码可以工作,但是速度很慢。 2300条记录的22秒计算时间是荒谬的。 我已经将问题缩小到for循环中的“ logs_per_day =(query.select()。where(fn.date(cls.datetime)== checkday).count())”行中。 我了解在一个循环中执行许多查询可能并不好。此外,将datetime对象转换为日期可能也无济于事。 可以给我指出一种更好的方法吗?

import datetime
import os

from peewee import *

db = SqliteDatabase('logs.db')
# db = SqliteDatabase(':memory:')
now = datetime.datetime.now()

class BaseModel(Model):

    class Meta:
        database = db


class Log(BaseModel):

    log_ID = AutoField()
    datetime = DateTimeField()
    letter = CharField()
    disk_path = CharField()
    ftp_path = CharField()
    out = BooleanField()
    employee = CharField(null=True)

    class Meta:
        table_name = 'log'

    @classmethod
    def get_histo_data(cls, employee="Some Dude", year=None):
        """returns a list with sublists (datetime object, integer)"""
        if not year: # if no year was provided the query return all entries from the employee
            query = cls.select().where(cls.employee == employee).order_by(cls.datetime)
            print(employee, len(query), " entries")

            firstday = query.order_by(cls.datetime).get().datetime.date()
            lastday = query.order_by(cls.datetime.desc()).get().datetime.date()

        else: # returns all entries in the given year
            query = (cls
                        .select()
                        .where(cls.employee == employee, cls.datetime.year == year)
                        .order_by(cls.datetime)
                    )
            print("{} has {} entries in the year {}".format(employee, len(query), year))

            firstday = datetime.date(year, 1, 1)
            lastday = datetime.date(year, 12, 31)

        print("first day sent: ", firstday)
        print("last day sent: ", lastday)

        daydelta = (lastday-firstday).days
        sendList = []

        for i in range(daydelta+1):   ### FIXME: This is extremely slow!!!
            checkday = firstday + datetime.timedelta(days=i)
            logs_per_day = (query
                                .select()
                                .where(fn.date(cls.datetime) == checkday)
                                .count()
                                )

            # print(checkday, "*** logs that day: ", logs_per_day)
            sendList.append([checkday, logs_per_day])

        return sendList

def initialize():
    db.connection()
    db.create_tables([Log], safe=True)
    db.close()

if __name__ == '__main__':
    initialize()

    Log.get_histo_data(employee="Mr Someone", year=2018)

输出应类似于“ [(2018-11-12,157),(2018-11-13,12),(2018-11-14,0)...]

1 个答案:

答案 0 :(得分:-1)

自己发现:

def get_histo_data(cls, employee="Some dude", year=None):
    """returns a list of tuples (datetime object, integer)"""
    if not year: # if no year was provided the query return all entries from the employee
        query = cls.select().where(cls.employee == employee).order_by(cls.datetime)
        print(employee, len(query), " entries")

        firstday = query.order_by(cls.datetime).get().datetime.date()
        lastday = query.order_by(cls.datetime.desc()).get().datetime.date()

    else: # returns all entries in the given year
        query = cls.get_query_by_year(employee, year)
        print("{} has {} entries in the year {}".format(employee, len(query), year))

        firstday = datetime.date(year, 1, 1)
        lastday = datetime.date(year, 12, 31)

    ### count the entries
    logDict = dict()
    for record in query:
        date = record.datetime.date()
        if date not in logDict:
            logDict[date] = 1
        else:
            logDict[date] += 1
    ### fill the null days
    daydelta = (lastday-firstday).days
    for i in range(daydelta+1):
        checkday = firstday + datetime.timedelta(days=i)
        if checkday not in logDict:
            logDict[checkday] = 0
        else:
            continue

    return list(sorted(logDict.items()))