角だけを同じ色にするように要求する場合は、次のことができます。
top left corner
join top right corner on equal x and color and greater y
join bottom left corner on equal y and color and greater x
join bottom right on equal x, y and color
order by (x1-x2)*(y1-x2) descending
limit 1
もちろん、制限 1 は、とにかくすべての正方形を生成する必要があるため、パフォーマンスに大きな影響はありません。
(color,x,y) および (color,y,x) インデックスを追加することで、(大幅に) 速度を向上させることができます。実行計画はおそらく次のようになります。
(1) full scan for all top left corners
(2) dependent index scan for all top right corners
(3) dependent index scan for all bottom left corners
(4) dependent index scan for the bottom right corner expecting at most one match
(5) (partial) table sort of the entire set of squares (cannot use indexes)