Home  >  Article  >  Backend Development  >  mysql union all 的效率有关问题

mysql union all 的效率有关问题

WBOY
WBOYOriginal
2016-06-13 12:50:371417browse

mysql union all 的效率问题

我有几个相同结构的表,需要进行联表查询,我使用如下的语句

select sum(cnt) from (select count(*) as cnt from t1 where a>100 union all select ...) as t;
这一句拿总和的挺快。

select * from t1 where a>100 union all select * from t2 where a>100 union all select * from t3 where a>100 order by b desc limit 0,10000;
这一句效率好像很低,等了很久才出来结果。

应该如何优化,谢谢


------解决方案--------------------
第二句有 order by b desc limit 0,10000 估计会影响点,去掉后试试。
------解决方案--------------------
在 union 结构中,只允许出现一个 order (子查询中的除外)
你的这种写法显然是不合理的,或是说是陈旧的(因为你做了人工分表,且分割不合理)
现在的 mysql 提供了分区的功能,只要设置合理与单个小规模表的速度不相上下(虽然也是切割成了多个数据文件)
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn