1010use Magento \Framework \App \ResourceConnection ;
1111use Magento \Framework \DB \Adapter \AdapterInterface ;
1212use Magento \Framework \DB \Select ;
13+ use Magento \Sales \Model \ResourceModel \Grid ;
1314
1415/**
1516 * Query builder for retrieving list of updated order ids that was not synced to grid table.
@@ -80,23 +81,6 @@ private function getConnection(): AdapterInterface
8081 return $ this ->connection ;
8182 }
8283
83- /**
84- * Returns update time of the last row in the grid.
85- *
86- * @param string $gridTableName
87- * @return string
88- */
89- private function getLastUpdatedAtValue (string $ gridTableName ): string
90- {
91- $ select = $ this ->getConnection ()->select ()
92- ->from ($ this ->getConnection ()->getTableName ($ gridTableName ), ['updated_at ' ])
93- ->order ('updated_at DESC ' )
94- ->limit (1 );
95- $ row = $ this ->getConnection ()->fetchRow ($ select );
96-
97- return $ row ['updated_at ' ] ?? '0000-00-00 00:00:00 ' ;
98- }
99-
10084 /**
10185 * Builds select object.
10286 *
@@ -107,9 +91,15 @@ private function getLastUpdatedAtValue(string $gridTableName): string
10791 public function build (string $ mainTableName , string $ gridTableName ): Select
10892 {
10993 $ select = $ this ->getConnection ()->select ()
110- ->from ($ mainTableName , [$ mainTableName . '.entity_id ' ]);
111- $ lastUpdateTime = $ this ->getLastUpdatedAtValue ($ gridTableName );
112- $ select ->where ($ mainTableName . '.updated_at >= ? ' , $ lastUpdateTime );
94+ ->from (['main_table ' => $ mainTableName ], ['main_table.entity_id ' ])
95+ ->joinLeft (
96+ ['grid_table ' => $ this ->resourceConnection ->getTableName ($ gridTableName )],
97+ 'main_table.entity_id = grid_table.entity_id ' ,
98+ []
99+ );
100+
101+ $ select ->where ('grid_table.entity_id IS NULL ' );
102+ $ select ->limit (Grid::BATCH_SIZE );
113103 foreach ($ this ->additionalGridTables as $ table ) {
114104 $ select ->joinLeft (
115105 [$ table => $ table ],
0 commit comments