StackOverflow如何优化问题显示的性能?
我正在尝试学习 C#.net 来编写网络应用程序。
在了解到 stackoverflow 使用 C#.net 后,我很高兴发现它。
每当我刷新页面时,我都会在主页或问题部分注意到这一点。 该页面总是以可接受的速度向我返回最新信息。
我不知道你是怎么做到的。 抱歉问了这么长的一系列问题。 我正在尝试了解数据检索、分页、性能等的最佳实践是什么,
我知道主页仅返回有限数量的问题及其统计数据,但问题部分实际上返回所有内容。
你如何优化它?
对于主页,您是否总是获取最近问题的所有统计数据? 所以您的查询类似于“select * from questions order by datetime_created limit 20”?
所以 * 包含所有信息,包括问题标题、ID、视图等?
您是否使用 HttpContext.Current.Server.cache 来帮助解决此问题?
对于问题来说,这更耐人寻味。
如何进行分页?
您是否总是从数据库中仅获取特定页面的结果?
或者您是否获取所有结果并将其存储到数据集中? 然后你使用某种数据网格控件来帮助分页?
如果是后者,如何维护数据更新呢?
I am trying to learn C#.net to program a web app.
And having learned that stackoverflow uses C#.net I am happy to discover it.
I noticed that at the home page or at the questions section, whenever I refresh the page. The page always returns me the latest information without fail and at acceptable speeds.
I am not sure how do you do it. Sorry for the long series of questions. I am trying to learn what is the best practices for data retrieval, paging, performance , etc
I know that the homepage only returns a limited number of questions and their stats but the questions section actually returns everything.
How do you optimise it?
For the homepage, do you always grab ALL the stats of the recent questions? so your query is something like "select * from questions order by datetime_created limit 20" ?
So the * contains ALL the info including question title, id, views, etc?
Do you use HttpContext.Current.Server.cache to help with that?
For the questions, this is even more intriguing.
How do you do the paging?
Do you always grab from the database only the results for the specific page?
Or do you grab all the results and store it into a dataset? Then you use some kind of datagrid control to help with the paging?
If it is the latter, how do you maintain the data to be updated?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
在 Stack Overflow 上,我们尝试在多个级别上使用积极的缓存:
HttpRuntime.Cache
用于此主页 由三个缓存的 html 片段组成 - 最近的问题、最近的标签、最近的徽章 - 每个都有不同的持续时间。
问题列表页面将缓存特定排序/标签过滤器的所有问题的 ID (
Int32[]
),从而使分页变得简单。 还完成了对统计数据(例如问题计数、相关标签计数)的进一步缓存。问题详细信息页面将为匿名用户完全缓存,而注册用户则可以看到最新的商品。 此外,侧面的相关问题会在磁盘上缓存更长的时间。
虽然我们尝试尽可能缓存整个页面,但我们确实在页面顶部显示用户信息 - 有些部分无法缓存。
因此,像拼图一样看待缓存 - 哪些部分可以在我的所有请求之间安全共享? 根据费用,哪些部分必须在我的所有请求中共享?
Here on Stack Overflow, we try to use aggressive caching on many levels:
HttpRuntime.Cache
is used for thisThe home page is made up of three cached html pieces - recent questions, recent tags, recent badges - each with a different duration.
A questions list page will cache the ids (
Int32[]
) of all questions for a particular sort/tag filter, making paging trivial. Further caching on the stats (e.g. question count, related tag counts) is done, as well.A question detail page will be entirely cached for anonymous users, while registered users see the latest goods. Also, the related questions on the side are cached to disk for a longer duration.
While we try to cache entire pages wherever possible, we do show user information at the page top - some parts just cannot be cached.
So look at caching like a puzzle - what parts can be safely shared between all my requests? Based on expense, what parts MUST be shared across all my requests?
我不知道他们是怎么做到的 - 我没有这么写。
对于这样的事情,我会为整个 Question 类使用某种缓存机制它的所有答案。 缓存的寿命是短暂的,但由于新的/热门的问题经常被查看,因此它们会保持活动状态。 较旧的问题必须向数据库请求。 当一个人回答问题而另一个线程查看问题时,这也可以防止出现线程问题。
您可以在这里注意到的另一件事是,他们大量使用 AJAX。 但由于 AJAX.Net 非常浪费带宽,因此他们实现了 AJAX 调用,因此它们会返回简单的 JSON 对象,例如,仅对具有新票数的成功对象进行投票或返回错误消息时,例如:(这是一个虚构的示例,并不代表所发生的情况,因为我现在懒得去检查)
AJAX.Net 将返回整个
UpdatePanel 的内容,无论其大小如何,无论多小,仍然会很大。
I have no idea how they did it - I didn't write SO.
For something like this, I'd use some sort of caching mechanism for the entire Question class with all of its Answers. The cache would be short-lived, but since new/hot questions are viewed very often they would stay alive. Older questions would have to be requested from the DB. This would also prevent threading problem when one person answers the question and while another thread views the question.
Another thing you can notice here, is they make heavy use of AJAX. But since AJAX.Net is extremely bandwidth-wasteful, they implemented the AJAX calls so they would return simple JSON objects e.g. when upvoting only a success object with a new number of votes or an error message is return, for example: (this is a made-up example and is not representative of what happens because I can't be bothered to check right now)
AJAX.Net would return the entire
UpdatePanel
's contents, whatever its size, which no matter how small, would still be quite large.SO 使用 MVC 和 LINQ2SQL。 我会听一些播客来更多地了解具体细节。 我确实知道他们使用了大量缓存,但不确定这是否包括主页上的问题列表。
SO uses MVC and LINQ2SQL. I'd listen to some of the podcasts to get more of an idea of the specifics. I do know they use a lot of caching but not sure if that includes the question list on the home page.
您可能还想看看Stack Overflow 启发的知识交流。
这是一组试图模仿 Stack Overflow 的文章。 您应该能够在 CodePlex 的此处找到代码库。
You may also want to take a look at this Stack Overflow Inspired Knowledge Exchange.
It's a group of articles trying to emulate Stack Overflow. You should be able to find the codebase here at CodePlex.