【发布时间】:2014-10-26 05:54:58
【问题描述】:
我在大约 170,000 条记录的数据库中有 3 个以限制 1 结尾的查询。
如果我分别运行这 3 个查询或使用 UNION 将所有 3 个查询结合起来提高速度,有什么区别吗?目前查询返回结果很慢。
所以最终结果会从数据库中返回 3 条记录。
我目前的查询:
select * from table where cat = '%1%' and brand like '%example%' limit 1;
select * from table where cat = '%2%' and brand like '%example2%' limit 1;
select * from table where cat = '%3%' and brand like '%example3%' limit 1;
使用 UNION:
(select * from table where cat = '%1%' and brand like '%example%' limit 1) UNION (select * from table where cat = '%2%' and brand like '%example2%' limit 1) UNION (select * from table where cat = '%3%' and brand like '%example3%' limit 1);
【问题讨论】:
标签: mysql database performance optimization union