Simplify history.CreateAccounts() to avoid select statement
The CreateAccounts , CreateHistoryClaimableBalances , CreateAssets, and CreateHistoryLiquidityPools functions are used to insert records into the history_accounts, history_claimable_balances, history_assets, and history_liquidity_pools tables. The functions are implemented by first executing insert statements into the respective tables followed by executing select statements to lookup the rows which where just inserted.
However, the select statements can be avoided because we can add a RETURNING clause to the insert statements which will return the row which was just inserted.
The RETURNING clause in an INSERT statement only returns the newly inserted rows. To return all rows, including existing ones, we can force an update using the ON CONFLICT UPDATE SET... clause. However this additional update will likely have a negative impact on performance.
To improve reap performance, we plan to add a last_Reference_ledger column to these tables. This column will be updated every time which will allow us to use the RETURNING clause. However, when reingesting older ledgers, we may not want to update the last_Reference_ledger if it is greater than the one we are reingesting. So at this point I'm not sure that we can combine the two statements.