PostgreSQL中的查询速度非常慢(按多列排序)

时间:2016-06-17 14:23:31

标签: postgresql postgresql-performance

如果您需要表格定义,请告诉我。我确信很明显,我有几个表格,其中包含有关用户(partydevicestatusgroupgps)的信息,每个用户都与partyrelationship中的用户有外键关系row具有" group"的标识符。那个用户在。

通过此查询,我只想对于特定组(在此示例中为6)获取每个用户的用户详细信息,位置和设备信息。

我可以清楚地看到解释,排序是这里的问题,因为有2列有大量数据。但是,我在两个列上都有一个索引,并且没有任何改进。我几乎可以肯定这是一个非常优化的查询,但我对PostgreSQL没有足够的经验来找到更好的查询?

SELECT DISTINCT ON("public".groupgps.groupmember)
 "public".groupgps.groupgps,
 "public".groupgps.groupmember,
 "public".groupgps.messagetype,
 "public".groupgps.lat,
 "public".groupgps.lon,
 "public".groupgps.date_stamp,
 "public".partyrelationship.to_party,
 "public".partyrelationship.to_name,
 "public".partyrelationship.image_url,
 "public".partyrelationship.partyrelationship,
 "public".partydevicestatus.connection_type,
 "public".partydevicestatus.battery_level,
 "public".partydevicestatus.charging_state,
 "public".partydevicestatus.timestamp
 FROM "public".groupgps
 INNER JOIN "public".partyrelationship
 ON "public".partyrelationship.partyrelationship = "public".groupgps.groupmember
 INNER JOIN "public".partysettings
 ON "public".partysettings.groupmember = "public".groupgps.groupmember
 LEFT JOIN "public".partydevicestatus
 ON "public".partydevicestatus.groupmember_id = "public".groupgps.groupmember
 WHERE "public".partyrelationship.from_party = 6
 AND "public".partysettings.gps_tracking_enabled = true
 ORDER BY "public".groupgps.groupmember, "public".groupgps.date_stamp DESC

解释结果

Unique  (cost=1368961.43..1390701.85 rows=25 width=192) (actual time=24622.609..27043.061 rows=4 loops=1)
  ->  Sort  (cost=1368961.43..1379831.64 rows=4348083 width=192) (actual time=24622.601..26604.659 rows=2221853 loops=1)
        Sort Key: groupgps.groupmember, groupgps.date_stamp DESC
        Sort Method: external merge  Disk: 431400kB
        ->  Hash Left Join  (cost=50.64..87013.93 rows=4348083 width=192) (actual time=0.499..3011.806 rows=2221853 loops=1)
              Hash Cond: (groupgps.groupmember = partydevicestatus.groupmember_id)
              ->  Hash Join  (cost=31.66..29732.32 rows=77101 width=167) (actual time=0.153..2242.950 rows=109041 loops=1)
                    Hash Cond: (groupgps.groupmember = partyrelationship.partyrelationship)
                    ->  Seq Scan on groupgps  (cost=0.00..24372.00 rows=1217200 width=50) (actual time=0.005..1933.528 rows=1217025 loops=1)
                    ->  Hash  (cost=31.48..31.48 rows=14 width=125) (actual time=0.141..0.141 rows=5 loops=1)
                          Buckets: 1024  Batches: 1  Memory Usage: 9kB
                          ->  Hash Join  (cost=10.31..31.48 rows=14 width=125) (actual time=0.092..0.138 rows=5 loops=1)
                                Hash Cond: (partysettings.groupmember = partyrelationship.partyrelationship)
                                ->  Seq Scan on partysettings  (cost=0.00..20.75 rows=75 width=8) (actual time=0.003..0.038 rows=75 loops=1)
                                      Filter: gps_tracking_enabled
                                ->  Hash  (cost=9.79..9.79 rows=42 width=117) (actual time=0.076..0.076 rows=42 loops=1)
                                      Buckets: 1024  Batches: 1  Memory Usage: 11kB
                                      ->  Seq Scan on partyrelationship  (cost=0.00..9.79 rows=42 width=117) (actual time=0.007..0.058 rows=42 loops=1)
                                            Filter: (from_party = 6)
                                            Rows Removed by Filter: 181
              ->  Hash  (cost=12.88..12.88 rows=488 width=29) (actual time=0.341..0.341 rows=489 loops=1)
                    Buckets: 1024  Batches: 1  Memory Usage: 41kB
                    ->  Seq Scan on partydevicestatus  (cost=0.00..12.88 rows=488 width=29) (actual time=0.023..0.163 rows=489 loops=1)
Planning time: 0.878 ms
Execution time: 27218.016 ms

0 个答案:

没有答案