【问题标题】:PHP: Query Exceeding Max Time (How to handle this)PHP:查询超过最大时间(如何处理)
【发布时间】:2011-06-01 14:47:27
【问题描述】:

好的。我最终为我的 php 文件编写了查询(它将查询写入 txt 文件)。但是,当我早些时候测试文件时,我显然超出了最大执行时间。我只使用一个大查询来完成这项工作,但现在我想知道是否最好使用多个较小的查询。 这会减少执行时间吗?

如果是这样,那么我还有一个问题要问大家,我将如何解决这个问题?这是我文件的大部分内容:

// 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."
?>

如您所见,我将 $query 拉入 $results,然后使用它将其插入 $row。 我想我的实际问题是(如果可能的话),我该如何处理 $query 和 $query2 并将 $query2 插入到同一个 while 语句中?

【问题讨论】:

  • 我不确定如何解析您的问题。我在代码中的任何地方都没有看到对 $query2 的引用。
  • 将主查询拆分为两个子查询并不能解决 php 的最大执行时间问题。

标签: php mysql variables join performance


【解决方案1】:

如果有的话,多个查询只会减慢速度。这意味着编译/执行多个查询,因此最终会产生更多开销。在查询中使用explain 以查看正在使用(和未使用)哪些索引,并在需要时添加更多索引。在某些情况下,事情自然会很慢。毕竟,您正在对 7 个表进行 8 路连接。

检查索引使用情况的明显位置:wherejoin 子句中的每个字段。

【讨论】:

  • 按照 David Chan 的建议设置超时,然后设置在查询之前和之后发出一些时间标记以及过程的其他各个部分。您会对所涉及的时间有所了解。
【解决方案2】:

您可以在 php ini 中更改 max_execution_time 指令

或者您可以将其添加到脚本的顶部

set_time_limit ( 0 );

零是无限的

【讨论】:

  • 如果你有一个未终止的循环或挂起的东西,最好将限制设置为特定的和已知的?您可以在执行期间多次更改限制 AFAIK
猜你喜欢
  • 1970-01-01
  • 2022-08-04
  • 2019-04-24
  • 2017-01-23
  • 2011-05-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-03-25
相关资源
最近更新 更多