1

我正在尝试从 OsCommerce 优化以下修改后的 MySQL 查询:

select distinct p.products_id, pd.products_name, m.manufacturers_name, s.specials_new_products_price from products p 
inner join products_description pd on p.products_id = pd.products_id
inner join products_to_categories p2c on p.products_id = p2c.products_id 
left join manufacturers m on p.manufacturers_id = m.manufacturers_id 
left join specials s on p.products_id = s.products_id and s.specials_b2bgroup =0 
where p.products_status = '1' and p.products_model not like '%_VIP' and pd.language_id = '4' and p2c.categories_id = '1574' 
order by p.products_ordernum, p.products_model

在生产服务器上运行解释似乎没有索引用于连接表产品:

id  select_type     table   type    possible_keys   key     key_len     ref     rows    Extra
1   SIMPLE  p   ALL     PRIMARY     NULL        NULL NULL   6729    Using where; Using temporary; Using filesort
1   SIMPLE  m   eq_ref  PRIMARY     PRIMARY     4   p.manufacturers_id  1    
1   SIMPLE  s   ref     products_id products_id 4   p.products_id   2    
1   SIMPLE  pd  eq_ref  PRIMARY     PRIMARY     8   p.products_id,const     1    
1   SIMPLE  p2c eq_ref  PRIMARY     PRIMARY     8   pd.products_id,const    1   Using where; Using index; Distinct

表产品的架构如下:

CREATE TABLE IF NOT EXISTS `products` (
  `products_id` int(11) NOT NULL auto_increment,
  `products_model` varchar(50) default NULL,
  `products_image` varchar(250) default NULL,
  `products_price` decimal(15,4) NOT NULL default '0.0000',
  `products_date_added` datetime NOT NULL default '0000-00-00 00:00:00',
  `products_last_modified` datetime default NULL,
  `products_date_available` datetime default NULL,
  `products_weight` decimal(5,2) NOT NULL default '0.00',
  `products_status` tinyint(1) NOT NULL default '0',
  `products_showprod` tinyint(1) NOT NULL default '0',
  `products_showprice` tinyint(1) NOT NULL default '0',
  `products_ordernum` int(6) NOT NULL default '100',
  `products_tax_class_id` int(11) NOT NULL default '0',
  `manufacturers_id` int(11) default NULL,
  PRIMARY KEY  (`products_id`),
  KEY `idx_products_model` (`products_model`),
) ENGINE=MyISAM  DEFAULT CHARSET=greek AUTO_INCREMENT=1;

我的服务器的 MySQL 版本是 5.0.92。任何关于在哪里寻找解决方案的想法都非常受欢迎!

4

2 回答 2

0

products是嵌套循环中的前导(最外层)表,因此用于访问该表的索引与连接无关。

这个条件:

p.products_model not like '%_VIP'

是不可分割的。

您可以尝试在 上创建一个索引products (status),如果它有足够的选择性(即,带有 的值很少status = 1

于 2011-03-14T22:24:03.297 回答
0

该表的查询中只有两个约束,products您已将其声明为“主”表(因为其他所有内容都是JOIN ON):( products_status未编制索引)和products_model. 但是NOT LIKE '%...'不是可索引的约束,所以做一个简单的扫描会更快。

%如果出现在模式的中间或末尾,该索引将很有用LIKE。即便如此,它NOT仍然可能使线性扫描更快。

于 2011-03-14T22:25:29.973 回答