|
28 | 28 | use Throwable; |
29 | 29 |
|
30 | 30 | class Row2Mapper { |
31 | | - private const ROWS_LOADING_CHUNK_SIZE = 5000; |
| 31 | + private const MAX_DB_PARAMETERS = 65535; |
32 | 32 |
|
33 | 33 | use TTransactional; |
34 | 34 |
|
@@ -195,16 +195,37 @@ public function findAll(array $showColumnIds, int $tableId, ?int $limit = null, |
195 | 195 | * @throws InternalError |
196 | 196 | */ |
197 | 197 | private function getRows(array $rowIds, array $columnIds): array { |
198 | | - if (count($rowIds) > self::ROWS_LOADING_CHUNK_SIZE) { |
199 | | - $chunks = []; |
200 | | - do { |
201 | | - $chunkRowIds = array_splice($rowIds, 0, self::ROWS_LOADING_CHUNK_SIZE); |
202 | | - $chunks[] = $this->getRows($chunkRowIds, $columnIds); |
203 | | - } while (count($rowIds) > self::ROWS_LOADING_CHUNK_SIZE); |
| 198 | + if (empty($rowIds)) { |
| 199 | + return []; |
| 200 | + } |
| 201 | + |
| 202 | + $columnTypesCount = count($this->columnsHelper->columns); |
| 203 | + if ($columnTypesCount === 0) { |
| 204 | + return []; |
| 205 | + } |
204 | 206 |
|
205 | | - return array_merge(...$chunks); |
| 207 | + $columnsCount = count($columnIds); |
| 208 | + $maxParamsPerType = floor(self::MAX_DB_PARAMETERS / $columnTypesCount) ; |
| 209 | + $calculatedChunkSize = (int)($maxParamsPerType - $columnsCount); |
| 210 | + $chunkSize = max(1, $calculatedChunkSize); |
| 211 | + |
| 212 | + $rowIdChunks = array_chunk($rowIds, $chunkSize); |
| 213 | + $chunks = []; |
| 214 | + foreach ($rowIdChunks as $chunkedRowIds) { |
| 215 | + $chunks[] = $this->getRowsChunk($chunkedRowIds, $columnIds); |
206 | 216 | } |
207 | 217 |
|
| 218 | + return array_merge(...$chunks); |
| 219 | + } |
| 220 | + |
| 221 | + /** |
| 222 | + * Builds and executes the UNION ALL query for a specific chunk of rows. |
| 223 | + * @param array $rowIds |
| 224 | + * @param array $columnIds |
| 225 | + * @return Row2[] |
| 226 | + * @throws InternalError |
| 227 | + */ |
| 228 | + private function getRowsChunk(array $rowIds, array $columnIds): array { |
208 | 229 | $qb = $this->db->getQueryBuilder(); |
209 | 230 |
|
210 | 231 | $qbSqlForColumnTypes = null; |
|
0 commit comments