【发布时间】: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