Şu anda, yanıtlanmış soruya göre kullanıcılarla eşleşen bir webapp yazıyorum. Eşleme algoritmamı sadece bir sorguda gerçekleştirdim ve şu ana kadar 2 kullanıcı arasındaki eşleşme yüzdesini hesaplamak için 8.2ms almasını sağladım. Ancak web sunucumun bir kullanıcı listesini alması ve bu sorguyu gerçekleştiren listede yinelemesi gerekiyor. 5000 kullanıcı için yerel makinemde 50 sn sürdü. Kullanıcı_kimliği ve bir sütun hesaplanan eşleme ile bir sütun döndüren bir sorguya her şeyi koymak mümkün mü? Veya saklı bir prosedür bir seçenek midir?SQL: kullanıcı yüzdesini, eşleşme yüzdesi için hesaplanan sütunla döndürün?
Şu anda MySQL ile çalışıyorum ancak gerekirse veritabanlarını değiştirmeye istekliyim. http://sqlfiddle.com/#!2/84233/1
ve benim eşleme sorgusu:
SELECT COALESCE(SQRT((100.0*as1.actual_score/ps1.possible_score) * (100.0*as2.actual_score/ps2.possible_score)) - (100/ps1.commonquestions), 0) AS perc
FROM (SELECT SUM(imp.value) AS actual_score
FROM user_questions AS uq1
INNER JOIN importances imp ON imp.id = uq1.importance
INNER JOIN user_questions uq2 ON uq2.question_id = uq1.question_id AND uq2.user_id = 101
AND (uq1.accans1 = uq2.answer_id
OR uq1.accans2 = uq2.answer_id
OR uq1.accans3 = uq2.answer_id
OR uq1.accans4 = uq2.answer_id)
WHERE uq1.user_id = 1) AS as1,
(SELECT SUM(value) AS possible_score, COUNT(*) AS commonquestions
FROM user_questions AS uq1
INNER JOIN importances ON importances.id = uq1.importance
INNER JOIN user_questions uq2 ON uq1.question_id = uq2.question_id AND uq2.user_id = 101
WHERE uq1.user_id = 1) AS ps1,
(SELECT SUM(imp.value) AS actual_score
FROM user_questions AS uq1
INNER JOIN importances imp ON imp.id = uq1.importance
INNER JOIN user_questions uq2 ON uq2.question_id = uq1.question_id AND uq2.user_id = 1
AND (uq1.accans1 = uq2.answer_id
OR uq1.accans2 = uq2.answer_id
OR uq1.accans3 = uq2.answer_id
OR uq1.accans4 = uq2.answer_id)
WHERE uq1.user_id = 101) AS as2,
(SELECT SUM(value) AS possible_score
FROM user_questions AS uq1
INNER JOIN importances ON importances.id = uq1.importance
INNER JOIN user_questions uq2 ON uq1.question_id = uq2.question_id AND uq2.user_id = 1
WHERE uq1.user_id = 101) AS ps2
Sorgunun iki "ayağının" "ortak soruları" alt ifadesini birleştirebilirsiniz. Ayrıca, kullanıcı = 1 ve kullanıcı = 101 alt sorgularını bir genelleştirilmiş CTE sorgusuna genellestirebilirsiniz (DBMS'niz onları destekliyorsa. Ama önce: Lütfen tablo tanımlarını ve belki de bazı verileri gösterin. – wildplasser
Evet, istenen çıktıya sahip veriler –
Benimle oynamak için bir SQLFiddle oluşturdum :) Kullanıcı 1 ve 5'i eşleştirdiğimde sonuç '43 .678 'olmalıdır. Http://sqlfiddle.com/#!2/84233/1 – Mexxer