zoukankan      html  css  js  c++  java
  • laravel框架存储50万条数据

    <?php
    
    namespace AppConsoleCommands;
    
    use IlluminateConsoleCommand;
    use IlluminateSupportFacadesDB;
    
    /**
     * 插入50W数据脚本
     * Class CreateDataCommand
     * @package AppConsoleCommands
     */
    class CreateDataCommand extends Command
    {
        /**
         * The name and signature of the console command.
         *
         * @var string
         */
        protected $signature = 'command:create_data';
    
        /**
         * The console command description.
         *
         * @var string
         */
        protected $description = 'Command description';
    
        /**
         * Create a new command instance.
         *
         * @return void
         */
        public function __construct()
        {
            parent::__construct();
        }
    
        const TABLE_NAME = 'person_';
    
        const COLUMN_NAME_ARR = ['account','name','area','title','motto'];
    
        const RANDOM_STRING='abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
    
        /**
         * Execute the console command.
         *
         * @return mixed
         */
        public function handle()
        {
            $startTime = time();
            for($i = 0; $i< 500000; $i++){
    
                $this->insertData($i);
    
                if($i % 1000 == 1){
                    echo ".";
                }
    
            }
    
            $endTime = time();
            echo "success 
    ";
            echo "time:".($endTime - $startTime);
        }
    
        /**
         * 插入数据
         * 定义属性 static
         * @param $i
         */
    
        private function insertData($i){
    
    
    
            foreach(self::COLUMN_NAME_ARR as $columnName){
                $params[$columnName] = $this->getRandomString(10);
            }
            $isSuccess = DB::table($this->getTableName($i))->insert($params);
            if(!$isSuccess){
                echo "insert fail!
    ";
            }
        }
    
        /**
         * 获取表名
         * @param $i
         * @return string
         */
        private function getTableName($i){
            return self::TABLE_NAME.($i%10);
        }
    
        /**
         * 获取随机字符
         * @param $length
         * @return string
         */
        private function getRandomString($length){
            $result = "";
            for($i=0; $i<$length-1; $i++){
                $result .= self::RANDOM_STRING[mt_rand(0,strlen(self::RANDOM_STRING)-1)];
            }
            return $result;
        }
    }

    然后进入根目录执行命令

    command:create_data
    然后,数据就进入数据库了。
  • 相关阅读:
    Token ,Cookie和Session的区别
    极致Web性能 —— SPA性能指南
    关于前端数据&逻辑的思考
    移动端Retina屏boder 1px显示为2px或3px的解决方法
    Java连载8-基本数据类型2
    HTML连载25-通配符选择器&选择器综合练习
    Python连载25-函数tell&write&writeline$&持久化
    Python连载24-函数list&read&seek
    Java连载7-变量&数据类型
    HTML连载24-属性选择器(下)
  • 原文地址:https://www.cnblogs.com/stj123/p/10896024.html
Copyright © 2011-2022 走看看