|
我面临一个非常奇怪的问题,我在这里问过有关加快MYSql插入速度的问题尤其是有关插入多个GB大小的巨大SQL文件的问题。他们建议我使用MyISAM引擎。我做了以下事情:
% I0 L( }0 {! Z9 L+ T) ~& e: nALTER TABLE revision ENGINE=MyISAM;
+ `* j) R) T( a, S$ K+ L( @使用ALTER TABLE .. DISABLE KEYS。 K2 D9 s: f ]
(仅限MyISAM)设置bulk_insert_buffer_size为500M。" k; r p' w6 p6 z1 j
(仅限MyISAM)设置unique_checks = 0。没有检查。' V; q7 J, m- Z" V0 {( p8 [
SET autocommit=0; ... SQL import statements ... COMMIT;7 B A" }* W! O% M
SET foreign_key_checks=0;6 H- P2 H5 w/ g3 _+ `: ?$ r9 H
% ~6 o6 ~0 Y5 o- k4 A6 E' y
它将过程加快到5分钟,之前花费了2个小时,给我留下了深刻的印象。但是现在当我在其他表上尝试相同的操作时,则无法加快速度,并且 又要花费几个小时
* W. G/ N+ A+ \ G: D( S:(…1 e" s4 Q6 A1 P8 Y2 n' V4 B1 o0 C
最初获得成功时,我的 CPU使用率约为90% ,插入仅用了5分钟,但是现在按照相同的程序,我的 CPU使用率 最大 约为5%
$ ~' @: f1 R* \" v. ^" l* P。它表明有问题。
$ |8 G9 @7 d* X+ {" }我还通过以下方法验证了我的表引擎是MyISAM:+ C* i% n1 p @7 D+ ]: w' e9 y
SHOW TABLE STATUS WHERE Name = 'xxx';
1 _: T% N5 M5 r% n. j/ o注意:我使用的是Wikipedia数据库架构,我$ `& _0 g' d G Q
从Wikipedia数据集中获得了Categorylinks表的成功。我在修订版,页面和文本表上没有成功(非常慢的插入速度)。/ u1 c- e% v) ~( b5 s0 O4 t! V- K
请帮助我解决这个奇怪的问题。- B5 P8 U( q2 ]1 ~( u! o
) N3 @- q. P5 Z- c- G# z" E
解决方案:
; W D9 M t" X7 O4 T
: l$ ?1 k- l. A0 q( Q( D1 V3 h0 B7 {( W( i
% y1 S& y) |, t1 m/ L0 l 问题的背后,我还没有找到理想的原因。但是,当我深入研究时,我发现它通常与具有丰富关系的表相比,性能低。尽管我已禁用了这些键。.但是我仍然相信以下设置最适合插入大文件,因为以下设置在多数表上具有最佳性能:
! l- c' @6 }$ l" k+ T3 E - ALTER TABLE revision ENGINE=MyISAM;5 h$ Z" w3 ^* L f7 y
- Use ALTER TABLE .. DISABLE KEYS .
' ~- k0 u& @. M - (MyISAM only) Set bulk_insert_buffer_size to 500M.% c- m' V$ ]- x) a/ Z4 k {6 I' Y
- (MyISAM only) Set unique_checks = 0 . not checked.
+ H) u6 s" k. T0 f3 q) r# l$ h6 h1 n - SET autocommit=0; ... SQL import
8 s. y9 q/ {- y! B ]: n - statements ... COMMIT;7 m2 c* G3 U$ {3 k( u! m
- SET foreign_key_checks=0;
0 h9 z. G$ f3 B+ y+ z7 s有关性能调整的其他调整如下:请参阅完整的my.cnf文件。; B5 |! D, G% a% l3 {' |! o a' n
#9 |; K/ r8 ?' p5 l& x4 u8 l
# The MySQL database server configuration file.8 X8 I3 e' w# T" e# y9 H+ f/ \8 [; s
#% g" u6 p* I4 {& c& f l
# You can copy this to one of: h6 }5 p1 O6 O; M
# - "/etc/mysql/my.cnf" to set global options,
' w) [, C4 }+ D: L# - "~/.my.cnf" to set user-specific options.+ g& s/ O* c: D# j
#
1 ^9 L; h# r- B+ f$ \' ?# One can use all long options that the program supports./ D+ X% J9 n; A- d! f
# Run program with --help to get a list of available options and with
2 i I( l: W6 ?# --print-defaults to see which it would actually understand and use.
; w; T6 }: L3 ^ j& k#
6 \9 h8 ^- x F7 s& Q5 e3 m' P1 @# For explanations see( F8 f% I. n6 S" l* |! G
# http://dev.mysql.com/doc/mysql/en/server-system-variables.html5 c0 V y( n! l$ u& D* w( ^ Q
# This will be passed to all mysql clients2 Y. d+ g F6 W
# It has been reported that passwords should be enclosed with ticks/quotes
! l* I& d) [' X) ?& b5 ^: N) K# escpecially if they contain "#" chars...5 C5 N% o) ~1 U1 W7 _4 q8 e1 ^
# Remember to edit /etc/mysql/debian.cnf when changing the socket location.+ s s/ J: M k# |
[client]: ]6 ?8 n( R3 ?0 O
port = 3306$ |& H) @! p' Z
socket = /var/run/mysqld/mysqld.sock1 r6 T* l+ |" N4 S. o% |
# Here is entries for some specific programs
, R* i3 v8 }) N3 i7 b8 Y) i# The following values assume you have at least 32M ram+ `! P) O1 Y, D7 N. a
# This was formally known as [safe_mysqld]. Both versions are currently parsed.5 i) V4 R- P8 y4 e
[mysqld_safe]1 H, M! W0 }' T6 g7 e- X9 b
socket = /var/run/mysqld/mysqld.sock
7 B: a' Y5 o7 [: {5 ?* Pnice = 0 E* W5 d0 B" A0 _0 b R9 x
[mysqld]" c# N; a. T9 @6 ]& V+ K- G
#8 q" [+ }( ]0 {( G, e5 q2 y( e! }
# * Basic Settings+ [2 \7 B; K8 j! o; h
#; s2 I6 x7 p' p, d; C/ @
user = mysql- D5 h; Z ]! @+ N: L+ O
pid-file = /var/run/mysqld/mysqld.pid
; T& i" V8 p1 U. G5 bsocket = /var/run/mysqld/mysqld.sock3 a% m8 F' }, ~' {3 v* z8 j- M. J
port = 3306
6 ~/ P9 S, G- bbasedir = /usr
% _5 s% L3 \/ q3 Xdatadir = /var/lib/mysql
7 R! U9 t" W) V% T+ z) Z: w5 Utmpdir = /tmp( `+ a: ]% ^8 B! J
bulk_insert_buffer_size = 1G' h* Y' M# O9 X2 w
lc-messages-dir = /usr/share/mysql, |7 ]/ { @* ?2 ~9 P% ~
skip-grant-tables4 i' M, z6 B4 n
skip-networking
- `, u8 K- h1 l# q* Yskip-external-locking/ m8 A; \' N8 k+ l6 Q& g
init_connect='SET autocommit=0'
- F7 L& n. C3 V% Ginnodb_buffer_pool_size = 1G. y7 T- |, O! b+ z% n
innodb_flush_log_at_trx_commit = 0: N; t! e9 Q5 I: }: o# G
max_allowed_packet = 500M9 \6 S# h5 \' R
table_open_cache = 512; S" K" v1 G3 @6 @4 c
max_connections=100# M( \0 K% T1 N( v& M7 t
query_cache_size=32M
$ Q% u8 i J5 U" k) Y) @- |) F+ `table_cache=5126 k Y' E4 o! l2 r4 s- _% X6 d5 u
tmp_table_size=64M
' ?& @: D. @. P* A# c1 nthread_cache_size=8
$ T$ D4 b$ g0 F8 c* h- Gmyisam_max_sort_file_size=100G
# M- l% o* i) ^, f, Imyisam_sort_buffer_size=256M
4 s" C5 ?. I1 H( R z7 Gkey_buffer_size=512M
/ f1 ?; J/ U: S## y E. m2 w+ l9 {' ^# U
# Instead of skip-networking the default is now to listen only on. G& e6 r* [& k/ k3 Z
# localhost which is more compatible and is not less secure.
3 s' N' C. t( Z/ qbind-address = 127.0.0.15 `2 ]* M$ G6 T& m+ t( a0 w! M
#8 a& y/ t6 [3 o1 ? R7 D
# * Fine Tuning
D7 h% ?# u1 C( k#0 ~" ?* C4 N0 _
key_buffer = 16M" k7 U: z f- b6 |
max_allowed_packet = 16M
( N5 e$ e6 W+ Fthread_stack = 192K" N; V( M5 ?8 P `
thread_cache_size = 8
& b B/ G* R- {# This replaces the startup script and checks MyISAM tables if needed
( m3 N" p6 S. F) G; O# [3 @# the first time they are touched
- l. Q( [ G0 q$ o& r) @4 dmyisam-recover = BACKUP4 Z, H" R4 y2 d
#max_connections = 100
$ a G- {& \9 r+ B. K0 x) q#table_cache = 642 _2 t- x% B! z7 J+ W* B
#thread_concurrency = 10
/ `! W% B1 e" F. [6 D+ m l#
7 h# u- A( z" Q4 ^5 ]# * Query Cache Configuration
2 B6 i5 D3 f/ n5 W6 g& F6 o5 ~#
3 s: i1 F5 w8 l4 X+ n, nquery_cache_limit = 1M4 z5 D) t- f6 k6 I7 f
query_cache_size = 16M
( X) x- _3 N9 r7 g#
/ k. v" G: \! P: J0 r) J: K# * Logging and Replication
! i/ T! X0 ?# G$ U0 |" t#; a& B+ c: j% N# \6 z" _ b
# Both location gets rotated by the cronjob." L' t# R' h$ F& h6 q% e6 G% x4 m
# Be aware that this log type is a performance killer.& m3 p) z, o, K3 w( ?; c% V) a" j
# As of 5.1 you can enable the log at runtime!/ P8 c4 ]) { r) p4 p- v$ `5 X8 q
#general_log_file = /var/log/mysql/mysql.log- ^6 Z$ B% H0 B
general_log=02 e/ w$ A5 p' h4 L/ P. n: F" S
#
1 Z' @7 v- K9 j6 s( |2 ?2 D& s# Error log - should be very few entries.3 }7 E9 D9 t5 @3 I% ?
#' ~4 G: U e9 P' L# h
log_error = /var/log/mysql/error.log
$ q" p7 m2 q6 }' p+ `9 |#* T1 k1 Q% S m, U9 R; Z N7 ?0 f
# Here you can see queries with especially long duration( U% J0 l+ l0 F5 k* _/ Q7 w" Y
#log_slow_queries = /var/log/mysql/mysql-slow.log- Z( C; s2 N/ a
#long_query_time = 2
5 @3 s/ N+ P% V" x#log-queries-not-using-indexes" g4 e# `6 T& g+ C# Q
#' y5 ?! k( y7 c( m# `! j' F
# The following can be used as easy to replay backup logs or for replication.
% a# T8 A2 s' K* W# note: if you are setting up a replication slave, see README.Debian about' m. i8 D& H C" v {" f! B. i
# other settings you may need to change.( M' R$ A1 n9 r* c# E# D2 W
#server-id = 1+ j, N5 Z! b2 Y; n( |
#log_bin = /var/log/mysql/mysql-bin.log
2 k) U; F, I+ G! Q/ @- R% h# Zexpire_logs_days = 10
7 ^; b H6 m3 L: ^/ ymax_binlog_size = 1G
2 F2 @2 ~' n" \8 E+ y8 z' ]#binlog_do_db = include_database_name7 b. }$ D6 P- b! ~
#binlog_ignore_db = include_database_name/ b) c& S: q5 g
#
$ W; V/ n# x4 G# * InnoDB
" W1 k/ T8 I4 q1 ?2 \% {% X, q& [#% w' K; G: |; R% z! m' B
# InnoDB is enabled by default with a 10MB datafile in /var/lib/mysql/.- l9 n; y( b7 U$ q6 a+ P: i
# Read the manual for more InnoDB related options. There are many!+ b* o' ]2 P" P, m
#+ h# \$ k% ^& S1 C
# * Security Features
0 D9 X# ^; R7 u6 U4 p#
% S/ n5 u' M. _6 f: j# Read the manual, too, if you want chroot!" W5 w1 O- F9 N3 _2 K8 v: A ?
# chroot = /var/lib/mysql/8 [9 L, A" j: s& N
#7 m* o B6 ~1 y, X& O4 |2 b! R ]
# For generating SSL certificates I recommend the OpenSSL GUI "tinyca".
) W5 V6 R: Y- L! r8 P7 b#
& c! A1 P) C e1 B7 q- U1 @7 K# ssl-ca=/etc/mysql/cacert.pem
9 y* D7 n; C' ~* S4 w# ssl-cert=/etc/mysql/server-cert.pem! d6 I7 I( |5 o- U5 V2 d4 z, r! P
# ssl-key=/etc/mysql/server-key.pem
( `. q$ t5 B# o' e6 k8 S' H( X" Z5 _+ s
[mysqldump]% o* E h/ F( ~+ h7 Z
quick
+ H$ N2 }" W. g; k, Q! O5 @quote-names
) U7 k% L% E, smax_allowed_packet = 16M% A8 ?1 J6 x) a2 z6 ~
[mysql]8 _7 f! M9 k: g7 L8 f+ W; h# c5 I
#no-auto-rehash # faster start of mysql but no tab completition+ A# i! I9 V8 w- g6 v2 v
[isamchk]
3 k; Z7 `: O# V0 tkey_buffer = 16M
' O9 X. I2 b5 N0 w8 Z( ?8 P! T#
0 P- e" s! v3 j# C3 f# * IMPORTANT: Additional settings that can override those from this file!) e1 x5 E- J$ B* w
# The files must end with '.cnf', otherwise they'll be ignored.% b: r9 T3 ^% a. M/ n) `
#1 T1 c; ]! n( e
!includedir /etc/mysql/conf.d/
$ Z$ ^ G' E# ` y+ o/ p因此,总体上建议使用这些设置,您将获得可观的性能优势。 |
|