我有一个包含 500 万行的表。 示例
+----+-----------+---------+-------
| product | date | count |
+-------------+------------+-----------+
| платье | 04-01-2024 | 186574 |
| штаны | 04-01-2024 | 20564 |
| кепка | 07-01-2024 | 104443 |
| штаны | 06-01-2024 | 10574 |
| платье | 06-01-2024 | 223001 |
+-------------+------------+-----------+
我需要选择具有 2 个自定义日期的行,然后如果产品 name1 = name2 获得计数之间的差异,然后获得此差异的前 100 名。
现在我创建 2 个选择,然后循环遍历数组来获取它。但我有关于记忆的错误。我如何在 mysql 中创建 1 个查询?
我的询问
$get_ex = $connection->prepare("SELECT * FROM product WHERE date = :date");
$get_ex->execute(array(':date' => $first_date));
$ex_list_2 = $get_ex->fetchAll(PDO::FETCH_ASSOC);
$get_ex_3 = $connection->prepare("SELECT * FROM product WHERE date = :date");
$get_ex_3->execute(array(':date' => $second_date));
$ex_list_3 = $get_ex_3->fetchAll(PDO::FETCH_ASSOC);
$result = Array();
$i=0;
foreach($ex_list_2 as $key => $value)
{
foreach($ex_list_3 as $key2 => $value2)
{
if($value['product_name'] == $value2['product_name'])
{
$result[$i]['product_name'] = $value['product_name'];
$result[$i]['count_1'] = $value['count'];
$result[$i]['count_2'] = $value2['count'];
$result[$i]['res'] = $value['count']- $value2['count'];
$i++;
}
}
}
和结果-
致命错误:允许的内存大小 1073741824 字节已耗尽
内存限制= 2048mb
对于结果,我需要带有下一个信息的数组
+-------------+-------------+-------------+-----------+
| product | 04-01-2024 | 06-01-2024 | diff |
+-------------+-------------+-------------+-----------+
| платье | 186574 | 223001 | 36427 |
| штаны | 20564 | 10574 | 99000 |
+-------------+-------------+-------------+-----------+
无法使用 order by 并且在 mysql 中出现错误 1317
不要执行两个查询,使用单个
JOIN
查询。
SELECT t1.product_name, t1.count AS count1, t2.count AS count2, t1.count - t2.count AS res
FROM product AS t1
JOIN product AS t2 ON t1.product_name = t2.product_name
WHERE t1.date = :first_date AND t2.date = :second_date
ORDER BY res DESC
LIMIT 100
为了优化查询并避免内存问题,您可以使用单个 SQL 查询来执行所有必要的操作。目的是将表与产品名称本身连接起来,按两个特定日期过滤行,计算计数差异,然后对结果进行排序以获得前 100 个差异。这种方法效率更高,因为它利用数据库引擎进行计算,减少应用程序的内存负载。
这是一个示例 SQL 查询,应该可以实现您正在寻找的内容:
SELECT
t1.product,
t1.count AS date1,
t2.count AS date2,
t2.count - t1.count AS diff
FROM
product AS t1
JOIN
product AS t2 ON t1.product = t2.product
WHERE
t1.date = '2024-01-04' AND
t2.date = '2024-01-06'
ORDER BY
ABS(diff) DESC
LIMIT 100;
以下是如何调整 PHP 代码以使用新的 SQL 查询:
try {
// Assuming $connection is your PDO connection
$first_date = '2024-01-04'; // Set your first date
$second_date = '2024-01-06'; // Set your second date
// Prepare the optimized SQL query
$sql = "
SELECT
t1.product,
t1.count AS date1,
t2.count AS date2,
t2.count - t1.count AS diff
FROM
product AS t1
JOIN
product AS t2 ON t1.product = t2.product
WHERE
t1.date = :first_date AND
t2.date = :second_date
ORDER BY
ABS(diff) DESC
LIMIT 100";
$stmt = $connection->prepare($sql);
// Bind the parameters
$stmt->bindParam(':first_date', $first_date);
$stmt->bindParam(':second_date', $second_date);
// Execute the query
$stmt->execute();
// Fetch the results
$result = $stmt->fetchAll(PDO::FETCH_ASSOC);
// Output or process the results as needed
foreach ($result as $row) {
echo "Product: " . $row['product'] . ", Count on " . $first_date . ": " . $row['date1'] . ", Count on " . $second_date . ": " . $row['date2'] . ", Difference: " . $row['diff'] . "<br/>";
}
} catch (Exception $e) {
echo "Error: " . $e->getMessage();
}