在PolarDB这样的sql能优化么?
select from
( select a.id as id, a.num_site as numSite, a.seller_code as sellerCode, a.seller_short as sellerShort,
a.id_seller as idSeller, a.type as type, a.portfolio_id as portfolioId, a.portfolio_name as portfolioName,
a.campaign_id as campaignId, a.campaign_type as campaignType, a.name
as name
, a.state as state,
a.targeting_type as targetingType, a.start_date as startDate, a.end_date as endDate, a.strategy as strategy,
a.budget as budget, a.suggested_budget as suggestedBudget, a.budget_type as budgetType, a.effective_budget as effectiveBudget,
a.last_update_date_time as lastUpdateDateTime, a.serving_status as servingStatus, a.creation_date_time as creationDateTime,
a.usage_updated_timestamp as usageUpdatedTimestamp, a.placement_bidding as placementBidding, a.del_flag as delFlag,
a.create_by as createBy, a.create_name as createName, a.create_time as createTime, a.update_time as updateTime,
a.update_by as updateBy, a.update_name as updateName, a.cost_type as costType, a.bid_optimization as bidOptimization ,
b., c.* from ams_campaign a
inner join (
select campaign_id as campaign_id, group_concat(distinct id_ad_user) as idAdUserList,
group_concat(distinct id_practice) as idPracticeList,
group_concat(distinct ad_user_name) as adUserNameList, group_concat(distinct practice_user_name) as practiceUserNameList
from ams_data_auth WHERE 1=1
and (
id_ad_user in ( 1562393201367879680 )
or id_practice in ( 1562393201367879680 ) )
group by campaign_id ) b
on a.campaign_id = b.campaign_id
left join (
select campaignId as rCampaignId, round(max(topOfSearchImpressionShare + 0), 4) as topOfSearchImpressionShare,
round(sum(impressions + 0), 0) as impressions, round(sum(clicks + 0), 0) as clicks,
round(sum(clicks + 0) / sum(impressions + 0), 4) as ctr, round(sum(cost + 0) / sum(clicks + 0), 2) as cpc,
round(sum(cost + 0), 2) as cost, round(sum(sales14d + 0), 2) as sales, round(sum(cost + 0) / sum(sales14d + 0), 4) as acos,
round(sum(sales14d + 0) / sum(cost + 0), 2) as roas, round(sum(purchases14d + 0), 0) as orderNums,
round(sum(purchasesSameSku14d + 0), 0) as orderNumsSame, round(sum(purchasesOtherSku14d + 0), 0) as orderNumsOther,
round(sum(spend + 0) / sum(purchases14d + 0), 2) as cpa, round(sum(purchases14d + 0) / sum(clicks + 0), 4) as cvr,
round(sum(unitsSoldClicks14d + 0), 0) as unitsSold, round(sum(viewableImpressions + 0) / sum(impressions + 0), 4) as vtr,
round(sum(viewableImpressions + 0) / sum(clicks + 0), 4) as vctr, round(sum(attributedOrdersNewToBrand14d + 0), 0) as brandNewBuyerOrderNums,
round(sum(attributedOrdersNewToBrand14d + 0) / sum(clicks + 0), 4) as brandNewBuyerOrderRate,
round(sum(attributedSalesNewToBrand14d + 0), 0) as brandNewBuyerSales, round(sum(attributedUnitsOrderedNewToBrand14d + 0), 0) as brandNewBuyerUnitsOrdered,
round(sum(video5SecondViews + 0), 0) as video5SecondViews, round(sum(video5SecondViews + 0) / sum(viewableImpressions + 0), 4) as video5SecondViewRate,
round(sum(videoFirstQuartileViews + 0), 0) as videoFirstQuartileViews, round(sum(videoMidpointViews + 0), 0) as videoMidpointViews,
round(sum(videoThirdQuartileViews + 0), 0) as videoThirdQuartileViews, round(sum(videoCompleteViews + 0), 0) as videoCompleteViews,
round(sum(videoUnmutes + 0), 0) as videoUnmutes, round(sum(attributedBrandedSearches14d + 0), 0) as brandSearches,
round(sum(cumulativeReach + 0), 0) as cumulativeReach, round(sum(impressionsFrequencyAverage + 0), 0) as impressionsFrequencyAverage
from ams_sp_campaign
WHERE 1=1
and report_category = 'SPONSORED_PRODUCTS'
and date
between '2024-01-21' and '2024-01-21' group by campaignId ) c
on a.campaign_id = c.rCampaignId
WHERE 1=1
and a.del_flag = 0
and a.type = 'SPONSORED_PRODUCTS'
and a.serving_status != 'CAMPAIGN_INCOMPLETE'
and a.num_site in ( 7 ) ) a order by a.id desc limit 0,10
这个SQL查询看起来已经相当优化了。它使用了多个子查询和连接操作,以及聚合函数和条件过滤来获取所需的数据。以下是一些可能的优化建议:
索引优化:确保在相关表上创建适当的索引,以加速查询速度。例如,可以在ams_campaign
表上的campaign_id
、del_flag
、type
、serving_status
和num_site
字段上创建索引。同样,在ams_data_auth
表上的campaign_id
、id_ad_user
和id_practice
字段上创建索引。最后,在ams_sp_campaign
表上的report_category
、date
和campaignId
字段上创建索引。
减少子查询:尽量减少子查询的使用,因为它们可能会导致性能下降。在这个查询中,可以考虑将部分子查询合并到主查询中,或者使用临时表来存储中间结果。
避免全表扫描:尽量避免在查询中使用全表扫描,因为这会导致性能下降。可以通过添加适当的索引和使用更精确的过滤条件来避免全表扫描。
限制返回的数据量:尽量只返回所需的数据,而不是返回整个表的所有列。在这个查询中,可以通过仅选择需要的列来减少数据传输量。
使用并行查询:如果数据库支持并行查询,可以尝试启用并行查询来提高查询性能。这可以通过设置适当的并行度参数来实现。
请注意,这些优化建议可能需要根据实际数据模型和查询需求进行调整。在进行任何优化之前,请务必备份数据并在测试环境中进行测试,以确保优化后的查询仍然正确且性能得到提升。
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。
阿里云关系型数据库主要有以下几种:RDS MySQL版、RDS PostgreSQL 版、RDS SQL Server 版、PolarDB MySQL版、PolarDB PostgreSQL 版、PolarDB分布式版 。