<html>
<head>
</head>
<body>
        <div style="text-align: left;">
                <pre>Hallo

To sort out if the bottleneck is internal in the db or just the transfer
of the geometries from the database to your client you can try to run some
function that is not too costly but have to alter the whole geometry,
just returning a number, for instance ST_Area

So, if you run your query, but instead of including the geometry do
"ST_Area(geometry)" 

What timing do you get then?

/Nicklas

</pre></div><br />
         2012-12-03 ivan marchesini  wrote:<br />
        <br />
        Dear users,<br />
        
>I apologize for cross-posting since I already sent this request to the<br />
        
>PostgreSQL Mailing List. But given the answers probably this is the<br />
        
>right mailing list for our problem.<br />
        
> We are facing with a strange efficiency problem.<br />
        
><br />
        
>We have two tables:<br />
        
>1) the first largest (30000 rows) contains a timestamp field (named<br />
        
>"tempo_calcolo") and other fields.<br />
        
>2) the second contains only few rows (150), a PostGIS geometric POLYGON<br />
        
>field (named "the_geom") and other fields.<br />
        
><br />
        
>The query we want to execute is a join between the two table based on a<br />
        
>common field, filtered by the timestamp field and ordered by the same<br />
        
>field.<br />
        
><br />
        
>Including and excluding the PostGIS geometric POLYGON field (that is not<br />
        
>used neither in the joining or in the filtering) the execution times are<br />
        
>really different.<br />
        
><br />
        
>Without the geometric field it takes around 86 ms !!<br />
        
>With the geometric field it takes around 14000 ms !! <br />
        
><br />
        
>These are the EXPLAIN ANALYSE results obtained when we perform the query<br />
        
>without the geometric field:<br />
        
>____________________________<br />
        
>"Sort  (cost=1217.29..1240.40 rows=9246 width=133) (actual<br />
        
>time=2.964..3.095 rows=1474 loops=1)"<br />
        
>"  Sort Key: storico_zone_allertamento.tempo_calcolo"<br />
        
>"  Sort Method:  quicksort  Memory: 440kB"<br />
        
>"  InitPlan 1 (returns $0)"<br />
        
>"    ->  Result  (cost=0.00..0.02 rows=1 width=0) (actual<br />
        
>time=0.022..0.022 rows=1 loops=1)"<br />
        
>"  ->  Hash Join  (cost=6.01..608.21 rows=9246 width=133) (actual<br />
        
>time=0.143..1.916 rows=1474 loops=1)"<br />
        
>"        Hash Cond: (storico_zone_allertamento.gid_fk =<br />
        
>zone_allertamento.gid)"<br />
        
>"        ->  Index Scan using<br />
        
>index_storico_zone_allertamento_tempo_calcolo on<br />
        
>storico_zone_allertamento  (cost=0.00..475.06 rows=9246 width=112)<br />
        
>(actual time=0.042..0.440 rows=1474 loops=1)"<br />
        
>"              Index Cond: (tempo_calcolo > $0)"<br />
        
>"        ->  Hash  (cost=4.34..4.34 rows=134 width=25) (actual<br />
        
>time=0.090..0.090 rows=134 loops=1)"<br />
        
>"              Buckets: 1024  Batches: 1  Memory Usage: 8kB"<br />
        
>"              ->  Seq Scan on zone_allertamento  (cost=0.00..4.34<br />
        
>rows=134 width=25) (actual time=0.004..0.035 rows=134 loops=1)"<br />
        
>"Total runtime: 3.285 ms"<br />
        
>__________________________<br />
        
><br />
        
><br />
        
>These are the EXPLAIN ANALYSE results obtained when we perform the query<br />
        
>with the geometric field:<br />
        
>___________________________<br />
        
>"Sort  (cost=776019.29..776042.40 rows=9246 width=196111) (actual<br />
        
>time=3.039..3.163 rows=1474 loops=1)"<br />
        
>"  Sort Key: storico_zone_allertamento.tempo_calcolo"<br />
        
>"  Sort Method:  quicksort  Memory: 567kB"<br />
        
>"  InitPlan 1 (returns $0)"<br />
        
>"    ->  Result  (cost=0.00..0.02 rows=1 width=0) (actual<br />
        
