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

标签 mysql database performance optimization union

我有 3 个查询从大约 170,000 条记录的数据库中以限制 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;

联合:

(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 将执行所有三个查询,然后尝试从中删除重复项,因此它几乎肯定会降低 性能。使用 union all 而不是运行三个单独的查询可能会略微提高性能,因为它将节省三个独立执行的来回。

关于MySQL 多查询限制为 1,UNION 会提高速度吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26570234/

相关文章:

c++ - 在 C/C++ 中比较特定半字节的快速方法

mysql - 检查上一步状态是否完成并显示下一步是否完成

javascript - 如何将Javascript函数更改为MySQL可以使用的PHP?

mysql - 我可以在两个不同的表中使用相同的外键约束吗?

SQL 服务器 : Update reporting table in real time

c# - 使用 lambda 进行复杂分组

mysql - 用于填充网页的大量静态数据 - 存储在数据库中,还是静态的?

php - 在不知道行数的情况下将数组保存到 mySQL 数据库中的单独行

c# - SQL Server 中的错误处理问题

PHP:将完整数组与空数组合并还是先检查 isset()?