MariaDB复制表查询比原始表运行得更快

时间:2015-07-08 10:06:53

标签: mysql mariadb clustered-index

情景是:

  

群集上的Maria db

     

我有一个表,每天更新两次,当我查询它时   6-7秒,(它不是超过100k行的大表),但是当我   制作表的副本并运行相同的查询它返回0.7   秒。

     

现在我试图优化原始表但没有运气。有   索引和BTree。我有一种感觉,当复制一个表索引   可以说是挺直的。

有人有想法吗?我尝试了一些没有运气的建议。

(从评论中复制)

SELECT  SQL_NO_CACHE
        value1, tbl1.C, Src,
        GROUP_CONCAT( DISTINCT ROUND( tbl1.price, 4 )
            ORDER BY  tbl1.purdate DESC, tbl1.acttime DESC SEPARATOR ' - '
            ) AS purChase,
        CONCAT_WS (" ", curdef.country , curdef.currname) AS defin
    FROM  tbl1
    LEFT JOIN  curdef ON tbl1.curr = curdef.curr
    WHERE  purdate BETWEEN DATE_SUB("2015-07-06",INTERVAL 1 DAY) AND "2015-07-06"
      AND  curdef.curr_Def IS NOT NULL
      AND  BASE = "USD"
    GROUP BY  Curr,curdef.curr_id
    ORDER BY  tbl1.Curr ASC, tbl1.feeddate DESC;

HERE是原始表中的EXPLAIN

SELECT Type =   SIMPLE                                                                             
TAble       =   curdef                                                                           
Type        =   ALL                                                                                            
possible Keys = null                                                                               
Key           = null                                                                           
Key Len     =   null                                                                           
ref         =   null                                                                                   
rows        =   195                                                                            
Extra       =   Using where; Using temporary; Using filesort

JOIN表

SELECT Type =   SIMPLE  
TAble       =   tble1   
Type        =   ref                 
possible Keys = Curr,Base,Feeddate,Src  
Key           = Curr
Key Len     =   257     
ref         =   curdef.curr 
rows        =   343 

Extra       =   Using index condition; Using where

/*****************************************************************************/

这是复制表中的第二个解释表

SELECT Type =   SIMPLE  
TAble       =   curdef  
Type        =   ALL                 
possible Keys = null    
Key           = null
Key Len     =   null
ref         =   null        
rows        =   195 

Extra       =   Using where; Using temporary; Using filesort

/*****************************************************************************/

SELECT Type =   SIMPLE  
TAble       =   tbl1_test_01    
Type        =   range   
possible Keys = Curr,Base,Feeddate,Src  
Key           = Feeddate    
Key Len     =   3       
ref         =   null
rows        =   1462    

Extra       = Using index condition; Using where; Using join buffer (flat, BNL join)

/ ********************************************** ********* /

CREATE TABLE `tbl1` (
  `ID` int(11) NOT NULL AUTO_INCREMENT,
  `Purchase` varchar(255) NOT NULL,
  `Rate` double NOT NULL,
  `Feeddate` date NOT NULL DEFAULT '0000-00-00',
  `Base` varchar(255) NOT NULL DEFAULT 'USD',
  `Acttime` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  `Src` varchar(255) NOT NULL,
  `time` varchar(255) DEFAULT NULL,
  `actflag` char(255) NOT NULL,
  PRIMARY KEY (`ID`),
  KEY `Curr` (`Curr`),
  KEY `Base` (`Base`),
  KEY `Feeddate` (`Feeddate`),
  KEY `Src` (`Src`)
) ENGINE=InnoDB AUTO_INCREMENT=1911063 DEFAULT CHARSET=latin1

/ ********************************************** ********* /

CREATE TABLE `curdef` (
  `curr_Id` int(11) NOT NULL AUTO_INCREMENT,
  `curr` varchar(3) NOT NULL,
  `curr_Def` varchar(255) NOT NULL,
  `country` varchar(60) DEFAULT NULL,
  `currName` varchar(60) DEFAULT NULL,
  `Region` varchar(45) DEFAULT NULL,
  `country_code` varchar(3) DEFAULT NULL,
  PRIMARY KEY (`curr_Id`)
) ENGINE=InnoDB AUTO_INCREMENT=214 DEFAULT CHARSET=latin1'

/ ********************************************** ********* /

CREATE TABLE `tbl1_test_01` (
  `ID` int(11) NOT NULL AUTO_INCREMENT,
  `Purchase` varchar(255) NOT NULL,
  `Rate` double NOT NULL,
  `Feeddate` date NOT NULL DEFAULT '0000-00-00',
  `Base` varchar(255) NOT NULL DEFAULT 'USD',
  `Acttime` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE 
CURRENT_TIMESTAMP,
  `Src` varchar(255) NOT NULL,
  `time` varchar(255) DEFAULT NULL,
  `actflag` char(255) NOT NULL,
  PRIMARY KEY (`ID`),
  KEY `Curr` (`Curr`),
  KEY `Base` (`Base`),
  KEY `Feeddate` (`Feeddate`),
  KEY `Src` (`Src`)
) ENGINE=InnoDB AUTO_INCREMENT=1911063 DEFAULT CHARSET=latin1

所以这是show create table with tbl1 and tbl1_test_01 join to curdef

1 个答案:

答案 0 :(得分:0)

在制作表副本后,所有数据都位于缓存中。因此,它的运行速度提高了10倍。

让我们检查缓存大小。内存多少钱?哪个发动机? innodb_buffer_pool_size的价值是什么?

请为这两个表提供SHOW CREATE TABLE

请对列名进行限定,以便我们可以确定每列中的哪个表。

如果BASEpurdate位于同一个表中,则此复合索引可能会加快查询速度(复制表之后之前):

INDEX(BASE, purdate)

如果您需要进行更多讨论,请提供EXPLAIN SELECT ...

修改

副本似乎有更多索引。但它不一定包含复合索引,其中包含 BASEpurdate