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

  • SEARCH
  • Home
  • 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 8300995
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T16:46:15+00:00 2026-06-08T16:46:15+00:00

I am creating a table that stores data, actually counter, for products for each

  • 0

I am creating a table that stores data, actually counter, for products for each week.

Example:

 id = 1
 productId = 195
 DateTime = 01/07/2012
 Counter = 0

My question to you is about database storage space, query flexibility and performance.

Instead of the DateTime column, I thought about using a SmallInt ‘WeekNumber’ column.

I will decide on the Date that the weeks start (base date). Let’s say 10/10/2012.

For each product and for each week, there will be a row that represents the total of something that I count on a daily basis (ie. Pageviews for a specific product page).

From What I’v eread:

Date column is 4 bytes

SmallInt is 2 bytes

I want to save as much space as possible, but I want to be able to query the database base on range of dates (august 2012 to September 2013), specific week in a specific year, etc.

Is this approach to the schema is good, or I will find myself having problem with poor SQL performance, Query flexibility, indexes, etc.

  • 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-08T16:46:17+00:00Added an answer on June 8, 2026 at 4:46 pm

    If this table has no child tables (no foreign keys referencing it), to conserve space, you might consider omitting the surrogate primary key (id), and instead use a composite key (productId,date_) as the primary key. (From what you describe, it sounds as you are going to want to have the combination of those columns as UNIQUE, and both of those columns as NOT NULL.

    If what you want to store is a “week” identifier rather than a DATE, there’s no problem on the database side of things, as long as your queries aren’t wrapping that column in an expression to get a DATE values to use in predicates. That is, for performance, your predicates are going to need to be on the bare “week identifier” column, e.g.

    WHERE t.product_id = 195 AND t.week_id >= 27 AND t.week_id < 40
    

    Predicates like that on the bare column will be sargable (that is, allow for an index to be used.) You do NOT want to be wrapping that week_id column in an expression to return a DATE, and use WHERE clause on that expression. (Having expressions on the literal side of the comparison is not a problem… you just don’t want them on the “table” side.

    That’s really going to be the determining factor of whether you can use a week_id in place of a DATE column.

    Using a “period id” in place of a DATE is fairly straightforward to implement for periods that are whole months. (It’s also straightforward for “days”, but is really of less benefit there.) Implementing this approach for “week” periods is more complicated, because of the handling you need for a week that is split between two years.

    Consider, for example, that the last two days of this year (2012) are on Sunday and Monday, but Tuesday thru Saturday of that same week are in 2013. You’d need to decide whether that’s two separate weeks, or whether that’s the same week.

    But the 1-byte savings (of SMALLINT vs DATE) isn’t the real benefit. What the “week_id” column gets you (as I see it) is that you have a single id value that identifies a week. Consider the date values of '2012-07-30', '2012-07-31', '2012-08-01' they all really represent the same week. So you have multiple values for the week, such that a UNIQUE constraint on (product_id,date) doesn’t really GUARANTEE (on the database side) that you don’t have more than row for the same week. (That’s not an insurmountable problem of course, you can specify that you only store a Sunday (or Monday) date value.)


    In summary,

    To conserve space, I would first drop that surrogate id column, and make the combination of the product_id and the DATE be the primary key.

    Then I would ONLY consider changing that DATE into a SMALLINT, if I could GUARANTEE that all queries would be referencing that bare SMALLINT column, and NOT referencing an expression that converts the SMALLINT column back into DATE.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm creating a VB.Net application that stores data in a SQLite backend. The data
I have a C# Application I am creating that stores all data in SQL
I'm creating a simple app that stores data into a sqlite database and retrieves
I'm creating a temp table that's the result of a stored procedure's result set.
I'm creating a custom table that has a button which allows a user to
I've trying to mess around with django-tables2 for creating a leaderboard table that allows
I am creating a table for an application that handles scheduling and deals with
I am creating a table where it has a foreign key so that it
I'm creating a table inside a repeater programmatically. My problem is that the cells
Let's say I'm creating a table foo with a column bar that should be

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.