Click here to Skip to main content
15,881,938 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
I have a database with many records from members that each member creates posts for themselves. I want to show daily, weekly, monthly and yearly created posts for each user. What is the best and most effective way to do this? best query and optimize code. Use caching system?
What data structure should I use?
My Database is SQL Server and code behind is C#


What I have tried:

optimization of code and query
Posted
Updated 25-May-19 1:19am

1 solution

You've already decided the "data structure" by deciding on a database! It is table, column, and row based, so your structure is going to be reasonably flat.

Me? I'd have a table for users, and a separate table for posts, which includes a foreign key to the users table. And the Posts table would just have a timestamp column which lets your queries retrieve user-based posts for any time period(s) you might desire.
 
Share this answer
 
Comments
Salim Hoseini 26-May-19 2:33am    
By your method, I need to get the all database for 4 queries per day, weekly, monthly and yearly, which is not optimal.
Richard Deeming 29-May-19 13:28pm    
Assuming you just want the number of posts, you can use GROUPING SETS to get the summary by year, month, and day in a single query.

Summarizing Data Using the GROUPING SETS Operator - Simple Talk[^]

SELECT
    YEAR(PostDate) AS PostYear,
    MONTH(PostDate) AS PostMonth,
    DAY(PostDate) As PostDay,
    COUNT(1) As NumberOfPosts
FROM
    UserPosts
WHERE
    UserID = @UserID
GROUP BY
    GROUPING SETS
    (
        YEAR(PostDate),
        (YEAR(PostDate), MONTH(PostDate)),
        (YEAR(PostDate), MONTH(PostDate), DAY(PostDate))
    )
;

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900