Note that there are some explanatory texts on larger screens.

plurals
  1. POShould I have less tables and use complex queries to fetch data or have more tables to simplify queries?
    primarykey
    data
    text
    <p>Let's take a senario where user is tracking <code>traffic</code> for certain <code>cities</code>. The traffic is updated every two hours and we've to keep previous data to plot graph. So I've a <code>traffic_stats</code> table which looks like this -</p> <pre><code>traffic_stats(id,city_id,user_id,traffic,created_at) </code></pre> <p>(given traffic is a number)</p> <p>There is a stats refresher daemon which takes the <strong>unique</strong> <code>city_id</code>s, gets current traffic stats for these cities and adds new entry to this table itself. The daemon uses this query to fetch <code>city_id</code> -</p> <pre><code>SELECT * FROM traffic_stats GROUP BY city_id </code></pre> <p>and adds new entry for each <code>city_id</code> in the same table. The <code>user_id</code> attribute for each new entry is 0 since it doesn't matter which user has subscribed for the city. If the <code>city_id</code> is in the table, it's traffic_stats is refreshed.</p> <p>On the front end, following query is run to fetch data for user -</p> <pre><code>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 </code></pre> <p>This gives <strong>single <em>latest</em></strong> entry for a city_id.</p> <p>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 <code>traffic stats</code> table every two hours. That's 2400 entries a day and the table will keep growing.</p> <p>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.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload