select* from (SELECT p1.productId AS productId,p1.productCode AS productCode,p1.productName AS productName,p1.productTypeId AS productTypeId,p1.modelId AS modelId,p1.specificationsId AS specificationsId,p1.productClassification AS productClassification,p1.daughterDoard AS daughterDoard,p1.createDate AS createDate ,p1.createUserId AS createUserId,p1.updateDate AS updateDate,p1.updateUserId AS updateUserId,p1.productRemark AS productRemark from tbl_product as p1 where p1.productId > 0) as p inner join (select pt1.productTypeId AS productTypeId,pt1.productTypeName AS productTypeName,pt1.productTypeLetter AS productTypeLetter from tbl_product_type as pt1 where pt1.productTypeId > 0) as pt on pt.productTypeId = p.productTypeId left join (select pm1.modelId AS modelId,pm1.modelName AS modelName from tbl_product_model pm1 where pm1.modelId > 0) pm on pm.modelId = p.modelId left join (select ps1.specificationId AS specificationId,ps1.specificationName AS specificationName from tbl_product_specification as ps1 where ps1.specificationId > 0) as ps on ps.specificationId =p.specificationsId:请优化下以上语句
时间: 2024-04-23 11:25:11 浏览: 97
首先,可以将子查询的表格单独提出来,避免在主查询中使用子查询。然后,可以考虑使用联结查询来代替内部查询和左外部查询。
优化后的查询语句如下:
SELECT p.productId AS productId,
p.productCode AS productCode,
p.productName AS productName,
p.productTypeId AS productTypeId,
pt.productTypeName AS productTypeName,
pt.productTypeLetter AS productTypeLetter,
pm.modelName AS modelName,
p.specificationsId AS specificationId,
ps.specificationName AS specificationName,
p.productClassification AS productClassification,
p.daughterDoard AS daughterDoard,
p.createDate AS createDate,
p.createUserId AS createUserId,
p.updateDate AS updateDate,
p.updateUserId AS updateUserId,
p.productRemark AS productRemark
FROM tbl_product AS p
INNER JOIN tbl_product_type AS pt ON pt.productTypeId = p.productTypeId
LEFT JOIN tbl_product_model AS pm ON pm.modelId = p.modelId
LEFT JOIN tbl_product_specification AS ps ON ps.specificationId = p.specificationsId
WHERE p.productId > 0;
这个查询将返回与原查询相同的结果,但是运行速度可能会更快。
阅读全文