문제

My problem is basically that doctrine doesn't use the apc cached result when re-running the same query. Because of this i get long execution time. I didn't find a similar problem on the net.

My environment:

WAMP (32bit) with PHP 5.4.16 on Windows7. MySQL database on remote database with a 87ms average ping. Laravel4 with laravel-doctrine package installed via composer:

doctrine/dbal - 2.4.x-dev
doctrine/orm  - 2.4.x-dev

My problem:

The cached doctrine query builder doesn't use the cached apc data.

Query:

    $query = Doctrine::createQueryBuilder();

    $query->select('e')->from("Persistent\Users\Experts\Expert", "e");

    $this->prepareSort($sort, $query);
    $query->orderBy($this->sortField, $this->sortVector);

    //prepare paginator
    $queryPaginator = new DoctrinePaginator($query);
    $this->total = $queryPaginator->count();

    return $queryPaginator  ->getQuery()
                            ->setResultCacheDriver(new ApcCache())
                            ->useResultCache(true, 300, 'expertsearch')
                            ->setFirstResult(0)
                            ->setMaxResults(20)
                            ->getResult();

Result:

The query takes about 17 seconds to complete because of distance between my machine and the database server.

Enabling caching as shown above doesn't affect the speed.

The APC use cache looks as follows:

DoctrineNamespaceCacheKey[] 1   584 2013/10/30 08:33:08 2013/10/30 08:33:08 2013/10/30 08:33:08 None    [Delete Now]
[expertsearch][1]   0   27880   2013/10/30 08:33:08 2013/10/30 08:33:08 2013/10/30 08:33:08 300 seconds [Delete Now]
[121cbb9b3c38e7d5a0c0e7b74c59e640][1]   0   3672    2013/10/30 08:33:08 2013/10/30 08:33:08 2013/10/30 08:33:08 None    [Delete Now]

Webgrind shows no change in the number of calls to PDO compared to when not using the cache in the query builder.

Function        Invocation Count    Total Self Cost     Total Inclusive Cost
php::PDOStatement->execute      183     15932   15932
php::PDO->query         7   588     588
도움이 되었습니까?

해결책

APC cache is used to store OPCode if you want to store queries you should use Memcache

http://docs.doctrine-project.org/en/2.0.x/reference/caching.html#memcache

Edit 1 :

As you asked me to develop my answer I'll do so.

How PHP works (simplified) ? RE2C will first do a Lexical analysies (for >=PHP5.3 and Flex for <=PHP5.2).

Tokens are generated.

Then, the Parser have to analyze the tokens. For example it looks if you have a "(" after a function (T_FUNCTION, T_IF, T_OPEN...) The parsers name is Bison.

Then, your code is compiled in table of OPCode. For the end, there is an executer that will execute your code PHP.

So if you want, just to execute your code and not analyze and parse.. so you can use a PHP accelerator like APC. So, your PHP code will execute a lot faster but, the queries are executed every time you call your code. You're right, the documentation of Doctrine doesn't say it doesn't store the RESULT of queries but since it uses APC, it just stores THE QUERY.

Memcache is a Distributed Object Caching Memory System, it stores the result of a query. It has a distributed server.

I hope I was more clear in this response, sorry if my english is not very well...

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top