Mysql SQL-可以重新考虑这一点以提高效率吗?
在一些查询中有大量冗余。我已经创建了一个子查询ArtistSales,它将产品连接到可用性,然后像您一样连接到订单。我只包括“待售”或“售出”元素,但包括所有日期。通过使用SUM IF,我们可以对查询进行一次检查,并一次性获得每种类型的current vs archive的计数。然后,通过左连接将其用于所有联机类型的艺术家。由于我没有直接的MySQL,也没有结构,所以我将等待您的结果Mysql SQL-可以重新考虑这一点以提高效率吗?,mysql,sql,Mysql,Sql,在一些查询中有大量冗余。我已经创建了一个子查询ArtistSales,它将产品连接到可用性,然后像您一样连接到订单。我只包括“待售”或“售出”元素,但包括所有日期。通过使用SUM IF,我们可以对查询进行一次检查,并一次性获得每种类型的current vs archive的计数。然后,通过左连接将其用于所有联机类型的艺术家。由于我没有直接的MySQL,也没有结构,所以我将等待您的结果 SELECT `Artist`.`id`, CONCAT_WS(' ', `Person`.`f
SELECT
`Artist`.`id`,
CONCAT_WS(' ', `Person`.`first_name`, `Person`.`last_name`, `Person`.`post_nominal_letters`) AS `Artist__name`,
`Portfolio`.`count`,
`Archive`.`count`,
`LatestImage`.`id`
FROM
`people` as `Person`,
`artists` as `Artist`
LEFT OUTER JOIN
(SELECT
`Product`.`artist_id`,
COUNT(DISTINCT `Product`.`id`) AS `count`
FROM
`product_availabilities` AS `ProductAvailability`,
`products` AS `Product`
LEFT OUTER JOIN
`order_details` AS `OrderDetail`
ON
`Product`.`id` = `OrderDetail`.`product_id`
LEFT OUTER JOIN
`orders` AS `Order`
ON
`Order`.`id` = `OrderDetail`.`order_id`
WHERE
`ProductAvailability`.`id` = `Product`.`product_availability_id`
AND
`Product`.`online` = true
AND
(`ProductAvailability`.`name` = 'For sale')
OR
((`ProductAvailability`.`name` = 'Sold') AND (DATEDIFF(now(),`Order`.`order_date`) <= 30))
GROUP BY
`Product`.`artist_id`)
AS
`Portfolio`
ON
`Artist`.`id` = `Portfolio`.`artist_id`
LEFT OUTER JOIN
(SELECT
`Product`.`artist_id`,
COUNT(DISTINCT `Product`.`id`) AS `count`
FROM
`product_availabilities` AS `ProductAvailability`,
`products` AS `Product`
LEFT OUTER JOIN
`order_details` AS `OrderDetail`
ON
`Product`.`id` = `OrderDetail`.`product_id`
LEFT OUTER JOIN
`orders` AS `Order`
ON
`Order`.`id` = `OrderDetail`.`order_id`
WHERE
`ProductAvailability`.`id` = `Product`.`product_availability_id`
AND
`Product`.`online` = true
AND
(`ProductAvailability`.`name` = 'Not for sale')
OR
((`ProductAvailability`.`name` = 'Sold') AND (DATEDIFF(now(),`Order`.`order_date`) >= 30))
GROUP BY
`Product`.`artist_id`)
AS
`Archive`
ON
`Artist`.`id` = `Archive`.`artist_id`
LEFT OUTER JOIN
(SELECT
`Product`.`artist_id`,
`Product`.`id`
FROM
`products` AS `Product`
WHERE
`Product`.`online` = true
ORDER BY
`Product`.`id` DESC)
AS
`LatestImage`
ON
`Artist`.`id` = `LatestImage`.`artist_id`
WHERE
`Artist`.`person_id` = `Person`.`id`
AND
`Artist`.`online` = true
GROUP BY
`Artist`.`id`
ORDER BY
`Person`.`last_name`, `Person`.`first_name`;
当前的性能问题是什么样的?每个表有多少行?这些表是什么样子的?通常,应该尽量减少示例以删除无关的代码。然而,这在这里可能是不可能的。请发布表架构CREATE table语句,并针对该语句发布。通过组合implict和explict联接,您可能会得到错误的结果。implicit联接是一种不应使用的sql反模式。
SELECT STRAIGHT_JOIN
A.ID,
CONCAT_WS(' ', Person.first_name, Person.last_name, Person.post_nominal_letters) AS ArtistName,
ArtistSales.RecentCount,
ArtistSales.ArchiveCount,
MAX( IF( prod.Online, prod.ID, 0 ) LatestImage
from
artists A
JOIN people as Person
ON a.Person_ID = Person.ID
LEFT JOIN Products prod
ON A.ID = prod.artist_id
LEFT JOIN
( SELECT A2.id,
COUNT( DISTINCT P2.ID ) as DistProducts,
SUM( IF( DATEDIFF( now(), O.Order_Date ) <= 30, 1, 0 )) RecentCount,
SUM( IF( DATEDIFF( now(), O.Order_Date ) > 30, 1, 0 )) ArchiveCount
from
Artists A2
JOIN Products P2
on A1.ID = P2.Artist_ID
AND P2.Online
JOIN product_availabilities AS PAvail
ON P2.Product_Availability_ID = PAvail.ID
AND PAvail.name IN( 'For sale', 'Sold' )
LEFT JOIN order_details AS OD
ON P2.ID = OD.Product_ID
LEFT JOIN Orders O
ON OD.Order_ID = O.ID
where
A2.Online ) ArtistSales
ON A.ID = ArtistSales.Artist_ID
where
A.Online
order by
Person.Last_Name,
Person.FirstName