简体   繁体   English

COUNT(*)vs手动跟踪计数器?

[英]COUNT(*) vs manual tracking of counters?

I have a table with approx. 我有一张大约一张桌子。 70000 entries. 70000个条目。 It holds information about brands, models and categories of goods. 它包含有关商品的品牌,型号和类别的信息。 The user can query them using any combination of those, and the displayed counter of goods matching the criteria has to be updated according to his selection. 用户可以使用它们的任意组合来查询它们,并且必须根据他的选择更新所显示的符合标准的商品的计数器。

I have it done using a query like 我使用像这样的查询完成它

SELECT model,COUNT(*) AS count FROM table$model_where 
          GROUP BY model
          ORDER BY count DESC

where $model_where depends on what the other conditions were. 其中$model_where取决于其他条件。 But my boss asked me to redo these queries into using a special counter table, because he believes they are slowing the whole process down, but a benchmark I put suggests otherwise, sample output: 但是我的老板让我重新使用一个特殊的计数器来重新调查这些查询,因为他认为他们正在减慢整个过程,但我提出的基准测试表明,样本输出:

The code took: 0 wallclock secs (0.02 usr + 0.00 sys = 0.02 CPU)

and it measures the whole routine from the start and until the data is send to the user, you can see it's really fast. 它从一开始就测量整个例程 ,直到数据发送给用户,你可以看到它真的很快。

I have done some research on this matter, but I still haven't seen a definitive answer as to when to use COUNT(*) vs counter tables. 我已就此问题做了一些研究,但我还没有看到何时使用COUNT(*) vs counter table的明确答案。 Who is right? 谁是对的? I'm not persuaded we actually need manual tracking of this, but maybe I just know little. 我不相信我们实际上需要手动跟踪这个,但也许我只是知之甚少。

Depending on your specific case, this might, or might not be a case of premature optimization . 根据您的具体情况,这可能是,也可能不是过早优化的情况。

If next week you'll have 100x bigger tables, it might not be the case, but otherwise it is. 如果下周你会有100倍大表,可能不是这样,但不然如此。

Also, your boss should take into consideration that you and everybody else will have to make sure that counters are updated whenever an INSERT or DELETE happens on the counted records. 此外,您的老板应该考虑到您和其他所有人必须确保只要在计数记录上发生INSERTDELETE时计数器就会更新。 There are frameworks which do that automatically (ruby on rails's ActiveRecord comes to mind), but if you're not using one of them, there are about a gazillion ways you can end up with wrong counters in the DB 有一些框架可以自动完成(ruby on rails的ActiveRecord浮现在脑海中),但是如果你没有使用其中的一个,那么你可以在数据库中找到错误的计数器。

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

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