快速将虚拟数据插入mysql(insert dummy data to mysql fast)

我在我的调试模型中有1个功能,我想用它来添加虚拟数据到我的应用程序来测试它的速度等...

问题是它需要将记录添加到2个不同的表中,并在每个记录添加到db之前检查重复的用户名等,这样需要一点时间......

此过程重复约$ $想要在for循环中一次添加的不同虚拟记录...

例如,对于我要添加的100个新用户,大约需要5秒钟才能继续。

这次还好还是我需要优化它?

如果我想一次添加1000,10000个用户怎么办? 可能吗?

编辑:调用函数插入数据:

public function registerRandomUsers($total = 1){ $this->load->model("misc_model"); $this->load->model("encryption_model"); $this->load->model("signup_model"); for ($i=1;$i<=$total;$i++){ $username = $this->misc_model->generateRandomString(15); $flag = false; while ($flag == false){ if ($this->user_model->usernameExist($username)){ $username = $this->misc_model->generateRandomString(15); }else{ $flag = true; $password = 'Test123'; $email = $username.'@email.com'; $data = array( 'username' => $username, 'password' => $password, 'email' => $email ); $this->signup_model->submitRegistration($data); $userdata = $this->user_model->getUserData($username, "username"); } } } }

I have 1 function in my debug model which i want to use in order to add dummy data to my app to test its speed and such...

the problem is that it needs to add records to 2 different tables and also check for duplicates usernames etc before each record is added to db so it takes a little time...

also this procedure is repeated about $total different dummy records i want to add at once in a for loop...

for example for 100 new users i want to add it takes around 5 seconds to proceed.

is this time fine or do i need to optimize it?

what if i want to add 1000,10000 users at once?? is it possible?

EDIT: Function called to insert data:

public function registerRandomUsers($total = 1){ $this->load->model("misc_model"); $this->load->model("encryption_model"); $this->load->model("signup_model"); for ($i=1;$i<=$total;$i++){ $username = $this->misc_model->generateRandomString(15); $flag = false; while ($flag == false){ if ($this->user_model->usernameExist($username)){ $username = $this->misc_model->generateRandomString(15); }else{ $flag = true; $password = 'Test123'; $email = $username.'@email.com'; $data = array( 'username' => $username, 'password' => $password, 'email' => $email ); $this->signup_model->submitRegistration($data); $userdata = $this->user_model->getUserData($username, "username"); } } } }

最满意答案

如果您不担心随机字符串作为用户名,只需设置$email = 'user'.$i.'@email.com'; (所以你不必担心碰撞) 。 这将导致运行缓慢的主要原因是因为您在循环的每次迭代中向数据库发送新查询 - 生成批量插入字符串会快得多:

INSERT INTO user (email,pass) VALUES ('user1@email.com','Test123') , ('user2@email.com','Test123') , ('user3@email.com','Test123') , ('user4@email.com','Test123') , ('user5@email.com','Test123');

这样,您就可以避免tcp流量从发送10000个查询到数据库的开销,并让它一次完成所有操作。

If you're not worried about having a random string as the user name, just set the $email = 'user'.$i.'@email.com'; (so you don't have to worry about collisions). The main reason this will be running slow is because you're sending a new query to the database on each iteration of the loop - it would be much much faster to generate a bulk insert string like:

INSERT INTO user (email,pass) VALUES ('user1@email.com','Test123') , ('user2@email.com','Test123') , ('user3@email.com','Test123') , ('user4@email.com','Test123') , ('user5@email.com','Test123');

This way you can avoid the overhead of tcp traffic from sending 10000 queries to the database and have it do it all in one go.

更多推荐