From cbf9086e10f768007e3e62884bfede112853ac63 Mon Sep 17 00:00:00 2001 From: Kevin Papst Date: Tue, 9 Jul 2019 03:40:48 +0200 Subject: [PATCH] performance improvements (#927) --- src/Entity/Activity.php | 30 ++++------ src/Entity/ActivityMeta.php | 2 +- src/Entity/BudgetTrait.php | 2 +- src/Entity/Customer.php | 24 +++----- src/Entity/CustomerMeta.php | 2 +- src/Entity/MetaTableTypeTrait.php | 2 +- src/Entity/Project.php | 44 ++++----------- src/Entity/ProjectMeta.php | 2 +- src/Entity/Timesheet.php | 17 +++++- src/Entity/TimesheetMeta.php | 2 +- src/Migrations/Version20190706224211.php | 53 ++++++++++++++++++ src/Migrations/Version20190706224219.php | 70 ++++++++++++++++++++++++ tests/Entity/ActivityTest.php | 1 - tests/Entity/CustomerTest.php | 2 - tests/Entity/ProjectTest.php | 4 -- 15 files changed, 172 insertions(+), 85 deletions(-) create mode 100644 src/Migrations/Version20190706224211.php create mode 100644 src/Migrations/Version20190706224219.php diff --git a/src/Entity/Activity.php b/src/Entity/Activity.php index 01c10bc9cf..9cef4507c2 100644 --- a/src/Entity/Activity.php +++ b/src/Entity/Activity.php @@ -15,8 +15,18 @@ use Symfony\Component\Validator\Constraints as Assert; /** - * @ORM\Table(name="kimai2_activities") + * @ORM\Table(name="kimai2_activities", + * indexes={ + * @ORM\Index(columns={"visible","project_id"}), + * @ORM\Index(columns={"visible","project_id","name"}), + * @ORM\Index(columns={"visible","name"}) + * } + * ) * @ORM\Entity(repositoryClass="App\Repository\ActivityRepository") + * + * columns={"visible","name"} => IDX_8811FE1C7AB0E8595E237E06 => activity administration without filter + * columns={"visible","project_id"} => IDX_8811FE1C7AB0E859166D1F9C => activity administration with customer or project filter + * columns={"visible","project_id","name"} => IDX_8811FE1C7AB0E859166D1F9C5E237E06 => activity drop-down for global activities in toolbar or globalsOnly filter in activity administration */ class Activity implements EntityWithMetaFields { @@ -32,7 +42,7 @@ class Activity implements EntityWithMetaFields /** * @var Project|null * - * @ORM\ManyToOne(targetEntity="App\Entity\Project", inversedBy="activities") + * @ORM\ManyToOne(targetEntity="App\Entity\Project") * @ORM\JoinColumn(onDelete="CASCADE") */ private $project; @@ -61,13 +71,6 @@ class Activity implements EntityWithMetaFields */ private $visible = true; - /** - * @var Timesheet[]|ArrayCollection - * - * @ORM\OneToMany(targetEntity="App\Entity\Timesheet", mappedBy="activity") - */ - private $timesheets; - // keep the trait include exactly here, for placing the column at the correct position use RatesTrait; use ColorTrait; @@ -82,7 +85,6 @@ class Activity implements EntityWithMetaFields public function __construct() { - $this->timesheets = new ArrayCollection(); $this->meta = new ArrayCollection(); } @@ -91,14 +93,6 @@ public function getId(): ?int return $this->id; } - /** - * @return Collection - */ - public function getTimesheets(): Collection - { - return $this->timesheets; - } - public function getProject(): ?Project { return $this->project; diff --git a/src/Entity/ActivityMeta.php b/src/Entity/ActivityMeta.php index fde3c407e4..ece560033b 100644 --- a/src/Entity/ActivityMeta.php +++ b/src/Entity/ActivityMeta.php @@ -30,7 +30,7 @@ class ActivityMeta implements MetaTableTypeInterface * @var Activity * * @ORM\ManyToOne(targetEntity="App\Entity\Activity", inversedBy="meta") - * @ORM\JoinColumn(onDelete="CASCADE") + * @ORM\JoinColumn(onDelete="CASCADE", nullable=false) * @Assert\NotNull() */ private $activity; diff --git a/src/Entity/BudgetTrait.php b/src/Entity/BudgetTrait.php index fc303331d7..b1871f0808 100644 --- a/src/Entity/BudgetTrait.php +++ b/src/Entity/BudgetTrait.php @@ -27,7 +27,7 @@ trait BudgetTrait * * @var int * - * @ORM\Column(name="time_budget", type="integer", precision=10, scale=2, nullable=false) + * @ORM\Column(name="time_budget", type="integer", nullable=false) * @Assert\NotNull() */ private $timeBudget = 0; diff --git a/src/Entity/Customer.php b/src/Entity/Customer.php index 79a4061128..0de21ec4fd 100644 --- a/src/Entity/Customer.php +++ b/src/Entity/Customer.php @@ -15,8 +15,14 @@ use Symfony\Component\Validator\Constraints as Assert; /** - * @ORM\Table(name="kimai2_customers") + * @ORM\Table(name="kimai2_customers", + * indexes={ + * @ORM\Index(columns={"visible"}) + * } + * ) * @ORM\Entity(repositoryClass="App\Repository\CustomerRepository") + * + * columns={"visible"} => IDX_5A9760447AB0E859 => used in customer dropdown */ class Customer implements EntityWithMetaFields { @@ -54,13 +60,6 @@ class Customer implements EntityWithMetaFields */ private $comment; - /** - * @var Project[]|ArrayCollection - * - * @ORM\OneToMany(targetEntity="App\Entity\Project", mappedBy="customer") - */ - private $projects; - /** * @var bool * @@ -163,7 +162,6 @@ class Customer implements EntityWithMetaFields public function __construct() { - $this->projects = new ArrayCollection(); $this->meta = new ArrayCollection(); } @@ -352,14 +350,6 @@ public function getTimezone(): ?string return $this->timezone; } - /** - * @return Collection - */ - public function getProjects(): Collection - { - return $this->projects; - } - /** * @internal only here for symfony forms * @return Collection|MetaTableTypeInterface[] diff --git a/src/Entity/CustomerMeta.php b/src/Entity/CustomerMeta.php index 8334d19375..acf818c80d 100644 --- a/src/Entity/CustomerMeta.php +++ b/src/Entity/CustomerMeta.php @@ -30,7 +30,7 @@ class CustomerMeta implements MetaTableTypeInterface * @var Customer * * @ORM\ManyToOne(targetEntity="App\Entity\Customer", inversedBy="meta") - * @ORM\JoinColumn(onDelete="CASCADE") + * @ORM\JoinColumn(onDelete="CASCADE", nullable=false) * @Assert\NotNull() */ private $customer; diff --git a/src/Entity/MetaTableTypeTrait.php b/src/Entity/MetaTableTypeTrait.php index ae7cf462fb..5f9a880927 100644 --- a/src/Entity/MetaTableTypeTrait.php +++ b/src/Entity/MetaTableTypeTrait.php @@ -44,7 +44,7 @@ trait MetaTableTypeTrait /** * @var bool * - * @ORM\Column(name="visible", type="boolean", nullable=false) + * @ORM\Column(name="visible", type="boolean", nullable=false, options={"default": false}) * @Assert\NotNull() */ private $visible = false; diff --git a/src/Entity/Project.php b/src/Entity/Project.php index 2863b68f47..7ba41e1c83 100644 --- a/src/Entity/Project.php +++ b/src/Entity/Project.php @@ -15,8 +15,16 @@ use Symfony\Component\Validator\Constraints as Assert; /** - * @ORM\Table(name="kimai2_projects") + * @ORM\Table(name="kimai2_projects", + * indexes={ + * @ORM\Index(columns={"customer_id","visible","name"}), + * @ORM\Index(columns={"customer_id","visible","id"}) + * } + * ) * @ORM\Entity(repositoryClass="App\Repository\ProjectRepository") + * + * columns={"customer_id","visible","name"} => IDX_407F12069395C3F37AB0E8595E237E06 => project administration without filter + * columns={"customer_id","visible","id"} => IDX_407F12069395C3F37AB0E859BF396750 => used in joins between project and customer, eg. dropdowns and activity administration page */ class Project implements EntityWithMetaFields { @@ -32,7 +40,7 @@ class Project implements EntityWithMetaFields /** * @var Customer * - * @ORM\ManyToOne(targetEntity="App\Entity\Customer", inversedBy="projects") + * @ORM\ManyToOne(targetEntity="App\Entity\Customer") * @ORM\JoinColumn(onDelete="CASCADE", nullable=false) * @Assert\NotNull() */ @@ -70,25 +78,11 @@ class Project implements EntityWithMetaFields */ private $visible = true; - /** - * @var Activity[]|ArrayCollection - * - * @ORM\OneToMany(targetEntity="App\Entity\Activity", mappedBy="project") - */ - private $activities; - // keep the trait include exactly here, for placing the column at the correct position use RatesTrait; use ColorTrait; use BudgetTrait; - /** - * @var Timesheet[]|ArrayCollection - * - * @ORM\OneToMany(targetEntity="App\Entity\Timesheet", mappedBy="project") - */ - private $timesheets; - /** * @var ProjectMeta[]|Collection * @@ -98,8 +92,6 @@ class Project implements EntityWithMetaFields public function __construct() { - $this->activities = new ArrayCollection(); - $this->timesheets = new ArrayCollection(); $this->meta = new ArrayCollection(); } @@ -163,22 +155,6 @@ public function getVisible(): bool return $this->visible; } - /** - * @return Collection - */ - public function getTimesheets(): Collection - { - return $this->timesheets; - } - - /** - * @return Collection - */ - public function getActivities(): Collection - { - return $this->activities; - } - /** * @return string|null */ diff --git a/src/Entity/ProjectMeta.php b/src/Entity/ProjectMeta.php index f70b9dcac3..375061d41b 100644 --- a/src/Entity/ProjectMeta.php +++ b/src/Entity/ProjectMeta.php @@ -30,7 +30,7 @@ class ProjectMeta implements MetaTableTypeInterface * @var Project * * @ORM\ManyToOne(targetEntity="App\Entity\Project", inversedBy="meta") - * @ORM\JoinColumn(onDelete="CASCADE") + * @ORM\JoinColumn(onDelete="CASCADE", nullable=false) * @Assert\NotNull() */ private $project; diff --git a/src/Entity/Timesheet.php b/src/Entity/Timesheet.php index 81d0c706f2..b945818db9 100644 --- a/src/Entity/Timesheet.php +++ b/src/Entity/Timesheet.php @@ -18,12 +18,23 @@ * @ORM\Table(name="kimai2_timesheet", * indexes={ * @ORM\Index(columns={"user"}), - * @ORM\Index(columns={"activity_id"}) + * @ORM\Index(columns={"activity_id"}), + * @ORM\Index(columns={"user","start_time"}), + * @ORM\Index(columns={"start_time"}), + * @ORM\Index(columns={"start_time","end_time"}), + * @ORM\Index(columns={"start_time","end_time","user"}), * } * ) * @ORM\Entity(repositoryClass="App\Repository\TimesheetRepository") * @ORM\HasLifecycleCallbacks() * @App\Validator\Constraints\Timesheet + * + * columns={"user"} => IDX_4F60C6B18D93D649 => count results for user timesheets + * columns={"activity_id"} => IDX_4F60C6B181C06096 => ??? + * columns={"user","start_time"} => IDX_4F60C6B18D93D649502DF587 => recent activities, user timesheet with date filzer + * columns={"start_time"} => IDX_4F60C6B1502DF587 => team timesheets with timerange filter only + * columns={"start_time","end_time"} => IDX_4F60C6B1502DF58741561401 => ??? + * columns={"start_time","end_time","user"} => IDX_4F60C6B1502DF587415614018D93D649 => ??? */ class Timesheet implements EntityWithMetaFields { @@ -83,7 +94,7 @@ class Timesheet implements EntityWithMetaFields /** * @var Activity * - * @ORM\ManyToOne(targetEntity="App\Entity\Activity", inversedBy="timesheets") + * @ORM\ManyToOne(targetEntity="App\Entity\Activity") * @ORM\JoinColumn(onDelete="CASCADE", nullable=false) * @Assert\NotNull() */ @@ -92,7 +103,7 @@ class Timesheet implements EntityWithMetaFields /** * @var Project * - * @ORM\ManyToOne(targetEntity="App\Entity\Project", inversedBy="timesheets") + * @ORM\ManyToOne(targetEntity="App\Entity\Project") * @ORM\JoinColumn(onDelete="CASCADE", nullable=false) * @Assert\NotNull() */ diff --git a/src/Entity/TimesheetMeta.php b/src/Entity/TimesheetMeta.php index 291b9e12da..cbcc9427d6 100644 --- a/src/Entity/TimesheetMeta.php +++ b/src/Entity/TimesheetMeta.php @@ -30,7 +30,7 @@ class TimesheetMeta implements MetaTableTypeInterface * @var Timesheet * * @ORM\ManyToOne(targetEntity="App\Entity\Timesheet", inversedBy="meta") - * @ORM\JoinColumn(onDelete="CASCADE") + * @ORM\JoinColumn(onDelete="CASCADE", nullable=false) * @Assert\NotNull() */ private $timesheet; diff --git a/src/Migrations/Version20190706224211.php b/src/Migrations/Version20190706224211.php new file mode 100644 index 0000000000..3438a20196 --- /dev/null +++ b/src/Migrations/Version20190706224211.php @@ -0,0 +1,53 @@ +getTable('kimai2_timesheet_meta'); + $timesheetMeta->changeColumn('visible', ['notnull' => true, 'default' => false]); + $timesheetMeta->changeColumn('timesheet_id', ['notnull' => true]); + + $projectMeta = $schema->getTable('kimai2_projects_meta'); + $projectMeta->changeColumn('visible', ['notnull' => true, 'default' => false]); + $projectMeta->changeColumn('project_id', ['notnull' => true]); + + $customerMeta = $schema->getTable('kimai2_customers_meta'); + $customerMeta->changeColumn('visible', ['notnull' => true, 'default' => false]); + $customerMeta->changeColumn('customer_id', ['notnull' => true]); + + $activityMeta = $schema->getTable('kimai2_activities_meta'); + $activityMeta->changeColumn('visible', ['notnull' => true, 'default' => false]); + $activityMeta->changeColumn('activity_id', ['notnull' => true]); + } + + public function down(Schema $schema): void + { + // the columns above were created incorrect in migration Version20190617100845 for upgraded systems + // that's why there are no equivalent changes in down() + } +} diff --git a/src/Migrations/Version20190706224219.php b/src/Migrations/Version20190706224219.php new file mode 100644 index 0000000000..f7f8116c43 --- /dev/null +++ b/src/Migrations/Version20190706224219.php @@ -0,0 +1,70 @@ +getTable('kimai2_timesheet'); + $timesheet->addIndex(['user', 'start_time'], 'IDX_4F60C6B18D93D649502DF587'); + $timesheet->addIndex(['start_time'], 'IDX_4F60C6B1502DF587'); + $timesheet->addIndex(['start_time', 'end_time'], 'IDX_4F60C6B1502DF58741561401'); + $timesheet->addIndex(['start_time', 'end_time', 'user'], 'IDX_4F60C6B1502DF587415614018D93D649'); + + $activity = $schema->getTable('kimai2_activities'); + $activity->addIndex(['visible', 'project_id'], 'IDX_8811FE1C7AB0E859166D1F9C'); + $activity->addIndex(['visible', 'project_id', 'name'], 'IDX_8811FE1C7AB0E859166D1F9C5E237E06'); + $activity->addIndex(['visible', 'name'], 'IDX_8811FE1C7AB0E8595E237E06'); + + $project = $schema->getTable('kimai2_projects'); + $project->addIndex(['customer_id', 'visible', 'name'], 'IDX_407F12069395C3F37AB0E8595E237E06'); + $project->addIndex(['customer_id', 'visible', 'id'], 'IDX_407F12069395C3F37AB0E859BF396750'); + + $customer = $schema->getTable('kimai2_customers'); + $customer->addIndex(['visible'], 'IDX_5A9760447AB0E859'); + } + + public function down(Schema $schema): void + { + $activity = $schema->getTable('kimai2_activities'); + $activity->dropIndex('IDX_8811FE1C7AB0E859166D1F9C'); + $activity->dropIndex('IDX_8811FE1C7AB0E859166D1F9C5E237E06'); + $activity->dropIndex('IDX_8811FE1C7AB0E8595E237E06'); + + $customer = $schema->getTable('kimai2_customers'); + $customer->dropIndex('IDX_5A9760447AB0E859'); + + $project = $schema->getTable('kimai2_projects'); + $project->dropIndex('IDX_407F12069395C3F37AB0E8595E237E06'); + $project->dropIndex('IDX_407F12069395C3F37AB0E859BF396750'); + + $timesheet = $schema->getTable('kimai2_timesheet'); + $timesheet->dropIndex('IDX_4F60C6B18D93D649502DF587'); + $timesheet->dropIndex('IDX_4F60C6B1502DF587'); + $timesheet->dropIndex('IDX_4F60C6B1502DF587415614018D93D649'); + $timesheet->dropIndex('idx_4f60c6b1502df58741561401'); + } +} diff --git a/tests/Entity/ActivityTest.php b/tests/Entity/ActivityTest.php index 09c50fe11c..ba6e13cd3a 100644 --- a/tests/Entity/ActivityTest.php +++ b/tests/Entity/ActivityTest.php @@ -27,7 +27,6 @@ public function testDefaultValues() $this->assertNull($sut->getName()); $this->assertNull($sut->getComment()); $this->assertTrue($sut->getVisible()); - self::assertIsIterable($sut->getTimesheets()); $this->assertNull($sut->getFixedRate()); $this->assertNull($sut->getHourlyRate()); $this->assertNull($sut->getColor()); diff --git a/tests/Entity/CustomerTest.php b/tests/Entity/CustomerTest.php index cb675e56ce..20e760662c 100644 --- a/tests/Entity/CustomerTest.php +++ b/tests/Entity/CustomerTest.php @@ -26,8 +26,6 @@ public function testDefaultValues() $this->assertNull($sut->getName()); $this->assertNull($sut->getNumber()); $this->assertNull($sut->getComment()); - self::assertIsIterable($sut->getProjects()); - self::assertEmpty($sut->getProjects()); $this->assertTrue($sut->getVisible()); $this->assertNull($sut->getCompany()); diff --git a/tests/Entity/ProjectTest.php b/tests/Entity/ProjectTest.php index b8a4b1835f..82091c7e7c 100644 --- a/tests/Entity/ProjectTest.php +++ b/tests/Entity/ProjectTest.php @@ -31,10 +31,6 @@ public function testDefaultValues() $this->assertTrue($sut->getVisible()); $this->assertNull($sut->getFixedRate()); $this->assertNull($sut->getHourlyRate()); - self::assertIsIterable($sut->getTimesheets()); - self::assertEmpty($sut->getTimesheets()); - self::assertIsIterable($sut->getActivities()); - self::assertEmpty($sut->getActivities()); $this->assertNull($sut->getColor()); $this->assertEquals(0.0, $sut->getBudget()); $this->assertEquals(0, $sut->getTimeBudget());