如何让这个mysql查询运行得更快?

arknldoa  于 2021-06-18  发布在  Mysql
关注(0)|答案(1)|浏览(352)

我在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`
plupiseo

plupiseo1#

请确保在stock2上为groupby中的列创建了一个复合索引

create index  idx1 on stock2 ( PhoneModal_Name,  Rate, Unit_Name,
                               STORE_NAME, PhoneModal_No )

相关问题