查询有效。使用10.000 procuts,需要11秒。如果我不使用ORDER BY,则仅需1秒。但我需要ORDER BY。
我们可以优化它吗?
SELECT
u.urunID,
i.urunadi,
u.seo,
u.stok_kodu,
u.kstok_sayisi,
u.stok_sayisi,
u.goruntuleme,
(SELECT SUM(su.adet) FROM siparis_urunler su LEFT JOIN siparis s ON s.siparisID = su.siparisID WHERE s.durum_id NOT IN (26, 24) AND su.urunID = u.urunID) AS sadet
FROM
urunler u
INNER JOIN urun_isim i ON u.urunID = i.urunID
WHERE
u.stok_sayisi <= u.kstok_sayisi
AND u.durum = 1
GROUP BY
u.urunID
ORDER BY
sadet DESC
LIMIT 0, 20
说明:
+----+--------------------+-------+--------+---------------------------------+-----------+---------+-----------------------------+------+----------------------------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+--------------------+-------+--------+---------------------------------+-----------+---------+-----------------------------+------+----------------------------------------------+
| 1 | PRIMARY | i | index | PRIMARY,urunadi2 | urunadi | 768 | NULL | 4997 | Using index; Using temporary; Using filesort |
| 1 | PRIMARY | u | eq_ref | PRIMARY,urunID,urunler,urunler2 | PRIMARY | 4 | katalog_db.i.urunID | 1 | Using where |
| 3 | DEPENDENT SUBQUERY | sp | ALL | NULL | NULL | NULL | NULL | 11 | Using where |
| 2 | DEPENDENT SUBQUERY | s | ALL | PRIMARY,siparis | NULL | NULL | NULL | 805 | Using where |
| 2 | DEPENDENT SUBQUERY | su | ref | surunler2 | surunler2 | 10 | katalog_db.s.siparisID,func | 1 | Using where |
+----+--------------------+-------+--------+---------------------------------+-----------+---------+-----------------------------+------+----------------------------------------------+
答案 0 :(得分:1)
这样运行得更快吗?
SELECT
u.urunID,
i.urunadi,
u.seo,
u.stok_kodu,
u.kstok_sayisi,
u.stok_sayisi,
u.goruntuleme,
SUM(su.adet) AS sadet
FROM
urunler u
INNER JOIN urun_isim i ON u.urunID = i.urunID
INNER JOIN siparis_urunler su ON su.urunID = u.urunID
LEFT JOIN siparis s ON s.siparisID = su.siparisID
WHERE
u.stok_sayisi <= u.kstok_sayisi
AND s.durum_id NOT IN (26, 24)
AND u.durum = 1
GROUP BY
u.urunID,
i.urunadi,
u.seo,
u.stok_kodu,
u.kstok_sayisi,
u.stok_sayisi,
u.goruntuleme
ORDER BY 8 DESC