1

初めて投稿します... 通常は検索で答えを見つけることができますが、これはあいまいな状況のようです。

ポイントに。親テーブルからの検索用に特別に設計されたテーブルを使用して、いくつかの検索を作成しました。私が作成したクエリが、WHERE 句で OR を使用する場合に PRIMARY KEY インデックスを使用しない状況に遭遇しましたが、AND を交換すると意図したとおりに機能します。テーブルを次に示します。

CREATE TABLE `tag_idx` (
  `tag_id` bigint(20) NOT NULL DEFAULT '0',
  `animation_id` int(10) NOT NULL DEFAULT '0',
  `tag_text` varchar(64) DEFAULT NULL,
  PRIMARY KEY (`animation_id`,`tag_id`),
  KEY `animation_id` (`animation_id`),
  FULLTEXT KEY `tag_text` (`tag_text`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8

CREATE TABLE `animation_notag_idx` (
  `animation_id` int(10) NOT NULL DEFAULT '0',
  `animation_user_id` int(10) NOT NULL,
  `contest_score` int(11) DEFAULT NULL,
  `animation_views` int(11) DEFAULT NULL,
  `animation_votes` int(255) DEFAULT NULL,
  `animation_finished` int(50) DEFAULT NULL,
  `animation_title` varchar(50) DEFAULT NULL,
  `artist_nickname` varchar(16) DEFAULT NULL,
  PRIMARY KEY (`animation_id`),
  KEY `animation_id` (`animation_id`),
  FULLTEXT KEY `animation_title` (`animation_title`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 

クエリ。クエリでは、animation_title と tag_text のウェイトが異なることに注意してください。

SELECT
a.animation_id,
t.tag_id,
a.contest_score,
a.animation_views,
a.animation_votes,
a.animation_finished,
a.animation_title,
a.artist_nickname,
t.tag_text,
match(a.animation_title) AGAINST ('+*funny*') as title_score,
(
SELECT MAX(
MATCH(animation_title) AGAINST('+*funny*')
 ) FROM animation_notag_idx WHERE MATCH(animation_title) AGAINST('+*funny*')
) as max_name_relevance,
match(t.tag_text) AGAINST ('+*funny*') as meta_score,
(
SELECT MAX(
MATCH(tag_text) AGAINST('+*funny*')
) FROM tag_idx WHERE MATCH(tag_text) AGAINST('+*funny*')
) as max_meta_relevance

FROM
animation_notag_idx AS a

LEFT JOIN

tag_idx AS t

on 

t.animation_id = a.animation_id
WHERE

match(a.animation_title) AGAINST ('+%funny%' IN BOOLEAN MODE)

OR

match(t.tag_text) AGAINST ('+%funny%' IN BOOLEAN MODE)


ORDER BY ((title_score / max_name_relevance) * 0.5) + ((title_score / max_meta_relevance) * 0.2) DESC

LIMIT 5
OFFSET 0;

と説明すると……。

*************************** 1. row ***************************
           id: 1
  select_type: PRIMARY
        table: a
         type: ALL
possible_keys: NULL
          key: NULL
      key_len: NULL
          ref: NULL
         rows: 3226035
        Extra: Using filesort
*************************** 2. row ***************************
           id: 1
  select_type: PRIMARY
        table: t
         type: ref
possible_keys: PRIMARY,animation_id
          key: animation_id
      key_len: 4
          ref: text_index.a.animation_id
         rows: 1
        Extra: Using where
*************************** 3. row ***************************
           id: 3
  select_type: SUBQUERY
        table: tag_idx
         type: fulltext
possible_keys: tag_text
          key: tag_text
      key_len: 0
          ref: 
         rows: 1
        Extra: Using where
*************************** 4. row ***************************
           id: 2
  select_type: SUBQUERY
        table: animation_notag_idx
         type: fulltext
possible_keys: animation_title
          key: animation_title
      key_len: 0
          ref: 
         rows: 1
        Extra: Using where
4 rows in set (0.00 sec)


And if I swap out for the AND...

*************************** 1. row ***************************
           id: 1
  select_type: PRIMARY
        table: a
         type: fulltext
possible_keys: animation_title
          key: animation_title
      key_len: 0
          ref: 
         rows: 1
        Extra: Using where; Using filesort
*************************** 2. row ***************************
           id: 1
  select_type: PRIMARY
        table: t
         type: ref
possible_keys: PRIMARY,animation_id
          key: animation_id
      key_len: 4
          ref: text_index.a.animation_id
         rows: 1
        Extra: Using where
*************************** 3. row ***************************
           id: 3
  select_type: SUBQUERY
        table: tag_idx
         type: fulltext
possible_keys: tag_text
          key: tag_text
      key_len: 0
          ref: 
         rows: 1
        Extra: Using where
*************************** 4. row ***************************
           id: 2
  select_type: SUBQUERY
        table: animation_notag_idx
         type: fulltext
possible_keys: animation_title
          key: animation_title
      key_len: 0
          ref: 
         rows: 1
        Extra: Using where
4 rows in set (0.00 sec)

どうして??このクエリを何度か書き直しましたが、オンラインで読んだものはすべて、このクエリが正しく記述されていることを示しているようです。何か案は?前もって感謝します!

4

0 に答える 0