谁能帮我优化这个查询。运行需要一段时间:
su_pref 表有近 900,000 行
SELECT p.mykey,
p.merchant_name,
m.merchant_url,
p.name,
p.description,
p.image_url,
p.deep_link,
p.rrp_price,
p.display_price,
c.category,
p.su_parent_name
FROM su_pref p #
INNER JOIN su_categoryrefs cr ON p.mykey = cr.mykey
INNER JOIN su_categories c ON cr.id = c.id
INNER JOIN su_merchants m ON p.merchant_id=m.id
WHERE
cr.id =36
ORDER BY p.date_created DESC LIMIT 0,20
这就是我使用 EXPLAIN 时得到的
我真的很难理解如何使用 EXPLAIN 来优化查询,所以任何帮助将不胜感激。
这是表定义。su_pref 表很大,所以我去掉了一些字段
CREATE TABLE `su_pref` (
`mykey` varchar(50) NOT NULL,
`merchant_id` smallint(3) default NULL,
`merchant_name` varchar(50) NOT NULL default '',
`brand` varchar(50) default NULL,
`merchantproductkey` varchar(50) default NULL,
`upc` varchar(15) default NULL,
`name` varchar(255) NOT NULL default '',
`description` varchar(2500) NOT NULL default '',
`short_description` varchar(500) default NULL,
`thumb_url` varchar(500) default NULL,
`image_url` varchar(500) default NULL,
`deep_link` varchar(1000) default NULL,
`merchant_link` varchar(255) default NULL,
`rrp_price` decimal(11,2) default NULL,
`display_price` decimal(11,2) default NULL,
`delivery_cost` decimal(11,2) default NULL,
`price_range` tinyint(1) default NULL,
`keywords` varchar(500) default NULL,
`su_parent_name` enum('Women','Men','') NOT NULL,
`date_created` date default NULL,
`date_modified` datetime default NULL,
`wp_featured` varchar(1) default NULL,
`hp_featured` varchar(1) default NULL,
`published` varchar(1) default NULL,
`in_todays_feed` varchar(1) default NULL,
PRIMARY KEY (`mykey`),
KEY `merchant_id` (`merchant_id`),
KEY `date_created` (`date_created`),
FULLTEXT KEY `product_search`
(`name`,`description`,`short_description`,
`keywords`, `product_type`,`colour`,`style`,
`material`,`datafeed_category_name`,
`datafeed_subcategory_name`,
`brand`,`merchant_name`),
FULLTEXT KEY `name` (`name`,`datafeed_category_name`,
`datafeed_subcategory_name`,`product_type`,`keywords`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1
-
CREATE TABLE `su_categories` (
`id` mediumint(9) NOT NULL,
`category` varchar(100) NOT NULL,
`parent_id` tinyint(4) NOT NULL,
`update_query` varchar(3000) default NULL,
`url` varchar(150) default NULL,
`last_update` datetime default NULL,
PRIMARY KEY (`id`,`category`,`parent_id`),
KEY `parent_id` (`parent_id`),
KEY `category_id` (`id`),
FULLTEXT KEY `category_name` (`category`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1
-
CREATE TABLE `su_categoryrefs` (
`mykey` varchar(255) NOT NULL,
`id` smallint(4) NOT NULL,
PRIMARY KEY (`mykey`,`id`),
KEY `id` (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1
-
CREATE TABLE `su_merchants` (
`id` mediumint(9) NOT NULL,
`merchant_name` varchar(40) NOT NULL,
`merchant_url` varchar(40) default NULL,
`merchant_website` varchar(40) default NULL,
`merchant_description` varchar(2000) default NULL,
`merchant_featured` varchar(1) default NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `merchant_name` (`merchant_name`)
) ENGINE=MyISAM AUTO_INCREMENT=128 DEFAULT CHARSET=latin1
先感谢您