我有一个查询为大约5000个select语句执行UNION ALL。当我运行这个我得到这个错误
Got a packet bigger than 'max_allowed_packet' bytes
基于类似的其他问题,我更改了my.ini
文件中的值并重新启动了服务器。我已将值设置为1GB,这是mysql接受的最大值。我可以看到服务器拾取了1GB的新值。我可以通过运行它来检查它:
SHOW VARIABLES LIKE 'max_allowed_packet';
我仍然继续收到此错误。我将实际的SQL语句保存到文本文件中,只保存了4.4mb。
我该怎么做才能解决这个问题。
示例选择声明:其中大约有5000个。唯一改变的是id
SELECT
SUM(postpay_GA + prepay_ga) AS GA,
CAST(SUM(EDGE_NUM) / SUM(EDGE_DEN) AS DECIMAL (10, 4)) * 100 AS EDGE,
SUM(ACC_REV) / SUM(ATR_D) AS ARD,
(CAST(SUM(promot) AS DECIMAL (10, 4)) / SUM(surv) - CAST(SUM(DETRAC) AS DECIMAL (10, 4)) / SUM(surv)) * 100 AS NPS,
SUM(Renew) AS GR
FROM
store_dashboard_data_dly
WHERE ACTIVITY_MTH BETWEEN '2014-09-01' AND '2014-09-30'
AND region_cd = **'NE'**
AND logic = 'eis'
UNION
ALL
SELECT
SUM(quota * (metric_id = 1)),
AVG(NULLIF(quota * (metric_id = 7), 0)),
AVG(NULLIF(quota * (metric_id = 10), 0)),
AVG(NULLIF(quota * (metric_id = 14), 0)),
SUM(quota * (metric_id = 2))
FROM
rop_budget
WHERE id = **'NE'**
AND start_date BETWEEN '2014-09-01' AND '2014-09-30'
答案 0 :(得分:2)
您可以使用group by
select
x.ga,
x.edge,
x.ard,
x.nps,
x.gr
from (
select
d.region_cd,
sum(d.postpay_ga + d.prepay_ga) as ga,
cast(sum(d.edge_num) / sum(d.edge_den) as decimal(10, 4)) * 100 as edge,
sum(d.acc_rev) / sum(d.atr_d) as ard,
(cast(sum(d.promot) as decimal(10, 4)) / sum(d.surv)
- cast(sum(d.detrac) as decimal(10, 4)) / sum(d.surv)) * 100 as nps,
sum(d.Renew) as gr,
1 as ord
from
store_dashboard_data_dly as d
where
d.activity_mth between '2014-09-01' and '2014-09-30' and
d.region_cd in ('NE', 'R2', 'R3', ...) and
d.logic = 'eis'
group by
d.region_cd
union all
select
id,
sum(quota * (metric_id = 1)),
avg(nullif(quota * (metric_id = 7), 0)),
avg(nullif(quota * (metric_id = 10), 0)),
avg(nullif(quota * (metric_id = 14), 0)),
sum(quota * (metric_id = 2)),
2
from
rop_budget
where
id in ('NE', 'R2', 'R3', ...) and
start_date between '2014-09-01' and '2014-09-30'
) x
order by
x.id,
x.ord
如果您希望按照定义查询的相同顺序排序结果,则可以使用region_cd, sequence_no
创建临时表。插入区域/序列,然后使用它连接报告表。