一般来说循环中逐条查询的效率是比较低的,每次都产生连接以及并发的开销。
是否有些场景是适合在循环中逐个去查询,而不适合一次性查表的场景呢?
阿神2017-04-17 15:02:31
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!
黄舟2017-04-17 15:02:31
Yes, for example:
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.