Note that there are some explanatory texts on larger screens.

plurals
  1. POEffectively COUNTing and SUMing on a large inner join
    primarykey
    data
    text
    <p>Please consider the following case (simplified for the sake of the question):</p> <p>I have the following tables in the SQL Server 2012 database:</p> <pre><code>Parent_Table Id | Parent table fields ----+-------------------- 1 | ... 2 | ... 3 | ... ... Child_Table Id | ParentId | Child table fields ----+----------+------------------- 1 | 2 | ... 2 | 1 | ... 3 | 1 | ... 4 | 3 | ... 5 | 2 | ... ... Big_Table Id | ChildId | Value | Status | Other fields --------+----------+--------|--------------------- 1 | 12 | 672 | Closed | 2 | 23 | 133 | Closed | 3 | 7 | 2611 | Open | 4 | 14 | 84 | Closed | ... 1295769 | 23 | 458 | Closed | 1295770 | 18 | 1046 | Open | 1295771 | 7 | 8 | Open | </code></pre> <p>The Child and Parent tables are relatively small (roughly 100 Parent entries and 5 Child entries per parent) and their entries are inserted or deleted only a few times a day.</p> <p>The "Big Table", on the other hand, is growing fast (100 entries per second, for the sake of discussion), and the status of rows becomes Closed after a while (think of client sessions, which is the actually the case here).</p> <p>I need to periodically (every few seconds) provide the number of Big_Table rows and the sum of the Big_Table.Value column <strong>for a specified Parent.Id</strong> - a different one every time.</p> <p>I suspect that the straightforward implementation (using inner joins etc.) might be extremely inefficient, and better solution may include additional tables, a table of counters of some sort or I should just implement this in my service code (?!) and take care of persistence somehow.</p> <p>What would be the "right" (efficiency-wise) way of implementing the above? A solution which handles additional levels of parents/children would be the best one. </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.
 

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