[MySql] ถ้ามีข้อมูลเยอะๆ จะทำยังไงไม่ให้ query ช้าครับ

เริ่มโดย 20auguzt, 23 มีนาคม 2011, 14:28:26

หัวข้อก่อนหน้า - หัวข้อถัดไป

0 สมาชิก และ 1 ผู้มาเยือน กำลังดูหัวข้อนี้

bonshington

ถ้าหัวใจเต้นทีนึง insert 1 ที แบบนี้ ถือว่า ออกแบบผิดนะ มันทื่อเกินไป
ผมแนะนำใช้ cache ใน app จากนั้น insert ทีเดียวยาว แบบนี้ จำนวน transaction ขึ้นกับจำนวนคนไข้ ที่คาดว่า ปริมาณการ insert ต่อวันน่าจะลดลงหลายพันเท่าอยู่

เช่น

C#
public static class Cache
{
 private static List<KeyValuePair<string, DateTime>> cache = new ...

 public static void Add(string patientName){
   cache.Add(new KeyValuPair(patientName, DateTime.Now));
 }

 public static Action Flush(){
   cache.GroupBy(entry => entry.Key)
     .ToList()
     // ถ้า กลัว performance ไม่แรง ให้ใส่ .AsParallel().ForAll เปลี่ยน ForEach เป็น ForAll มันจะยิง insert โดยจำนวน connection = จำนวน thread ถ้ากลัวแรงเกิน ก็กำหนด max thread ให้มัน
     .ForEach(patient => DB.call_proc("heartbeat_inst", patient.Key, patient.Select(data => data.Value).ToArray()));
     
 }

 static Cache(){
   System.Threading.Tasks.Task.Factory.StartNew(() => {
     System.Threading.Thread.Sleep(5000); // 5 seconds
     Cache.Flush();
   });
 }
}


ถ้าเป็น postgre มันจะ insert array ได้
ปล ของ postgre function กับ store proc คืออันเดียวกัน

CREATE OR REPLACE FUNCTION heartbeat_inst(_name text, _beat timestamptz[])
 RETURNS setof hartbeat AS
$BODY$

begin

 return query
   insert into hartbeat(name, beat)
   select _name, unnest(_beat)
   returning *;

end;

$BODY$
 LANGUAGE plpgsql VOLATILE
 COST 100
 ROWS 100;

ohmohm