SQLite 3,使用unixEpoch并优化时间条件

时间:2018-01-03 11:12:26

标签: python datetime sqlite

我有大约15天的数据,我想查询过去5天。我也想在我的条件下使用epochTime。

以下是我现在正在使用的内容:

  • 要获得每200行x值,通过仅使用rowid非常快速的方法。

    connection = sql.connect("/home/pi/data.db")
    cursor = connection.cursor()
    cursor.execute("SELECT epochTime,x from (select rowid,epochTime,x from table1 where (rowid % 200 = 0)) order by rowid ASC")
    x = cursor.fetchall()
    
  • 首先尝试使用经典的时间戳,但是我很惊讶这比soluce 1花了更多的时间(如果获取15天,大约需要+2秒)。我做错了吗?

    cursor.execute("SELECT timestamp, epochTime, x from (select rowid,epochTime,x from table1 where (rowid % 200 = 0) and timestamp>(select datetime('now','-5 day'))) order by rowid ASC")
    
  • 最后,第一次尝试使用epochTime,但它不起作用:

    cursor.execute("SELECT epochTime, x from (select rowid,epochTime,x from table1 where (rowid % 200 = 0) and epochTime>(select datetime('now','-68400'))) order by rowid ASC")
    

注意:这里是如何解析epochTime,我不得不添加%s100来处理highcharts,它可能导致sqlite条件的问题? :

now = datetime.datetime.now()
epochTime = now.strftime("%s100")

使用“时间”条件会自动减慢请求吗? 我应该怎么写这个以便与UnixEpoch一起正常工作?

非常感谢!

0 个答案:

没有答案