I am working on a financial software where we have huge table of ticks with more than 800M rows.
A single row: symbol_id, broker_id, date, time, bid, ask
The problem is the table has many ticks with identical first four columns and different bid
and ask
.
What is the best way to update the table and combine such rows into one by taking an average of bid
and ask
columns?
Example:
|1|2|2012-01-01|12:00:00|1.3456|1.3678|
|1|2|2012-01-01|12:00:00|1.3463|1.3612|
Result row:
|1|2|2012-01-01|12:00:00|1.3459|1.3645|
create table as select ...
using a group by, the drop the old table and rename the new oneUPDATE
lock held on a given aggregate record. Periodic re-aggregation will be much more efficient. You've also got to watch accumulated rounding errors when you repeatedly recalculate an average; better to store the sum and number of values and increment them, then returnsum/n
on the fly.symbol_id, broker_id, date, time
and just ignore records with the same PK oninsert
.