UNIQUE constraint not used added by sjamaan on Fri Jan 17 14:30:13 2020

=> EXPLAIN ANALYZE SELECT "activity_truckposition"."id", "activity_truckposition"."data_external_id" FROM "activity_truckposition" INNER JOIN "asset_truck" ON ("activity_truckposition"."truck_id" = "asset_truck"."id") WHERE ("activity_truckposition"."data_external_id" IN (10773869489, 10773870493, 10773873338, 10773872592) AND "activity_truckposition"."data_source" = 'fleetvisor') ORDER BY "asset_truck"."id" ASC, "activity_truckposition"."measured_at" DESC;

                                                                          QUERY PLAN                                                      
                     
------------------------------------------------------------------------------------------------------------------------------------------
---------------------
 Gather Merge  (cost=373808.81..373809.28 rows=4 width=24) (actual time=1220.470..1222.242 rows=0 loops=1)
   Workers Planned: 2
   Workers Launched: 2
   ->  Sort  (cost=372808.79..372808.79 rows=2 width=24) (actual time=1198.766..1198.766 rows=0 loops=3)
         Sort Key: asset_truck.id, activity_truckposition.measured_at DESC
         Sort Method: quicksort  Memory: 25kB
         ->  Nested Loop  (cost=0.28..372808.78 rows=2 width=24) (actual time=1198.738..1198.739 rows=0 loops=3)
               ->  Parallel Seq Scan on activity_truckposition  (cost=0.00..372801.78 rows=2 width=24) (actual time=1198.738..1198.738 row
s=0 loops=3)
                     Filter: ((data_source = 'fleetvisor'::text) AND (data_external_id = ANY ('{10773869489,10773870493,10773873338,107738
72592}'::bigint[])))
                     Rows Removed by Filter: 4871076
               ->  Index Only Scan using asset_truck_pkey on asset_truck  (cost=0.28..3.49 rows=1 width=4) (never executed)
                     Index Cond: (id = activity_truckposition.truck_id)
                     Heap Fetches: 0
 Planning time: 0.758 ms
 Execution time: 1222.312 ms
(15 rows)


=> \d activity_truckposition
                                          Table "public.activity_truckposition"
      Column      |           Type           | Collation | Nullable |                      Default                       
------------------+--------------------------+-----------+----------+----------------------------------------------------
 id               | integer                  |           | not null | nextval('activity_truckposition_id_seq'::regclass)
 data_external_id | bigint                   |           | not null | 
 altitude         | integer                  |           |          | 
 measured_at      | timestamp with time zone |           | not null | 
 ignition         | boolean                  |           |          | 
 speed            | smallint                 |           |          | 
 direction        | smallint                 |           |          | 
 odometer         | integer                  |           |          | 
 truck_id         | integer                  |           | not null | 
 fuel_level       | smallint                 |           |          | 
 fuel_used        | integer                  |           |          | 
 point            | geography(Point,4326)    |           | not null | 
 closest_city     | text                     |           | not null | 
 closest_country  | character varying(2)     |           | not null | 
 closest_street   | text                     |           | not null | 
 data_source      | text                     |           | not null | 
Indexes:
    "activity_truckposition_pkey" PRIMARY KEY, btree (id)
    "fleetvisor_position_uniqueness_on_external_id" UNIQUE, btree (data_source, data_external_id) WHERE data_source = 'fleetvisor'::text
    "activity_tr_truck_i_239ba9_idx" btree (truck_id, measured_at DESC, odometer)
    "activity_tr_truck_i_d4b4f3_idx" btree (truck_id, odometer)
    "activity_truckposition_truck_id_08765e63" btree (truck_id)
Check constraints:
    "activity_truckposition_direction_check" CHECK (direction >= 0)
    "activity_truckposition_fuel_level_check" CHECK (fuel_level >= 0)
    "activity_truckposition_fuel_used_check" CHECK (fuel_used >= 0)
    "activity_truckposition_odometer_check" CHECK (odometer >= 0)
    "activity_truckposition_speed_check" CHECK (speed >= 0)
Foreign-key constraints:
    "activity_truckposition_truck_id_08765e63_fk_asset_truck_id" FOREIGN KEY (truck_id) REFERENCES asset_truck(id) DEFERRABLE INITIALLY DE
FERRED