[英]What are the full implications of not using the default 'id' primary_key in your Django model?
[英]Split a (slow) mysql query in chunks of many primary_key ranges (id 1-100, id 101-200, ...)
Using mariadb version 10.5.15, Flask 2.1.2, Flask-Session 0.2.0 and python 3.9
My problem is a very slow query of my database table. Especially when the result of the query contains many rows, it takes too long.
Now I have the idea to split every query up in many queries, by just including an extra "WHERE" part like this:
My question is if there is a better way or if this attempt brings other problems (or not)
The result of the query will afterwards be an answer to a POST request. And because I'm using sessions, I would continuously push results of each chunked query in a session variable "search_query: list".
While this search_query gets filled I check if there are enough results (or if the query is done the query is done) I sent a first chunk as response.
My guess:
This is the query:
WITH pf
AS (
SELECT short_description, long_description, count, unit, cost_characteristic, size, family_id, is_parent
FROM positions
INNER JOIN project_crafts
USING (project_craft_id)
INNER JOIN projects
USING (project_id)
WHERE ---dynamic WHERE conditions---
)
SELECT positions.short_description, positions.long_description, positions.count, positions.unit, positions.cost_characteristic, project_crafts.size, positions.family_id, positions.is_parent
FROM positions
INNER JOIN pf
ON positions.family_id=pf.family_id
AND COALESCE(positions.is_parent, -1) <> COALESCE(pf.is_parent, -1)
INNER JOIN project_crafts
USING (project_craft_id)
UNION SELECT short_description, long_description, count, unit, cost_characteristic, size, family_id, is_parent
FROM pf
The query is slow, when ---dynamic WHERE conditions--- is empty. Then the query returns every single row and the JOINs get expensive (I think).
让我们看看查询的 rest... 你在取什么列; WHERE 里还有更多的东西吗?
SELECT CEILING(pk/100) * 100 AS 'through', COUNT(*) AS 'how many', SUM(foo) FROM... WHERE... GROUP BY CEILING(pk/100) ORDER BY CEILING(pk/100)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.