相关MySQL查询替换存储过程

时间:2015-09-24 12:57:17

标签: mysql stored-procedures

我接手了我的前任建造的一个项目。该项目包含一个最初从此处获取的存储过程:

https://stackoverflow.com/a/9100182/439925

目前这阻止我们更新MySQL,因此我一直在尝试删除它并将对subStringCount()的调用替换为对该问题的最佳答案的调​​整。

(( Round (( Char_length(`title`) - Char_length(REPLACE(`title`, 'info', "")) ) / Char_length('info')) * 30 ))  AS `title_score`,

查询用于计算搜索字符串在多个字段中出现的次数,然后按总计排序。不幸的是,我无法获得与旧的查询结果匹配的新查询结果。

2个完整查询如下:

旧的存储过程:

SELECT SQL_CALC_FOUND_ROWS `temp`.*,
                       ( `title_score` + `source_score`
                         + `abstract_score` + `authors_score`
                         + `drugs_score` + `uploader_score`
                         + `area_score`
                         + Ifnull(`document_content_score`, 0) ) AS
                       `relevance`
FROM   (SELECT `kb_uploads`.*,
           (( Substringcount(`title`, 'info') * 30 )) AS `title_score`,
           (( Substringcount(`source`, 'info') * 15 )) AS`source_score`,
           (( Substringcount(`abstract`, 'info') * 20 ) AS`abstract_score`,
           (( Substringcount(`authors`, 'info') * 30 )) AS `authors_score`,
           (( Substringcount(`drugs`, 'info') * 20 ))  AS `drugs_score`,
           (( Substringcount(`kb_users`.`name`, 'info') * 20 )) AS `uploader_score`,
           (( Substringcount(`kb_upload_areas`.`name`, 'info') * 20 )) AS `area_score`,
           ( `content_tbl`.`index_score` * 1 ) AS `document_content_score`
    FROM   `kb_uploads`
           LEFT JOIN `kb_users`
                  ON `kb_users`.`id` = `kb_uploads`.`uploader`
           LEFT JOIN `kb_upload_areas`
                  ON `kb_upload_areas`.`id` = `kb_uploads`.`area`
           LEFT JOIN (SELECT `upload`,
                             Sum(`weighting`) AS `index_score`
                      FROM   `kb_search_index`
                      WHERE  `word` = 'info'
                      GROUP  BY `upload`) AS `content_tbl`
                  ON `content_tbl`.`upload` = `kb_uploads`.`id`) AS `temp`
WHERE `is_deleted` = 0
HAVING `relevance` > 0
ORDER BY `relevance` DESC
LIMIT 10 OFFSET 0

新的没有存储的Proc:

SELECT SQL_CALC_FOUND_ROWS `temp`.*,
                       ( `title_score` + `source_score`
                         + `abstract_score` + `authors_score`
                         + `drugs_score` + `uploader_score`
                         + `area_score`
                         + Ifnull(`document_content_score`, 0) ) AS
                       `relevance`
FROM   (SELECT `kb_uploads`.*,
           (( Round (( Char_length(`title`) - Char_length(REPLACE(`title`, 'info', "")) ) / Char_length('info')) * 30 ))  AS `title_score`,
           (( Round (( Char_length(`source`) - Char_length(REPLACE(`source`,'info' , "")) ) / Char_length('info')) * 15 )) AS `source_score`,
           (( Round (( Char_length(`abstract`) - Char_length(REPLACE(`abstract`, 'info', "" ) ) ) / Char_length('info')) * 20 )) AS `abstract_score`,
           (( Round (( Char_length(`authors`) - Char_length(REPLACE(`authors`,'info', "")))/Char_length('info')) * 30 )) AS `authors_score`,
           (( Round (( Char_length(`drugs`) - Char_length(REPLACE(`drugs`,'info',"")) ) / Char_length('info')) * 20 )) AS `drugs_score`,
           (( Round (( Char_length(`kb_users`.`name`) - Char_length(REPLACE(`kb_users`.`name`,'info',""))) / Char_length('info')) * 20 )) AS `uploader_score`,
           (( Round (( Char_length(`kb_upload_areas`.`name`) - Char_length(REPLACE(`kb_upload_areas`.`name`,'info', "")) ) / Char_length('info'))* 20 )) AS`area_score`,
           ( `content_tbl`.`index_score` * 1 ) AS `document_content_score`
    FROM   `kb_uploads`
           LEFT JOIN `kb_users`
                  ON `kb_users`.`id` = `kb_uploads`.`uploader`
           LEFT JOIN `kb_upload_areas`
                  ON `kb_upload_areas`.`id` = `kb_uploads`.`area`
           LEFT JOIN (SELECT `upload`,
                             Sum(`weighting`) AS `index_score`
                      FROM   `kb_search_index`
                      WHERE  `word` = 'info'
                      GROUP  BY `upload`) AS `content_tbl`
                  ON `content_tbl`.`upload` = `kb_uploads`.`id`) AS `temp`
WHERE `is_deleted` = 0
HAVING `relevance` > 0
ORDER BY `relevance` DESC
LIMIT 10 OFFSET 0

有两个主要问题。

1)Ifnull在新查询中不起作用。表列主要包含null而不是0

2)新查询中的相关性数字与旧查询中的数字不匹配,可能与IFNULL无法正常工作有关。

完整的查询是用PHP构造的,我已经将代码保留为逻辑没有改变,只有字符串concats来替换存储过程。

0 个答案:

没有答案