2009-08-06 14 views
7

検索対象の4つのテーブルがあります。それぞれにフルテキストインデックスがあります。クエリですべてのインデックスを使用できますか?複数のテーブルにまたがるmySQLの一致

CREATE TABLE `categories` (
    `id` int(5) unsigned NOT NULL auto_increment, 
    `display_order` int(5) unsigned default NULL, 
    `name` varchar(64) default NULL, 
    `last_modified` timestamp NOT NULL default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP, 
    PRIMARY KEY (`id`), 
    UNIQUE KEY `order` (`display_order`), 
    FULLTEXT KEY `full_name` (`name`) 
) ENGINE=MyISAM AUTO_INCREMENT=6 DEFAULT CHARSET=latin1; 

CREATE TABLE `host_types` (
    `id` int(5) unsigned NOT NULL auto_increment, 
    `category_id` int(5) unsigned default NULL, 
    `display_order` int(5) unsigned default NULL, 
    `name` varchar(64) default NULL, 
    `last_modified` timestamp NOT NULL default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP, 
    PRIMARY KEY (`id`), 
    UNIQUE KEY `order` (`category_id`,`display_order`), 
    FULLTEXT KEY `full_name` (`name`) 
) ENGINE=MyISAM AUTO_INCREMENT=13 DEFAULT CHARSET=latin1; 


CREATE TABLE `hosts` (
    `id` int(5) unsigned NOT NULL auto_increment, 
    `host_id` int(5) unsigned default NULL, 
    `display_order` int(5) unsigned default NULL, 
    `name` varchar(64) default NULL, 
    `last_modified` timestamp NOT NULL default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP, 
    PRIMARY KEY (`id`), 
    UNIQUE KEY `order` (`host_id`,`display_order`), 
    FULLTEXT KEY `full_name` (`name`) 
) ENGINE=MyISAM AUTO_INCREMENT=4 DEFAULT CHARSET=latin1; 


CREATE TABLE `products` (
    `id` int(11) unsigned NOT NULL auto_increment, 
    `host_id` int(5) unsigned default NULL, 
    `display_order` int(5) unsigned default NULL, 
    `uid` varchar(10) default NULL, 
    `name` varchar(128) default NULL, 
    `keywords` text, 
    `description` text, 
    `price` decimal(10,2) default NULL, 
    `quantity` int(11) unsigned default NULL, 
    `last_modified` timestamp NOT NULL default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP, 
    PRIMARY KEY (`id`), 
    FULLTEXT KEY `full_name` (`name`,`keywords`,`description`,`uid`) 
) ENGINE=MyISAM AUTO_INCREMENT=14 DEFAULT CHARSET=latin1; 

ここは私の質問です。

SELECT categories.name AS category, 
    categories.id AS category_id, 
    host_types.name AS host_type, 
    host_types.id AS host_type_id, 
    hosts.name AS host, 
    hosts.id AS host_id, 
    products.name as name, 
    products.id AS product_id, 
    products.keywords as keywords, 
    products.description AS description, 
    products.quantity AS quantity, 
    products.price AS price, 
    products.uid as catalogue, 
    MATCH(categories.name, host_types.name, hosts.name, products.name, 
     products.keywords, products.description, products.uid) 
     AGAINST('search term') as score 
FROM products 
LEFT JOIN hosts ON products.host_id = hosts.id 
LEFT JOIN host_types ON hosts.host_id = host_types.id 
LEFT JOIN categories ON host_types.category_id = categories.id 
WHERE MATCH(categories.name, host_types.name, hosts.name, products.name, 
      products.keywords, products.description, products.uid) 
     AGAINST('search term') 
ORDER BY score DESC; 
  • categories.name == FULLTEXT - 1
  • host_types.name == FULLTEXT - 2
  • hosts.name == FULLTEXT - 3
  • products.name、products.keywords、 products.description、products.uid == FULLTEXT - 4

ここでは私のSQL構造であり、かつ私は上記のクエリを使用しました。

SELECT 
    categories.name AS category, 
    categories.id AS category_id, 
    host_types.name AS host_type, 
    host_types.id AS host_type_id, 
    hosts.name AS host, 
    hosts.id AS host_id, 
    products.name as name, 
    products.id AS product_id, 
    products.keywords as keywords, 
    products.description AS description, 
    products.quantity AS quantity, 
    products.price AS price, 
    products.uid as catalgue 
    MATCH(categories.name) AGAINST('search term') as cscore, 
    MATCH(host_types.name) AGAINST('search term') as htscore, 
    MATCH(hosts.name) AGAINST('search term') as hscore, 
    MATCH(products.name, products.keywords, products.description, products.uid) 
    AGAINST('search term') as score 
FROM products 
LEFT JOIN hosts ON products.host_id = hosts.id 
LEFT JOIN host_types ON hosts.host_id = host_types.id 
LEFT JOIN categories ON host_types.category_id = categories.id 
WHERE 
    MATCH(categories.name) AGAINST('search term') OR 
    MATCH(host_types.name) AGAINST('search term') OR 
    MATCH(hosts.name) AGAINST('search term') OR 
    MATCH(products.name, products.keywords, products.description, products.uid) 
    AGAINST('search term') 
