[英]MySQL Insert from results of left & Right Join results in memory issue
我在2個大表上運行插入查詢時遇到問題。 一個表是67,000,000,另一個表是100,000。 我正在嘗試對2個表進行LEFT和RIGHT Join並將結果放入另一個表中。 該查詢在1M條目下的較小表上運行完美。 但是當進入較高的條目時,它就會炸毀。 我收到此錯誤:
Incorrect key file for table 'C:\Windows\TEMP\#sql3838_2_6.MYI'; try to repair it
在在線閱讀解決方案后,他們說要增加mysql及其索引使用的鍵所占用的內存。 我已經嘗試過了,但仍然遇到同樣的問題。 在這一點上,我不確定這是不是針對mysql的錯誤配置或條形碼查詢。
因此,我確實在尋找優化查詢的解決方案,以提高內存效率或更改my.config來處理查詢。 或者將查詢分為2個不同的插入??? 有幫助嗎?
MySQL查詢
INSERT INTO schema.orphan_results (_Doc_ID, Orphan_Entries, Entries_Table, Orphan_File)
SELECT C.A__Doc_ID, C.A_File, C.A_Table, C.B_File
FROM( SELECT A._Doc_ID AS A__Doc_ID, A.File AS A_File, A.Table AS A_Table, B.File AS B_File
FROM schema.Temp_Entries A
LEFT JOIN schema.temp_dir_scan B ON A.File = B.File
UNION SELECT A._Doc_ID as A__Doc_ID, A.File AS A_File, A.Table AS A_Table, B.File AS B_File
FROM schema.Temp_Entries A
RIGHT JOIN schema.temp_dir_scan B ON A.File = B.File) C
WHERE C.A_File IS NULL OR C.B_File IS NULL
這是MySql的my.config
default-storage-engine=INNODB
max_connections=800
query_cache_size=186M
table_cache=1520
tmp_table_size=900M
thread_cache_size=38
myisam_max_sort_file_size=100G
myisam_sort_buffer_size=268M
key_buffer_size=1160M
read_buffer_size=128K
read_rnd_buffer_size=512K
sort_buffer_size=512K
innodb_additional_mem_pool_size=96M
innodb_buffer_pool_size=563M
我的系統
16 Gigs of Mem
52 Gigs of Free disk space.
該錯誤消息通常是由於磁盤空間不足而引起的,但是由於52gigs應該足夠了(我假設您的文件系統可以處理2GB以上的文件),因此可能有所不同。
以下兩件事應該可以用來限制所需的臨時空間:
您應該為temp_dir_scan.File和Temp_Entries.File創建一個索引。
您應該使用“全部聯盟”而不是“聯盟”(或者,按照您的建議,拆分查詢)。
您可以重寫代碼(仍然請創建索引):
INSERT INTO schema.orphan_results (_Doc_ID, Orphan_Entries, Entries_Table, Orphan_File)
SELECT A._Doc_ID, A.File, A.Table, null
FROM schema.Temp_Entries A
where not exists (select 1 from schema.temp_dir_scan B where A.File = B.File)
-- or a.file is null -- you might need that if a.file can be null
INSERT INTO schema.orphan_results (_Doc_ID, Orphan_Entries, Entries_Table, Orphan_File)
select null, null, null, B.File
from schema.temp_dir_scan B
where not exists (select 1 from schema.Temp_Entries A where A.File = B.File)
自聯盟有一個內置的distinct
(雖然我不知道你是否意識到這一點),您可能需要使用select distinct A._Doc_ID ...
,但如果你並不真的需要它,不要!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.