2011-03-03 95 views
0

我正在寫一個SQL查詢,它給我一個慢的性能。因爲它給我504網關超時問題。請幫我重新制作這個查詢,以便我的輸出結果更快。我會把下面的查詢。Sql查詢性能下降

select 
    r.c1, 
    parent_item.c2, 
    parent_item.c3, 
    parent_item.c4, 
    parent_item.c5, 
    parent_item.c6, 
    parent_item.c7, 
    pt.c8, 
    child_item.c9, 
    t.c10, 
    child_item.c11, 
from 
    table1 child_item, 
    table2 t, 
    table3 r, 
    table1 parent_item, 
    table4 pt 
where 
    r.col1 = child_item.id and 
    t.id=child_item.typeid and 
    parent_item.id = r.parent_itemid and 
    pt.id = parent_item.typeid and parent_item.id=800 and 
    parent_item.id = (select 
         itemid 
        from 
         table5 
        where 
         itemid=parent_item.id 
        ((10!= 1) ? and (holder_itemid in (10,100) and level > 0): "")) and 
    child_item.id = (select 
        itemid 
        from 
        table5 
        where 
        itemid=child_item.id 
        ((10 != 1) ? and (holder_itemid in (10,100) and level > 0) : "")) 
order by 
    r.parent_itemid, 
    r.relation_typeid, 
    r.ordinal 
+2

可能重複的[慢性能查詢](http://stackoverflow.com/questions/5177323/slow-performance-of-query) – 2011-03-28 11:55:01

回答

1

這可能是兩個子查詢,但我們沒有足夠的有關您的模式的信息。

您應該通過EXPLAIN運行查詢並查看它說了什麼。

JOIN可能會有幫助,但我們再次無法確定。

+0

我再次簡化了這個查詢,但仍然存在問題,我添加了簡化的,你能否更加有效地簡化它。 – user642378 2011-03-03 06:06:37

0

如果我們不知道數據庫模式,就很難準確地指出性能問題。 (數據庫模式意味着你的表定義,索引等)

此外,這個位應該做什麼?

((10!= 1) ? and (holder_itemid in (10,100) and level > 0): "")) 

AFAICS,這不是一個有效的SQL查詢,並且會導致語法錯誤。