MrQing 发表于 2016-09-06 13:39

请教一个sql语句优化的问题

:dizzy:本人小白,请教个sql语句优化的问题

SQL:
select platform, user_name, min(user_ip) ip, min(login_time) time from ( select * from login_201510 union all select * from login_201511 union all select * from login_201512 union all select * from login_201601 union all select * from login_201602 union all select * from login_201603 union all select * from login_201604 union all select * from login_201605 union all select * from login_201606 ) t where user_name is not null group by platform, user_name having min(login_time) >= unix_timestamp('2011-01-01') + -3600 and min(login_time) < unix_timestamp('2016-07-01') + -3600 limit 100;
其中 UNION ALL 这块
select * from login_201510 union all select * from login_201511 union all select * from login_201512 union all select * from login_201601 union all select * from login_201602 union all select * from login_201603 union all select * from login_201604 union all select * from login_201605 union all select * from login_201606执行约26秒,有960W左右记录数,但是整体执行会error~~ ENGINE=BRIGHTHOUSE ,有人能点拨一下吗?谢谢!

王楠w_n 发表于 2016-09-06 15:53

你用程序 取出每个表的数据然后自己在遍历下

stay_sun 发表于 2016-09-06 15:55

取出 每个月的 需要的值用程序遍历下   取出结果   

seesea2517 发表于 2016-09-06 17:18

会error,error是啥样的呢?

seesea2517 发表于 2016-09-06 17:36

可以尝试把 where 条件放到每一个 union 的子查询里用,甚至把 group by 也放进去处理,最后再 union 一起后(或使用临时表,把每一个子查询结果插入到临时表),再做一次整体的 group by 处理过滤出想要的数据。

ganluo960214 发表于 2016-09-07 11:51

我觉得 是不是满在合并数据的地方 你这表的字段很多?

MrQing 发表于 2016-09-07 15:23

回复 6# ganluo960214

字段不算太多。 最后是分开处理的,就写成子句来执行。。~

MrQing 发表于 2016-09-07 15:23

回复 5# seesea2517

谢谢,我尝试一下
页: [1]
查看完整版本: 请教一个sql语句优化的问题