简体   繁体   English

mysql查询性能指标

[英]mysql query performance index

my following query needs more then two minutes and i don't which index is the best to improve the performance: 我的以下查询需要两分钟以上的时间,而我认为哪个索引不是提高性能的最佳方法:

SELECT COUNT(sid), COUNT(DISTINCT(cid)), shop 
FROM forwarding 
WHERE fDate BETWEEN '2011-06-01' AND '2011-06-30' 
GROUP BY shop;

The EXPLAIN result: 解释结果:

id   select_type   table            type    possible_keys                                                        key       key_len    ref       rows     Extra
1    SIMPLE        sal_forwarding   index   forwardDate,forwardDate_2,forwardDate_3,forwardDate_4,forwardDate_5  f_shop    40         (NULL)    2448997  Using where; Using index

The key f_shop has the following structure: (f_shop, forwardDate, cid) 密钥f_shop具有以下结构: (f_shop,forwardDate,cid)

What is the best Index to improve the performance for my query? 改善查询性能的最佳索引是什么?

Thank you very much. 非常感谢你。

UPDATE: Here is the table Create Statement: 更新:这是表Create Statement:

CREATE TABLE `forwarding` (
  `sid` BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT,
  `f_shop` INT(11) NOT NULL,
  `f_offer` INT(11) DEFAULT NULL,
  `cid` CHAR(32) DEFAULT NULL,
  `f_partner` VARCHAR(20) NOT NULL,
   .
   . 
   .
   +8-10
   .
   .
   .
  `fDate` DATE NOT NULL,
  PRIMARY KEY (`sid`),
  KEY `f_shop` (`f_shop`,`fDate`,`cid`),
  KEY `f_partner` (`f_partner`,`fDate`),
  KEY `fDate` (`fDate`,`cid`),
  KEY `fDate_2` (`fDate`,`f_shop`),
  KEY `fDate_3` (`fDate`,`f_shop`,`f_partner`),
  KEY `fDate_4` (`fDate`,`f_partner`,`cid`),
  KEY `fDate_5` (`fDate`,`f_affiliateId`)
) ENGINE=INNODB AUTO_INCREMENT=10747233 DEFAULT CHARSET=latin1

Actually there are more then 5million rows. 实际上有超过500万行。

您需要一个复合索引

ALTER TABLE forwarding ADD INDEX shopdate (shop, fDate)

What is the best Index to improve the performance for my query? 改善查询性能的最佳索引是什么?

a clustered primary key 群集主键

http://dev.mysql.com/doc/refman/5.0/en/innodb-index-types.html http://dev.mysql.com/doc/refman/5.0/en/innodb-index-types.html

http://www.xaprb.com/blog/2006/07/04/how-to-exploit-mysql-index-optimizations/ http://www.xaprb.com/blog/2006/07/04/how-to-exploit-mysql-index-optimizations/

MySQL and NoSQL: Help me to choose the right one MySQL和NoSQL:帮我选择合适的

60 million entries, select entries from a certain month. 6000万条目,从某个月中选择条目。 How to optimize database? 如何优化数据库?

How to avoid "Using temporary" in many-to-many queries? 如何在多对多查询中避免“使用临时”?

however your current clustered PK sid wont be much help so try something along the lines of: 但是,您当前的集群PK sid不会有太大帮助,因此请尝试以下方法:

create table forwarding
(
f_date date not null,
f_shop int unsigned not null,
sid int unsigned not null, -- added for uniqueness
...
primary key (f_date, f_shop, sid) -- clustered primary key
)
engine=innodb;

hope this helps :) 希望这可以帮助 :)

索引商店列,你可以在这里实现的另外一件事是按日期使用分区,你的查询会快速运行

I think you need a key for the forwardDate, since that is the only attribute used in the WHERE clause of your query. 我认为你需要一个用于forwardDate的键,因为这是查询的WHERE子句中使用的唯一属性。

EDIT As noted in other answers, a compound index on shop and forwardDate is the way to go. 编辑如其他答案中所述,shop和forwardDate上的复合索引是解决之道。 I missed the last part of the query due to the single line formatting. 由于单行格式化,我错过了查询的最后一部分。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM