现有一张表customer,其中数据有1百万条,现在要对它进行查询遍历,$customer = new Customer();
$customers = $customer->getAll();
foreach($customers as $c) {
   // ...
}这样查询很慢,请问应该怎么优化?

解决方案 »

  1.   

    数据有100万 难道你的查询不应该在SQL里执行获取需要的结果吗?
    非要得到100万条记录然后来foreach?
      

  2.   


    表结构是这样CREATE TABLE IF NOT EXISTS `bd_customer` (
      `customer_id` int(11) NOT NULL AUTO_INCREMENT,
      `company_id` int(11) DEFAULT '0',
      `customer_name` varchar(180) DEFAULT NULL,
      `enterprise_id` int(11) DEFAULT NULL,
      `customer_type` tinyint(4) NOT NULL DEFAULT '1',
      `gender` tinyint(1) NOT NULL DEFAULT '1',
      `mobile` varchar(13) DEFAULT NULL,
      `telephone` varchar(20) DEFAULT NULL,
      `telephone_ext` varchar(10) DEFAULT NULL,
      `birthday` int(11) DEFAULT NULL,
      `email` varchar(160) DEFAULT NULL,
      `fax` varchar(20) DEFAULT NULL,
      `post_code` varchar(6) DEFAULT NULL,
      `identify_type` tinyint(1) DEFAULT '0',
      `identify_code` varchar(50) DEFAULT NULL,
      `in_time` int(11) NOT NULL,
      `province_id` tinyint(11) NOT NULL,
      `city` varchar(20) NOT NULL,
      `address` varchar(255) DEFAULT NULL,
      `delete_status` tinyint(1) NOT NULL DEFAULT '0',
      `capital_word` varchar(1) DEFAULT NULL,
      `call_id` varchar(50) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL,
      `t_cusomter_id` varchar(50) DEFAULT NULL,
      PRIMARY KEY (`customer_id`),
      KEY `Index_1` (`enterprise_id`,`telephone`),
      KEY `Index_2` (`company_id`)
    ) ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=1105537 ;
      

  3.   

    google mysql 内存表
      

  4.   

    既然是放到 redis 里, 那也就是一次性的工作。依然不存在优化问题