加快MySql中的查询执行速度

时间:2014-02-26 11:55:34

标签: mysql performance

我正在尝试在MySql中执行此查询:

SELECT areamaster.areaname, 
       builtydetails.builtycode, 
       builtydetails.builtyno, 
       builtydetails.builtydate, 
       builtydetails.pvtmarks, 
       areamaster_to.areaname, 
       stockview.pkgs, 
       stockview.state, 
       areamaster_from.areaname, 
       builtydetails.actualweight, 
       builtydetails.chargedweight, 
       consigner.customername, 
       consignee.customername, 
       stockview.calc, 
       stockview.stockdate 
FROM   (((((designpl_snps.stockview stockview 
            INNER JOIN designpl_snps.areamaster areamaster 
                    ON stockview.location = areamaster.areacode) 
           INNER JOIN designpl_snps.builtydetails builtydetails 
                   ON stockview.builtycode = builtydetails.builtycode) 
          INNER JOIN designpl_snps.areamaster areamaster_to 
                  ON builtydetails.toloc = areamaster_to.areacode) 
         INNER JOIN designpl_snps.areamaster areamaster_from 
                 ON builtydetails.fromloc = areamaster_from.areacode) 
        INNER JOIN designpl_snps.customermaster consigner 
                ON builtydetails.customercode = consigner.customercode) 
       INNER JOIN designpl_snps.customermaster consignee 
               ON builtydetails.customercode_consignee = consignee.customercode 
ORDER  BY areamaster.areaname, 
          builtydetails.builtycode  

但执行时间太长了。使用这个或任何其他mysql查询可以更快地运行的方法和技巧是什么?

2 个答案:

答案 0 :(得分:0)

在MySQL中,这不是一件容易的事。我不知道像MSSQL中的查询分析工具。也许你可以用mtop尝试一下。关于这个主题还有另一个好主题:Best MySQL performance tuning tool?

尝试将查询放入VIEW中。因为mysql缓存查询的结果。这意味着,查询第一次需要更长时间,之后会加快速度,因为服务器不需要执行整个查询。

另一个选项是EXPLAIN命令,请参见此处:http://www.sitepoint.com/using-explain-to-write-better-mysql-queries/

我希望它有所帮助

干杯

答案 1 :(得分:0)

(1):您应该使用连接上的索引并按列排序

(2):尝试使用where代替inner join。 Where子句将带来更快的结果inner join

SELECT areamaster.areaname, 
       builtydetails.builtycode, 
       builtydetails.builtyno, 
       builtydetails.builtydate, 
       builtydetails.pvtmarks, 
       areamaster_to.areaname, 
       stockview.pkgs, 
       stockview.state, 
       areamaster_from.areaname, 
       builtydetails.actualweight, 
       builtydetails.chargedweight, 
       consigner.customername, 
       consignee.customername, 
       stockview.calc, 
       stockview.stockdate 
FROM   designpl_snps.stockview stockview ,
       designpl_snps.areamaster areamaster ,
       designpl_snps.builtydetails builtydetails ,
       designpl_snps.areamaster areamaster_to ,
       designpl_snps.areamaster areamaster_from ,
       designpl_snps.customermaster consigner ,
       designpl_snps.customermaster consignee 

 WHERE

  stockview.location = areamaster.areacode and 
  stockview.builtycode =   builtydetails.builtycode and 
  builtydetails.toloc = areamaster_to.areacode and 
  builtydetails.fromloc = areamaster_from.areacode and 
  builtydetails.customercode = consigner.customercode and  
  builtydetails.customercode_consignee = consignee.customercode 

ORDER  BY areamaster.areaname, 
          builtydetails.builtycode