英文:
Is it good design to use channels for concurrent database calls?
问题
我正在开发一个使用Go语言编写的Web应用程序,其中包含一个统计页面。该页面包含多个图表,这些图表使用来自数据库的数据支持。目前,数据库的多个调用是按顺序进行的。
在这种情况下,使用通道来实现并行调用数据库并可能提高性能是一个好的选择吗?无论性能是否提升,对于这种情况使用通道是否被认为是良好的设计?
英文:
I am working on a web application (written in go) with a statistics page. This page contains several graphs that is backed up with data from the database. There are several calls to the database made sequentially as it stands now.
Is this a good place to use channels in order to get parallel calls to the database and possibly boost performance? Regardless of the performance boost - is it considered good design to use channels for a case like this?
答案1
得分: 5
通道本身并不能提高性能。实际上,通道操作比函数调用要昂贵得多。
你应该问自己的问题是:“我是否应该同时调用数据库?”。
如果答案是“是”,那么你下一步应该决定如何构建数据库客户端代码以实现并发调用。Goroutines肯定会发挥作用,而通道很可能也会很有用,用于将结果数据发送到需要的位置。
英文:
Channels don't by themselves boost performance. In fact channel operations are quite a bit more expensive than function calls.
What you should be asking yourself is "Should I be making concurrent calls to the database?".
If the answer to that is "yes", then your next step should be deciding
how to structure your database client code to enable that.
Goroutines will certainly feature, and channels
are highly likely to be useful too, in order to send the resulting
data to where it is needed.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论