我有3個表(SuccessOrder,FailedOrder和PendingOrder)具有相同的列。每張表都有超過200萬條記錄。我需要結合這3個表中的所有數據,並通過CreatedDate對它們進行排序,以顯示在我的門戶中。我使用UNION ALL來組合所有表格的結果。MySQL聯盟性能問題
如果我執行每個子查詢,在1或2秒內得到結果。如果我執行整個查詢(UNION ALL有3個子查詢),則需要5分鐘以上。
select * from (
select * from SuccessOrder
UNION ALL
select * from FailedOrder
UNION ALL
select * from PendingOrder
) t order by t.ID;
UNION ALL還有其他的選擇嗎?
是否有可能從3個查詢創建視圖沒有UNION ALL?
以下是從工作臺測試的個人和聯合查詢。我看不出太大的差別UNION ALL和UNION ALL之間 - ORDER BY
首先查詢
持續時間/提取時間:2.182秒/ 1.513秒
SELECT col1, col2, ...
FROM CompleteTxn ct
left outer join CompleteItem ci ON (ct.Id = ci.TxnId)
left outer join ItemDispute id ON (ct.Id = id.TxnId and ci.Id = id.ItemId)
left outer join Merchant mc ON (ct.MerchantId = mc.Id)
left outer join AdditionalTxnInfo addti ON (ct.Id = addti.TxnId)
where (ct.PartitionKey>=55 AND ct.PartitionKey<=56)
and (ct.TxnCompleteTime >= '2016-08-01 17:00:00'
and ct.TxnCompleteTime <= '2016-08-09 17:00:00'
)
and ct.MnoId in (22,24,25,23,26,220,221,200,223,224,320, 400)
and ct.Status in (1,2,3,4,5);
第二查詢
持續時間/提取時間:0.279秒/ 0.861秒
SELECT col1, col2, ...
FROM FailedOrder ct
left outer join FailedItem ci ON (ct.Id = ci.TxnId)
left outer join ItemDispute id ON (ct.Id = id.TxnId and ci.Id = id.ItemId)
left outer join Merchant mc ON (ct.MerchantId = mc.Id)
left outer join AdditionalTxnInfo addti ON (ct.Id = addti.TxnId)
where (ct.PartitionKey>=55 AND ct.PartitionKey<=56)
and (ct.TxnCompleteTime >= '2016-08-01 17:00:00'
and ct.TxnCompleteTime <= '2016-08-09 17:00:00'
)
and ct.MnoId in (22,24,25,23,26,220,221,200,223,224,320, 400);
聯盟所有全無爲了通過
持續時間/提取時間:104.802秒/ 0.00027秒
select *
FROM
(
SELECT col1, col2, ...
FROM FailedOrder ct
left outer join FailedItem ci ON (ct.Id = ci.TxnId)
left outer join ItemDispute id ON (ct.Id = id.TxnId
and ci.Id = id.ItemId
)
left outer join Merchant mc ON (ct.MerchantId = mc.Id)
left outer join AdditionalTxnInfo addti ON (ct.Id = addti.TxnId)
where (ct.PartitionKey>=55 AND ct.PartitionKey<=56)
and (ct.TxnCompleteTime >= '2016-08-01 17:00:00'
and ct.TxnCompleteTime <= '2016-08-09 17:00:00'
)
and ct.MnoId in (22,24,25,23,26,220,221,200,223,224,320, 400)
and ct.Status in (1,2,3,4,5)
UNION ALL
SELECT col1, col2, ...
FROM CompleteTxn ct
left outer join CompleteItem ci ON (ct.Id = ci.TxnId)
left outer join ItemDispute id ON (ct.Id = id.TxnId
and ci.Id = id.ItemId
)
left outer join Merchant mc ON (ct.MerchantId = mc.Id)
left outer join AdditionalTxnInfo addti ON (ct.Id = addti.TxnId)
where (ct.PartitionKey>=55 AND ct.PartitionKey<=56)
and (ct.TxnCompleteTime >= '2016-08-01 17:00:00'
and ct.TxnCompleteTime <= '2016-08-09 17:00:00'
)
and ct.MnoId in (22,24,25,23,26,220,221,200,223,224,320, 400) ) t ;
聯盟所有與ORDER BY
持續時間/提取時間:104.895秒/ 0.00028秒
SELECT * FROM(
SELECT COL1,COL2,... FROM FailedOrder CT左外部聯接FailedItem ci ON(ct.Id = ci.TxnId)left外部聯接ItemDispute id ON(ct.Id = id.TxnId和ci.Id = id.ItemId)left outer join Merchant mc ON(ct.MerchantId = mc .Id)left outer join AdditionalTxnInfo addti ON(ct.Id = addti.TxnId)where(ct.PartitionKey> = 55 AND ct.PartitionKey < = 56)和(ct.TxnCompleteTime> ='2016-08-01 17:00:00'和ct.TxnCompleteTime < ='2016-08-09 17:00:00')和ct.MnoId in (22,24,25,23,26,220,221,200,223,224,320,400)和ct.Status在(1,2,3,4,5)
UNION ALL
SELECT COL1,COL2,... FROM CompleteTxn CT左外加入CompleteItem ci ON(ct.Id = ci.TxnId)left outer join ItemDispute id ON(ct.Id = id.TxnId和ci.Id = id。ItemId)left outer join Merchant mc ON(ct.MerchantId = mc.Id)left outer join AdditionalTxnInfo addti ON(ct.Id = addti.TxnId)其中(ct.PartitionKey> = 55 AND ct.PartitionKey < = 56)和( ct.TxnCompleteTime> ='2016-08-01 17:00:00'and ct.TxnCompleteTime < ='2016-08-09 17:00:00')and ct.MnoId in(22,24,25,23, 26,220,221,200,223,224,320,400)
)t ORDER BY id desc;
如果您刪除't.ID'的訂單,那麼我猜它會快得多,否則應該需要時間。 – 1000111
爲什麼你要一次檢索600萬條記錄?這是三個表掃描。索引在這裏用處不大。 – e4c5
我們不是一次檢索600萬條記錄。我們對每個子查詢都有條件。我們需要檢查從這3個表中的搜索,並通過選定的字段(大多數ID)排序,並顯示前100條記錄給用戶分頁 – rkvegiraju