您可以使用此查询。您可以节省75%的时间。我检查了更多数据集。子查询需要更多时间。
SELECt p1.id, p1.security, p1.buy_date FROM positions p1left join positions p2 on p1.security = p2.security and p1.buy_date < p2.buy_date where p2.id is null;
SQL小提琴链接

您可以使用此查询。您可以节省75%的时间。我检查了更多数据集。子查询需要更多时间。
SELECt p1.id, p1.security, p1.buy_date FROM positions p1left join positions p2 on p1.security = p2.security and p1.buy_date < p2.buy_date where p2.id is null;
SQL小提琴链接