2

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

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 );
4

2 回答 2

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);

注意:总行数将为您提供计数器的值

于 2013-10-14T10:00:54.233 回答
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或查询组件的类似属性来获取计数器值。

于 2013-10-14T10:24:11.917 回答