2
เคียวรีที่มีประสิทธิภาพเพื่อรับค่าสูงสุดต่อกลุ่มจากตารางขนาดใหญ่
รับตาราง: Column | Type id | integer latitude | numeric(9,6) longitude | numeric(9,6) speed | integer equipment_id | integer created_at | timestamp without time zone Indexes: "geoposition_records_pkey" PRIMARY KEY, btree (id) ตารางมี 20 ล้านบันทึกที่ไม่ได้พูดค่อนข้างมาก แต่มันทำให้การสแกนตามลำดับช้าลง ฉันจะได้รับบันทึกสุดท้ายmax(created_at)ของแต่ละรายการได้equipment_idอย่างไร ฉันได้ลองค้นหาทั้งสองข้อต่อไปนี้โดยมีหลายรุ่นที่ฉันได้อ่านจากคำตอบของหัวข้อนี้: select max(created_at),equipment_id from geoposition_records group by equipment_id; select distinct on (equipment_id) equipment_id,created_at from …