Last active
December 19, 2015 15:49
-
-
Save baburdick/5979124 to your computer and use it in GitHub Desktop.
This is a query that gathers 12 pieces of information from an XML column by using the xpath function. It performs very poorly, and causes PG's memory use to balloon, risking the availability of what is a production DB. (PG 9.1) How can this be rewritten to be more RAM-efficient, and possibly return results more quickly?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
drop table if exists temp_foo1; | |
select | |
foorr.id as foo_raw_responses_id | |
, foorr.created_at as foo_created_at | |
, foorr.customer_id | |
, (xpath( '////foo:response/foo:RecordCount/text()', | |
xmlconcat( foorr.value::xml ), | |
ARRAY[ ARRAY[ 'soap', 'http://schemas.xmlsoap.org/soap/envelope/' ], | |
ARRAY[ 'foo', 'http://example.com/ExampleService' ] ] ))[1]::varchar::numeric | |
as foo_RecordCount | |
, unnest(xpath( '////foo:response/foo:Records/foo:Record/foo:ObjectInfo/foo:ObjectID/text()', | |
xmlconcat( foorr.value::xml ), | |
ARRAY[ ARRAY[ 'soap', 'http://schemas.xmlsoap.org/soap/envelope/' ], | |
ARRAY[ 'foo', 'http://example.com/ExampleService' ] ] ) )::varchar | |
as foo_ObjectID | |
, unnest(xpath( '////foo:response/foo:Records/foo:Record/foo:Registrants/foo:Registrant/foo:HistoryDescription/text()', | |
xmlconcat( foorr.value::xml ), | |
ARRAY[ ARRAY[ 'soap', 'http://schemas.xmlsoap.org/soap/envelope/' ], | |
ARRAY[ 'foo', 'http://example.com/ExampleService' ] ] ) )::varchar | |
as foo_HistoryDescription | |
, unnest(xpath( '////foo:response/foo:Records/foo:Record/foo:Registrants/foo:Registrant/foo:RegistrationInfo/foo:LatestEffectiveDate/foo:Year/text()', | |
xmlconcat( foorr.value::xml ), | |
ARRAY[ ARRAY[ 'soap', 'http://schemas.xmlsoap.org/soap/envelope/' ], | |
ARRAY[ 'foo', 'http://example.com/ExampleService' ] ] ) )::varchar::numeric | |
as foo_LatestExpirationDate_year | |
, unnest(xpath( '////foo:response/foo:Records/foo:Record/foo:Registrants/foo:Registrant/foo:RegistrantInfo/foo:Name/foo:First/text()', | |
xmlconcat( foorr.value::xml ), | |
ARRAY[ ARRAY[ 'soap', 'http://schemas.xmlsoap.org/soap/envelope/' ], | |
ARRAY[ 'foo', 'http://example.com/ExampleService' ] ] ) )::varchar | |
as foo_FirstName | |
, unnest(xpath( '////foo:response/foo:Records/foo:Record/foo:Registrants/foo:Registrant/foo:RegistrantInfo/foo:Name/foo:Last/text()', | |
xmlconcat( foorr.value::xml ), | |
ARRAY[ ARRAY[ 'soap', 'http://schemas.xmlsoap.org/soap/envelope/' ], | |
ARRAY[ 'foo', 'http://example.com/ExampleService' ] ] ) )::varchar | |
as foo_LastName | |
, unnest(xpath( '////foo:response/foo:Records/foo:Record/foo:Registrants/foo:Registrant/foo:RegistrantInfo/foo:Address/foo:StreetNumber/text()', | |
xmlconcat( foorr.value::xml ), | |
ARRAY[ ARRAY[ 'soap', 'http://schemas.xmlsoap.org/soap/envelope/' ], | |
ARRAY[ 'foo', 'http://example.com/ExampleService' ] ] ) )::varchar | |
as foo_StreetNumber | |
, unnest(xpath( '////foo:response/foo:Records/foo:Record/foo:Registrants/foo:Registrant/foo:RegistrantInfo/foo:Address/foo:StreetName/text()', | |
xmlconcat( foorr.value::xml ), | |
ARRAY[ ARRAY[ 'soap', 'http://schemas.xmlsoap.org/soap/envelope/' ], | |
ARRAY[ 'foo', 'http://example.com/ExampleService' ] ] ) )::varchar | |
as foo_StreetName | |
, unnest(xpath( '////foo:response/foo:Records/foo:Record/foo:Registrants/foo:Registrant/foo:RegistrantInfo/foo:Address/foo:StreetSuffix/text()', | |
xmlconcat( foorr.value::xml ), | |
ARRAY[ ARRAY[ 'soap', 'http://schemas.xmlsoap.org/soap/envelope/' ], | |
ARRAY[ 'foo', 'http://example.com/ExampleService' ] ] ) )::varchar | |
as foo_StreetSuffix | |
, unnest(xpath( '////foo:response/foo:Records/foo:Record/foo:Registrants/foo:Registrant/foo:RegistrantInfo/foo:Address/foo:City/text()', | |
xmlconcat( foorr.value::xml ), | |
ARRAY[ ARRAY[ 'soap', 'http://schemas.xmlsoap.org/soap/envelope/' ], | |
ARRAY[ 'foo', 'http://example.com/ExampleService' ] ] ) )::varchar | |
as foo_City | |
, unnest(xpath( '////foo:response/foo:Records/foo:Record/foo:Registrants/foo:Registrant/foo:RegistrantInfo/foo:Address/foo:State/text()', | |
xmlconcat( foorr.value::xml ), | |
ARRAY[ ARRAY[ 'soap', 'http://schemas.xmlsoap.org/soap/envelope/' ], | |
ARRAY[ 'foo', 'http://example.com/ExampleService' ] ] ) )::varchar | |
as foo_State | |
, unnest(xpath( '////foo:response/foo:Records/foo:Record/foo:Registrants/foo:Registrant/foo:RegistrantInfo/foo:Address/foo:Zip5/text()', | |
xmlconcat( foorr.value::xml ), | |
ARRAY[ ARRAY[ 'soap', 'http://schemas.xmlsoap.org/soap/envelope/' ], | |
ARRAY[ 'foo', 'http://example.com/ExampleService' ] ] ) )::varchar | |
as foo_Zip5 | |
into temp temp_foo1 | |
from foo_raw_responses foorr | |
limit 50 | |
; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment