The vertical heated pipe is widely used in thermal engineering applications, as buoyancy can help drive a flow, but several flow regimes are possible: shear-driven turbulence, laminarised flow and convective turbulence. Steady velocity fields that maximise heat transfer have previously been calculated for heated pipe flow, but were calculated independently of buoyancy forces, and hence independently of the flow regime and time-dependent dynamics of the flow. In this work, a variational method is applied to find an optimal body force of limited magnitude
$A_0$ that maximises heat transfer for the vertical arrangement, with the velocity field constrained by the full governing equations. In our calculations, mostly at Reynolds number
${\textit{Re}}=3000$, it is found that streamwise-independent rolls remain optimal, as in previous steady optimisations, but that the optimal number of rolls and their radial position is dependent on the flow regime. Surprisingly, while it is generally assumed that turbulence enhances heat transfer, for the strongly forced case, time dependence typically leads to a reduction. Beyond offering potential improvement through the targeting of the roll configuration for this application, wider implications are that optimisations under the steady flow assumption may overestimate improvements in heat transfer, and that strategies that simply aim to induce turbulence may not necessarily be efficient in enhancing heat transfer either. Including time dependence and the full governing equations in the optimisation is challenging but offers further enhancement and improved reliability in prediction.