私たちはPythonとLOAD DATA INFILEを使用して、CSVからステージングデータベースにデータをロードしています。ステージングから、実際の本番データベースにデータを移動するSQLスクリプトがあります。サブセレクション付きMySQLインサート
LOAD DATA INFILEは、ステージングから行を選択して本番環境に挿入するのに比べて高速です。
我々は、5.7であるInnoDBテーブルを使用して、我々は我々のインサートを最適化するために、以下の構成を適用した:メモリ(16ギガバイト)の半分に
- セットinnodb_autoinc_lock_mode InnoDBのバッファプールのサイズを設定し
- 2にします
- ログバッファサイズを4GBに設定
- トランザクションを使用しています
- SETオートコミット= 0を使用してください。
まだ、あるテーブルから別のテーブルへの挿入は、LOAD DATA INFILEと比較して大幅に遅くなります。
通常の挿入では最大500KB /秒ですが、負荷データinfileの場合は30MB/sまで上がります。
このパフォーマンスを改善する方法はありますか、私たちのアプローチを完全に再考する必要がありますか?サブクエリにOUTFILEを使用してINFILEでロードすることは考えられますが、正しいアプローチのようには聞こえません。
、文:
INSERT INTO documentkey (documentClassCode,dId,fileTypeCode,internet,pathId,creationTime,signature,CSVimportId)
SELECT case when csv.`Document Class` is null
then (select classCode from mydb.class where classDesc = 'Empty'
And LookupId = (select LookupId from mydb.Lookup where LookupGroupCode = 'C' and EntityLookedup = 'documentkey')
)
else (select classCode from mydb.class where classDesc = csv.`Document Class`
And LookupId = (select LookupId from mydb.Lookup where LookupGroupCode = 'C' and EntityLookedup = 'documentkey')
)
end,
csv.`dId`,
(select typeCode from mydb.type
Where typeDesc = csv.`File Type`
And LookupId = (select LookupId from mydb.Lookup where LookupGroupCode = 'T' and EntityLookedup = 'documentkey')
),
case when csv.`message ID` is null
then (select messageIncrId from message where internetdesc = 'Empty')
else case when exists (select internetMessageIncrId from internetMessage where internetdesc = csv.`Internet Message ID`)
then (select internetMessageIncrId from internetMessage where internetdesc = csv.`Internet Message ID`)
else 0
end
end,
case when exists (select pathId from Path where pathDesc = csv.`path`)
then (select pathId from Path where pathDesc = csv.`path`)
else 0
end,
case when csv.`Creation Time` <> '' then STR_TO_DATE(csv.`Creation Time`, '%d/%m/%Y %H:%i:%s') else '2016-06-16 10:00:00' end,
#STR_TO_DATE(csv.`Creation Time`, '%Y-%m-%d %H:%i:%s'),
csv.`Signature Hash`,
1
#csv.`CSV import id`
FROM `mydb_stage`.`csvDocumentKey` csv
where csv.`dId` is not null and csv.threadId = @thread;
クエリの選択部分のみが第二の部分を取ります。
は説明:
'1', 'PRIMARY', 'csv', NULL, 'ALL', NULL, NULL, NULL, NULL, '1', '100.00', 'Using where'
'12', 'DEPENDENT SUBQUERY', 'path', NULL, 'eq_ref', 'pathDesc_UNIQUE', 'pathDesc_UNIQUE', '1026', 'func', '1', '100.00', 'Using where; Using index'
'11', 'DEPENDENT SUBQUERY', 'path', NULL, 'eq_ref', 'pathDesc_UNIQUE', 'pathDesc_UNIQUE', '1026', 'func', '1', '100.00', 'Using where; Using index'
'10', 'SUBQUERY', 'message', NULL, 'const', 'messageDesc_UNIQUE', 'messageDesc_UNIQUE', '2050', 'const', '1', '100.00', 'Using index'
'9', 'DEPENDENT SUBQUERY', 'message', NULL, 'eq_ref', 'messageDesc_UNIQUE', 'messageDesc_UNIQUE', '2050', 'func', '1', '100.00', 'Using where; Using index'
'8', 'DEPENDENT SUBQUERY', 'message', NULL, 'eq_ref', 'messageDesc_UNIQUE', 'messageDesc_UNIQUE', '2050', 'func', '1', '100.00', 'Using where; Using index'
'6', 'DEPENDENT SUBQUERY', 'type', NULL, 'eq_ref', 'typeDesc_UNIQUE', 'typeDesc_UNIQUE', '1026', 'func', '1', '100.00', 'Using index condition; Using where'
'7', 'SUBQUERY', 'Lookup', NULL, 'ref', 'PRIMARY', 'PRIMARY', '6', 'const', '3', '10.00', 'Using where'
'4', 'SUBQUERY', 'class', NULL, 'const', 'classDesc_UNIQUE', 'classDesc_UNIQUE', '1026', 'const', '1', '100.00', NULL
'5', 'SUBQUERY', 'Lookup', NULL, 'ref', 'PRIMARY', 'PRIMARY', '6', 'const', '2', '10.00', 'Using where'
'2', 'DEPENDENT SUBQUERY', 'class', NULL, 'eq_ref', 'classDesc_UNIQUE', 'classDesc_UNIQUE', '1026', 'func', '1', '20.00', 'Using index condition; Using where'
'3', 'SUBQUERY', 'Lookup', NULL, 'ref', 'PRIMARY', 'PRIMARY', '6', 'const', '2', '10.00', 'Using where'
「LOAD DATA」が高速である理由の1つは、実際にはデータベースの内容を実行していないという理由がありますが、「INSERT」はそうです。 –
@ TimBiegeleisen私はそれがユーザーにとって透過的な特定の設定の設定で動作していると想定しています。あなたはINSERTと同様の設定とパフォーマンスを達成できると思います。それはどうやってどうやっているのかの問題です。 – L4zl0w
どのようにデータを選択して挿入しますか? –