如何使此MySQL查询运行得更快?

时间:2018-12-02 16:22:44

标签: mysql performance

我在MySQL数据库中有以下查询,执行该查询最多需要1.9秒,我想使其运行更快

select 
    1 AS `auto`,
    `stock2`.`PhoneModal_Name` AS `PhoneModal_Name`,
    `stock2`.`Rate` AS `Rate`,
    (sum(`stock2`.`Quantity_take`) - sum(`stock2`.`Quantity_give`)) AS `balance`,
    `stock2`.`Unit_Name` AS `Unit_Name`,
    `stock2`.`STORE_NAME` AS `STORE_NAME`,
    `stock2`.`PhoneModal_No` AS `PhoneModal_No`,
    ((1 / 1) / 2018) AS `orderdate`,
    sum(`stock2`.`Quantity_take`) AS `take`,
    sum(`stock2`.`Quantity_give`) AS `give`
from
    `stock2`
group by `stock2`.`PhoneModal_Name` , `stock2`.`Rate` , `stock2`.`Unit_Name` ,
         `stock2`.`STORE_NAME` , `stock2`.`PhoneModal_No`

1 个答案:

答案 0 :(得分:0)

请确保您对group2中的列在stock2上具有复合索引

{|f| f.name == 'b'}