├── LICENSE ├── README.md ├── modman └── src └── app └── code └── local └── Mage └── Catalog └── Helper └── Category └── Url └── Rewrite.php /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 Henry van Megen 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Magento 1 fix for slow category pages using MySQL 5.7 2 | 3 | ## magento-mysql-5.7-join-fix 4 | This patch fixes an issue with Magento bizarre way of constructing queries to join all data required for category listings causing MySQL's table indexes to fail and queries to take 5 to 40 times longer than normal when using MySQL / MariaDB / Percona equivalent to MySQL 5.7 and up, compatible with Magento 1.x. 5 | 6 | ## How does it work? 7 | This bugfix short-circuit's the query by checking if category_id IS NOT null in the first place, before going on to a like query where this category_id is used. 8 | 9 | ## Is this just for Magento 1.9.x ? 10 | I've only tested it on Magento 1.9.x. After installing this fix and flushing all your caches, your category pages (and other things that use category listings (think Megamenu or other stuff) will become really fast (again). 11 | 12 | ## Are you telling me the bug still exists in 1.9.4.x ?! 13 | Yes, as of Magento 1.9.4.x, this category_id short circuiting has not been included Magento's Core 'Url.php'. 14 | 15 | ## Does Magento 2.x have this issue? 16 | I'm pretty sure the problem still exists in Magento 2.x if they build their queries the same way as in one, for which I have no reason to believe that they don't.. feel free to correct me if I'm wrong. 17 | 18 | ## How to use? 19 | Either copy the contents of the src folder to your Magento installation folder or deploy this module with [modman](https://github.com/colinmollenhour/modman). 20 | -------------------------------------------------------------------------------- /modman: -------------------------------------------------------------------------------- 1 | # app/code/local/Mage override 2 | src/app/code/local/Mage/Catalog/Helper/Category/Url/Rewrite.php app/code/local/Mage/Catalog/Helper/Category/Url/Rewrite.php 3 | -------------------------------------------------------------------------------- /src/app/code/local/Mage/Catalog/Helper/Category/Url/Rewrite.php: -------------------------------------------------------------------------------- 1 | 33 | */ 34 | class Mage_Catalog_Helper_Category_Url_Rewrite 35 | implements Mage_Catalog_Helper_Category_Url_Rewrite_Interface 36 | { 37 | /** 38 | * Adapter instance 39 | * 40 | * @var Varien_Db_Adapter_Interface 41 | */ 42 | protected $_connection; 43 | 44 | /** 45 | * Resource instance 46 | * 47 | * @var Mage_Core_Model_Resource 48 | */ 49 | protected $_resource; 50 | 51 | /** 52 | * Initialize resource and connection instances 53 | * 54 | * @param array $args 55 | */ 56 | public function __construct(array $args = array()) 57 | { 58 | $this->_resource = Mage::getSingleton('core/resource'); 59 | $this->_connection = !empty($args['connection']) ? $args['connection'] : $this->_resource 60 | ->getConnection(Mage_Core_Model_Resource::DEFAULT_READ_RESOURCE); 61 | } 62 | 63 | /** 64 | * Join url rewrite table to eav collection 65 | * 66 | * @param Mage_Eav_Model_Entity_Collection_Abstract $collection 67 | * @param int $storeId 68 | * @return Mage_Catalog_Helper_Category_Url_Rewrite 69 | */ 70 | public function joinTableToEavCollection(Mage_Eav_Model_Entity_Collection_Abstract $collection, $storeId) 71 | { 72 | $collection->joinTable( 73 | 'core/url_rewrite', 74 | 'category_id=entity_id', 75 | array('request_path'), 76 | "{{table}}.is_system=1 AND " . 77 | "{{table}}.store_id='{$storeId}' AND " . 78 | "{{table}}.category_id IS NOT null AND " . 79 | "{{table}}.id_path LIKE 'category/%'", 80 | 'left' 81 | ); 82 | return $this; 83 | } 84 | 85 | /** 86 | * Join url rewrite table to collection 87 | * 88 | * @param Mage_Catalog_Model_Resource_Category_Flat_Collection $collection 89 | * @param int $storeId 90 | * @return Mage_Catalog_Helper_Category_Url_Rewrite|Mage_Catalog_Helper_Category_Url_Rewrite_Interface 91 | */ 92 | public function joinTableToCollection(Mage_Catalog_Model_Resource_Category_Flat_Collection $collection, $storeId) 93 | { 94 | $collection->getSelect()->joinLeft( 95 | array('url_rewrite' => $collection->getTable('core/url_rewrite')), 96 | 'url_rewrite.category_id = main_table.entity_id AND url_rewrite.is_system = 1 '. 97 | ' AND ' . $collection->getConnection()->quoteInto('url_rewrite.store_id = ?', $storeId). 98 | ' AND ' . 'url_rewrite.category_id IS NOT null' . 99 | ' AND ' . $collection->getConnection()->quoteInto('url_rewrite.id_path LIKE ?', 'category/%'), 100 | array('request_path') 101 | ); 102 | return $this; 103 | } 104 | 105 | /** 106 | * Join url rewrite to select 107 | * 108 | * @param Varien_Db_Select $select 109 | * @param int $storeId 110 | * @return Mage_Catalog_Helper_Category_Url_Rewrite 111 | */ 112 | public function joinTableToSelect(Varien_Db_Select $select, $storeId) 113 | { 114 | $select->joinLeft( 115 | array('url_rewrite' => $this->_resource->getTableName('core/url_rewrite')), 116 | 'url_rewrite.category_id=main_table.entity_id AND url_rewrite.is_system=1 AND ' . 117 | $this->_connection->quoteInto('url_rewrite.store_id = ? AND ', (int)$storeId) . 118 | 'url_rewrite.category_id IS NOT null AND ' . 119 | $this->_connection->prepareSqlCondition('url_rewrite.id_path', array('like' => 'category/%')), 120 | array('request_path' => 'url_rewrite.request_path')); 121 | return $this; 122 | } 123 | } 124 | --------------------------------------------------------------------------------