2013-10-14 104 views
2

这个查询需要3秒钟,我想让它运行得更快。 请提供任何建议如何优化这个sql查询

SELECT Concat(e.estimate1, '-', e.estimate2) AS estimateid, 
     e.estimatetype, 
     e.createdby, 
     e.estimateid AS estID, 
     e.`layoutnumber`, 
     sd.specno, 
     sd.samplenumber, 
     sd.numberon, 
     c.customerid, 
     c.custprosname, 
     c.`custtype`, 
     (SELECT Count(*) 
     FROM (SELECT e.estimate1 
       FROM `simpleestimatedetails` sd, 
         estimatemaster e, 
         `vcustomer_prospect` c 
       WHERE c.customerid IN (e.customernumber, e.prospectnumber) 
         AND (e.estimate1 LIKE '%1%') 
         AND (sd.`simpleestid` = e.estimateid)) AS counter) AS 
     counter 
FROM `simpleestimatedetails` sd, 
     estimatemaster e, 
     `vcustomer_prospect` c 
WHERE c.customerid IN (e.customernumber, e.prospectnumber) 
     AND (e.estimate1 LIKE '%1%') 
     AND (sd.`simpleestid` = e.estimateid); 
+2

使用[说明](http://dev.mysql.com/doc/refman/5.0/en/explain.html)来分析您的查询 – VeNoMiS

+0

对不起,我没有那access.I猜上述查询doesnot需要任何description.we只需要使用不同的方法 – Samir

+0

对不起,你被分配优化一个查询,你不能运行EXPLAIN?人们如何期待你这样做?你有没有一台开发机器(你有权利),可以测试重写,索引和分析? –

回答

1

在您的SQL查询'计数器'调用多个表的冗余连接。

请忽略计数器列,并尝试将值作为上一次从SQl查询返回的总行数。

希望这会提高查询性能。你会被下面的查询

select concat(e.estimate1,'-',e.estimate2) as estimateid, 
     e.estimatetype, 
     e.CreatedBy, 
     e.EstimateID as estID, 
     e.`LayoutNumber`, 
      sd.specNo, 
      sd.SampleNumber, 
      sd.NumberON, c.customerid, 
      c.CustProsName, 
      c.`CustType` 
      from `simpleestimatedetails` sd, 
       estimatemaster e, 
       `vcustomer_prospect` c 
      where c.customerid in (e.customernumber,e.ProspectNumber) 
      and (e.estimate1 like '%1%') 
       and (sd.`SimpleEstID`=e.estimateid); 

音符得到期望的结果:行总数会给你值计数器的

+0

我没有得到counterId。 它是一个重要的专栏。请提供一些建议。 – Samir

+0

正如所建议的那样,计数器值可以在处理从SQL查询中获取的值时进行。它可以像计算提取的行数一样简单。 –

+0

你能告诉我如何? – Samir

1
SELECT Concat(e.estimate1, '-', e.estimate2) AS estimateid, 
     e.estimatetype, 
     e.createdby, 
     e.estimateid AS estID, 
     e.`layoutnumber`, 
     sd.specno, 
     sd.samplenumber, 
     sd.numberon, 
     c.customerid, 
     c.custprosname, 
     c.`custtype` 
    FROM estimatemaster e Inner Join 
     `vcustomer_prospect` c 
    On c.customerid IN (e.customernumber, e.prospectnumber) 
Inner Join `simpleestimatedetails` sd 
    On sd.`simpleestid` = e.estimateid 
WHERE e.estimate1 LIKE '%1%' 

注:我已经删除了计数器列。如果您是从某个前端执行此操作,则可以通过检查RowsAffectedRowCountRecordsCount或查询组件的Somthing Similar属性来获取计数器值。

+0

如何使用rowcount得到计数器? – Samir

+0

由于您的'计数器'查询与主要查询具有相同的'from'和'where'子句。因此,您可以删除该列并将该组件的属性用于“counter”。 – Romesh

+0

请告诉我一些关于如何使用组件的property.Heard第一次 – Samir