用js写的 Mysql 插入数据 100条用8s 怎么办啊啊啊啊啊 代码如下:function insertComment(comments, weiboid){
var conn = new Jaxer.DB.MySQL.Connection({
HOST: 'localhost',
PORT: '3306',
USER: 'root',
PASS: 'admin',
NAME: 'sinaweibo',
CLOSE_AFTER_EXECUTE: true
});
var count = 0, statement = "", f_statement = "",u_statement = "", times = 0, ttimes = 0,utimes = 0;//初始化
for (var one in comments) {
var f_count = 0, b_count = 0;
var c_time = new Date(comments[one].created_at);
var i_time = new Date();//当前时间
f_count = Is_Num2(comments[one].followers_count);
c_time = formatDate2(c_time);
i_time = formatDate2(i_time);
if (getCommentData(comments[one].id)) {
if (times == 0) {
Statement = "('" + c_time +
"','" + i_time +
"','" + comments[one].id +
"','" + comments[one].mid +
"','" + comments[one].idstr +
"','" + comments[one].text +
"','" + comments[one].userid +
"','" + comments[one].username +
"','" + comments[one].followers_count +
"','" + comments[one].weiboid + "')";
times = 1;
}
else {
statement += ",('" +
c_time +
"','"+
i_time +
"','" +
comments[one].id +
"','" +
comments[one].mid +
"','" +
comments[one].idstr +
"','" +
comments[one].text +
"','" +
comments[one].userid +
"','" +
comments[one].username +
"','" +
comments[one].followers_count +
"','" +
comments[one].weiboid +
"')";
}
count++;//评论数
} }
try {
[code=javascript][code=javascript][/code]if (times != 0) {//至少有开头
var result = conn.execute("insert into comment(created_at,insert_at, commentid, mid, idstr, text,userid,username,followers_count,weiboid) values " + statement);
}
return count;
}
catch (e) {
count = -1;
return count;
}}[/code]mysqljavascript
var conn = new Jaxer.DB.MySQL.Connection({
HOST: 'localhost',
PORT: '3306',
USER: 'root',
PASS: 'admin',
NAME: 'sinaweibo',
CLOSE_AFTER_EXECUTE: true
});
var count = 0, statement = "", f_statement = "",u_statement = "", times = 0, ttimes = 0,utimes = 0;//初始化
for (var one in comments) {
var f_count = 0, b_count = 0;
var c_time = new Date(comments[one].created_at);
var i_time = new Date();//当前时间
f_count = Is_Num2(comments[one].followers_count);
c_time = formatDate2(c_time);
i_time = formatDate2(i_time);
if (getCommentData(comments[one].id)) {
if (times == 0) {
Statement = "('" + c_time +
"','" + i_time +
"','" + comments[one].id +
"','" + comments[one].mid +
"','" + comments[one].idstr +
"','" + comments[one].text +
"','" + comments[one].userid +
"','" + comments[one].username +
"','" + comments[one].followers_count +
"','" + comments[one].weiboid + "')";
times = 1;
}
else {
statement += ",('" +
c_time +
"','"+
i_time +
"','" +
comments[one].id +
"','" +
comments[one].mid +
"','" +
comments[one].idstr +
"','" +
comments[one].text +
"','" +
comments[one].userid +
"','" +
comments[one].username +
"','" +
comments[one].followers_count +
"','" +
comments[one].weiboid +
"')";
}
count++;//评论数
} }
try {
[code=javascript][code=javascript][/code]if (times != 0) {//至少有开头
var result = conn.execute("insert into comment(created_at,insert_at, commentid, mid, idstr, text,userid,username,followers_count,weiboid) values " + statement);
}
return count;
}
catch (e) {
count = -1;
return count;
}}[/code]mysqljavascript
2 是否是程序跑的慢
3 数据库端你可以设置innodb_flush_log_at_trx_commit=0来提高插入速度
建议你单独在数据库执行一番。看看是否while,for有不合理之处。
单纯post对insertComment请求用10s左右,所以问题就在这段代码了,但是代码已经很简单了,应该只会与
for (var one in comments), if (getCommentData(comments[one].id))这两者有关了,前者for循环无法避免,后者是一个根据id查找数据库中是否有改数据,有的话就进行下次循环,没有的话,拼接sql,我需要怎么改才能提高速度呢?大神帮帮忙,多谢多谢!!
估计 就是慢在这儿, 看看能不能提前获取这些id对应的commentdata?
把得到的sql 在mysql 执行一番,
若时间在8s/你的循环次数,
则说明是数据库的事,自己调试一番