douqilai4263 2014-12-15 07:20
浏览 52
已采纳

树根主义

I want to get roots of tree in database, it works correctly but problem is here that it query database 500 times for 500 nodes, is there a way to improve this?

$query = $this->createQueryBuilder('n')
            ->where('n.parent IS NULL')
            ->getQuery();`

entity structure:

Radio\ArchiveBundle\Entity\Category:
    type: entity
    table: null
    repositoryClass: Radio\ArchiveBundle\Repository\CategoryRepository
    id:
        id:
            type: integer
            id: true
            generator:
                strategy: AUTO
    fields:
        name:
            type: string
            length: 255
        isActive:
            type: boolean
            nullable: true

    oneToMany:
        tracks:
            targetEntity: Radio\ArchiveBundle\Entity\Track
            mappedBy: category
    manyToOne:
        parent:
            targetEntity: Radio\ArchiveBundle\Entity\Category
            inversedBy: childrens
            fetch: EAGER
    oneToMany:
        childrens:
            targetEntity: Radio\ArchiveBundle\Entity\Category
            mappedBy: parent
            cascade: [persist, remove]

Symfony2 profiler image: enter image description here

  • 写回答

2条回答 默认 最新

  • duanke8011 2014-12-17 10:06
    关注

    Why 500 queries?

    As you say, you've fetched 500 root nodes from the database. Somewhere in the request the property Category::$childrens is accessed. Probably in a view by calling a method on Category that uses that property (like $node->getChildren()).

    Accessing that property means that Doctrine will need to load the Collection in order to fetch the children of that node. This results in a query for each root node. Hence you have 500 additional queries.

    If you need to access the children, it's wise to fetch them immediately when querying for the root nodes:

    SELECT rt, chld FROM Category rt LEFT JOIN rt.childrens chld WHERE rt.parent IS NULL
    

    This way the direct children of the root nodes are hydrated along with the root nodes themselves. You'll have reduced the number of queries from 501 to just 1.

    PS: The plural form of "child" is "children" (not "childrens").

    Nested Set model

    IMHO the structure of the Category tree could be improved by switching to the Nested Set model. While it is slightly less performant when inserting new nodes to the tree, updating and selecting trees becomes significantly cheaper.

    For example: Fetching an entire tree with the parent/children model you use now will need roughly 1 query for each level of depth (or 1 JOIN for each level, but this only works when the maximum depth is known). With the Nested Set model you can fetch an entire tree with just a single query (regardless of the depth). This will improve performance drastically when the level of depth increases.

    Have a look at the Tree behavior of the Doctrine Extensions library, with which you can easily implement the Nested Set model. There's also a Symfony 2 bundle to integrate that library.

    Simply add it to Composer and follow the instructions.

    composer.phar require stof/doctrine-extensions-bundle
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 目详情-五一模拟赛详情页
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b