Opencart Journal 主题的一个脚本效率问题
事情起因:
最近一个客户安装好Journal 后,通过火车头导入商品数据后,数据一超过200个,网站首页就502了。
分析过程:
服务器资源爆了,首先考虑当然是服务器的参数设置问题,内存,php内存以及nginx/mysql一通优化猛如虎,然而并没有鸟用…..
打开 mysql 慢日志,看到:
# Time: 2023-07-07T06:20:01.676865Z
# User@Host: sql_hallowe[sql_hallowe] @ localhost [] Id: 1954
# Query_time: 100.490589 Lock_time: 0.000350 Rows_sent: 22529 Rows_examined: 46456030
SET timestamp=1688710801;
SELECT
product_id,
image
FROM oc_product_image
WHERE sort_order = (
SELECT MIN(sort_order)
FROM oc_product_image AS p
WHERE p.product_id = oc_product_image.product_id
);
到数据库去数据库执行,果然数据库100秒后崩溃…..
查看数据表:
该表居然有50万行之多,考虑到客户商品只有3000上下,事情必有异常
SELECT
product_id, count(product_id) as total
FROM oc_product_image GROUP BY product_id SORT BY total DESC;
显示有超过一百多商品,每个都有多达2400-5000个图像,应该是采集出错了
回头看下代码:
catalog\model\journal3\product.php #86行
public function getProductsSecondImage() {
$cache_key = 'product.image';
$results = $this->journal3->cache->get($cache_key);
if ($results === false) {
$results = array();
$query = $this->db->query("
SELECT
product_id,
image
FROM {$this->dbPrefix('product_image')}
WHERE sort_order = (
SELECT MIN(sort_order)
FROM {$this->dbPrefix('product_image')} AS p
WHERE p.product_id = {$this->dbPrefix('product_image')}.product_id
)
");
实际这脚本效率低下的很,可以改为(未验证结果,但速度确实是大幅提升)
public function getProductsSecondImage() {
$cache_key = 'product.image';
$results = $this->journal3->cache->get($cache_key);
if ($results === false) {
$results = array();
$query = $this->db->query("
SELECT
product_id,
image, MIN(sort_order)
FROM {$this->dbPrefix('product_image')}
GROUP BY product_id
");