开发者

When is it too late to optimize for performance?

开发者 https://www.devze.com 2023-02-08 13:56 出处:网络
I know that you shouldnt optimize too early, and you should instead aim for maintainability. My question is, at what point is it too late?

I know that you shouldnt optimize too early, and you should instead aim for maintainability. My question is, at what point is it too late?

I'm working on a website, similar to yahoo answers, and my database structure is exactly what I feel it should be. Table for users, questions, 开发者_运维知识库answers, question_comments, answer_comments, etc.

My question is, IF the site were to grow, how would this architecture scale? I'm thinking of putting both questions and answers in a single table (posts), separating them by type, and then putting both question_comments and answer_comments in the same table (comments). I believe this is similar to stackoverflow's DB scheme.

I know what you guys are gonna say, "Dont worry about it until it becomes an actual problem". But wouldn't it be a little too late to worry about it then?

Thanks


The reason why it's a bad practice to optimize early is you don't know where your bottlenecks will be until your website sees a significant amount of traffic. How your users access and interact with your site is an unknown at this point.

It's almost always best to start with a 'good' architecture (normalized database, MVC architecture, DRY, well-written frontend code, etc) and go from there. It will be much easier to scale a clean, organized architecture than one that was prematurely optimized.

At best right now you can do some load testing via ab or another load testing tool to see where your current bottlenecks are. It certainly won't find all of them, but it will find some.

If you're really worried about this (and you shouldn't be yet), install Nagios or Munin on your server to monitor performance. Use a third party tool to measure page load time daily. Once you start seeing issues then you can profile and tune.


You absolutely should optimize if a fast service is a fundamental requirement of the application.

If sub-second responses are not a requirement, than you can write clean code and optimize later.

A good example of this was JavaScript before the latest version of browsers, people who wrote nice, clean, extensible JS for their pages had terrible performance and had to start from scratch.


One huge table is generally harder to maintain. People usually cut their tables into partitions and even their databases into shards.

I don't see how putting all comments into the same table would save you a join. Really, putting questions and answers into the same table won't save you a join either, you'll just be joining by the same table.

If you want to save on joins, I'd expect you use a document-oriented NoSQL database, such as MongoDB. That's where you can store a question with all related answers and comments in a single 'record', fetchable with one operation.


Databases need to be designed with performance in mind not wait until you havea problem later. Premature optimization doesn't mean don't do it in design, it means don't get ridiculously excessive about it. However, there are known performance killers for every database backend and it is foolish to design to use one of those when a differnt technique will be faster and take the same amount of time to write code for if you are familar with it. So before designing any database, read up on performance tuning and you will never write database code the same way again.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号