【问题标题】:How can i improce performance of sqlite with large table?如何使用大表提高 sqlite 的性能?
【发布时间】:2019-09-20 21:11:31
【问题描述】:

具有单表和 60,000,000 条记录的 SQlite 数据库。运行简单查询的时间超过 100 秒。
我试图切换到 postgeSQL,但它的性能甚至更差。 没有在 mySQL 或 msSQL 上测试它。
Shell 我拆分表(假设每个 pointID 有不同的表 - 有数百个?或者每个月有不同的表 - 那么我最多会有 10,000,000 条记录?)

sql方案:

CREATE TABLE `collectedData` (
    `id`    INTEGER,
    `timeStamp` double,
    `timeDateStr`   nvarchar,
    `pointID`   nvarchar,
    `pointIDindex`  double,
    `trendNumber`   integer,
    `status`    nvarchar,
    `value` double,
    PRIMARY KEY(`id`)
);

CREATE INDEX `idx_pointID` ON `collectedData` (
    `pointID`
);

CREATE INDEX `idx_pointIDindex` ON `collectedData` (
    `pointIDindex`
);

CREATE INDEX `idx_timeStamp` ON `collectedData` (
    `timeStamp`
);

CREATE INDEX `idx_trendNumber` ON `collectedData` (
    `trendNumber`
);

下一个查询耗时 107 秒:

select * from collectedData 
where 
trendNumber =1 
and status <> ''  and 
timestamp <=1556793244 
and pointid in ('point1','point2','pont3','point4','point5','point6','point7','point8','point9','pointa') 
and pointIDindex % 1 = 0  
order by timestamp desc, id desc limit 5000

下一个查询耗时 150 秒(条件更少)

select * from collectedData 
where 
trendNumber =1 
and status <> ''  and 
timestamp <=1556793244 
and pointIDindex % 1 = 0  
order by timestamp desc, id desc limit 5000

编辑: Asnwer from another place - 添加下一个索引:

CREATE INDEX idx_All ON collectedData (trendNumber, pointid, pointIDindex, status, timestamp desc, id desc, timeDateStr, value)

性能提高了 3 倍。

编辑#2:@Raymond Nijland 提供:执行计划是: 使用 COVERING INDEX idx_All (trendNumber=? AND pointID=?) 搜索表收集的数据" "0" "0" "0" "执行列表子查询 1" "0" "0" "0" "使用 TEMP B-TREE FOR ORDER BY"

感谢他 - 使用这些数据,我将查询中的规则顺序更改为下一个:

select * from (
select * from collectedData 
where 
trendNumber =1 
and status <> ''  and 
timestamp <=1556793244 
and pointid in ('point1','point2','pont3','point4','point5','point6','point7','point8','point9','pointa') 

and pointIDindex % 1 = 0  
order by id desc limit 5000
) order by timestamp desc

这取得了很大的进步(对我来说已经解决了)。

【问题讨论】:

  • 关于性能的问题还应该包括一些query plan output 用于问题中涉及的每个查询
  • 此外,您无法比较 PostgreSQL 和 SQLite 的性能,因为它们解决了完全不同的问题。因为 PostgreSQL 是服务器/客户端应用程序,而 SQLite 是文件嵌入式数据库应用程序。
  • 感谢@RaymondNijland - 这帮助我解决了问题(稍微改变了查询)。

标签: performance sqlite


【解决方案1】:

在@RaymondNijland 让我检查执行计划后,我将查询更改为:

select * from (
select * from collectedData 
where 
trendNumber =1 
and status <> ''  and 
timestamp <=1556793244 
and pointid in ('point1','point2','pont3','point4','point5','point6','point7','point8','point9','pointa') 

and pointIDindex % 1 = 0  
order by id desc limit 5000
) order by timestamp desc

此查询提供与其他查询相同的结果,但速度不是 120 倍(在排序前减少记录数)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-12
    • 1970-01-01
    • 2011-12-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多