mysql查询优化 - join和orderby

时间:2011-01-20 16:28:42

标签: sql mysql

我需要一点mysql查询优化的帮助。这是一个简单的查询,但任何事情都不对,我找不到它: - (

我有2个表:产品(> 40000行)和product_tags(> 5 mil)

表1 - >之间存在关系。 N.每个产品都可以在表格产品标签中包含许多标签。

我有这个简单的查询:

EXPLAIN SELECT t.product_id, kwt.tag_id
FROM products AS t, product_tags AS kwt
WHERE 1
AND t.product_id = kwt.product_id
AND kwt.tag_id =11
ORDER BY t.order_date 

wchich返回55个结果。

第一种情况:如果我有表的这种表结构:

CREATE TABLE IF NOT EXISTS `products` (
  `product_id` int(10) unsigned NOT NULL auto_increment,
  `product_source_id` smallint(5) unsigned NOT NULL,
  `order_date` int(10) unsigned NOT NULL,
  PRIMARY KEY  (`product_id`),
  KEY `order_date` (`order_date`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 ;


CREATE TABLE IF NOT EXISTS `product_tags` (
  `product_tag_id` int(10) unsigned NOT NULL auto_increment,
  `tag_id` int(10) unsigned NOT NULL,
  `product_id` int(11) NOT NULL,
  PRIMARY KEY  (`product_tag_id`),
  KEY `product_id` (`product_id`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8  ;

然后查询的解释是:

+----+-------------+-------+-------+---------------+------------+---------+---------------------------+-------+-------------+
| id | select_type | table | type  | possible_keys | key        | key_len | ref                       | rows  | Extra       |
+----+-------------+-------+-------+---------------+------------+---------+---------------------------+-------+-------------+
|  1 | SIMPLE      | t     | index | PRIMARY       | order_date | 4       | NULL                      | 45392 | Using index | 
|  1 | SIMPLE      | kwt   | ref   | product_id    | product_id | 4       | t.product_id          |     3 | Using where | 
+----+-------------+-------+-------+---------------+------------+---------+---------------------------+-------+-------------+

从表产品中获取所有行,但临时表没有任何内容。

第二种情况:如果我在product_tags中为字段“tag_id”添加索引,那么图片就不同了:

+----+-------------+-------+--------+-------------------+---------+---------+-----------------------------+------+---------------------------------+
| id | select_type | table | type   | possible_keys     | key     | key_len | ref                         | rows | Extra                           |
+----+-------------+-------+--------+-------------------+---------+---------+-----------------------------+------+---------------------------------+
|  1 | SIMPLE      | kwt   | ref    | product_id,tag_id | tag_id  | 4       | const                       |   55 | Using temporary; Using filesort | 
|  1 | SIMPLE      | t     | eq_ref | PRIMARY           | PRIMARY | 4       | kwt.product_id              |    1 | Using where                     | 
+----+-------------+-------+--------+-------------------+---------+---------+-----------------------------+------+---------------------------------+

现在它只选择55行,这是正确的,但查询是havy :(

我的错误在哪里?

由于 尼克

1 个答案:

答案 0 :(得分:1)

这就是我要做的事情:

阅读这些资源

重新设计系统以利用群集主键

简化架构

drop table if exists products;
create table products
(
prod_id int unsigned not null auto_increment primary key,
name varchar(255) not null unique
)
engine = innodb; 

drop table if exists tags;
create table tags
(
tag_id mediumint unsigned not null auto_increment primary key,
name varchar(255) not null unique
)
engine = innodb; 

drop table if exists product_tags;
create table product_tags
(
tag_id mediumint unsigned not null,
prod_id int unsigned not null,
created_date date not null,
primary key (tag_id, prod_id), -- note the clustered composite index and the order !!
key (prod_id)
)
engine = innodb;

测试架构

select
 pt.tag_id,
 pt.prod_id
from
 product_tags pt
inner join products p on pt.prod_id = p.prod_id
where
 pt.tag_id = 11
order by
 pt.created_date
limit 10;

我甚至可以将product_tags PK更改为主键(tag_id,prod_id,created_date),但这一切都取决于您运行的典型查询。你可以,如果你认为这会提高性能,那么只需在创建日期创建一个非聚集的二级索引。

希望这会有所帮助:)