Skip to content

Commit ef5a255

Browse files
committed
simpler fixture
1 parent 84e89ac commit ef5a255

File tree

1 file changed

+1
-46
lines changed

1 file changed

+1
-46
lines changed

test/emails/email_one_is_not_on.txt

Lines changed: 1 addition & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -2,54 +2,9 @@ Thank, this is really helpful.
22

33
One outstanding question I had:
44

5-
Locally (on development), when I run LocationEvent.first(:conditions => ["user_id=? and date_created>=? and date_created<=?",@user.id.to_s,dateBeg,dateEnd])
6-
7-
The first, time I run the query it is quite long (4+seconds). The subsequent time it is quite quick (50ms). I'm assuming this is because it is now cached. What I don't understand is why the original query is so long? (The indexing seems to be working).
8-
9-
LocationEvent Load (4235.8ms) SELECT "location_events".* FROM "location_events" WHERE (user_id=4213 and date_created>='2012-09-25 07:00:00.000000' and date_created<'2012-10-03 07:00:00.000000') LIMIT 1
10-
EXPLAIN (0.7ms) EXPLAIN SELECT "location_events".* FROM "location_events" WHERE (user_id=4213 and date_created>='2012-09-25 07:00:00.000000' and date_created<'2012-10-03 07:00:00.000000') LIMIT 1
11-
EXPLAIN for: SELECT "location_events".* FROM "location_events" WHERE (user_id=4213 and date_created>='2012-09-25 07:00:00.000000' and date_created<'2012-10-03 07:00:00.000000') LIMIT 1
12-
QUERY PLAN
13-
----------------------------------------------------------------------------------------------------------------------------------------------------------------
14-
Limit (cost=0.00..4.85 rows=1 width=80)
15-
-> Index Scan using index_location_events_on_user_id on location_events (cost=0.00..3650.32 rows=753 width=80)
16-
Index Cond: (user_id = 4213)
17-
Filter: ((date_created >= '2012-09-25 07:00:00'::timestamp without time zone) AND (date_created < '2012-10-03 07:00:00'::timestamp without time zone))
18-
(4 rows)
19-
20-
5+
Locally (on development), when I run...
216

227
On Oct 1, 2012, at 11:55 PM, Dave Tapley wrote:
238

249
> The good news is that I've found a much better query for lastLocation.
2510
>
26-
> All you need to do is add :order => 'date_created ASC' to the arguments to last.
27-
>
28-
> It seems weird that adding more ordering drastically improves it, if we look at the SQL:
29-
>
30-
> SELECT "location_events".* FROM "location_events" WHERE (user_id='4249' and date_created>='2012-09-17' and date_created<='2012-09-23') ORDER BY date_created ASC LIMIT 1
31-
> And explain it:
32-
>
33-
> Limit (cost=1568.63..1568.63 rows=1 width=80)
34-
> -> Sort (cost=1568.63..1569.17 rows=215 width=80)
35-
> Sort Key: date_created
36-
> -> Index Scan using index_location_events_on_user_id on location_events (cost=0.00..1567.56 rows=215 width=80)
37-
> Index Cond: (user_id = 4249)
38-
> Filter: ((date_created >= '2012-09-17 00:00:00'::timestamp without time zone) AND (date_created <= '2012-09-23 00:00:00'::timestamp without time zone))
39-
> Postgres now concedes that it's better to do the index scan on user id first, and then do a sort on the 215 rows within that date range (for that user).
40-
>
41-
> I should note that you can also flip .last with .first, and ASC for DESC in the definition of lastLocation, so you get:
42-
>
43-
> LocationEvent.first(:conditions => ["user_id=? and date_created>=? and date_created<=?",@user.id.to_s,dateBeg,dateEnd], :order => 'date_created DESC')
44-
> And it's conceptually the same query.
45-
>
46-
> Finally, there is a (very, very) subtle bug in the definition of firstLocation (the 'bad thing' I mentioned in the above comment), which can be fixed in the same way. It seems that when Rails turns .first in to LIMIT 1 with no ORDER BY we always get out the matching location event with the highest ID. I don't believe that's guaranteed (I'll check up), but if it's not we might be safer throwing a :order => 'date_created ASC' in to the .first as well. It is slightly slower than the current code, but not by much.
47-
>
48-
> Hopefully you've managed to follow me, the realization at the end is that this is what we should have written to start with:
49-
>
50-
> firstLocation = LocationEvent.first(:conditions => ["user_id=? and date_created>=? and date_created<=?",@user.id.to_s,dateBeg,dateEnd], :order => 'date_created ASC')
51-
> lastLocation = LocationEvent.first(:conditions => ["user_id=? and date_created>=? and date_created<=?",@user.id.to_s,dateBeg,dateEnd], :order => 'date_created DESC')
52-
> —
53-
> Reply to this email directly or view it on GitHub.
54-
>
55-
>

0 commit comments

Comments
 (0)