简体   繁体   English

最好的mysql方法?

[英]best mysql approach?

I'm writing an application that displays posts, much like a simple forum system. 我正在编写一个显示帖子的应用程序,很像一个简单的论坛系统。 Posts can be marked as urgent by users when they submit the post, but a moderator must approve the "urgent" status. 用户可以在提交帖子时将其标记为紧急,但是主持人必须批准“紧急”状态。 A post will still be displayed even if it has not been approved as urgent, but will just appear to be a normal post until the moderator approves the urgent status at which point the post will get special treatment. 即使尚未被批准为紧急,该帖子仍会显示,但在主持人批准紧急状态之前,该帖子似乎是正常的帖子,届时该帖子将受到特殊对待。

I have considered two approaches for this: 我已经考虑了两种方法:

1) have two flags in the posts tables. 1)在posts表中有两个标志。 One to say the user has requested urgent status, and a second to indicate if admin has approved urgent status. 一说用户已请求紧急状态,一说用户是否已批准紧急状态。 Only if both are true will the post be show as being urgent. 仅当两者都为真时,该帖子才会显示为紧急。

2) have two tables. 2)有两个表。 A requests pending table which holds all the pending urgent approvals. 请求待处理表,其中包含所有待处理的紧急批准。 Once admin approves urgent status, I would delete the request from the pending table and update the posts table so that the urgent field becomes true for that post. 一旦管理员批准了紧急状态,我将从待处理表中删除该请求并更新帖子表,以便该帖子的紧急字段变为true。

I'm not sure if either approach is better than the other. 我不确定哪种方法是否比另一种更好。

The first solution means I only have one table to worry about, but it ends up with more fields in it. 第一个解决方案意味着我只需要担心一个表,但是最后它包含更多字段。 Not sure if this actually makes querying any slower or not, considering that the posts table will be the most queried table in the app. 考虑到posts表将是应用程序中查询最多的表,因此不确定是否确实使查询速度变慢。

The second solution keeps the posts table leaner but ads another table to deal with (not that this is hard). 第二种解决方案使posts表保持精简,但在另一个表上进行广告处理(这并不难)。

I'm leaning towards the second solution, but wonder if I'm not over analysing hings and making my life more complicated than it needs to be. 我倾向于第二种解决方案,但想知道我是否没有过度分析铰链并使我的生活变得比原来复杂的多。 Advice? 建议吗?

There's another solution that comes in mind :) 还有另外一个解决方案:)

You can have a table with post statuses and in your Posts table you will have a columnt which references a status.. 您可以有一个带有职位状态的表,在您的职位表中,您将有一个引用状态的列。

This apporach has several advantages - like you can seamlessly add more statuses in the future.. or you can even have another table holding rules how statuses can be changed (workflow). 此方法具有多个优点-例如您将来可以无缝添加更多状态。或者甚至可以包含另一个表,该规则包含如何更改状态(工作流)的规则。

Definitely 1). 绝对是1)。 The additional table just messes things up. 附加表只是搞砸了。 One extra status field is enough, with values: 0=normal, 1=urgent_requested, 2=urgent_approved for example. 一个额外的状态字段就足够了,例如:0 =正常,1 =紧急请求,2 =紧急批准。

You could query with status=1 for queries needing approval, and if you order by status desc, you naturally get the urgent messages up front. 您可以使用status = 1查询需要批准的查询,如果按status desc进行订购,您自然会提前收到紧急消息。

The second approach is cleanest in terms of design, and it will probably end up using less disk space. 第二种方法在设计方面是最干净的,最终可能会使用更少的磁盘空间。 The first approach is less "pure", but from a maintenance and coding point of view it's simpler; 第一种方法不太“纯粹”,但是从维护和编码的角度来看,它更简单。 hence I'd go with this approach. 因此,我会采用这种方法。

Also, it's great to see someone thinking about design before they go off and write reams of code. 另外,很高兴看到有人在开始编写代码之前就开始思考设计。 :) Can't tell you how many messed-up projects I've seen, where a single hour of thinking about the design would've saved many hours of effort for all involved... :)无法告诉您我看到了多少个混乱的项目,一个小时的设计思考就可以为所有相关人员节省很多时间...

I think option 1 is the best. 我认为选项1是最好的。 The only thing you need to do is make an index with the two fields. 您唯一需要做的就是使用两个字段建立索引。 The option 2 add too much complexity 选项2增加了太多的复杂性

I have a mysql query just for things like this. 我有这样的mysql查询。 I will post it as soon as I remember/find the correct syntax 我会记住/找到正确的语法后立即发布

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM