我在3个表格select *
,gis_wcdma_cells
,wcdma_cells
上的视图中执行wcdma_cells_statistic
:
CREATE OR REPLACE VIEW gisview_wcdma_cell_statistic AS
SELECT gis_wcdma_cells.wcdma_cell_id,
gis_wcdma_cells.geometry,
wcdma_cells."Cellname",
wcdma_cells."Longitude",
wcdma_cells."Latitude",
wcdma_cells."Orientation",
wcdma_cell_statistic.avg_ecio,
wcdma_cell_statistic.avg_rssi,
wcdma_cell_statistic.message_count,
wcdma_cell_statistic.measurement_count
FROM gis_wcdma_cells,
wcdma_cells
LEFT JOIN wcdma_cell_statistic ON wcdma_cells.id = wcdma_cell_statistic.cell_id
WHERE gis_wcdma_cells.wcdma_cell_id = wcdma_cells.id;
但是,虽然我在视图中为3个表创建了索引,但我觉得它很慢:
我发现select * from view
比从视图中选择特定列要慢得多。虽然select *
的SQL解释看起来更快(我的意思是select *
情况下的成本要低得多。)。你能帮忙解释一下原因吗?如果有任何建议可以让查询更快?
Nested Loop Left Join (cost=0.00..11501.93 rows=41036 width=479) (actual time=0.034..326.680 rows=41036 loops=1)
Buffers: shared hit=238761
-> Nested Loop (cost=0.00..9017.44 rows=41036 width=467) (actual time=0.026..183.752 rows=41036 loops=1)
Buffers: shared hit=125390
-> Seq Scan on gis_wcdma_cells (cost=0.00..2690.36 rows=41036 width=396) (actual time=0.007..13.359 rows=41036 loops=1)
Buffers: shared hit=2280
-> Index Scan using wcdma_cells_id_idx on wcdma_cells (cost=0.00..0.14 rows=1 width=71) (actual time=0.002..0.003 rows=1 loops=41036)
Index Cond: (id = gis_wcdma_cells.wcdma_cell_id)
Rows Removed by Index Recheck: 0
Buffers: shared hit=123110
-> Index Scan using wcdma_cell_statistic_cell_id_idx on wcdma_cell_statistic (cost=0.00..0.05 rows=1 width=20) (actual time=0.002..0.002 rows=1 loops=41036)
Index Cond: (wcdma_cells.id = cell_id)
Rows Removed by Index Recheck: 0
Buffers: shared hit=113371
Total runtime: 334.551 ms
Nested Loop Left Join (cost=2340.31..9007.59 rows=41036 width=58) (actual time=46.498..263.589 rows=41036 loops=1)
Buffers: shared hit=116667, temp read=400 written=394
-> Hash Join (cost=2340.31..6523.10 rows=41036 width=58) (actual time=46.471..119.983 rows=41036 loops=1)
Hash Cond: (gis_wcdma_cells.wcdma_cell_id = wcdma_cells.id)
Buffers: shared hit=3296, temp read=400 written=394
-> Seq Scan on gis_wcdma_cells (cost=0.00..2690.36 rows=41036 width=4) (actual time=0.007..14.688 rows=41036 loops=1)
Buffers: shared hit=2280
-> Hash (cost=1426.36..1426.36 rows=41036 width=54) (actual time=46.358..46.358 rows=41036 loops=1)
Buckets: 2048 Batches: 4 Memory Usage: 927kB
Buffers: shared hit=1016, temp written=299
-> Seq Scan on wcdma_cells (cost=0.00..1426.36 rows=41036 width=54) (actual time=0.004..24.612 rows=41036 loops=1)
Buffers: shared hit=1016
-> Index Scan using wcdma_cell_statistic_cell_id_idx on wcdma_cell_statistic (cost=0.00..0.05 rows=1 width=8) (actual time=0.002..0.002 rows=1 loops=41036)
Index Cond: (wcdma_cells.id = cell_id)
Rows Removed by Index Recheck: 0
Buffers: shared hit=113371
Total runtime: 271.146 ms
我在Navicat或PgAdmin工具中运行SQL查询。查询执行时间为: 15秒用于选择特定列,几分钟用于select *
。