× The internal search function is temporarily non-functional. The current search engine is no longer viable and we are researching alternatives.
As a stop gap measure, we are using Google's custom search engine service.
If you know of an easy to use, open source, search engine ... please contact support@midrange.com.


  • Subject: SQL CTE of a UNION kills performance
  • From: Buck Calabro <kc2hiz@xxxxxxxxx>
  • Date: Tue, 28 Aug 2018 12:41:10 -0400
  • Autocrypt: addr=kc2hiz@xxxxxxxxx; keydata= xsDiBEcbaT4RBADqmM9OgXil65pjrxclJpxuAF6vraI3kkmJbEHb5ElL7EquHE3QDuFqFgIB 4NZLHDbVAh0AD5exAX+r+xg//UvtBc2k34HROnCpWTMnIOaSVhhVjpYEbZGLz6wfrRpu4Qyn 45iaKT4F0qcHo+0LrGQPef3xrFkUhxURgzY5zgo6+wCg/XjYJ155witPWB2CbNf6RAm9QT0D /jSp6YhvE3xPE12aBuRYM678JTbaQfuYv4HUfug1Wz/0zH5btfEihWVN4wbKaoQ/H/29v2TP /Lyh8XTVd3Z0rz4iaSD5fGicn81WPANBeIepLB8vpfEik6UhHpN1DJkz6Ryw2mgx8p53LhHV Ck4Jt0HP2TAl3f7QTXGFOiFzJwEqBACsHk/gFpKAHdv7n4vJoHqp0RNgOOyhnTThlulPilt6 tAaSe10FOrrugBuLMn7wXBANQ1ApmIb5yNjhYqPREj65OVv2MUbw8H2HnQs//Z6aodyR/kzU 2q2G9A/YFI1LL0m/gvaVbEj/wE0ybBgFkrcoEFeStkqS5HzLEFGUDFXhD80fQnVjayBDYWxh YnJvIDxrYzJoaXpAZ21haWwuY29tPsKFBBMRAgBFAhsDBgsJCAcDAgQVAggDBBYCAwECHgEC F4AFAkcbdMokGGh0dHA6Ly9rZXlzZXJ2ZXIudmVyaWRpcy5jb206MTEzNzEvAAoJEN7KcclH umuRfngAoNXU6AXqyTR8FRuoXKBGS4k7bPUEAJ912WKSkjpCt0axjrq6j22e5XgWzc7BTQRH G2k+EAgAnLXJ9hOqedgsIYM3LuomBBNN+7WTFSVaJ3Rqz8XVZtJvLL0bIRAvpVK9L9rYXlCR cPAm0YNK6H2DR7sQxWlxEH4mWB+jTCTALpcVq+Kpfbw5qDdn+9DVMS7tBOchtTlPSGgdKgn7 sTObra8cHtX/ddTB6OLzHeTXr4PZbUwVeQdIStdwMmozKBQvgjXWKi1GiuYbwYkCM/zJEUCs J36BIE4li9xohJ5O4iKC20YVckMJfZLbn1a2gVgn6Re8C5ezNewT0qM8ZDCUNENWAxsU/c9J UCFQ2QcMU+25b84D5yPxnEKna5U9Fz2JjRjWy5ZKZx2+WhZj0r2Tw6/kGb28AwADBgf/WBsn JSMHxyVfg+LKLHpdANwa9jdrKOt2WjJbWOiJ9l7SmqD0oi3c22FFxRXKsFfjCikLk9wbLZKH SqqnOePvMMHqNcqQTSv7+ARjxnBH4g6dhqg+zmebKpt8zV2awQzYSSm4YY6IqzkWmPNAN7BU zUtSAfL4UU2PljTnT9m443aVCTXMne5l90HQv/gdJ121owg5KuGE6LodTpoR4hn9nbdKWtfY pDNoykvR+GN5y335yF2Zp/j6QgdxWezjou5Y3/6PUZLEsJagWe9hAcKb1eiO2bmg+1bFYu0T g5Mvb27nqfFeHHFysC7a7sXtxp/pqNLNDcK6j/7Th6vF7/n98cJJBBgRAgAJBQJHG2k+AhsM AAoJEN7KcclHumuR9SgAnRuJWHon4GP58xbqCiFR/jSUfvRgAJ47KZ1UNoXgdftoePnbrZu6 W+poEw==
  • List-archive: <https://archive.midrange.com/midrange-l/>
  • List-help: <mailto:midrange-l-request@midrange.com?subject=help>
  • List-id: Midrange Systems Technical Discussion <midrange-l.midrange.com>
  • List-post: <mailto:midrange-l@midrange.com>
  • List-subscribe: <https://lists.midrange.com/mailman/listinfo/midrange-l>, <mailto:midrange-l-request@midrange.com?subject=subscribe>
  • List-unsubscribe: <https://lists.midrange.com/mailman/options/midrange-l>, <mailto:midrange-l-request@midrange.com?subject=unsubscribe>

7.3 TR4, latest cume, DB group 10, Power 9.

I'm trying to suss out the performance issues in a very complex view,
and Visual Explain pointe me to this piece. 4 tables, UNIONed together
as a CTE. VE tells me that the UDFs are the culprit, but that makes no
sense as the exact same UDFs run in .001 over the exact same tables when
I run the 4 UNIONs alone.

This runs in milliseconds:

