[英]Optimizing MySQL SELECT query with subqueries
Hi is it possible to optimize the SELECT query below?嗨,可以优化下面的 SELECT 查询吗? The query itself is working but when we are querying large data we are encountering a message in php which is "Maximum execution time of 30 seconds exceeded".
查询本身正在运行,但是当我们查询大数据时,我们在 php 中遇到一条消息,即“超过 30 秒的最大执行时间”。 I reduced the columns in the query up to
Topping3
but I am querying up to Topping15
column.我将查询中的列减少到
Topping3
但我查询到Topping15
列。
SELECT
itemID,
itemName,
Topping1,
(SELECT DISTINCT Description FROM items WHERE PLU = a.Topping1 AND ClientID = 1679) AS Top1_desc,
Topping2,
(SELECT DISTINCT Description FROM items WHERE PLU = a.Topping2 AND ClientID = 1679) AS Top2_desc,
Topping3,
(SELECT DISTINCT Description FROM items WHERE PLU = a.Topping2 AND ClientID = 1679) AS Top3_desc,
FROM
items a
WHERE
...
Current data on items
table items
表上的当前数据
--------------------------------------------------------------------
| itemID | itemName | Description | Topping1 | Topping2 | Topping3 |
--------------------------------------------------------------------
| 1 | HAM1 | Hamburger | ONI1 | TOMO1 | |
--------------------------------------------------------------------
| 2 | ONI1 | Onion | | | |
--------------------------------------------------------------------
| 3 | TOMO1 | Tomato | | | |
--------------------------------------------------------------------
and this is the expected result这是预期的结果
--------------------------------------------------------------------------------------------------------
| itemID | itemName | Description | Topping1 | Top1_desc | Topping2 | Top2_desc | Topping3 | Top3_desc |
--------------------------------------------------------------------------------------------------------
| 1 | HAM1 | Hamburger | ONI1 | Onion | TOMO1 | Tomato | | |
--------------------------------------------------------------------------------------------------------
| 2 | ONI1 | Onion | | | | | | |
--------------------------------------------------------------------------------------------------------
| 3 | TOMO1 | Tomato | | | | | | |
--------------------------------------------------------------------------------------------------------
This should be fast, unless there are a lot of entries with the same client ID.这应该很快,除非有很多条目具有相同的客户端 ID。 You could add
LIMIT 1
after all subqueries, ie: (SELECT DISTINCT Description FROM items WHERE PLU = a.Topping1 AND ClientID = 1679 LIMIT 1)
etc.您可以在所有子查询后添加
LIMIT 1
,即: (SELECT DISTINCT Description FROM items WHERE PLU = a.Topping1 AND ClientID = 1679 LIMIT 1)
等。
I suspect however that it is an index problem.然而,我怀疑这是一个索引问题。 Are the fields ClientID and PLU indexed?
ClientID 和 PLU 字段是否已编入索引?
EDIT: Alternative for your query:编辑:您查询的替代方案:
SELECT
itemID,
itemName,
Topping1,
t1.Description AS Top1_desc,
Topping2,
t2.Description AS Top2_desc,
Topping3,
t3.Description AS Top3_desc,
FROM
items a
LEFT JOIN
items t1 ON t1.PLU=a.Topping1 AND t1.ClientID = 1679
LEFT JOIN
items t2 ON t2.PLU=a.Topping2 AND t2.ClientID = 1679
LEFT JOIN
items t3 ON t3.PLU=a.Topping3 AND t3.ClientID = 1679
WHERE
...
GROUP BY
a.itemID
Fields itemID
, PLU
and ClientID
need indexes.字段
itemID
、 PLU
和ClientID
需要索引。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.