doctrineアカンパターン
TRANSCRIPT
DoctrineアカンパターンRW社内勉強会 3月 岡本秀高
アカンパターン
日本語訳:アンチパターン
要は「やったらアカンこと」
「先人の失敗」に学ぼう
“
”
愚者は経験に学び、賢者は歴史に学ぶ。
オットー・フォン・ビスマルク
Fools say they learn from experience; I prefer to learn from the experience of others.
http://ja.wikiquote.org/wiki/オットー・フォン・ビスマルク
今回のアカンやつ
$result = $this->backend->getEM()
->getRepository('Entities\Item\Item')
->findBy(array('owner_no' => $owner_no)
);
return count($result);
$result = $this->backend->getEM()
->getRepository('Entities\Item\Item')
->findBy(array('owner_no' => $owner_no)
);
return count($result);
countのためだけに
商品情報を全取得
測ってみた$result = $this->backend->getEM()
->getRepository('Entities\Item\Item')
-
>findBy(array('owner_no' => $owner_no)
);
return count($result);
1商品につき0.0012秒
160商品で0.35秒
4750商品で6.13秒
ID:zagzagで処理落ち
_(:3」∠)_
データを数えるだけならSQLで!
$em = $this->backend->getEM();
$expr = $em->createQueryBuilder()->expr();
$qb = $em->createQueryBuilder();
$amountItems = $qb->select('count(p)')
->from('Entities\Item\Item', 'p')
->where($expr->eq('p.owner_no', '?1'))
->setParameter(1, $owner_no)
->getQuery()
->getSingleScalarResult();
return $amountItems;
$em = $this->backend->getEM();
$expr = $em->createQueryBuilder()->expr();
$qb = $em->createQueryBuilder();
$amountItems = $qb->select('count(p)')
->from('Entities\Item\Item', 'p')
->where($expr->eq('p.owner_no', '?1'))
->setParameter(1, $owner_no)
->getQuery()
->getSingleScalarResult();return $amountItems;
速度比較PHPで数えた場合
1商品につき0.0012秒
160商品で0.35秒
4750商品で6.13秒
SQLで数えた場合
1商品につき0.000000454秒
160商品で0.000072秒
4750商品で0.00216秒
速度比較PHPで数えた場合
1商品につき0.0012秒
160商品で0.35秒
4750商品で6.13秒
_(:3」∠)_
SQLで数えた場合
1商品につき0.000000454秒
160商品で0.000072秒
4750商品で0.00216秒
(☝ ՞ਊ ՞)☝
使おう、
QueryBuilder
TRY IT!╭( ・ㅂ・)و ̑̑
15. The QueryBuilder — Doctrine 2 ORM 2 documentation :
http://doctrine-orm.readthedocs.org/en/latest/reference/query-builder.html
COUNT関数 - MySQL関数の使い方 - MySQLの使い方 :
http://www.dbonline.jp/mysql/function/index6.html
6章 -データを扱う- Symfony :
http://symfony.com/legacy/doc/doctrine/1_2/ja/06-working-with-dataA