Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8227733
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T16:13:35+00:00 2026-06-07T16:13:35+00:00

I want to store data in MySQL and query it based on the current

  • 0

I want to store data in MySQL and query it based on the current day. I want to know what is the best practice to do so.

I want to store data totals for each day, so queries total data will be quick. I thought about modeling my table as follows:

TotalsByCountry
- Year
- Month
- Day
- countryId
- totalNumber

When I query the totals for a specific day and for specific country, I will query the table based on 4 columns, the Year, Month, Day and countryId.

I wanted to know if this is a good practice, or there is a better way to do so, like using one columns for data that holds the month, day and year, and query only two columns, the datetime columns and the coutryId.

need you help in choosing the right way to model the table. I also want to make another table that store totals based on gender, so take that into consideration too.

The data will need to be accessed frequently, maybe in real time because I want to show the data changes in real time. I will be developing the web app in asp.net and probably use web sockets to create constant connection that will update the data on the user in real time. So when data changes, it will be reflected on the user webpage in real-time. That’s why I need a table modeling that will be ready for many queries. I will use caching for a few seconds so it want stress the db too much.

I hope I provided enough information, if not, please comment and I will reply.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T16:13:38+00:00Added an answer on June 7, 2026 at 4:13 pm

    Having three separate columns to store each individual element of a date (year/month/day) will add unnecessary overhead to your database in terms of insert performance and disk space.

    What you will want to do is simply have a single DATETIME column to store the date and time, and have a composite index set up on (countryId, datetime_col).

    Even if you wanted to query all rows based on a specific day or month, MySQL will still be able to utilize indexes on the DATETIME field, provided that you are writing your queries in the right way and making sure to never to wrap the DATETIME column within a function when you perform your conditional check.

    Here is how you can write your query so that it will still be able to utilize indexes:

    -- Get the sum of totalNumber of all rows based on current day
    -- where countryId = 1
    
    SELECT SUM(totalNumber) AS totalsum
    FROM   tbl
    WHERE  countryId = 1 AND
           datetime_col >= CAST(CURDATE() AS DATETIME) AND
           datetime_col <  CAST(CURDATE() + INTERVAL 1 DAY AS DATETIME)
    

    By making the comparison on the bare DATETIME column, the query remains sargable(i.e. able to utilize index range scans) and MySQL will be able to use indexes to quickly look up rows.

    On the other hand, if you were to try to wrap the DATETIME column within a function to make the comparison:

    -- Get the sum of totalNumber of all rows based on current day
    -- where countryId = 1
    
    SELECT SUM(totalNumber) AS totalsum
    FROM   tbl
    WHERE  countryId = 1 AND
           DATE(datetime_col) = CURDATE()
    

    …It would be quite inefficient because the DATE() function that wraps the column effectively renders the query as non-sargable, and any kind of index you have set up containing the DATETIME column will not be utilized.

    You can also efficiently query for the total sum of all rows in the current month:

    -- Get the sum of totalNumber of all rows based on current month
    -- where countryId = 1
    
    SELECT SUM(totalNumber) AS monthsum
    FROM   tbl
    WHERE  countryId = 1 AND
           datetime_col >= CAST(CONCAT(YEAR(NOW()), '-', MONTH(NOW()), '-01') AS DATETIME) AND
           datetime_col <  CAST(CONCAT(YEAR(NOW()), '-', MONTH(NOW()), '-01') AS DATETIME) + INTERVAL 1 MONTH
    

    And within the current year:

    -- Get the sum of totalNumber of all rows based on current year
    -- where countryId = 1
    
    SELECT SUM(totalNumber) AS yearsum
    FROM   tbl
    WHERE  countryId = 1 AND
           datetime_col >= CAST(CONCAT(YEAR(NOW()), '-01-01') AS DATETIME) AND
           datetime_col <  CAST(CONCAT(YEAR(NOW()), '-01-01') AS DATETIME) + INTERVAL 1 YEAR
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to store data in string form to MySQL. I have created the
I store some data in MySQL and want to filter out rows that match
I want to store mysql query in zend registry because i have one action
i am using mysql to store geolocation data. i would like to know how
I want to store the data and time in my MYSQL db.I have a
I want to store data with every request (what user viewed what page of
I am a new developer on Android and I want store user data in
I want to store a data structure thats a table with one column Strings
I want to store some data during my site viewing. Sometime i need to
hi i want to store textfield data in a variable but my code is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.