>time=0.009..0.009 rows=1 loops=1)"<br />
        
>"  ->  Hash Join  (cost=6.01..608.21 rows=9246 width=196111) (actual<br />
        
>time=0.139..1.957 rows=1474 loops=1)"<br />
        
>"        Hash Cond: (storico_zone_allertamento.gid_fk =<br />
        
>zone_allertamento.gid)"<br />
        
>"        ->  Index Scan using<br />
        
>index_storico_zone_allertamento_tempo_calcolo on<br />
        
>storico_zone_allertamento  (cost=0.00..475.06 rows=9246 width=112)<br />
        
>(actual time=0.029..0.431 rows=1474 loops=1)"<br />
        
>"              Index Cond: (tempo_calcolo > $0)"<br />
        
>"        ->  Hash  (cost=4.34..4.34 rows=134 width=196003) (actual<br />
        
>time=0.091..0.091 rows=134 loops=1)"<br />
        
>"              Buckets: 1024  Batches: 1  Memory Usage: 19kB"<br />
        
>"              ->  Seq Scan on zone_allertamento  (cost=0.00..4.34<br />
        
>rows=134 width=196003) (actual time=0.006..0.038 rows=134 loops=1)"<br />
        
>"Total runtime: 3.355 ms"<br />
        
>__________________________<br />
        
><br />
        
><br />
        
>It seems that the difference is primary in the "width" and then in the<br />
        
>"cost" of the Sort.<br />
        
><br />
        
>A similar task performed on two similar tables (both larger than the<br />
        
>previous ones!!) but including and excluding a PostGIS geometric POINT<br />
        
>(!!) field instead of POLYGON one, doesn't result in significant query<br />
        
>execution time difference.<br />
        
><br />
        
>Do you have please any suggestion to improve our query efficiency??<br />
        
><br />
        
>In the PostgreSQL mailing Tom Lane suggest that the problem could be due<br />
        
>to:<br />
        
>_____________________<br />
        
>        (1) time to fetch the wide geometry values from the table's<br />
        
>        TOAST table<br />
        
>        (2) time to convert the geometry values to text form<br />
        
>        (3) time to transmit the larger volume of data to the client<br />
        
>        (4) client-side processing time<br />
        
>        None of these costs are expended in an EXPLAIN ANALYZE, which is<br />
        
>        why the time reported for that doesn't change materially.<br />
        
>____________________<br />
        
><br />
        
>We think that the first is the most probable cause of the high query<br />
        
>execution time, because the query is a simple join( without, we think,<br />
        
>conversion from geometry to text), the delay is confirmed also using<br />
        
>psql on the server, the client side processing time should be really<br />
        
>small since data are really few.<br />
        
><br />
        
>Can you suggest a way to solve this problem?<br />
        
>Any suggestion is really appreciated !!!!<br />
        
><br />
        
>Thanks in advance<br />
        
><br />
        
>Ivan and Mauro<br />
        
><br />
        
><br />
        
>-- <br />
        
>Ti prego di cercare di non inviarmi files .dwg, .doc, .xls, .ppt.<br />
        
>Preferisco formati liberi.<br />
        
>Please try to avoid to send me .dwg, .doc, .xls, .ppt files.<br />
        
>I prefer free formats.<br />
        
>http://it.wikipedia.org/wiki/Formato_aperto<br />
        
>http://en.wikipedia.org/wiki/Open_format<br />
        
><br />
        
>Ivan Marchesini<br />
        
>Perugia (Italy)<br />
        
>Socio fondatore GFOSS "Geospatial Free and Open Source Software" http://www.gfoss.it<br />
        
>e-mail: ivan.marchesini@irpi.cnr.it<br />
        
>        ivan.marchesini@gmail.com<br />
        
>fax (mailfax): +39 1782092534<br />
        
>jabber: geoivan73@jabber.org<br />
        
>skype: geoivan73<br />
        
><br />
        
>_______________________________________________<br />
        
>postgis-users mailing list<br />
        
>postgis-users@lists.osgeo.org<br />
        
>http://lists.osgeo.org/cgi-bin/mailman/listinfo/postgis-users<br />
        
><br />
        
>
 
</body>
</html>