gpt4 book ai didi

performance - sqlite即使索引也很难查询

转载 作者:行者123 更新时间:2023-12-03 14:59:23 24 4
gpt4 key购买 nike

我有一个非常简单的数据库,只有一个表:

CREATE TABLE records ( 
id INTEGER PRIMARY KEY AUTOINCREMENT,
symbol VARCHAR(20) NOT NULL,

time_ts INTEGER NOT NULL,
open_ts INTEGER NOT NULL,
close_ts INTEGER NOT NULL,

open_price REAL NOT NULL,
high_price REAL NOT NULL,
low_price REAL NOT NULL,
close_price REAL NOT NULL,

trades_count INTEGER NOT NULL,

volume_amount REAL NOT NULL,
quote_asset_volume REAL NOT NULL,
taker_buy_base_asset_volume REAL NOT NULL,
taker_buy_quote_asset_volume REAL NOT NULL)


和一个索引:

CREATE INDEX symbol_index ON records (symbol)


数据库的大小为 12.63GB

我正在运行此查询:

SELECT 
symbol,
MAX(close_ts) max_close_ts,
MIN(close_ts) min_close_ts
FROM records
GROUP BY symbol


执行它大约需要一分钟。

如您所见,在 symbol列上创建了一个索引。.然而,即使这样做-查询也非常缓慢。

甚至像这样的查询:

select count(id) from records;


执行大约需要77秒。表中的总行数为 115_944_904

我希望以后的记录数量会增加两倍。我可以做些什么来使查询工作更快?即使在主键和符号列上都有索引,我的性能也相当差。

我有没有达到任何极限?

最佳答案

您可以创建覆盖索引以避免访问表:

CREATE INDEX symbol_index ON records (symbol,close_ts)

SELECT
symbol,
MAX(close_ts) max_close_ts,
MIN(close_ts) min_close_ts
FROM records
GROUP BY symbol;

关于performance - sqlite即使索引也很难查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57610961/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com