10
10
use Magento \Framework \App \ResourceConnection ;
11
11
use Magento \Framework \DB \Adapter \AdapterInterface ;
12
12
use Magento \Framework \DB \Select ;
13
+ use Magento \Sales \Model \ResourceModel \Grid ;
13
14
14
15
/**
15
16
* Query builder for retrieving list of updated order ids that was not synced to grid table.
@@ -80,23 +81,6 @@ private function getConnection(): AdapterInterface
80
81
return $ this ->connection ;
81
82
}
82
83
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
-
100
84
/**
101
85
* Builds select object.
102
86
*
@@ -107,9 +91,15 @@ private function getLastUpdatedAtValue(string $gridTableName): string
107
91
public function build (string $ mainTableName , string $ gridTableName ): Select
108
92
{
109
93
$ 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 );
113
103
foreach ($ this ->additionalGridTables as $ table ) {
114
104
$ select ->joinLeft (
115
105
[$ table => $ table ],
0 commit comments