mysql - 循环中逐个查询数据库和联表一次性查询数据库分别适用于哪种场景?
伊谢尔伦
伊谢尔伦 2017-04-17 15:00:31
0
2
854

一般来说循环中逐条查询的效率是比较低的,每次都产生连接以及并发的开销。
是否有些场景是适合在循环中逐个去查询,而不适合一次性查表的场景呢?

伊谢尔伦
伊谢尔伦

小伙看你根骨奇佳,潜力无限,来学PHP伐。

reply all(2)
阿神

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:

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.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!