我正在尝试在SQLite数据库中构建一些速度变量。我从下表开始:
CREATE TABLE transactions (
id TEXT PRIMARY KEY,
grp TEXT,
ts NUMERIC
);
BEGIN TRANSACTION;
INSERT INTO exclusions VALUES (
'1751d4ad-0690-44d7-a13d-1106f48c7761',
'A',
STRFTIME ('%s', '2016-01-01 00:00:00')
);
-- ...
END TRANSACTION;
CREATE INDEX grp ON transactions (grp, ts);
然后对于每个id
,我想看看在最后x秒内同一grp
中发生了多少次CREATE TABLE v_grp (
id TEXT PRIMARY KEY,
count_1m NUMERIC,
count_7m NUMERIC,
count_40m NUMERIC,
count_4h NUMERIC,
count_1d NUMERIC,
count_1w NUMERIC
);
BEGIN TRANSACTION;
INSERT INTO v_grp
SELECT
tx1.id AS id,
SUM (CASE WHEN tx1.ts - tx2.ts <= 60 THEN 1 ELSE 0 END) AS count_1m,
SUM (CASE WHEN tx1.ts - tx2.ts <= 420 THEN 1 ELSE 0 END) AS count_7m,
SUM (CASE WHEN tx1.ts - tx2.ts <= 2400 THEN 1 ELSE 0 END) AS count_40m,
SUM (CASE WHEN tx1.ts - tx2.ts <= 14400 THEN 1 ELSE 0 END) AS count_4h,
SUM (CASE WHEN tx1.ts - tx2.ts <= 86400 THEN 1 ELSE 0 END) AS count_1d,
SUM (CASE WHEN tx1.ts - tx2.ts <= 604800 THEN 1 ELSE 0 END) AS count_1w
FROM transactions as tx1
JOIN transactions as tx2 ON
tx1.grp = tx2.grp
AND tx1.ts > tx2.ts
AND tx1.ts <= tx2.ts + 604800
GROUP BY tx1.id
;
END TRANSACTION;
。为此,我做了一个永远运行的自我加入。
$string = 'Andreas';
$reversedString = '';
for($i = mb_strlen($string) - 1; $i >= 0; $i--){
$reversedString .= $string[$i];
}
我认为这是可以控制的,因为我有超过1年的13M交易,有6k组(最大的一个是交易的10%,第十大是1%)。
我有什么方法可以让这种运行更快,例如,更好的索引或改进选择?