Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Just out of interest, how does the following query perform? I'm particularly interested in how Postgresql processes the innermost query (states table + scalar sub query).</p> <p>There has to a compound index on users(state_id, creation_date) for this to work. </p> <pre><code>select s2.id ,s2.state ,(select count(*) from users u where u.state_id = s2.id and u.creationdate &gt; s2.max_date) as numberOfUsers from (select s.id ,s.state ,(select max(u.creationdate) - interval '3 months' from users u where u.state_id = s.id) as max_date from states s ) s2; </code></pre> <p><em>edit</em> this is the plan produced for that query with 100,000 user rows against 3 states:</p> <pre><code> Seq Scan on states s (actual time=4.033..13.949 rows=3 loops=1) Buffers: shared hit=1743 SubPlan 3 -&gt; Aggregate (actual time=4.636..4.636 rows=1 loops=3) Buffers: shared hit=1742 InitPlan 2 (returns $2) -&gt; Result (actual time=0.028..0.028 rows=1 loops=3) Buffers: shared hit=12 InitPlan 1 (returns $1) -&gt; Limit (actual time=0.022..0.022 rows=1 loops=3) Buffers: shared hit=12 -&gt; Index Scan Backward using users_state_id_creationdate_idx on users u (actual time=0.019..0.019 rows=1 loops=3) Index Cond: ((state_id = $0) AND (creationdate IS NOT NULL)) Buffers: shared hit=12 -&gt; Bitmap Heap Scan on users u (actual time=1.095..3.693 rows=8425 loops=3) Recheck Cond: ((state_id = $0) AND (creationdate &gt; $2)) Buffers: shared hit=1730 -&gt; Bitmap Index Scan on users_state_id_creationdate_idx (actual time=1.017..1.017 rows=8425 loops=3) Index Cond: ((state_id = $0) AND (creationdate &gt; $2)) Buffers: shared hit=107 Total runtime: 14.017 ms </code></pre>
    singulars
    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.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      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