我的查询应该搜索表格中的行,查找包含搜索词的任何行(例如,狗%'),然后返回相关父级的ID(已加入)来自不同的表)。因此,我按父ID对结果进行分组,然后对其进行重新排序,最后设置限制/偏移以获取10个uniqe ID的列表。查询在内部存储过程中,我从PHP调用它。
问题是,我经常得到不同的结果。尽管IN params是相同的,并且数据库中没有任何内容被更改,但每个请求的结果可能会有所不同 - ID不一样,顺序也有差异,我在两个请求中得到重复的ID ...
这是程序:
CREATE DEFINER=`root`@`localhost` PROCEDURE `FIND_VOCABULARY_ID_BY_MEANING`(IN `in_searched_value` varchar(50)
, IN `in_offset` INT)
LANGUAGE SQL
NOT DETERMINISTIC
CONTAINS SQL
SQL SECURITY DEFINER
COMMENT ''
BEGIN
insert into test values (in_searched_value,in_offset);
SELECT
v.id
FROM vocabulary AS v
LEFT JOIN vocabulary_sense AS vs ON vs.vocabulary_id = v.id
LEFT JOIN vocabulary_sense_gloss_eng AS vsg ON vsg.sense_id = vs.id
WHERE vsg.gloss LIKE CONCAT(in_searched_value,'%')
GROUP BY v.id
ORDER BY v.calculated_prio DESC
LIMIT 10 OFFSET in_offset;
END
我该怎么称呼它:
$stmt = $this->pdo->prepare("CALL $procedureToCall('$searchedTerm', $offset)");
在PHP中,我将日志值(请求及其结果)记录到日志文件中。以下是两种不同的方法,每种方法有五种要求:
[2016-10-11 13:35:19] CALL FIND_VOCABULARY_ID_BY_MEANING('vulgar', 0)
[2016-10-11 13:35:19] 17141,16446,38334,58166,17121,45822,35328,37553,41185,45832
[2016-10-11 13:35:22] CALL FIND_VOCABULARY_ID_BY_MEANING('vulgar', 10)
[2016-10-11 13:35:22] 46659,51149,53639,55276,56388,95,63900,71780,73935,17134
[2016-10-11 13:35:25] CALL FIND_VOCABULARY_ID_BY_MEANING('vulgar', 20)
[2016-10-11 13:35:25] 83260,97433,17176,103416,111512,135069,147790,38335,159709,38338
[2016-10-11 13:35:27] CALL FIND_VOCABULARY_ID_BY_MEANING('vulgar', 30)
[2016-10-11 13:35:27] 162898,38340,163783,38359,165067,38360,171044,38364,38378,38380
[2016-10-11 13:35:31] CALL FIND_VOCABULARY_ID_BY_MEANING('vulgar', 40)
[2016-10-11 13:35:31] 38384,41163,41211,45832,45833,45837
[2016-10-11 13:35:33] CALL FIND_VOCABULARY_ID_BY_MEANING('vulgar', 50)
[2016-10-11 13:50:38] CALL FIND_VOCABULARY_ID_BY_MEANING('vulgar', 0)
[2016-10-11 13:50:38] 17141,16446,38334,58166,17121,45822,35328,37553,41185,56388
[2016-10-11 13:50:41] CALL FIND_VOCABULARY_ID_BY_MEANING('vulgar', 10)
[2016-10-11 13:50:41] 95,63900,71780,73935,17134,83260,97433,17176,103416,111512
[2016-10-11 13:50:45] CALL FIND_VOCABULARY_ID_BY_MEANING('vulgar', 20)
[2016-10-11 13:50:45] 135069,147790,38335,159709,38338,162898,38340,163783,38359,165067
[2016-10-11 13:50:48] CALL FIND_VOCABULARY_ID_BY_MEANING('vulgar', 30)
[2016-10-11 13:50:48] 38360,171044,38364,38378,38380,38384,41163,41211,45832,45833
[2016-10-11 13:50:50] CALL FIND_VOCABULARY_ID_BY_MEANING('vulgar', 40)
[2016-10-11 13:50:50] 45837,45841,46659,51149,53639,55276
[2016-10-11 13:50:53] CALL FIND_VOCABULARY_ID_BY_MEANING('vulgar', 50)
如您所见,45832 id在第一次接近时发送给我两次(偏移0和40)。在第二种方法中,它存在一次,但在偏移30 ...
我还在Mysql中记录输入参数 - offset和searchingTerm - 也是正确的,与上面生成的php一致。那么为什么我会有这些不同的情况呢?我在这里做错了什么?
修改
我发现直接从MYSQL客户端(而不是php)调用过程给了我很好的结果 - 但是当我只调用普通查询时:
SELECT
v.id
FROM vocabulary AS v
LEFT JOIN vocabulary_sense AS vs ON vs.vocabulary_id = v.id
LEFT JOIN vocabulary_sense_gloss_eng AS vsg ON vsg.sense_id = vs.id
WHERE vsg.gloss LIKE CONCAT('vulgar','%')
GROUP BY v.id
ORDER BY v.calculated_prio DESC
LIMIT 10 OFFSET 0;
结果也不同(仍然有意思,我得到相同的结果,但这些结果与查询内部程序不同)...
编辑2
以下是查询中使用的表的结构:
CREATE TABLE `vocabulary` (
`id` MEDIUMINT(8) UNSIGNED NOT NULL AUTO_INCREMENT,
`entry_sequence` MEDIUMINT(8) UNSIGNED NOT NULL,
`jlpt_level` TINYINT(4) NULL DEFAULT NULL,
`calculated_prio` MEDIUMINT(9) NOT NULL DEFAULT '0',
PRIMARY KEY (`id`),
INDEX `idx_vocabulary_calculated_prio` (`calculated_prio`)
)
COLLATE='utf8_general_ci'
ENGINE=InnoDB
AUTO_INCREMENT=174912
;
CREATE TABLE `vocabulary_sense` (
`id` MEDIUMINT(8) UNSIGNED NOT NULL AUTO_INCREMENT,
`vocabulary_id` MEDIUMINT(8) UNSIGNED NOT NULL,
PRIMARY KEY (`id`),
INDEX `fk_vocabulary_sense_vocabulary_id` (`vocabulary_id`),
CONSTRAINT `fk_vocabulary_sense_vocabulary_id` FOREIGN KEY (`vocabulary_id`) REFERENCES `vocabulary` (`id`)
)
COLLATE='utf8_general_ci'
ENGINE=InnoDB
AUTO_INCREMENT=195529
;
CREATE TABLE `vocabulary_sense_gloss_eng` (
`id` MEDIUMINT(8) UNSIGNED NOT NULL AUTO_INCREMENT,
`sense_id` MEDIUMINT(8) UNSIGNED NOT NULL,
`gloss` TEXT NOT NULL,
PRIMARY KEY (`id`),
INDEX `vocabulary_sense_gloss_vocabulary_sense_id` (`sense_id`),
INDEX `vocabulary_sense_gloss_gloss` (`gloss`(255)),
CONSTRAINT `vocabulary_sense_gloss_eng` FOREIGN KEY (`sense_id`) REFERENCES `vocabulary_sense` (`id`)
)
COLLATE='utf8_general_ci'
ENGINE=InnoDB
ROW_FORMAT=COMPACT
AUTO_INCREMENT=317857
;
词汇是主要的入口。 vocabulary_sense(一对多)指向它。并且vocabulary_sense_gloss_eng(再次,一对多)指向vocabulary_sense。
" calculated_prio"只是一个静态INT值。
答案 0 :(得分:0)
哦,该死的,我真笨:)
问题在于排序,calculated_prio只有前九行的正值,而其余的只有0.所以只有前九行是特定的顺序,其他一切都是随机的。