为什么UNION似乎都要等待两个查询完成呢?

时间:2022-09-20 14:45:52

Running this query:

运行该查询:

select null, "hello" union all select sleep(4), "world";

on 5.5.29 doesn't return the first row right away as expected. Instead, I have to wait 4 seconds before getting anything. How can I make MySQL return the first row right away?

第5.5.29页没有像预期的那样立刻返回第一行。相反,我必须等4秒钟才能得到任何东西。如何让MySQL立即返回第一行?

I am trying to test the handling of slow queries. It makes no sense to divide it, since I would then be testing something else.

我正在尝试测试缓慢查询的处理。把它分开是没有意义的,因为我要测试别的东西。

2 个解决方案

#1


4  

How can I make MySQL return the first row right away?

如何让MySQL立即返回第一行?

Run two queries separately instead of using a UNION. I admit this answer may seem kind of trite, but it may be the only real answer to your question.

分别运行两个查询,而不是使用联合。我承认这个答案似乎有点老套,但它可能是你问题的唯一真实答案。

One reason it has to execute the second query is that a UNION requires that all the columns are the same in all unioned subqueries. That is, same in number and compatible in data type. It may even promote the data type based on what is returned by the second query (e.g. expand an INT to a BIGINT). So it has to get at least one row back from all subqueries before it can determine the data type for the first row of the result set.

它必须执行第二个查询的一个原因是,联合要求所有联合子查询中的所有列都是相同的。也就是说,数量相同,数据类型兼容。它甚至可以根据第二个查询返回的内容来提升数据类型(例如,将INT扩展为BIGINT)。因此,在确定结果集第一行的数据类型之前,它必须从所有子查询中返回至少一行。

#2


0  

You have asked for a single records set by using the UNION ALL construct. If you want to see one part followed by the other, tell the SQL engine that; run the first part as one query to return a recordset, and then run the second half as another query to return the second recordset. The two can be joined together on the client side.

您已经要求使用UNION ALL构造设置单个记录。如果你想看到一个部分接着另一个部分,告诉SQL引擎;将第一部分作为一个查询运行以返回一个记录集,然后将第二部分作为另一个查询运行以返回第二个记录集。可以在客户端将两者连接在一起。

#1


4  

How can I make MySQL return the first row right away?

如何让MySQL立即返回第一行?

Run two queries separately instead of using a UNION. I admit this answer may seem kind of trite, but it may be the only real answer to your question.

分别运行两个查询,而不是使用联合。我承认这个答案似乎有点老套,但它可能是你问题的唯一真实答案。

One reason it has to execute the second query is that a UNION requires that all the columns are the same in all unioned subqueries. That is, same in number and compatible in data type. It may even promote the data type based on what is returned by the second query (e.g. expand an INT to a BIGINT). So it has to get at least one row back from all subqueries before it can determine the data type for the first row of the result set.

它必须执行第二个查询的一个原因是,联合要求所有联合子查询中的所有列都是相同的。也就是说,数量相同,数据类型兼容。它甚至可以根据第二个查询返回的内容来提升数据类型(例如,将INT扩展为BIGINT)。因此,在确定结果集第一行的数据类型之前,它必须从所有子查询中返回至少一行。

#2


0  

You have asked for a single records set by using the UNION ALL construct. If you want to see one part followed by the other, tell the SQL engine that; run the first part as one query to return a recordset, and then run the second half as another query to return the second recordset. The two can be joined together on the client side.

您已经要求使用UNION ALL构造设置单个记录。如果你想看到一个部分接着另一个部分,告诉SQL引擎;将第一部分作为一个查询运行以返回一个记录集,然后将第二部分作为另一个查询运行以返回第二个记录集。可以在客户端将两者连接在一起。