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 8628763
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T08:37:53+00:00 2026-06-12T08:37:53+00:00

Let’s take a senario where user is tracking traffic for certain cities . The

  • 0

Let’s take a senario where user is tracking traffic for certain cities. The traffic is updated every two hours and we’ve to keep previous data to plot graph. So I’ve a traffic_stats table which looks like this –

traffic_stats(id,city_id,user_id,traffic,created_at)

(given traffic is a number)

There is a stats refresher daemon which takes the unique city_ids, gets current traffic stats for these cities and adds new entry to this table itself. The daemon uses this query to fetch city_id –

SELECT * FROM traffic_stats GROUP BY city_id

and adds new entry for each city_id in the same table. The user_id attribute for each new entry is 0 since it doesn’t matter which user has subscribed for the city. If the city_id is in the table, it’s traffic_stats is refreshed.

On the front end, following query is run to fetch data for user –

SELECT * FROM 
(SELECT * FROM traffic_stats WHERE user_id = #{session[:user_id]} ORDER BY created_at DESC)
as traffic_for_user_in_descending_order 
GROUP BY city_id

This gives single latest entry for a city_id.

This should work fine except for the fact that if 100 users are tracking 200 unique cities, there will be 200 new entry in the traffic stats table every two hours. That’s
2400 entries a day and the table will keep growing.

Now, I could have had one table which has data about the cities that users are tracking and another table that the refresher daemon adds entry to. But I’m not sure if there’s any performance advantage to this approach.

  • 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-12T08:37:54+00:00Added an answer on June 12, 2026 at 8:37 am

    It might be better to create a separate City table, that way you can query the distinct city ids from that rather than scanning the whole table in the first select statement. It would also make reading the database a bit easier. If you’d rather not do this, I would suggest using SELECT DISTINCT city_id FROM traffic_stats. This way you will be retrieving less information.

    Having a single table seems reasonable in this case, as the application you are using the information for is simple. As for historical data, it might be nice to create a separate table to store aggregated information. You could prune the primary table, selecting and storing averages for a particular length of time (day, week, month, etc.), and then filter even more by basing information off of the user id. This would cut down on the database disk usage and query time.

    Personally I like to break things out as much as possible. It does make for more complicated queries, but it makes using and reading information from a database much easier in my opinion.

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

Sidebar

Related Questions

Let's say I have a method in java, which looks up a user in
Let's say I have two text files that I need to extract data out
Let's say we have two pages in Orchard CMS. The homepage and the About
Let's say I'm making a tool to help epicureans keep track of which delicacies
Let's assume that a user votes for some movies in a scale of 1
Let assume we have two activities. A - main activity, that is home launcher
Let's say I have two tables orgs and states orgs is (o_ID, state_abbr) and
Let's assume that we are building a high traffic site that will be used
Let's say I have an facebook application running using the JS SDK. First user
Let's say I have a user enter the URL of an image. After URL

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.