2013-10-14 171 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