Reputation: 1178
I am working with a table that for the purposes of this question is defined like this
CREATE TABLE players (match_id, hero_id, player_slot);
This table is very frequently updated (a transaction every 20 minutes or so, with roughly 1000 inserts). It contains ~6 million rows atm, but will be constantly growing. I want to read (again, very frequently) the following SELECT statement from the up to date table.
SELECT t1.match_id,
GROUP_CONCAT(hero_id) AS team,
COUNT(DISTINCT hero_id) AS team_size
FROM (SELECT match_id, hero_id
FROM players
WHERE player_slot in (1, 2, 3, 4, 5)
ORDER BY hero_id) t1
GROUP BY t1.match_id;
This is a pretty simplified version of the query, but it's still to slow for me to run every time I want an updated version. As such my hope was to set up a separate table and trigger on insert to the "players" table. Fortunately I will always be inserting all of the records with a given match_id in a single statement, e.g.
INSERT INTO players (match_id, hero_id, player_slot) VALUES
(1, 2, 3),
(1, 4, 4),
(1, 8, 5);
I want to write a trigger that triggers on these inserts; ordering, and then grouping the rows inserted. I haven't found how to do this anywhere. It doesn't seem possible to get all of the rows being inserted as a single table on which GROUP BY and ORDER BY can be performed. Is there an efficient way to do this? If not is there a workaround that will be fast enough?
Upvotes: 1
Views: 1020
Reputation: 180162
This query cannot be sped up by much; the WHERE and ORDER BY clauses conflict and cannot use the same index; and rule 15 prevents flattening of the inner query (which might have allowed the outer query to use some index).
The documentation says:
At this time SQLite supports only FOR EACH ROW triggers, not FOR EACH STATEMENT triggers.
So it is not possible to fill the cache table automatically; you have to update the cache from your code after you've written a match_id
.
Upvotes: 2