SELECT 'SOC' FILESOURCE, SOC.LOCNO LOCNO, SOC.SOCSEC SOCSEC,
SOC.FLNM3 || SOC.FLNM24 NAME, SOC.LSTNAM,
SOC.STREET, SOC.ADDR2, SOC.CITY, SOC.STATE, SOC.ZIP, SOC.ZIPPT2,
SOC.MEMBCD, DONOR, SOC.MEMSIN,
CAST(SOC.RFDLT AS CHAR(1)) RFDLT, SOC.NSTMEM, SOC.AFTMEM, SOC.LEVEL,
CVTNMDYTONUSA(BDAY, '1') DOB, TELENO, SEX,
SENDST,
CONDST, SOC.AD, LD, SOC.COUNTY, SDCODE, PRTYRG, REGSTD, RETCNL,
CAST(SOC.NMSUFX AS CHAR(10)) NMSUFX, MMDDYY2YYYYMMDD(SOC.DATE) DATE,
BLDGNO,
COALESCE(
(SELECT BLDNAME
FROM EMRBUILD
WHERE LOCAL = SOC.LOCNO AND BLDNUM = BLDGNO
ORDER BY LENGTH(TRIM(BLDNAME)) DESC
FETCH FIRST 1 ROWS ONLY), '') BUILDNAME,
CURPAY, ELEDST
FROM SOCMST SOC

UNION ALL
SELECT 'UFT' FILESOURCE, UFT.LOCNO, UFT.SOCSEC,
UFT.FLNM3 || UFT.FLNM24 NAME, UFT.LSTNAM,
UFT.STREET, UFT.NOST2, UFT.CITY, UFT.STATE, UFT.ZIP, UFT.ZIPPT2,
UFT.MEMBCD, DONOR, SUBSTR(DIGITS(MEMSIN), 5, 2),
CAST(' ' AS CHAR(1)), UFT.NSTMEM, UFT.AFTMEM, UFT.LEVEL, UFT.DOB DOB,
TELENO, SEX, UFT.SD,
CONDST, UFT.AD, LD, UFT.COUNTY, SDCODE, PRTYRG, REGSTD, '',
CAST(' ' AS CHAR(10)), CVTNUSATONISO(UFT.DATE ),
BLDGNO, '' BUILDNAME, CURPAY, ELEDST
FROM UFTMSTR UFT

UNION ALL
SELECT 'UUP' FILESOURCE, CAMP1 CONCAT CAMP2, UUP.SOCSEC,
UUP.FLNM3 || UUP.FLNM24 NAME, UUP.LSTNAM,
UUP.STREET, UUP.ADDR2, UUP.CITY, UUP.STATE, UUP.ZIP, UUP.ZIPPT2,
UUP.MEMBCD, DONOR, UUP.MEMSIN,
CAST(' ' AS CHAR(1)), UUP.NSTMEM, UUP.AFTMEM, UUP.LEVEL,
CVTNMDYTONUSA(UUP.BIRTHD, '1') DOB, TELENO, SEX,
DIGITS(SENATE),
UUP.CD, DIGITS(UUP.AD), SUBSTR(WARD, 1, 2), UUP.COUNTY, SDCODE, PRTYRG,
REGSTD, '',
CAST(' ' AS CHAR(10)), MMDDYY2YYYYMMDD(UUP.TRANDT ),
0, '' BUILDNAME, CURPAY, ED
FROM UUPMSTR UUP
WHERE MEMBCD <> 'C'

UNION ALL
SELECT 'PSC' FILESOURCE, PSC.LOCNO, PSC.SOCSEC,
PSC.FLNM3 || PSC.FLNM24 NAME, PSC.LSTNAM,
PSC.STREET, PMST.NOST2, PSC.CITY, PSC.STATE, PSC.ZIP, ZIPPT2,
PSC.MEMBCD, DONOR, PSC.MEMSIN,
CAST(' ' AS CHAR(1)), PSC.NSTMEM, PSC.AFTMEM, PSC.LEVEL,
CVTNMDYTONUSA(PMST.DOB, '1') DOB, PSC.TELENO, PSC.SEX,
CASE WHEN HEX(SN) <> '4040' THEN DIGITS(SN) ELSE ' ' END,
CONDST, DIGITS(ASMDST), SUBSTR(WARD, 1, 2), PSC.COUNTY, SDCODE, PRTYRG,
REGSTD, '',
CAST(' ' AS CHAR(10)), MMDDYY2YYYYMMDD(PSC.DATE ),
BLDGNO, '' BUILDNAME, CURPAY, BLK3
FROM PSCSOC PSC
LEFT JOIN PSCMST PMST ON PSC.SOCSEC = PMST.SOC

Wrapping it in a CTE takes 2 minutes.

with temp as (
-- the exact same 4 UNIONs
)
select *
from temp

Visual Explain is correct in that removing the UDFs does in fact speed
the thing up. The question is this: Why does wrapping this an a CTE
slow it down?


As an Amazon Associate we earn from qualifying purchases.

This thread ...

Follow-Ups:

Follow On AppleNews
Return to Archive home page | Return to MIDRANGE.COM home page

This mailing list archive is Copyright 1997-2024 by midrange.com and David Gibbs as a compilation work. Use of the archive is restricted to research of a business or technical nature. Any other uses are prohibited. Full details are available on our policy page. If you have questions about this, please contact [javascript protected email address].

Operating expenses for this site are earned using the Amazon Associate program and Google Adsense.