问题
While researching an unrelated topic, I noticed the use of LIMIT ALL in an example on the IBM Knowledge Center website for Netezza. I'm unclear about the benefits of specifying LIMIT ALL here, and I am seeking clarification of the explanation (quoted below) from IBM. When might I need to specify LIMIT ALL?
SELECT CASE WHEN rand = .1 THEN 'A' WHEN rand = .2 THEN 'B' ELSE 'C' END
FROM (SELECT random() rand FROM tblA LIMIT ALL) subset
From the IBM Knowledge Center:
"The LIMIT ALL in the subquery prevents it from being pulled up into the parent query, and the random() function is invoked only once for each row of tblA, and therefore the same random() result is tested in each WHEN clause."
BTW... My question is unrelated to the use of "random()" in IBM's example.
Thank you!
回答1:
My understanding is for all databases that support LIMIT
, LIMIT ALL
is same as using no LIMIT
.
In some cases it might be used to "hint" query optimizer to create the query plan in some certain way. I believe the above excerpt is from Netezza 7.0 documentation.
回答2:
I have seen it used frequently when querying system catalog views (those that begins with a ‘_’) since the optimizer hint will cause the plan to NOT pull the entire content of the underlying catalog tables out of the (Postgres) database on the host and send their content to the SPUs. In general that is a good idea to try.
来源:https://stackoverflow.com/questions/55342683/what-are-the-benefits-of-using-limit-all-in-a-subquery