I have a table look like this.
parent VARCHAR PK
priority INT PK
child VARCHAR
Now I need to SELECT/COUNT, for Spring pagination, those who has children more than specified number.
Here comes my queries.
-- SELECT
SELECT
parent,
GROUP_CONCAT(child ORDER BY priority SEPARATOR 0x1D) AS concatenated_children,
COUNT(child) AS child_count
FROM mytable
GROUP BY parent
HAVING child_count >= ?1
ORDER BY parent ASC
LIMIT 2048,? -- GETTING SLOWER when the OFFSET is increases
-- COUNT
SELECT COUNT(wrk.parent)
FROM (SELECT parent, COUNT(child) AS child_count
FROM mytable
GROUP BY parent
HAVING child_count >= ?1) AS wrk
The ?1 part is a constant(8) and the the offset part(,?) is increases per pagination.
The problem is that the selection query is getting slower while the page number increases.
?size=2048,page=0 took 60 ms
?size=2048,page=100 took 2646 ms
How can I fix this?
CREATE TABLE `mytable` (
`parent` varchar(100) NOT NULL,
`child` varchar(255) NOT NULL,
`priority` int(11) NOT NULL,
PRIMARY KEY (`parent`,`priority`)
)
parent child priority
---------------------------------
NIKE Air Max 1
NIKE Air Jordan 2
WATCH Patek Philippe 1
WATCH Rolex 2
Criteria
Select all parents (and its children)
which each has at least some number children, say 8.