2011-06-01 46 views
0

好的。我最後爲我的php文件寫了我的查詢(這會將查詢寫入一個txt文件)。但是,當我之前測試文件時,我顯然正在優化最大執行時間。我只用一個大型查詢來完成這項工作,但現在我想知道是否最好使用多個較小的查詢。 這會減少執行時間嗎?PHP:查詢超過最大時間(如何處理)

如果是這樣,那麼我對你們所有人都有另一個問題,我會怎麼做呢?這是我的文件的大部分內容:

// Query the database for data 
$query = "SELECT 
    cards.card_id, 
    concat(cards.title, ' By Amy') AS cardTitle, 
    cards.meta_description, 
    cards.description, 
    '' as Weight, 
    '' as UPC, 
    cards.seo_keywords, 
    concat('http://www.amyadele.com/attachments//cards/',cards.card_id,'/',cards.card_image) AS ImageURL, 
    card_import.artist, 
    concat('ARTIST - ', card_import.artist) AS Brand, 
    min(card_lookup_values.card_price) AS LowPrice, 
replace(lower(concat('http://www.amyadele.com/', pcat.seoname,'/',cat.seoname, '/', cards.seoname, '.htm')),' ','+') AS link, 
      concat(pcat.name,'>',cat.name) as Merchant, 
round(min(card_lookup_values.card_price), 2) AS newPrice, 
min(cast(lookup_details.value as signed)) as quantity 

FROM 
    cards 
INNER JOIN card_cheapest on cards.card_id = card_cheapest.card_id 
LEFT JOIN card_import on card_import.card_id = cards.card_id 
INNER JOIN card_lookup_values on card_lookup_values.card_id = cards.card_id 
INNER JOIN card_categories cc ON cards.card_id = cc.card_id AND cards.card_live = 'y' AND cards.active = 'y' AND cc.active = 'Y' 
INNER JOIN categories cat ON cat.category_id = cc.category_id AND cat.active = 'Y' 
INNER JOIN categories pcat ON cat.parent_category_id = pcat.category_id 
INNER JOIN card_lookup_values as card_values ON cards.card_id = card_values.card_id 
INNER JOIN lookup_details ON card_lookup_values.lookup_detail_id = lookup_details.lookup_detail_id 

WHERE card_lookup_values.lookup_id = 7 
GROUP BY 
    cards.card_id 
ORDER BY 
    cards.card_id"; 


$result = mysql_query($query); 


// Open file for writing 
$myFile = "google.txt"; 
$fh = fopen($myFile, 'w') or die("can't open file"); 

// Loop through returned data and write (append) directly to file 
fprintf($fh, "%-25s %-200 \n", "ID", "Name"); 

fprintf($fh, "\n"); 
while ($row = mysql_fetch_assoc($result)) { 

fprintf($fh, "%-25s %-200s \n", $row['card_id'], $row['cardTitle']); 

} 

// Close out the file 
fclose($fh); 

echo "The file has been written sucessfully to googleproducts.txt. It will run again tomorrow at 12:00pm." 
?> 

正如你可以看到我在拉$ 查詢$結果,然後使用該將其插入到$行。 我想我的實際問題(如果可能的話),我怎麼去有$查詢,$ query2和插入$ query2到相同的while語句?

+0

我不知道如何解析你的問題。我在代碼中的任何地方都看不到對$ query2的引用。 – 2011-06-01 14:53:34

+0

在兩個子查詢中拆分主查詢將無法解決php的最大執行時間問題。 – 2011-06-01 14:55:55

回答

4

多個查詢,如果有的話,只會減慢速度。這意味着編譯/執行多個查詢,所以最終會帶來更多的開銷。在查詢中使用explain以查看正在使用哪些索引(並且未使用索引),並在需要時添加更多索引。在某些情況下,事情自然會變得緩慢。畢竟,你在7張桌子上進行8路加入。

明顯的檢查索引用法的地方:wherejoin條款中的每個字段。

+0

按照David Chan的建議設置超時,然後在查詢和該過程的其他各個部分之前和之後發出一些時間標記。你會了解所涉及的時間。 – horatio 2011-06-01 15:00:37

1

在該php INI

,或者你可以添加到您的腳本的頂部,你可以改變的max_execution_time指令

set_time_limit (0); 

零是無限的

+0

如果你有一個未終止的循環或什麼東西掛起的情況下,將限制設置爲特定和已知的情況會更好嗎?在執行AFAIK期間,您可以多次更改限制 – horatio 2011-06-01 14:58:55