Don’t query in a loop, I don’t see any reason to query in a loop! If you are an offline task, you don’t need to return immediately like a web request, and the amount of data is relatively large, then it is reasonable The best way is to cut the query to a certain extent and obtain dozens to hundreds of data each time (too much data will also cause slow transmission) Finally, even if you have to query in a loop, you You should also use the same connection instead of re-establishing the connection every time!
SELECT * FROM table
WHERE ...
ORDER BY ..
LIMIT 10
At this time, if there are several WHERE conditions (such as id=1, id=2), they can only be checked separately. Although GROUP BY is also possible, GROUP BY performance may be lower when the amount of data is large, and one GROUP BY query may take longer than multiple separate queries.
Don’t query in a loop, I don’t see any reason to query in a loop!
If you are an offline task, you don’t need to return immediately like a web request, and the amount of data is relatively large, then it is reasonable The best way is to cut the query to a certain extent and obtain dozens to hundreds of data each time (too much data will also cause slow transmission)
Finally, even if you have to query in a loop, you You should also use the same connection instead of re-establishing the connection every time!
Yes, for example:
At this time, if there are several
WHERE
conditions (such as id=1, id=2), they can only be checked separately.Although
GROUP BY
is also possible,GROUP BY
performance may be lower when the amount of data is large, and oneGROUP BY
query may take longer than multiple separate queries.