@@ -46,6 +46,19 @@ public function __construct(\Magento\Framework\App\ResourceConnection $resource,
46
46
\Magento \Framework \DB \Ddl \Table::TYPE_VARBINARY => 'blob ' ,
47
47
];
48
48
49
+ /**
50
+ * Attribute types that can be united via UNION into one query
51
+ * while selecting attribute`s data from tables like `catalog_product_entity_datatype`
52
+ *
53
+ * This helps to run one query with all data types instead of one per each data type
54
+ *
55
+ * This data types are determined as 'groupable' because their tables have the same structure
56
+ * which means that they can be used in one UNION query
57
+ *
58
+ * @var array
59
+ */
60
+ private $ _groupableTypes = ['varchar ' , 'text ' , 'decimal ' , 'datetime ' , 'int ' ];
61
+
49
62
/**
50
63
* Returns DDL type by column type in database
51
64
*
@@ -72,20 +85,41 @@ public function getDdlTypeByColumnType($columnType)
72
85
/**
73
86
* Groups selects to separate unions depend on type
74
87
*
88
+ * E.g. for input array:
89
+ * [
90
+ * varchar => [select1, select2],
91
+ * text => [select3],
92
+ * int => [select4],
93
+ * bool => [select5]
94
+ * ]
95
+ *
96
+ * The result array will be:
97
+ * [
98
+ * 0 => [select1, select2, select3, select4] // contains queries for varchar & text & int
99
+ * 1 => [select5] // contains queries for bool
100
+ * ]
101
+ *
75
102
* @param array $selects
76
103
* @return array
77
104
*/
78
105
public function getLoadAttributesSelectGroups ($ selects )
79
106
{
80
107
$ mainGroup = [];
81
- foreach ($ selects as $ selectGroup ) {
82
- $ mainGroup = array_merge ($ mainGroup , $ selectGroup );
108
+
109
+ foreach ($ selects as $ dataType => $ selectGroup ) {
110
+ if (in_array ($ dataType , $ this ->_groupableTypes )) {
111
+ $ mainGroup ['all ' ][] = $ selectGroup ;
112
+ continue ;
113
+ }
114
+
115
+ $ mainGroup [$ dataType ] = $ selectGroup ;
83
116
}
84
- return $ mainGroup ;
85
- }
86
117
87
- public function mergeToOneGroup ($ selects )
88
- {
89
- return [$ this ->getLoadAttributesSelectGroups ($ selects )];
118
+ if (array_key_exists ('all ' , $ mainGroup )) {
119
+ // it is better to call array_merge once after loop instead of calling it on each loop
120
+ $ mainGroup ['all ' ] = array_merge (...$ mainGroup ['all ' ]);
121
+ }
122
+
123
+ return array_values ($ mainGroup );
90
124
}
91
125
}
0 commit comments