@@ -34,36 +34,53 @@ public function deletePreview(Preview $preview): void {
3434 * @return \Generator<array{storageId: int, fileIds: int[]}>
3535 */
3636 public function getAvailableFileIds (): \Generator {
37- $ maxQb = $ this ->connection ->getQueryBuilder ();
38- $ maxQb ->select ($ maxQb ->func ()->max ('id ' ))
39- ->from ($ this ->previewMapper ->getTableName ())
40- ->groupBy ('file_id ' );
41-
42- $ qb = $ this ->connection ->getQueryBuilder ();
43- $ qb ->select ('file_id ' , 'storage_id ' )
44- ->from ($ this ->previewMapper ->getTableName ())
45- ->where ($ qb ->expr ()->in ('id ' , $ qb ->createFunction ($ maxQb ->getSQL ())));
46-
47- $ result = $ qb ->executeQuery ();
48-
49- $ lastStorageId = -1 ;
50- /** @var int[] $fileIds */
51- $ fileIds = [];
52-
53- // Previews next to each others in the database are likely in the same storage, so group them
54- while ($ row = $ result ->fetch ()) {
55- if ($ lastStorageId !== (int )$ row ['storage_id ' ]) {
56- if ($ lastStorageId !== -1 ) {
57- yield ['storageId ' => $ lastStorageId , 'fileIds ' => $ fileIds ];
58- $ fileIds = [];
37+ $ lastId = null ;
38+ while (true ) {
39+ $ maxQb = $ this ->connection ->getQueryBuilder ();
40+ $ maxQb ->selectAlias ($ maxQb ->func ()->max ('id ' ), 'max_id ' )
41+ ->from ($ this ->previewMapper ->getTableName ())
42+ ->groupBy ('file_id ' )
43+ ->orderBy ('max_id ' , 'ASC ' );
44+
45+ $ qb = $ this ->connection ->getQueryBuilder ();
46+
47+ if ($ lastId !== null ) {
48+ $ maxQb ->andWhere ($ maxQb ->expr ()->gt ('id ' , $ qb ->createNamedParameter ($ lastId )));
49+ }
50+
51+ $ qb ->select ('id ' , 'file_id ' , 'storage_id ' )
52+ ->from ($ this ->previewMapper ->getTableName (), 'p1 ' )
53+ ->innerJoin ('p1 ' , $ qb ->createFunction ('( ' . $ maxQb ->getSQL () . ') ' ), 'p2 ' , $ qb ->expr ()->eq ('p1.id ' , 'p2.max_id ' ))
54+ ->setMaxResults (1000 );
55+
56+ $ result = $ qb ->executeQuery ();
57+
58+ $ lastStorageId = -1 ;
59+ /** @var int[] $fileIds */
60+ $ fileIds = [];
61+
62+ $ found = false ;
63+ // Previews next to each others in the database are likely in the same storage, so group them
64+ while ($ row = $ result ->fetch ()) {
65+ $ found = true ;
66+ if ($ lastStorageId !== (int )$ row ['storage_id ' ]) {
67+ if ($ lastStorageId !== -1 ) {
68+ yield ['storageId ' => $ lastStorageId , 'fileIds ' => $ fileIds ];
69+ $ fileIds = [];
70+ }
71+ $ lastStorageId = (int )$ row ['storage_id ' ];
5972 }
60- $ lastStorageId = (int )$ row ['storage_id ' ];
73+ $ fileIds [] = (int )$ row ['file_id ' ];
74+ $ lastId = $ row ['id ' ];
75+ }
76+
77+ if (count ($ fileIds ) > 0 ) {
78+ yield ['storageId ' => $ lastStorageId , 'fileIds ' => $ fileIds ];
6179 }
62- $ fileIds [] = (int )$ row ['file_id ' ];
63- }
6480
65- if (count ($ fileIds ) > 0 ) {
66- yield ['storageId ' => $ lastStorageId , 'fileIds ' => $ fileIds ];
81+ if (!$ found ) {
82+ break ;
83+ }
6784 }
6885 }
6986
0 commit comments