简体   繁体   English

MySQL多个限制为1的查询会提高UNION的速度吗?

[英]MySQL multiple queries with limit 1, will UNION improve speed?

I have 3 queries ending in limit 1 from a database of around 170,000 records. 我从大约170,000条记录的数据库中有3个以限制1结尾的查询。

Is there any difference if I run these 3 queries separately or will combining all 3 by using UNION improve the speed? 如果我分别运行这三个查询,还是通过使用UNION组合所有三个查询,可以提高速度吗? Currently the query is slow to return the results. 当前查询返回结果的速度很慢。

So the end result will return 3 records from the database. 因此最终结果将从数据库返回3条记录。

My current queries: 我目前的查询:

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;

With UNION: 使用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);

union will execute all three queries, and then attempt to remove duplicates from them, so it will almost certainly decrease performance. union将执行所有三个查询,然后尝试从它们中删除重复项,因此几乎可以肯定会降低性能。 Using union all instead of running three separate queries may slightly improve performance, as it will save the back-and-forth of three independent executions. 使用union all而不是运行三个单独的查询可能会稍微提高性能,因为它将节省三个独立执行的来回时间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM