2

I have the following query that sort of works, but I find it to be kludgy and inefficient. Is there a better way to write this query?

I know this is a bit of an odd query, else I would probably have found some advice when searching, but it is necessary for our business logic. We've been doing this in Hibernate queries and using software to output the file but we're needing to improve performance drastically and that means using raw sql. The output of this query is to be used to partially populate a helper table that we query to figure out which of our 40-odd product tables a particular item is contained in.

Could the following query be replicated using something like GROUP_CONCAT in reverse? Note that the second and third column must be repeated on each line. The fields all contain the same type of data.

SELECT 'Pn', 'Mfg', 'TableId', 'SourceColumn', 'OtherData'
SELECT PN1, Mfg, '26', 'PN1', OtherData FROM Table 
UNION ALL
SELECT PN2, Mfg, '26', 'PN2', OtherData FROM Table
UNION ALL
SELECT PN3, Mfg, '26', 'PN3', OtherData FROM Table
UNION ALL
SELECT PN4, Mfg, '26', 'PN4', OtherData 
INTO OUTFILE 'XXX.dat'
FIELDS TERMINATED BY ','
OPTIONALLY ENCLOSED BY '"'
ESCAPED BY '\\'
LINES TERMINATED BY '\n'
FROM Table d;

Input looks like the following table:

    |  PN1  |  PN2  |  PN3    |  PN4    |  MFG  |  TableId  |  OtherData  |
    -----------------------------------------------------------------------
    |  asdf |  abde |  12354  |  asdfc  |  2    |  26       |  0.2456     |
    |  nupo |  1354 |  null   |  null   |  2    |  26       |  1.53       |
    |  ...  |  ...  |  ...    |  ...    |  ...  |  ...      |  ...        |
    |  ...  |  ...  |  ...    |  ...    |  ...  |  ...      |  ...        |
    |  ...  |  ...  |  ...    |  ...    |  ...  |  ...      |  ...        |

I want the output .dat file to look like this:

    "Pn","Mfg","TableId","SourceColumn","OtherData"
    "asdf",2,26,"PN1",0.2456
    "abde",2,26,"PN2",0.2456
    "12354",2,26,"PN3",0.2456
    "asdfc",2,26,"PN4",0.2456
    "nupo",2,26,"PN1",1.53
    "1354",2,26,"PN2",1.53
    ...
    ...
    ...

If I take the INTO OUTFILE clause out, I get an output that looks like this:

    |  Pn      |  Mfg     |  TableId    |  SourceColumn  | OtherData  |
    -----------------------------------------------------------------
    |  "Pn"    |  {blob}  |  "TableId"  |  "PN"          |  {blob}    |
    |  "asdf"  |  {blob}  |  "PN1"      |  "PN1"         |  {blob}    |
    |  "abde"  |  {blob}  |  "PN2"      |  "PN2"         |  {blob}    |
    |  "12354" |  {blob}  |  "PN3"      |  "PN3"         |  {blob}    |
    |  "asdfc" |  {blob}  |  "PN4"      |  "PN4"         |  {blob}    |
    |  "nupo"  |  {blob}  |  "PN1"      |  "PN1"         |  {blob}    |
    |  "1354"  |  {blob}  |  "PN2"      |  "PN2"         |  {blob}    |
    |  ...     |  ...     |  ...        |   ...          |   ...      |
    |  ...     |  ...     |  ...        |   ...          |   ...      |
    |  ...     |  ...     |  ...        |   ...          |   ...      |
0

0

Browse other questions tagged or ask your own question.