简体   繁体   English

Symfony2 Doctrine2获取所有表

[英]Symfony2 Doctrine2 fetch all table

I need fetch all table and do some calculation in loop, what is bets way to do this, my example is : 我需要获取所有表并在循环中进行一些计算,这样做的下注方式是什么,我的示例是:

$em = $this->getDoctrine()->getManager();

$increment = 0;

for($i = 0; $i>= 0; $i++){
    $query = $em->createQueryBuilder()
        ->select('com')
        ->from('CatalogWebBundle:ComCompany', 'com')
        ->getQuery()
        ->setMaxResults(2000)
        ->setFirstResult($increment);

    $info = $query->getArrayResult();
    if(!$info)
        break;
    $increment += 2000;
}

What I get: 我得到的是:

在此处输入图片说明

Query is not very slow 231ms, but PHP 6501s how to optimize this time to 1-2s it's possible ? 查询不是很慢的231ms,但是PHP 6501s如何将此时间优化为1-2s是可能的?

This is my table structure: 这是我的表结构:

CREATE TABLE IF NOT EXISTS `com_company` (
`cmp_id` int(11) NOT NULL,
  `cmp_category` int(11) DEFAULT NULL,
  `cmp_name` varchar(100) DEFAULT NULL,
  `cmp_code` int(10) DEFAULT NULL,
  `cmp_city` int(11) DEFAULT NULL,
  `cmp_vat` varchar(16) DEFAULT NULL,
  `cmp_emp` int(11) DEFAULT NULL,
  `cmp_return` varchar(255) DEFAULT NULL,
  `cmp_return_from` varchar(255) DEFAULT NULL,
  `cmp_return_till` varchar(255) DEFAULT NULL,
  `cmp_address` varchar(255) DEFAULT NULL,
  `cmp_phone` bigint(12) DEFAULT NULL,
  `cmp_email` varchar(100) DEFAULT NULL,
  `cmp_site` varchar(100) DEFAULT NULL,
  `cmp_ceo` varchar(50) DEFAULT NULL,
  `cmp_register` varchar(50) DEFAULT NULL,
  `cmp_url` varchar(100) NOT NULL
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=95749 ;


ALTER TABLE `com_company`
 ADD PRIMARY KEY (`cmp_id`), ADD KEY `cmp_city` (`cmp_city`), ADD KEY `cmp_category` (`cmp_category`);

ALTER TABLE `com_company`
MODIFY `cmp_id` int(11) NOT NULL AUTO_INCREMENT,AUTO_INCREMENT=95749;

ALTER TABLE `com_company`
ADD CONSTRAINT `com_company_ibfk_1` FOREIGN KEY (`cmp_city`) REFERENCES `com_city` (`cit_id`),
ADD CONSTRAINT `com_company_ibfk_2` FOREIGN KEY (`cmp_category`) REFERENCES `com_category` (`cat_id`);

Maybe my server is too weak ? 也许我的服务器太弱了? I need optimize this script to 1-2s max, it's possible ? 我需要将此脚本优化为最大1-2s,这可能吗?

Server: 服务器: 在此处输入图片说明

Table - 100k record, table size - 23,6 MiB 表格-100k记录,表格大小-23,6 MiB

It's not clear to me if the calculations must be done in PHP Code, or can be moved to SQL (MySQL procedures for instance). 对我来说,尚不清楚该计算是否必须在PHP代码中完成,或者可以移至SQL(例如MySQL过程)。 But if the latter is the case, I would advise you to take a look at MySQL Procedures. 但是如果是后者,我建议您看一下MySQL Procedures。 With your approach you are transferring a lot of data between the PHP Client and the MySQL Server. 使用您的方法,您将在PHP客户端和MySQL服务器之间传输大量数据。 And the end this is not a very scalable solution. 最后,这不是一个非常可扩展的解决方案。

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

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