Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize EAV collections #911

Merged
merged 6 commits into from
Sep 28, 2022
Merged

Conversation

Sekiphp
Copy link
Contributor

@Sekiphp Sekiphp commented Mar 16, 2020

Some DB background

Screenshot_7

Overview

In cases when Magento collections are extended from Mage_Eav_Model_Entity_Collection_Abstract there are filtering by entity_type_id but for example for customer entity_type_id is always 1 (viz. screenshot)
Screenshot_8

But this conditions slow down multiple queries and I think, that at this time is abandoned using entity_type_id for most of queries.

Copy link
Member

@colinmollenhour colinmollenhour left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I approve. Would be great to drop the column and index and save some db memory as well but this would likely break someone's code somewhere. I don't see an issue with this change though.

@colinmollenhour
Copy link
Member

@Sekiphp Do you agree with the changes? Please update PR if so. Thanks!

@joshua-bn
Copy link
Contributor

Bump @Sekiphp. Looks like a good suggestion from @colinmollenhour and pretty simple. Looks like a good change to implement.

@Sekiphp
Copy link
Contributor Author

Sekiphp commented Oct 7, 2020

Your suggestion implemented @colinmollenhour ;-)

colinmollenhour
colinmollenhour previously approved these changes Oct 8, 2020
Copy link
Member

@colinmollenhour colinmollenhour left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, thanks!

@Sekiphp
Copy link
Contributor Author

Sekiphp commented Dec 3, 2020

Finally fixed @colinmollenhour @joshua-bn @tmotyl

@joshua-bn
Copy link
Contributor

It's been a while since I looked at this so I might've lost some knowledge here.

For the entity tables like customer, catalog, etc, the entity_type_id will always be the same. For the eav_entity table (I don't think anyone ever uses that table) this does matter. The condition seems backwards then. If it is the default table, we should add the entity_type_id filter. If it is not then there's no need, right?

Copy link
Member

@colinmollenhour colinmollenhour left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@joshua-bn is right, I had the logic backwards! Doh!

@Sekiphp
Copy link
Contributor Author

Sekiphp commented Dec 27, 2020

It's been a while since I looked at this so I might've lost some knowledge here.

For the entity tables like customer, catalog, etc, the entity_type_id will always be the same. For the eav_entity table (I don't think anyone ever uses that table) this does matter. The condition seems backwards then. If it is the default table, we should add the entity_type_id filter. If it is not then there's no need, right?

So the right solution is?:

if ($this->getEntity()->getEntityType()->getEntityModel() == Mage_Eav_Model_Entity::DEFAULT_ENTITY_TABLE && $this->getEntity()->getTypeId()) { 
    $this->addAttributeToFilter('entity_type_id', $this->getEntity()->getTypeId());
}

@colinmollenhour I did some changes, because: $this->getEntity()->getEntityTable() returns table name and Mage_Eav_Model_Entity::DEFAULT_ENTITY_TABLE contains model path.

@joshua-bn
Copy link
Contributor

@Sekiphp it should be the table, not the model.

I think this is what we're looking for:

    protected function _initSelect()
    {
        $this->getSelect()->from(['e' => $this->getEntity()->getEntityTable()]);

        if ($this->getEntity()->getEntityTable() === Mage_Eav_Model_Entity::DEFAULT_ENTITY_TABLE && $this->getEntity()->getTypeId()) {
            $this->addAttributeToFilter('entity_type_id', $this->getEntity()->getTypeId());
        }

        return $this;
    }

@joshua-bn
Copy link
Contributor

btw, here's what M2 has for this https://github.com/magento/magento2/blob/2.4-develop/app/code/Magento/Eav/Model/Entity/Collection/AbstractCollection.php#L217

@luigifab
Copy link
Contributor

I'm not sure, what is the status of this PR?

@ADDISON74
Copy link
Contributor

With a little changes in code as requested we can merge this PR .

@fballiano fballiano dismissed colinmollenhour’s stale review May 29, 2022 12:45

As Colin wrote, changes were implemented and/or he noticed his comment was wrong

@fballiano fballiano requested a review from luigifab May 29, 2022 12:46
@luigifab
Copy link
Contributor

The SQL queries with our MariaDB 10.7:

MariaDB []> SELECT SQL_NO_CACHE COUNT(*) FROM customer_entity AS `e` WHERE (`e`.entity_type_id = '1');
+----------+
| COUNT(*) |
+----------+
|  1214517 |
+----------+
1 row in set (0.283 sec)

MariaDB []> SELECT SQL_NO_CACHE COUNT(*) FROM customer_entity AS `e` WHERE 1;
+----------+
| COUNT(*) |
+----------+
|  1214517 |
+----------+
1 row in set (0.245 sec)

I don't see problems at localhost with this change.

Co-authored-by: Colin Mollenhour <colin@mollenhour.com>
@github-actions github-actions bot removed the Component: Directory Relates to Mage_Directory label Sep 1, 2022
Copy link
Contributor

@kiatng kiatng left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tested.

@luigifab
Copy link
Contributor

luigifab commented Sep 5, 2022

So I tested the wrong version at localhost, but I didn't see any bad changes...

@fballiano
Copy link
Contributor

this one is approved for merging, is everybody comfortable with this?

@kiatng
Copy link
Contributor

kiatng commented Sep 28, 2022

I have this in production a month now. No issue so far.

@sreichel sreichel merged commit 2946fcb into OpenMage:1.9.4.x Sep 28, 2022
@github-actions
Copy link
Contributor

Unit Test Results

1 files  ±0  1 suites  ±0   0s ⏱️ ±0s
0 tests ±0  0 ✔️ ±0  0 💤 ±0  0 ❌ ±0 
7 runs  ±0  5 ✔️ ±0  2 💤 ±0  0 ❌ ±0 

Results for commit 2946fcb. ± Comparison against base commit 712a3ee.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

9 participants