2014-01-23 155 views
0

我正試圖在DB2中優化這個查詢。用於選擇url關鍵字的內部查詢從15秒到大約10分鐘查詢。我追蹤了這個問題,它似乎是因爲'urlkeyword = c.catentry_id'比較varchar字段(urlkeyword)和bigint字段的比較。如果我修改它以將varachar與字符串值進行比較,則它會變得更快。我的問題是:爲DB2優化查詢 - 如何從bigint轉換爲varchar/char?

什麼是將c.catentry_id從bigint轉換爲varchar的語法? (我想如果我能做到這一點,將修復它)

select 
        xmlelement(NAME "Products",xmlagg(xmlrow(c.field5 as "ExternalId", 
        cd.name as "Name", 
        cg.identifier as "CategoryExternalId", 
        concat('@[email protected]', cd.thumbnail) as "ImageUrl", 


    (select urlkeyword from seourl s 
         inner join seourlkeyword sk 
         on s.seourl_id = sk.seourl_id 
         where urlkeyword = c.catentry_id 

) as "ProductPageUrl",    
        mfpartnumber as "ManufacturerPartNumber", 
        cd.shortdescription as "Description", 
        c.field1 as "BrandExternalId" option row "Product"))) as xml_document 

        from catentry c 
        inner join catentdesc cd 
        on c.catentry_id = cd.catentry_id and cd.language_id = -1 
        inner join catgpenrel cgp 
        on c.catentry_id = cgp.catentry_id 
        inner join catgroup cg 
        on cg.catgroup_id = cgp.catgroup_id 
        where cgp.catalog_id in 
        (select catalog_id from catalog where identifier = 'Web_OW Default Web') 

回答

0

你可以用這個

where urlkeyword = CAST(c.catentry_id AS VARCHAR(n)) 

顯然,你需要更換n枝條urlkeyword字段的長度嘗試。

但是我認爲這個問題涉及索引正確的表。

我希望這可以幫助你。

+0

感謝它的作品,雖然我只是鑄造char。該表是索引,但索引掃描,無論出於什麼原因都將左側(字符串轉換爲數字),而不是轉換右側。所以鑄造右側的力量的行爲。這工作,查詢現在回到15秒... –