Calculation of the number of comments / posts

I am using ASP.net and an SQL database. I have a blog, such as a system, in which several comments are made to the message, and I want to show the number of these comments next to the message. To get this number, I could either keep it in the record, or add / subtract when adding or removing a comment, or I could use SQL to calculate the number of comments using a query every time a user lands on a page. The latter seems like a bad idea, as it is about to hit my SQL database, however, by holding the number against the record, it looks like it might be error prone. What do you think, in this case, the best use of coding?

+3
source share
9 answers

Always start with a normalized database (second option). Only denormalize if you have an absolute need for performance considerations. Designing it in a denormalized form (which is error prone, you guessed it) is a premature optimization. With the right indexes, this should be an accurate calculation of the number on the fly.

+3
source

I think the SQL statement should be fine. Another is duplicating the data that you already have. Request an invoice to be quick.

+2
source

. pagefault , .

+2

, . , , ( ). , .

+1

, , -. , , , .

+1

, , , - , "" , .

, 50 . .

, , .

, , ajax .

3 50 , 47 !

, . .

+1

, ... , , , , , IMHO

0

/ SQL, , db..

0

. , . , . sku SS2K5, (noexpand), ( ). sku , , , .

-, , , ;)

create view postCount__
as
select
    threadId
    ,postCount=count_big(*)
from thread
group by threadId
go
create unique clustered index postCount__xpk_threadid on postCount__(threadId)
go
create view postCount
as
select
    threadId
    ,postCount=cast(postCount as int)
from postCount__ with (noexpand)
go

, , . , noexpand. count_big, int , asp.net 32- int. , .

EDIT. I can say that forum software always denormalizes the number of posts in the thread table. It kills the database to constantly count the number of posts on each pageview, if you have an active forum. I like that mssql has indexed views, so you can describe denormalization declaratively rather than supporting it yourself.

0
source

Source: https://habr.com/ru/post/1707503/


All Articles