我目前有5臺:優化的MySQL 5個表的連接
trialSum
-------------
id total
-------------
1 2000
2 1000
3 500
trialLand
-------------
id total
-------------
1 1000
2 250
3 500
trialImp
-------------
id total
-------------
2 250
trialBldg
-------------
id total
-------------
1 1000
2 500
trial
---------------------
id name info
---------------------
1 xxxxx xxxxx
2 xxxxx xxxxx
3 xxxxx xxxxx
4 xxxxx xxxxx
該試驗檯將擁有最多的項目進入,所有的「ID」字段匹配彼此。每個表格中將有超過7000個條目。我想創建一個大表看起來像:
trial
----------------------------------------------------------------------
id name info totalSum totalLand totalImp totalBldg
----------------------------------------------------------------------
1 xxxxx xxxxx 2000 1000 0 1000
2 xxxxx xxxxx 1000 250 250 500
3 xxxxx xxxxx 500 500 0 0
4 xxxxx xxxxx 0 0 0 0
如果我這樣做,有4個左連接,它需要在40分鐘內完成一個巨大的MySQL查詢。必須有一個更簡單的方法。謝謝!
這聽起來像你沒有索引。如果所有'id'列都是'PRIMARY'索引,則查詢應該幾乎是即時的。請張貼您的表格結構和示例長時間運行查詢的'EXPLAIN'。 –
你可以給一個sqlfiddle創建表和插入值的例子嗎? –
您是否試過'EXPLAIN PLAN'?您可能會發現它很有幫助。更多[here](http://dev.mysql.com/doc/refman/5.0/en/using-explain.html)。 –