什么是优化mysql查询的好方法?

时间:2015-03-24 09:39:53

标签: mysql optimization

这是我的第一个问题......

我有一张桌子......"职位"

并且对应于此表中的每一行,在另一个表中有一行" calcPositions"。

所以这个(positions.getNumberOfRows == calcPositions.getNumberOfRows)是真的。

每当我想改变某些事情时,"位置"我需要更新" calcPositions"表..

这就是我创建程序的原因:" calcPositionsUpdate"。

DROP PROCEDURE IF EXISTS calcPositionsUpdate;
delimiter |
CREATE PROCEDURE calcPositionsUpdate(pId INT) BEGIN
    DELETE FROM calcPositions WHERE Id = pId;
    INSERT INTO calcPositions (Id, p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, p11, p12, p13, p14, p15, p16)
    SELECT
    cip.Id,
    SUM((COALESCE(cip.cost, cip.price) * COALESCE(CAST(cip.quantity AS SIGNED), 1)) * cip.modifier * (if(cip.type=0,1,0))) ,
    SUM(cip.modifier * cip.price * COALESCE(CAST(cip.quantity AS SIGNED), 1) * (if(cip.type=0,1,0)))*     ci.modifier ,
    SUM(cip.modifier * cip.priceGross * COALESCE(CAST(cip.quantity AS SIGNED), 1)* (if(cip.type=0,1,0)))*ci.modifier  ,
    COALESCE(SUM(cip.quantity),0)* (if(cip.type=0,1,0)),    
    SUM((COALESCE(cip.cost, cip.price) * COALESCE(CAST(cip.quantity AS SIGNED), 1))* (if(cip.type=1,1,0))) * cip.modifier  ,
    SUM(cip.modifier * cip.price * COALESCE(CAST(cip.quantity AS SIGNED), 1)* (if(cip.type=1,1,0)))*     ci.modifier  ,
    SUM(cip.modifier * cip.priceGross * COALESCE(CAST(cip.quantity AS SIGNED), 1)* (if(cip.type=1,1,0)))*ci.modifier  ,
    COALESCE(SUM(cip.quantity),0)* (if(cip.type=1,1,0)),    
    SUM((COALESCE(cip.cost,cip.price) * COALESCE(CAST(cip.quantity AS SIGNED), 1))* (if(cip.type=2,1,0))) * cip.modifier  ,
    SUM(cip.modifier * cip.price * COALESCE(CAST(cip.quantity AS SIGNED), 1)* (if(cip.type=2,1,0)))*     ci.modifier ,
    SUM(cip.modifier * cip.priceGross * COALESCE(CAST(cip.quantity AS SIGNED), 1)* (if(cip.type=2,1,0)))*ci.modifier  ,
    COALESCE(SUM(cip.quantity),0)* (if(cip.type=2,1,0)),    
    SUM((COALESCE(cip.cost,cip.price) * COALESCE(CAST(cip.quantity AS SIGNED), 1)) * (if(cip.type=3,1,0))) * cip.modifier ,
    SUM(cip.modifier * cip.price * COALESCE(CAST(cip.quantity AS SIGNED), 1) * (if(cip.type=3,1,0)))*     ci.modifier ,
    SUM(cip.modifier * cip.priceGross * COALESCE(CAST(cip.quantity AS SIGNED), 1) * (if(cip.type=3,1,0)))*ci.modifier ,
    COALESCE(SUM(cip.quantity),0)* (if(cip.type=3,1,0))
    FROM
    positions cip
    LEFT JOIN
    customerInvoice ci
    ON
    ci.id = cip.customerInvoiceId
    WHERE
    cip.customerInvoiceId = pCustomerInvoiceId; 
END;

目前在"位置"中更新了60 000行表,以及大约需要10个小时的每一行的适当程序..

那么你怎么看?是否有可能提高这一行动的速度,以及如何......

根据您的经验,解决此类问题的技术是什么。

提前致谢..感谢您的时间和精力。

0 个答案:

没有答案