简体   繁体   English

Symfony2中的主义多次插入超时

[英]Doctrine in Symfony2 multiple inserts time out

I am trying to create many of the same object in my database, to insert some users, more exactly. 我试图在数据库中创建许多相同的对象,以更准确地插入一些用户。 First, I tried flushing the entity manager after every loop, but it didn't work. 首先,我尝试在每个循环后刷新实体管理器,但是没有用。 After some research, I found out that I can do it with batches, so I tried flushing every 15 loops, but I still get timed out after 30 seconds(around 180 queries). 经过研究后,我发现可以批量处理,因此我尝试每15个循环刷新一次,但30秒后仍会超时(大约180个查询)。 Shouldn't these be fast, or almost instant for a number as small as 200-250? 对于200-250的数字,这些速度不是快速还是几乎是瞬间? How can I make this not time out and insert them faster? 如何使它不超时并更快地插入它们?

What my function does is make a request to randomuser.me and retrieves a json of users. 我的功能要做的是向randomuser.me发出请求,并检索用户的json。 I retrieve that data into an array and use it to fill my users info with it. 我将这些数据检索到一个数组中,并用它来填充我的用户信息。 I also have a $fields array somewhere that has certain medical fields(won't post it here as it is just an array of plain text). 我在某个具有某些医学领域的地方也有一个$ fields数组(因为它只是纯文本数组,所以不会在这里发布)。 It also adds a related settings entry for the user entry(containing the field in which the medic works and his picture). 它还为用户条目添加了一个相关的设置条目(包含医生在其中工作的字段及其图片)。

 public function generateAction()
    {
        $em = $this->getDoctrine()->getManager();
        $generated = 0;

        $url = "https://randomuser.me/api/?results=250&nat=us";
        $str = file_get_contents($url);
        $medics = json_decode($str);


   $medicsgruop = $this->getDoctrine()->getRepository('MedAppBundle:Group')->findOneBy(array('name' => 'medics'));

    if (!$medicsgruop) {
        $medicsgruop = new Group('medics');
        $medicsgruop->addRole('ROLE_MEDIC');
        $em->persist($medicsgruop);
        $em->flush();
    }
        $batchSize = 15;
        foreach ($medics->results as $i => $medic) {

            if ($this->fields[$i]) {
                $field = $this->fields[$i];
            } else {
                $field = array_rand($this->fields);
            }

            $fname = $medic->user->name->first;
            $lname = $medic->user->name->last;

            $image = $medic->user->picture->large;
            $email = $medic->user->email;

            $user = new User();
            //user
            $user->setUsername($lname.$fname);
            $user->setFirstname($fname);
            $user->setLastname($lname);
            $user->setEmail($email);
            $user->setEnabled(true);
            $user->setPlainPassword($fname.$lname);
            //group



            $user->addGroup($medicsgruop);
            $em->persist($user);
            //MEDIC SETTINGS
            $medsett = new MedicSettings();
            $medsett->setField($field);
            $medsett->setProfile($image);
            $medsett->setMedic($user);
            $em->persist($medsett);


            if (($i % $batchSize) == 0) {
                $em->flush();
                $em->clear();
            }
        }
        $em->flush();
        $em->clear();
        return $this->render(
            '@MedApp/Admin/generatemedics.html.twig',
            array('generated' => $generated, 'medics' => 'asd')
        );
    }

When using the batch method, around 40 users are being added to my database, but it stops after the maximum execution time is reached. 当使用批处理方法时,大约40个用户被添加到我的数据库中,但是在达到最大执行时间后停止。 What am I doing wrong? 我究竟做错了什么?

Using malcolm's suggestion to use the Client command from symfony. 使用malcolm的建议来使用symfony的Client命令。 The command still runs slow, though. 但是,该命令仍然运行缓慢。 Like ~1 second per user. 每个用户大约1秒。 And they are inserted in the DB 15 at a time, per batch. 并且每一批一次将它们插入DB 15中。

class GenMedicsCommand extends ContainerAwareCommand
{
    /**
     * {@inheritdoc}
     */
    protected function configure()
    {
        $this
            ->setName('doctrine:generate:medics')
            ->setDescription('Generate medics group, users, group-users, medic settings')
            ->addArgument(
                'number',
                InputArgument::REQUIRED,
                'How many medics to generate?'
            );
    }

    /**
     * {@inheritdoc}
     */
    protected function execute(InputInterface $input, OutputInterface $output)
    {
        $number = $input->getArgument('number');
        $this->generate($number,$output);

    }


    protected $fields = array(
        'Abdominal Radiology Radiology-Diagnostic',
        'Addiction Psychiatry Psychiatry',
        'Adolescent Medicine Pediatrics'    
    );

    //generate random medics for the win
    public function generate($nr,   OutputInterface $output)
    {


        $em = $this->getContainer()->get('doctrine.orm.entity_manager');
        $doctrine = $this->getContainer()->get('doctrine');

        $generated = 0;

        $url = "https://randomuser.me/api/?results=".$nr."&nat=us";
        $str = file_get_contents($url);
        $medics = json_decode($str);

        $medicsgruop = $doctrine->getRepository('MedAppBundle:Group')->findOneBy(array('name' => 'medics'));

        if (!$medicsgruop) {
            $medicsgruop = new Group('medics');
            $medicsgruop->addRole('ROLE_MEDIC');
            $em->persist($medicsgruop);
            $em->flush();
        }

        $batchSize = 15;
        foreach ($medics->results as $i => $medic) {
         //   set_time_limit(2);


            $randkey = array_rand($this->fields);
            $field = $this->fields[$randkey];

            $fname = $medic->user->name->first;
            $lname = $medic->user->name->last;

            $image = $medic->user->picture->large;
            $email = $medic->user->email;

            $user = new User();
            //user
            $user->setUsername($lname.$fname);
            $user->setFirstname($fname);
            $user->setLastname($lname);
            $user->setEmail($email);
            $user->setEnabled(true);
            $user->setPlainPassword($fname.$lname);
            //group


            $user->addGroup($medicsgruop);

            $em->persist($user);
            //MEDIC SETTINGS
            $medsett = new MedicSettings();
            $medsett->setField($field);
            $medsett->setProfile($image);
            $medsett->setMedic($user);
            $em->persist($medsett);
            $output->write(array($i+1,'|',$fname,'|',$lname,'|',$field,'|',$email,'|',$image));
            $output->writeln('');
            if (($i % $batchSize) == 0) {
                $em->flush();
                //  $em->clear();
            }
        }
        $em->flush();
        $em->clear();


        $output->writeln('done');
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM