English 中文(简体)
说明数据库中某些事件的发生次数
原标题:Counting the number of occurances of something in the database
  • 时间:2011-09-02 14:56:11
  •  标签:
  • sql-server

就我的网站而言,我想做一些像在Stackoverflow的标签那样做的工作,因此有些领域将拥有一个汽车完成器,汽车完成器将显示其他用户选择每项建议价值的次数。 我称我拥有这样一个数据库结构:

Articles
    ArticleID
    Content
    TagId

Tags
    TagId
    TagName
    Occurances

设想是,Occurances代表每个TagId的次数从条/代码表上查阅。

如何以最佳方式执行? 我可在<条码>上添加/替换。 更新<代码>第表格的每个储存程序栏目,但我可能错开一栏,如果用户将一个标签从某种东西中删除,则会有一些困难(因为用户很容易在新添加的标签的现场添加1个标记,但更难去做正更换的标签)。

很多东西我不了解serv-服务器。 是否有更强有力的计算方法,例如数据库系统将自行处理? 如果数据有一天或某个时间,那将是ok的。

最佳回答

你们可以利用这一问询来获得点数:

SELECT Tags.TagId, COUNT(Articles.TagId) as Occurances
  FROM Articles
  JOIN Tags ON Articles.TagId
  GROUP BY Tags.TagId

它可以用于一种观点或储存程序,而且你可以设立网站,按需要经常加以更新。

问题回答

为了能够有一个以上标的,你必须增加另一个表格,把本表连接到tag。 它称作许多关系。

article
  article_id
  content

article_tag
  article_id
  tag_id

tag
  tag_id
  tagname

Doing like this, article 1 can be attached to tag 2, and the next row can be 1 and 3 and so on, so one article points to many tags. To count a certain tag, you join the Article_Tag and Tag tables, and and count the rows in Article_Tag where Tag.tagname = mysql , for examle.

您可以形成一种指数观点,即把你需要的所有计数汇总起来,并自动保持:

create view TagCounts 
with schemabinding
as select TagId, count_big(*) as Occurances
from dbo.ArticleTags
group by TagId;
go

create unique clustered index cdxTagCounts on TagCounts (TagId);
go

Now the TagCounts.Occurances field is automatically maintained by SQL Server whenever you insert/delete/update the Articles table. You can query it like:

select Occurances from dbo.TagCounts with (noexpand) where TagId = ...;

And you can cache the result with LinqToCache, as such a query matches the restrictions of Query Notifications.

The trade off of using a pre-aggregated indexed view is scalability: as update of any article updates the count of Occurances for the tags of the article, an exclusive lock is required to update this count. Which implies that only one transaction can use a TagId at any moment. Depending on your traffic and on other elements of your design this restriction may or may not be acceptable.

The other alternative is a table of counts. Front ends (your ASP.Net farm) read this counts and then they update the in-memory count for each operation, keeping track of the delta from the counts in the table. Periodically the front ends merge their deltas into the table (eg. every 5 minutes) and refresh the in-memory table. This way front ends see a stale version of the truth, but an user sees immediate feedback of its actions: because of session stickiness his HTTP requests are processed by the same front end, and thus he see immediately his own article updates triggering modifications to the tag counts. User though do no immediately see the updates from other users that are load-balanced to another front end. Because a crash of the front end (or a process recycle...) will loose the deltas kept so far, the count table will drift in time away from the truth and would have to be periodically updated to the true count in the database.

如果您的准确性更高(所有用户都立即看到真实数字),那么你就能够做一些基于快速的内层关键价值储存的产品,这些储存与我的第一个建议基本相同,但通过量/排位相对性,或许是以

如果你使用一个关系数据库,处理这一问题的正确方式是,NOT储存表上的事件,而是动态地询问文章表上的事件数目。

如果你不这样做,你就会每增加一个字句,......一般不说什么。 如果你生动地询问,你在桌旁打上了字栏,而是会把这一信息列入你的说法。

使用:

SELECT COUNT(*) FROM ARTICLES WHERE TagId =  xxx  ;

这条路线是循环守则的一部分。





相关问题
Export tables from SQL Server to be imported to Oracle 10g

I m trying to export some tables from SQL Server 2005 and then create those tables and populate them in Oracle. I have about 10 tables, varying from 4 columns up to 25. I m not using any constraints/...

SQL server: Can NT accounts be mapped to SQL server accounts

In our database we have an SQL server account that has the correct roles to access some of the databases. We are now switching to windows authentication and I was wondering if we can create a NT user ...

SQL Server 2000, ADO 2.8, VB6

How to determine if a Transaction is active i.e. before issuing Begin Transaction I want to ensure that no previous transaction are open.. the platform is VB6, MS-SQL Server 2000 and ADO 2.8