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

本番サーバーでexplainを実行すると、結合時にテーブル製品にインデックスが使用されていないようです。

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'

sargableではありません。

products (status)十分に選択的である場合(つまり、の値が少ない場合status = 1)、でインデックスを作成してみることができます。

于 2011-03-14T22:24:03.297 に答える
0

productsテーブルのそのクエリには、「マスター」テーブルとして宣言した制約が2つだけあります(他のすべてがであるためJOIN ON):( products_statusインデックス付けされていない)とproducts_model。ただしNOT LIKE '%...'、インデックス付け可能な制約ではないため、単純なスキャンを実行する方が高速です。

インデックスは、パターン%の中央または最後にある場合に役立ちます。LIKEそれでもNOT、線形スキャンが高速になる可能性があります。

于 2011-03-14T22:25:29.973 に答える