计算项目或增加数字?

发布于 2024-11-27 00:32:14 字数 202 浏览 1 评论 0原文

对于比我更有经验的人来说,简单地计算表中的项目数量(例如计算类别中的主题数量)或保留一个保存该值的变量并仅递增和调用会是一个更好的主意吗?它(类别表中的额外字段)?

两者之间是否存在显着差异,或者只是非常轻微,即使差异很小,一种方法仍然比另一种方法更好吗?它不适用于任何一个特定项目,因此请笼统地回答(如果有意义的话),而不是基于用户数量之类的问题。

谢谢。

From someone with more experience than myself, would it be a better idea to simply count the number of items in a table (such as counting the number of topics in a category) or to keep a variable that holds that value and just increment and call it (an extra field in the category table)?

Is there a significant difference between the two or is it just very slight, and even if it is slight, would one method still be better than the other? It's not for any one particular project, so please answer generally (if that makes sense) rather than based on something like the number of users.

Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

不如归去 2024-12-04 00:32:14

要获取项目数(表中的行),您可以使用标准 SQL 并按需执行。

SELECT COUNT(*) FROM MyTable

注意,以防万一我遗漏了某些内容,表中的每个项目(行)都有一些唯一标识符,无论它是零件号、某些代码或自动增量。因此添加新行可能会触发列的“自动增量”。

这与“计数行”无关。由于 DELETE 或 ROLLBACK,数字可能不连续。

尝试单独维护行数将会以眼泪和/或灾难告终。尝试使用 COUNT(*)+1 或 MAX(id)+1 生成新的行标识符更糟糕

To get the number of items (rows in a table), you'd use standard SQL and do it on demand

SELECT COUNT(*) FROM MyTable

Note, in case I've missed something, each item (row) in the table has some unique identifier, whether it's a part number, some code, or an auto-increment. So adding a new row could trigger the "auto-increment" of a column.

This is unrelated to "counting rows". Because of DELETEs or ROLLBACK, numbers may not be contiguous.

Trying to maintain row counts separately will end in tears and/or disaster. Trying to use COUNT(*)+1 or MAX(id)+1 to generate a new row identifier is even worse

回忆躺在深渊里 2024-12-04 00:32:14

我认为你的问题有些混乱。我的解释是您是否想要执行 select count(*) 还是跟踪实际计数的列。

如果您没有理由这样做,我不会添加这样的专栏。这是不成熟的优化,会使软件设计变得复杂。

此外,您还希望避免将相同的信息存储在不同的位置。计数是一项微不足道的任务,因此您实际上重复了信息,这是一个坏主意。

I think there is some confusion about your question. My interpretation is whether you want to do a select count(*) or a column where you track your actual count.

I would not add such a column, if you don't have reasons to do so. This is premature optimization and you complicate your software design.

Also, you want to avoid having the same information stored in different places. Counting is a trivial task, so you actually duplicating information, which is a bad idea.

不忘初心 2024-12-04 00:32:14

我只想数数。如果您发现性能问题,您可以考虑其他选项,但是一旦保留单独的值,您就必须做一些工作以确保它始终正确。可以这么说,使用 COUNT() 您总是可以“直接从马嘴里”获得实际数字。

基本上,除非必要,否则不要开始优化。如果使用 COUNT() 一切正常且快速,那么就这样做。否则,将计数存储在某处,而不是通过添加/减去来更新存储的值,而是在需要时运行 COUNT() 来获取新的项目数

I'd go with just counting. If you notice a performance issue, you can consider other options, but as soon as you keep a value that's separate, you have to do some work to make sure it's always correct. Using COUNT() you always get the actual number "straight from the horse's mouth" so to speak.

Basically, don't start optimizing until you have to. If everything works fine and fast using COUNT(), then do that. Otherwise, store the count somewhere, but rather than adding/subtracting to update the stored value, run COUNT() when needed to get the new number of items

海螺姑娘 2024-12-04 00:32:14

在我的论坛中,我像这样计算论坛中的子线程:

SELECT COUNT(forumid) AS count FROM forumtable

只要您使用与指定哪个论坛和/或子部分相同的标识符,并且该列有一个索引键,它就非常快。因此没有理由添加超出您需要的列。

In my forum I count the sub-threads in a forum like this:

SELECT COUNT(forumid) AS count FROM forumtable

As long as you're using an identifier that is the same to specify what forum and/or sub-section, and the column has an index key, it's very fast. So there's no reason to add more columns than you need to.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文