两种关联表的SQL的update的效率对比和优化


  1. 这个效率比较高的写法
WITH componentTab AS (
    SELECT
        *
    FROM
        pgr_connectedComponents (
            'SELECT id,source,target,cost,reverse_cost FROM yh_map_supplypipe'
        )
) 
 UPDATE yh_map_supplypipe  SET componentid = componentTab.component from componentTab
 where  yh_map_supplypipe.SOURCE = componentTab.node

  2.这个效率比较低的写法

WITH componentTab AS (
    SELECT
        *
    FROM
        pgr_connectedComponents (
            'SELECT id,source,target,cost,reverse_cost FROM yh_map_supplypipe'
        )
) 

UPDATE yh_map_supplypipe
SET componentid = (
    SELECT
        componentTab.component
    FROM
        yh_map_supplypipe,
        componentTab
    WHERE
        yh_map_supplypipe. SOURCE = componentTab.node
    LIMIT 1
)

注意:里面千万不能用别名,会出现都是同一值的情况