2013-07-07 68 views
0

我有一個PHP腳本,它循環通過2000+記錄一個while循環。在這個while循環中,必須執行postgres sql查詢,不幸的是它不能從while循環中排除。當在while循環中添加postgres查詢時,PHP腳本非常慢

$sql = "(SELECT (timestamp) AS time FROM followups as f 
      JOIN campaigns as c ON c.id = f.campid 
      WHERE c.clientid = ".trim($clientid)." AND c.contractno = '".trim($c)."' AND (LOWER(person) IN (SELECT LOWER(userid) FROM users WHERE type IN('S','X')) OR LOWER(person) IN (SELECT LOWER(name) FROM users WHERE type IN('S','X')))) 
      UNION ALL (SELECT (timestamp) AS time FROM followups as f WHERE (contractno ='".trim($c)."' 
      OR contractno LIKE '%".trim($c)."||".trim($clientid)."%' 
       OR contractno = '".trim($c)."||".trim($clientid)."') AND (LOWER(person) IN (SELECT LOWER(userid) FROM users WHERE type IN('S','X')) OR LOWER(person) IN (SELECT LOWER(name) FROM users WHERE type IN('S','X'))) ) 
       UNION ALL (select (f.timestamp) AS time FROM followups as f 
       JOIN campaigns as c on c.id = f.campid WHERE c.clientid = ".trim($clientid)." 
        AND c.clientid in ( 
        SELECT id FROM easy_mapping where id = ".trim($clientid).") AND (LOWER(person) IN (SELECT LOWER(userid) 
        FROM users WHERE type IN('S','X')) OR LOWER(person) IN 
        (SELECT LOWER(name) FROM users WHERE type IN('S','X'))))"; 
$result = pg_query($conn,$sql); 

上面的查詢包含在while循環,前幾個記錄執行非常迅速,那麼腳本開始變慢,採取幾乎一天完成腳本。有沒有辦法將不同的上面的確切查詢寫入以獲得相同的結果?

UPDATE:

下面是完整的循環

$dates = array(); 
$clientid = str_replace("\t", '', $clientid); 
foreach ($contracts as $c) { 
    $c = str_replace("\t", '', $c); 
    $sql = "(SELECT MAX(timestamp) AS time FROM followups as f 
    JOIN campaigns as c ON c.id = f.campid 
    WHERE c.clientid = ".trim($clientid)." AND c.contractno = '".trim($c)."' AND (LOWER(person) IN (SELECT LOWER(userid) FROM users WHERE type IN('S','X')) OR LOWER(person) IN (SELECT LOWER(name) FROM users WHERE type IN('S','X')))) 
    UNION ALL (SELECT MAX(timestamp) AS time FROM followups as f WHERE (contractno ='".trim($c)."' 
    OR contractno LIKE '%".trim($c)."||".trim($clientid)."%' 
    OR contractno = '".trim($c)."||".trim($clientid)."') AND (LOWER(person) IN (SELECT LOWER(userid) FROM users WHERE type IN('S','X')) OR LOWER(person) IN (SELECT LOWER(name) FROM users WHERE type IN('S','X'))) ) 
    UNION ALL (select MAX(f.timestamp) AS time FROM followups as f 
    JOIN campaigns as c on c.id = f.campid WHERE c.clientid = ".trim($clientid)." 
    AND c.clientid in (SELECT id FROM easy_mapping where id = ".trim($clientid).") AND (LOWER(person) IN (SELECT LOWER(userid) FROM users WHERE type IN('S','X')) OR LOWER(person) IN (SELECT LOWER(name) FROM users WHERE type IN('S','X'))))"; 
    $result = pg_query($conn,$sql); 
    if (pg_num_rows($result)>0) { 
     while ($row = pg_fetch_array($result, null, PGSQL_ASSOC)) { 
      if (empty($row['time'])) { 
       continue; 
      } 
      $dates[] = $row['time']; 
     } 
    } 
    pg_free_result($result); 
} 
if (empty($dates)) { 
    return false; 
} else { 
    $max = max($dates); 
    if (strtotime(date("Y-m-d")) < strtotime(date("Y-m-t"))) { 
     $compdate = date("Y-m-01", strtotime("-1 month")); 
    } else { 
     $compdate = date("Y-m-01"); 
    } 
    if (strtotime($compdate) > $max) { 
     return false; 
    } else { 
     return true; 
    } 
} 
unset($dates); 
+0

顯示它周圍的環 – mnagel

+0

@mnagel - 我已經更新的問題,包括它周圍的環 – Roland

+0

有多大是每個查詢返回的數據集? 10行,100行1000行? –

回答

1

下面是什麼,我可以從你的真正junkish代碼理解的結果。

$clientid = trim(str_replace("\t", '', $clientid)); 
$sql = " 
select max(time) 
from (
(
    select max(timestamp) as time 
    from 
     followups f 
     inner join 
     campaigns c on c.id = f.campid 
     inner join 
     users u on lower(f.person) in (lower(u.userid), lower(u.name)) 
    where 
     c.clientid = $clientid 
     and u.type in('S','X') 
) 
union 
(
    select max(timestamp) as time 
    from 
     followups as f 
     inner join 
     users u on lower(f.person) in (lower(u.userid), lower(u.name)) 
    where 
     contractno like ('%' || $clientid || '%') 
     and u.type in('S','X') 
) 
union 
(
    select max(f.timestamp) as time 
    from 
     followups as f 
     join 
     campaigns as c on c.id = f.campid 
     inner join 
     users u on lower(f.person) in (lower(u.userid), lower(u.name)) 
     inner join 
     easy_mapping em on c.clientid = em.id 
    where 
     c.clientid = $clientid 
     and u.type in('S','X') 
)) s 
"; 
$result = pg_query($conn,$sql); 
if (pg_num_rows($result) == 0) { 
    return false; 
} else { 
    $max = $row['time']; 
    if (strtotime(date("Y-m-d")) < strtotime(date("Y-m-t"))) { 
     $compdate = date("Y-m-01", strtotime("-1 month")); 
    } else { 
     $compdate = date("Y-m-01"); 
    } 
    if (strtotime($compdate) > $max) { 
     return false; 
    } else { 
     return true; 
    } 
} 
pg_free_result($result);