ThaiSEOBoard.com

พัฒนาเว็บไซต์ => Programming => ข้อความที่เริ่มโดย: rawinson ที่ 15 สิงหาคม 2017, 22:50:54



หัวข้อ: ทิ้งเว็บไว้ 5 วัน sql ดึงข้อมูลมาแบบผิดๆ
เริ่มหัวข้อโดย: rawinson ที่ 15 สิงหาคม 2017, 22:50:54
ผมพัฒนาเว็บไซต์ โดย ASP C#
ดึงข้อมูลจาก SQL SERVER

หลังจากอัพลง SERVER ผ่านไปไม่กี่วัน
จะปรากฏ ข้อมูลที่ดึงมาจาก ฐานข้อมูลผิดๆ ไม่ตรงตามที่ต้องการ หรือทำงานไม่ตรงตามที่เขียนไว้

ตัวอย่างโค้ด SQL


SELECT * FROM(
select TOP(10) a.Property_Id, c.IsSale, c.IsLongTermRental, c.Latitue, c.Longitute, c.ZoomLevel
, c.IsShortTermRental, c.SalePrice, c.RentalPrice, c.ShortTermRentalPrice, c.Guid
, d.Title, e.Title as Category, f.Title as Province, k.Title as District, l.Title as SubDistrict
FROM Property_Featured a
JOIN Property_FeaturedPeriod b on a.Id = b.Featured_Id
JOIN Properties c on a.Property_Id = c.Id
JOIN Property_Local d ON a.Property_Id = d.Key_Id
JOIN Property_CategoryLocal e on c.Category_Id = e.Key_Id
JOIN Property_LocationLocal f on c.AddressLoc1_Id = f.Key_Id
JOIN Property_LocationLocal k on c.AddressLoc2_Id = k.Key_Id
JOIN Property_LocationLocal l on c.AddressLoc3_Id = l.Key_Id


WHERE b.EndDate >= getdate() and a.IsActive = 'true' AND
c.Status = '0' AND c.IsActive = 'true' AND d.Language_Id = 'th-TH'
and e.Language_Id = 'th-TH'   AND f.Language_Id = 'th-TH'  and
k.Language_Id = 'th-TH'   AND l.Language_Id = 'th-TH' and c.Category_Id='1'
order by a.Property_Id
) DUMMY_ALIAS1 UNION ALL

SELECT * FROM
(
SELECT TOP(10)  a.Id, a.IsSale, a.IsLongTermRental, a.Latitue, a.Longitute, a.ZoomLevel
, a.IsShortTermRental, a.SalePrice, a.RentalPrice, a.ShortTermRentalPrice, a.Guid 
, b.Title, c.Title as Category, d.Title as Province, e.Title as District, f.Title as SubDistrict
FROM Properties a
JOIN Property_Local b ON a.Id = b.Key_Id
JOIN Property_CategoryLocal c on a.Category_Id = c.Key_Id
JOIN Property_LocationLocal d on a.AddressLoc1_Id = d.Key_Id
JOIN Property_LocationLocal e on a.AddressLoc2_Id = e.Key_Id
JOIN Property_LocationLocal f on a.AddressLoc3_Id = f.Key_Id


WHERE a.Status = '0' AND a.IsActive = 'true' AND b.Language_Id = 'th-TH' 
AND c.Language_Id = 'th-TH'   AND d.Language_Id = 'th-TH'
AND e.Language_Id = 'th-TH'   AND f.Language_Id = 'th-TH' 
and a.Category_Id='1'

order by a.Id DESC )DUMMY_ALIAS2




โดยหากเพิ่งอัพลง SERVER ไม่นานไม่เกิน 3-4 วัน เว็บไซต์ยังทำงานปกติ
หากเกิน 5 วัน หรือผ่านไปสักพักจะมีอาการ คือ ได้ Record ซ้ำๆออกมา

เช่น หากทำงานปกติ จะได้ ข้อมูล Id001, Id002, Id003 ไปเรื่อยๆ
แต่หากดึงข้อมูลออกมาผิดๆ จะได้ Id001, Id001, Id001,....


ไม่รู้ว่าเกิดจากอะไรครับ รบกวนผู้รู้ช่วยชี้แนะที






หัวข้อ: Re: ทิ้งเว็บไว้ 5 วัน sql ดึงข้อมูลมาแบบผิดๆ
เริ่มหัวข้อโดย: ohmohm ที่ 11 กันยายน 2017, 12:56:51
ผู้ต้องสงสัย
  • Property_Local มี Key_Id ซ้ำกันไหม 
  • UNION ALL


หัวข้อ: Re: ทิ้งเว็บไว้ 5 วัน sql ดึงข้อมูลมาแบบผิดๆ
เริ่มหัวข้อโดย: infamous ที่ 11 กันยายน 2017, 14:53:17
ผมรู้สึกว่า query ชุดนี้มันทำงาน/เช็ค/join แบบซ้ำซ้อนยังไงไม่รู้ (สามารถลดได้เกิน 30%)

ลองดูครับ เผื่อฟลุ๊ค

- order by a.Id DESC )DUMMY_ALIAS2 order_by a.Id
- priority จะอยู่ที่ query แรกนะครับ
- เป็นไปได้เปลี่ยน top เป็น limit น่าจะดีกว่าครับ
- distinct อาจจะเป็นวิธีสุดท้ายของการตัด row ซ้ำครับ