开发者社区 > 数据库 > 关系型数据库 > 正文

在PolarDB这样的sql能优化么?

在PolarDB这样的sql能优化么?lQLPJxq64uP5K9PNA2TNBtSw216CZ9TNyA8FnvWVmk13AA_1748_868.png
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

展开
收起
三分钟热度的鱼 2024-02-01 20:54:28 32 0
1 条回答
写回答
取消 提交回答
  • 面对过去,不要迷离;面对未来,不必彷徨;活在今天,你只要把自己完全展示给别人看。

    这个SQL查询看起来已经相当优化了。它使用了多个子查询和连接操作,以及聚合函数和条件过滤来获取所需的数据。以下是一些可能的优化建议:

    1. 索引优化:确保在相关表上创建适当的索引,以加速查询速度。例如,可以在ams_campaign表上的campaign_iddel_flagtypeserving_statusnum_site字段上创建索引。同样,在ams_data_auth表上的campaign_idid_ad_userid_practice字段上创建索引。最后,在ams_sp_campaign表上的report_categorydatecampaignId字段上创建索引。

    2. 减少子查询:尽量减少子查询的使用,因为它们可能会导致性能下降。在这个查询中,可以考虑将部分子查询合并到主查询中,或者使用临时表来存储中间结果。

    3. 避免全表扫描:尽量避免在查询中使用全表扫描,因为这会导致性能下降。可以通过添加适当的索引和使用更精确的过滤条件来避免全表扫描。

    4. 限制返回的数据量:尽量只返回所需的数据,而不是返回整个表的所有列。在这个查询中,可以通过仅选择需要的列来减少数据传输量。

    5. 使用并行查询:如果数据库支持并行查询,可以尝试启用并行查询来提高查询性能。这可以通过设置适当的并行度参数来实现。

    请注意,这些优化建议可能需要根据实际数据模型和查询需求进行调整。在进行任何优化之前,请务必备份数据并在测试环境中进行测试,以确保优化后的查询仍然正确且性能得到提升。

    2024-02-02 12:13:39
    赞同 展开评论 打赏

阿里云关系型数据库主要有以下几种:RDS MySQL版、RDS PostgreSQL 版、RDS SQL Server 版、PolarDB MySQL版、PolarDB PostgreSQL 版、PolarDB分布式版 。

相关产品

  • 云原生数据库 PolarDB
  • 相关电子书

    更多
    云栖大会:开源 PolarDB 架构演进、关键技术与社区建设 立即下载
    2023云栖大会:和客户一起玩转PolarDB新特性 立即下载
    2023云栖大会:PolarDB for AI 立即下载