我随便使用phpMyAdmin建立了一个网站,并创建了以下SQL代码以获取前10天的最频繁价格,并将其与当前价格进行比较以确定价格下降。但是,我遇到的问题是,由于产品和价格的数量,它需要大约10秒钟才能运行。我正在寻找改善查询以减少网页加载时间的方法。
我有三个表产品,product_categories,价格。 product_categories是我要过滤的数据。表结构如下,但是不包括一些不相关的列:
价格表:
product_category表:
产品表:
SELECT currentPrice as 'prodprice', meanPrice, priceDiff, prodDetails.sku, prodDetails.product_name, prodDetails.img FROM (
SELECT products.sku, products.product_name, products.img, products.product_type, products.retailer, category_1, category_2, category_3 FROM products INNER JOIN product_categories on product_categories.product_type = products.product_type
WHERE category_3 = 'Laptops') as prodDetails INNER JOIN (
SELECT prices.product_price as 'currentPrice', prices.sku, prices.retailer, meanPrice, (prices.product_price/meanPrice)-1 as 'priceDiff' FROM prices RIGHT JOIN(
SELECT MaxCountSub.sku, CountSub.product_price as 'meanPrice', CountSub.retailer
FROM (SELECT prices.sku, prices.product_price, retailer, Count(prices.product_price) AS CountOfValue
FROM prices
WHERE price_date > CURRENT_DATE-10
GROUP BY prices.sku, prices.product_price) As CountSub
INNER JOIN
(SELECT dT.sku, Max(CountOfValue) As MaxCountOfValue, dT.retailer
FROM
(SELECT prices.sku, prices.product_price, retailer, Count(prices.product_price) AS CountOfValue
FROM prices WHERE price_date > CURRENT_DATE-10 GROUP BY prices.sku, prices.product_price) As dT
GROUP BY dT.sku) As MaxCountSub
ON CountSub.sku = MaxCountSub.sku
AND CountSub.CountOfValue = MaxCountSub.MaxCountOfValue
AND MaxCountSub.retailer = CountSub.retailer) as b
on b.retailer = prices.retailer and b.sku = prices.sku
WHERE prices.price_date = CURRENT_DATE ORDER BY priceDiff ASC ) c on c.retailer = prodDetails.retailer and c.sku = prodDetails.sku
ORDER BY `c`.`priceDiff` ASC LIMIT 8
产品表
+----------------+--------+----------+---------------+
| product_type | sku | retailer | product_name |
+----------------+--------+----------+---------------+
| 2 in 1 | 475750 | J | Dell Inspiron |
| 2 in 1 Devices | 324155 | H | Lenovo Chrome |
| 2 in 1 Devices | 311148 | H | HP Pavilion x |
| 2 in 1 Devices | 332274 | H | HP Envy x360 |
| 2 in 1 Devices | 332261 | H | Lenovo Ideapa |
| 2 in 1 Devices | 312084 | H | HP Spectre x3 |
| Laptop | 473661 | J | Lenovo IdeaPa |
| Laptop | 482998 | J | Asus VivoBook |
| Laptops | 331133 | H | Acer Nitro 5 |
| Laptops | 336095 | H | Lenovo Ideapa |
| Laptops | 326802 | H | Predator Heli |
| Laptops | 326712 | H | Acer Nitro 5 |
| Laptops | 326601 | H | Acer Nitro 5 |
| Laptops | 326589 | H | Predator Trit |
| Laptops | 336100 | H | Lenovo Legion |
| Laptops | 336101 | H | Lenovo Legion |
| Laptops | 311527 | H | Acer Swift 5 |
| Laptops | 295323 | H | HP 14-inch Ce |
| Laptops | 332257 | H | Lenovo Ideapa |
| Laptops | 327328 | H | Acer Aspire 5 |
+----------------+--------+----------+---------------+
价格表
+--------+---------------+------------+----------+
| sku | product_price | price_date | retailer |
+--------+---------------+------------+----------+
| 295323 | 358 | 20/09/2020 | H |
| 295323 | 358 | 21/09/2020 | H |
| 295323 | 448 | 22/09/2020 | H |
| 311148 | 1088 | 20/09/2020 | H |
| 311148 | 1088 | 21/09/2020 | H |
| 311148 | 1399 | 22/09/2020 | H |
| 311527 | 1099 | 20/09/2020 | H |
| 311527 | 1099 | 21/09/2020 | H |
| 311527 | 1799 | 22/09/2020 | H |
| 312084 | 1688 | 20/09/2020 | H |
| 312084 | 1688 | 21/09/2020 | H |
| 312084 | 1999 | 22/09/2020 | H |
| 326712 | 1899 | 20/09/2020 | H |
| 326712 | 1899 | 21/09/2020 | H |
| 326712 | 2399 | 22/09/2020 | H |
| 327328 | 1098 | 20/09/2020 | H |
| 327328 | 1098 | 21/09/2020 | H |
| 327328 | 1399 | 22/09/2020 | H |
| 332261 | 999 | 20/09/2020 | H |
| 332261 | 999 | 21/09/2020 | H |
| 332261 | 1199 | 22/09/2020 | H |
| 332274 | 1799 | 20/09/2020 | H |
| 332274 | 1799 | 21/09/2020 | H |
| 332274 | 2199 | 22/09/2020 | H |
| 336095 | 1349 | 20/09/2020 | H |
| 336095 | 1349 | 21/09/2020 | H |
| 336095 | 1799 | 22/09/2020 | H |
| 482998 | 763.3 | 20/09/2020 | J |
| 482998 | 763.3 | 21/09/2020 | J |
| 482998 | 763.3 | 22/09/2020 | J |
+--------+---------------+------------+----------+
产品类别表
+----------------+-------------+------------+------------+
| product_type | category_1 | category_2 | category_3 |
+----------------+-------------+------------+------------+
| 2 in 1 | Electronics | Computers | Laptops |
| 2 in 1 Devices | Electronics | Computers | Laptops |
| Chromebook | Electronics | Computers | Laptops |
| Laptop | Electronics | Computers | Laptops |
| Laptops | Electronics | Computers | Laptops |
| MacBook Air | Electronics | Computers | Laptops |
| MacBook Pro | Electronics | Computers | Laptops |
| Netbook | Electronics | Computers | Laptops |
| Ultrabook | Electronics | Computers | Laptops |
| 2 in 1 Laptops | Electronics | Computers | Laptops |
+----------------+-------------+------------+------------+