mysql 分表

DROP TABLE IF EXISTS `article`;
CREATE TABLE `article` (
`id` bigint(20) NOT NULL,
`subject` varchar(200) NOT NULL,
`content` text NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MRG_MyISAM DEFAULT CHARSET=utf8 UNION=(`article_0`,`article_1`,`article_2`,`article_3`,`article_4`,`article_5`,`article_6`,`article_7`,`article_8`,`article_9`);

 

DROP TABLE IF EXISTS `article_0`;
CREATE TABLE `article_0` (
`id` bigint(20) NOT NULL,
`subject` varchar(200) NOT NULL,
`content` text NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

 

DROP TABLE IF EXISTS `article_1`;
CREATE TABLE `article_1` (
`id` bigint(20) NOT NULL,
`subject` varchar(200) NOT NULL,
`content` text NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

 

DROP TABLE IF EXISTS `article_10`;
CREATE TABLE `article_10` (
`id` bigint(20) NOT NULL,
`subject` varchar(200) NOT NULL,
`content` text NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

 

DROP TABLE IF EXISTS `article_2`;
CREATE TABLE `article_2` (
`id` bigint(20) NOT NULL,
`subject` varchar(200) NOT NULL,
`content` text NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

 

DROP TABLE IF EXISTS `article_3`;
CREATE TABLE `article_3` (
`id` bigint(20) NOT NULL,
`subject` varchar(200) NOT NULL,
`content` text NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

 

DROP TABLE IF EXISTS `article_4`;
CREATE TABLE `article_4` (
`id` bigint(20) NOT NULL,
`subject` varchar(200) NOT NULL,
`content` text NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

 

DROP TABLE IF EXISTS `article_5`;
CREATE TABLE `article_5` (
`id` bigint(20) NOT NULL,
`subject` varchar(200) NOT NULL,
`content` text NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

 

DROP TABLE IF EXISTS `article_6`;
CREATE TABLE `article_6` (
`id` bigint(20) NOT NULL,
`subject` varchar(200) NOT NULL,
`content` text NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

 

DROP TABLE IF EXISTS `article_7`;
CREATE TABLE `article_7` (
`id` bigint(20) NOT NULL,
`subject` varchar(200) NOT NULL,
`content` text NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

 

DROP TABLE IF EXISTS `article_8`;
CREATE TABLE `article_8` (
`id` bigint(20) NOT NULL,
`subject` varchar(200) NOT NULL,
`content` text NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

 

DROP TABLE IF EXISTS `article_9`;
CREATE TABLE `article_9` (
`id` bigint(20) NOT NULL,
`subject` varchar(200) NOT NULL,
`content` text NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

 

DROP TABLE IF EXISTS `create_id`;
CREATE TABLE `create_id` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=127 DEFAULT CHARSET=utf8;

 

 

 //循环批量插入数据

public function index(){
  for ($i=0; $i < 10000; $i++) { 
    $this->new_Article();
  }
}

 //查询所有数据

public function sel(){
  $a = M('article')->Select();
  p($a);
}


//添加数据
public function new_Article() { 
  $id = $this->get_AI_ID(); 
  $table_name = $this->get_Table_Name($id); 
  $data = array(
    'id' => $id,
    'subject' =>"我就是大帅".$id,
    'content' =>'内容'
  );
  echo M($table_name)->add($data); 
} 
//修改数据
public function save(){
  $id = 127;
  $table_name = $this->get_Table_Name($id);
  $data = array(
    'id' => $id,
    'subject' =>"wdfasdfasd",
    'content' =>'2323'
  );
  $info = M($table_name)->save($data);
  p($info);
} 

//根据ID获取表明
public function get_Table_Name($id) { 
  $table = 'article_'.intval($id)%10; 
  return $table;
}

//获取插入的ID
public function get_AI_ID() { 
  $id= M('create_id')->add(array(''));
  return $id;
}

 

 //根据id获取需要的插入的表 

public function index(){
  for ($i=0; $i < 100; $i++) { 
    echo $this->get_hash_table('mes'.$i .'sage','user'.$i).'<br>'; 
  }
 }

public function get_hash_table($table,$userid) {
  $str = crc32($userid); 
  if($str<0){ 
    $hash = "0".substr(abs($str), 0, 1); 
  }else{ 
    $hash = substr($str, 0, 2); 
  } 
  return $table."_".$hash; 
}

 

posted on 2017-06-16 16:28  炊厨  阅读(172)  评论(0)    收藏  举报

导航