Mysql 如何优化此sql查询
这个查询需要3秒钟,我想让它运行得更快。 请提供任何建议Mysql 如何优化此sql查询,mysql,sql,Mysql,Sql,这个查询需要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,
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 );
在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 );
请忽略计数器列,并尝试获取值作为上一次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 );
希望这将提高查询性能。通过下面的查询,您将得到所需的结果
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 );
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);
注意:行的总数将给出计数器的值
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 );
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%'
注意:我已删除计数器列。如果您是从某个前端执行此操作,则可以通过检查
RowsAffected
或RowCount
或RecordsCount
或查询组件的类似属性来获取计数器值。用于分析您的查询。对不起,我没有该访问权限。我想上面的查询不需要任何描述。我们只是必须使用不同的方法Xcuse me,您被指派优化查询,但无法运行EXPLAIN?有人希望你怎么做?难道你没有一台开发机器,在那里你(有权利)可以测试重写、索引和分析吗?是的,我就是这样。我一拿到它就把它放在这里。但在那之前,你不能在没有先看到解释输出的情况下给我提出任何建议。我不会得到反ID。这是一个重要的列。请提供一些建议。按照建议,在处理从SQL查询获取的值时,计数器值可以放在最后。它可以像计算获取的行数一样简单。如何使用rowcount获取计数器?因为您的计数器
查询具有与主查询相同的from
和where
子句。因此,您可以删除该列并使用组件的属性作为计数器
。请告诉我如何使用组件的属性。第一次听说它您使用的前端是什么?另外,请在你的帖子中显示你的查询代码,请留下我的答案。如果你有Mysql作为前端,就意味着你可以访问一些查询你的Mysql数据库的工具。那么根据ypercube在你的帖子中的评论,为什么你不能解释一下这个命令呢?
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 );