MySQL / PHP - benzersiz bir görünüm sisteminin uygulanması nasıl?

2 Cevap php

Introduction

Ben eşsiz bir izleme sistemi uygulamak için en iyi yolu nedir merak ediyorum ... Ben nasıl biliyorum, ben bunu düşünüyorum nasıl açıklayabilir ve eğer herhangi bir hata veya iyileştirmeler işaret etmektedir.

Açıkçası ben bir video kimliği ve (nispeten) benzersiz kullanıcıyı tanımlayan bir şey içeren bir günlüğü tablosunu saklamak zorunda olacak. İlk başta ben başlık istek ve IP kombinasyonu kabul ama basit tutmak ve sadece IP kullanmaya karar verdi. Ayrıca bu şekilde bir kullanıcı farklı bir tarayıcı kullanarak kendi video görüşlerini artırmak olamaz.

This is how I would think to do it:

  1. When a user visits I do a SELECT similar to this:

    SELECT 1 FROM tbl_log WHERE IP = $usersip AND video_id = $video_id

  2. if there is no result then I must insert a record

    INSERT into tbl_log (IP,video_id) VALUES ($usersip, $video_id)

  3. 1'e ve görüşlerini artırmak

    SELECT views FROM tbl_video WHERE video_id = $video_id

    UPDATE tbl_video SET views = $result['views'] + 1 WHERE video_id = $video_id

Questions

  • I guess I do not want to have
    millions of log records slowing down my site so should I run a cron job to empty the log table once a day?

  • Should I make the views
    transactional? (I guess a slightly
    depreciated view count is less
    important than a slow site because of row locks)

  • Is there a way to reduce the load on the mysql server.... I fear if every view requires an increased view count and an IP log that it will be pretty expensive. I have seen that youtube
    and the like do not update the views instantly... do they cache the
    updates some how and then run them at once? if so how?

  • How efficient is my system? Can you think of any improvements?

2 Cevap

Burada yapabilirsiniz iyileştirmeler için bazı fikirler vardır.

IP + video_id olmak tbl_log birincil anahtar ayarlayın. Sonra sadece yapabilirsiniz

 REPLACE INTO tbl_log (IP,video_id) VALUES ($usersip, $video_id)

(SQL enjeksiyonu önlemek için bu php değişkenleri kaçmak için emin olun.)

Şimdi sadece tek bir sorgu ile günlük tablosunu güncelliyoruz. Sonra, periyodik gibi bir şey ile tbl_video olarak görünümleri alan güncelleyebilirsiniz:

UPDATE tbl_video SET views = (select count(*) from tbl_log where video_id = $video_id) where video_id = $video_id

Sen bir cron ile bunu yapabilirsiniz, veya bir 'last_count_update' alanını eklemek ve erişildiğinde son sayım süresi, 2 saat ya da her neyse daha eski ise videoyu güncelleyebilirsiniz. Sık sık ziyaret değil videoları bir grup varsa, bu biraz daha az iş olacaktır.

I guess I do not want to have millions of log records slowing down my site so should I run a cron job to empty the log table once a day?

Pahalı bir WHERE yan tümcesi hangi bir SEÇİMİ kullanmaktan kaçınmak YİNELENEN KEY UPDATE sözdizimi ON MySQL kullanmayı düşünün. Eğer günlük tablo aynı zamanda bir zaman damgası sütun olsaydı, o değerini yenilemek olabilir.

INSERT into tbl_log (IP,video_id) VALUES ($usersip, $video_id) ON DUPLICATE KEY UPDATE time_recorded = now();

Bu bir UNIQUE IP üzerinde kısıtlama ve video_id sütun olmasını gerektirecektir.

Should I make the views transactional? (I guess a slightly depreciated view count is less important than a slow site because of row locks)

Hayır, tek bir udpate sorgusu ile bunu başarabilirsiniz çünkü.

UPDATE tbl_video SET views = views + 1 WHERE video_id = $video_id

Is there a way to reduce the load on the mysql server.... I fear if every view requires an increased view count and an IP log that it will be pretty expensive. I have seen that youtube and the like do not update the views instantly... do they cache the updates some how and then run them at once? if so how?

Bu çok kötü değil - güvenilir rekor görünüm verilerini yakalamak için başka bir yol gerçekten var. (Onlar da değerini önbelleklediğiniz mümkün olsa) Youtube durumunda, daha büyük olasılıkla onlar yüzlerce sunucu beri fark gecikmeye neden olan yazar veya çoğaltma gecikmiş oluyor

How efficient is my system? Can you think of any improvements?

Ben kafamın üst kapalı, zaten burada bahsedilen ne başka.