简体   繁体   English

更快地获取多个 UNION ALL 查询

[英]Get a multiple UNION ALL query faster

I have an issue of performance with a query with multiple UNION ALL statements.我对包含多个 UNION ALL 语句的查询存在性能问题。 I need to add (row by row) data from different tables into the same columns.我需要将来自不同表的数据(逐行)添加到相同的列中。 The query need to be used to create a view in MySQL, so, here an example:该查询需要用于在 MySQL 中创建视图,因此,这里有一个示例:

CREATE OR REPLACE
    ALGORITHM = UNDEFINED 
    DEFINER = usr 
    SQL SECURITY DEFINER
VIEW my_view AS
SELECT DISTINCT 
   column 1, 
   column 2, 
   column 3
FROM 
   table 1
WHERE 
   condition 1
UNION ALL
SELECT DISTINCT 
   column 1, 
   column 2, 
   column 3
FROM 
   table 2
WHERE 
   condition 2
UNION ALL
SELECT DISTINCT 
   column 1, 
   column 2, 
   column 3
FROM 
   table 3
WHERE 
   condition 3

It seems pointless to do all the multiple UNION ALLs just to add (row by row) data from the same features (not just 3 columns as in the example, I have many more) coming from different tables because this is something that requires lots of resources from the DB, leading to "lost connection error during the query" due to the time it takes to run.执行所有多个 UNION ALL 似乎只是为了添加(逐行)来自不同表的相同功能(不仅仅是示例中的 3 列,我还有更多)的数据,因为这需要很多来自数据库的资源,由于运行所需的时间而导致“查询期间丢失连接错误”。

Is there any way to optimize this kind of query?有没有办法优化这种查询?

Thanks in advance.提前致谢。

UNION ALL is the most performant way of concatenating result sets. UNION ALL是连接结果集的最高效方式。 ( UNION is slower because it removes duplicates.) UNION速度较慢,因为它会删除重复项。)

Surely your timeout occurs when you use the view, not when you create it.当您使用视图时,肯定会发生超时,而不是在创建视图时。

Your performance issue stems from one or more of the SELECT queries in your UNION ALL cascade being very slow.您的性能问题源于您的UNION ALL级联中的一个或多个 SELECT 查询非常慢。 You, or your "data engineer" colleagues, may need to create appropriate indexes on your table 1 , table 2 , table 3 tables.您或您的“数据工程师”同事可能需要在您的table 1table 2table 3表上创建适当的索引。

To figure this out, do these things.要弄清楚这一点,请做这些事情。

  • Read Optimizing Queries With EXPLAIN .阅读使用 EXPLAIN 优化查询
  • Run SHOW CREATE TABLE whateverTableName;运行SHOW CREATE TABLE whateverTableName; . . Look at the output.看看输出。 It will show you the indexes.它会显示索引。
  • Run the SELECT queries using that same table prefixed with EXPLAIN .使用前缀为EXPLAIN的同一个表运行 SELECT 查询。 It will show you the indexes it used to satisfy the query.它将向您显示用于满足查询的索引。
  • Ask another question here showing us the output from those two steps.在这里提出另一个问题,向我们展示这两个步骤的输出。

Or, it's possible your resultset from your big query is vast.或者,您的大查询的结果集可能很大。 There's no magic that can process millions of rows faster than O(n).没有什么魔法可以比 O(n) 更快地处理数百万行。

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

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