SELECT * FROM Persons
p
– Batch Insert
INSERT INTO Persons
VALUES('bx9','z','Shanghai','Songjiang','2')
,('bx1','z','Shanghai','Songjiang','2')
,('bx2','z','Shanghai','Songjiang','2')
,('bx3','z','Shanghai','Songjiang','2')
,('bx4','z','Shanghai','Songjiang','2')
,('bx5','z','Shanghai','Songjiang','2')
,('bx6','z','Shanghai','Songjiang','2')
,('bx7','z','Shanghai','Songjiang','2')
,(‘bx8’, ‘z’, ‘Shanghai’, ‘Songjiang’, ‘2’);
– Batch modification
The first method: use insert into insert, the code is as follows:
The code is as follows
p
a
r
a
m
s
=
a
r
r
a
y
(
′
v
a
l
u
e
′
=
>
′
5
0
′
)
;
s
e
t
t
i
m
e
l
i
m
i
t
(
0
)
;
e
c
h
o
d
a
t
e
(
"
H
:
i
:
s
"
)
;
f
o
r
(
params = array('value'=>'50'); set_time_limit(0); echo date("H:i:s"); for(
params=array(′value′=>′50′);settimelimit(0);echodate("H:i:s");for(i=0;
i
<
2000000
;
i<2000000;
i<2000000;i++){
c
o
n
n
e
c
t
m
y
s
q
l
−
>
i
n
s
e
r
t
(
connect_mysql->insert(
connectmysql−>insert(params);
};
echo date(“H:i:s”);
The last display is: 23:25:05 01:32:05 It only took more than 2 hours!
The second method: Use transaction commits and batch insertion into the database (after every 10W commits) and the last display time is: 22:56:13 23:04:00, a total of 8 minutes and 13 seconds. The code is as follows:
The code is as follows
echo date(“H:i:s”);
$connect_mysql->query(‘BEGIN’);
p
a
r
a
m
s
=
a
r
r
a
y
(
′
v
a
l
u
e
′
=
>
′
5
0
′
)
;
f
o
r
(
params = array('value'=>'50'); for(
params=array(′value′=>′50′);for(i=0;
i
<
2000000
;
i<2000000;
i<2000000;i++){
c
o
n
n
e
c
t
m
y
s
q
l
−
>
i
n
s
e
r
t
(
connect_mysql->insert(
connectmysql−>insert(params);
if($i%100000==0){
$connect_mysql->query(‘COMMIT’);
$connect_mysql->query(‘BEGIN’);
}
}
$connect_mysql->query(‘COMMIT’);
echo date(“H:i:s”);
The third method: use optimized SQL statements: splice the SQL statements, use insert into table () values (),(),(),(), and then insert it again. If the string is too long,
You need to configure MYSQL and run it in the mysql command line: set global max_allowed_packet = 210241024*10; Consumption time is: 11:24:06 11:25:06;
It only took 1 minute to insert 200W test data! The code is as follows:
The code is as follows
s
q
l
=
"
i
n
s
e
r
t
i
n
t
o
t
w
e
n
t
y
m
i
l
l
i
o
n
(
v
a
l
u
e
)
v
a
l
u
e
s
"
;
f
o
r
(
sql= "insert into twenty_million (value) values"; for(
sql="insertintotwentymillion(value)values";for(i=0;
i
<
2000000
;
i<2000000;
i<2000000;i++){
$sql.="(‘50’),";
};
s
q
l
=
s
u
b
s
t
r
(
sql = substr(
sql=substr(sql,0,strlen($sql)-1);
c
o
n
n
e
c
t
m
y
s
q
l
−
>
q
u
e
r
y
(
connect_mysql->query(
connectmysql−>query(sql);