ORDER BY score DESC 



     CREATE TABLE `categories` (
    `id` int(5) unsigned NOT NULL auto_increment, 
    `display_order` int(5) unsigned default NULL, 
    `name` varchar(64) default NULL, 
    `last_modified` timestamp NOT NULL default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP, 
    PRIMARY KEY (`id`), 
    UNIQUE KEY `order` (`display_order`), 
    FULLTEXT KEY `full_name` (`name`) 
) ENGINE=MyISAM AUTO_INCREMENT=6 DEFAULT CHARSET=latin1; 

CREATE TABLE `host_types` (
    `id` int(5) unsigned NOT NULL auto_increment, 
    `category_id` int(5) unsigned default NULL, 
    `display_order` int(5) unsigned default NULL, 
    `name` varchar(64) default NULL, 
    `last_modified` timestamp NOT NULL default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP, 
    PRIMARY KEY (`id`), 
    UNIQUE KEY `order` (`category_id`,`display_order`), 
    FULLTEXT KEY `full_name` (`name`) 
) ENGINE=MyISAM AUTO_INCREMENT=13 DEFAULT CHARSET=latin1; 


CREATE TABLE `hosts` (
    `id` int(5) unsigned NOT NULL auto_increment, 
    `host_id` int(5) unsigned default NULL, 
    `display_order` int(5) unsigned default NULL, 
    `name` varchar(64) default NULL, 
    `last_modified` timestamp NOT NULL default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP, 
    PRIMARY KEY (`id`), 
    UNIQUE KEY `order` (`host_id`,`display_order`), 
    FULLTEXT KEY `full_name` (`name`) 
) ENGINE=MyISAM AUTO_INCREMENT=4 DEFAULT CHARSET=latin1; 


CREATE TABLE `products` (
    `id` int(11) unsigned NOT NULL auto_increment, 
    `host_id` int(5) unsigned default NULL, 
    `display_order` int(5) unsigned default NULL, 
    `uid` varchar(10) default NULL, 
    `name` varchar(128) default NULL, 
    `keywords` text, 
    `description` text, 
    `price` decimal(10,2) default NULL, 
    `quantity` int(11) unsigned default NULL, 
    `last_modified` timestamp NOT NULL default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP, 
    PRIMARY KEY (`id`), 
    FULLTEXT KEY `full_name` (`name`,`keywords`,`description`,`uid`) 
) ENGINE=MyISAM AUTO_INCREMENT=14 DEFAULT CHARSET=latin1; 
+2

50 .. 。正確に – Dirk

+0

彼は最長のSQLクエリを見せているように見えます。 –

+1

私は質問を探してそれを編集しました...それは謎ですか? –

答えて

22
  • あなたはMySQLで複数のテーブル間でフルテキストインデックス(またはインデックスのいずれかの種類)を定義することはできません。各インデックス定義は、まったく1つのテーブルを参照します。指定された全文索引のすべての列は、同じ表からのものでなければなりません。

  • MATCH()関数の引数として指定された列は、単一の全文索引の一部でなければなりません。 MATCH()への1回の呼び出しを使用して、データベース内の全文索引の一部であるすべての列を検索することはできません。

  • フルテキストインデックスは、CHARVARCHAR、およびTEXTデータ型で定義されたインデックス列のみをインデックスします。

  • 各テーブルでフルテキストインデックスを定義できます。

例:

CREATE TABLE categories (
    id SERIAL PRIMARY KEY, 
    name VARCHAR(100), 
    FULLTEXT INDEX ftcat (name) 
); 

CREATE TABLE host_types (
    id SERIAL PRIMARY KEY, 
    category_id BIGINT UNSIGNED, 
    name VARCHAR(100), 
    FULLTEXT INDEX ftht (name) 
); 

CREATE TABLE hosts (
    id SERIAL PRIMARY KEY, 
    host_id BIGINT UNSIGNED, 
    category_id BIGINT UNSIGNED, 
    name VARCHAR(100), 
    FULLTEXT INDEX fthost (name) 
); 

CREATE TABLE products (
    id SERIAL PRIMARY KEY, 
    name VARCHAR(100), 
    keywords VARCHAR(100), 
    uid VARCHAR(100), 
    description VARCHAR(100), 
    quantity INTEGER, 
    price NUMERIC(9,2), 
    host_id BIGINT UNSIGNED, 
    FULLTEXT INDEX ftprod (name, keywords, description, uid) 
); 

そして、あなたはそれぞれのフルテキストインデックスを使用するクエリを記述することができます。この...質問に答える人へ

SELECT ... 
    MATCH(categories.name) AGAINST('search term') as cscore, 
    MATCH(host_types.name) AGAINST('search term') as htscore, 
    MATCH(hosts.name) AGAINST('search term') as hscore, 
    MATCH(products.name, products.keywords, products.description, products.uid) 
    AGAINST('search term') as score 
FROM products 
LEFT JOIN hosts ON products.host_id = hosts.id 
LEFT JOIN host_types ON hosts.host_id = host_types.id 
LEFT JOIN categories ON host_types.category_id = categories.id 
WHERE 
    MATCH(categories.name) AGAINST('search term') OR 
    MATCH(host_types.name) AGAINST('search term') OR 
    MATCH(hosts.name) AGAINST('search term') OR 
    MATCH(products.name, products.keywords, products.description, products.uid) 
    AGAINST('search term') 
ORDER BY score DESC; 
関連する問題