MariaDB 副本 table 查询比原始 table 运行得更快
MariaDB copy table query runs quicker than original table
场景是:
Maria db on a cluster
I have a table, which is updated twice a day, when i query it takes
6-7 seconds, (it is not a big table over 100k of rows), but when i
make a copy of the table and run the same query it comes back 0.7
seconds.
Now i tried to optimize the original table but with no luck. There are
indexes and BTree. I have a feeling that when copy a table the index
is straighten so to speak.
有人有想法吗?我尝试了一些建议,但没有成功。
(从评论中复制)
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;
这是原文的解释 table
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
加入table
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
/*****************************************************************************/
这是复制的 table
中的第二个解释 table
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
所以这里是用 tbl1 创建 table 和 tbl1_test_01 加入 curdef
复制 table 后,所有数据都在缓存中。因此,它的运行速度是原来的 10 倍。
让我们检查缓存大小。多少内存?哪个引擎? innodb_buffer_pool_size
的值是多少?
请为两个 table 提供 SHOW CREATE TABLE
。
请限定列名,以便我们可以分辨出每列 table。
如果BASE
和purdate
在同一个table,这个复合索引可能会加快查询速度(前和后复制 table):
INDEX(BASE, purdate)
如需更多讨论,请提供EXPLAIN SELECT ...
。
编辑
副本似乎有更多的索引。但它不一定有包含 both BASE
和 purdate
.
的 composite 索引
场景是:
Maria db on a cluster
I have a table, which is updated twice a day, when i query it takes 6-7 seconds, (it is not a big table over 100k of rows), but when i make a copy of the table and run the same query it comes back 0.7 seconds.
Now i tried to optimize the original table but with no luck. There are indexes and BTree. I have a feeling that when copy a table the index is straighten so to speak.
有人有想法吗?我尝试了一些建议,但没有成功。
(从评论中复制)
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;
这是原文的解释 table
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
加入table
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
/*****************************************************************************/
这是复制的 table
中的第二个解释 tableSELECT 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
所以这里是用 tbl1 创建 table 和 tbl1_test_01 加入 curdef
复制 table 后,所有数据都在缓存中。因此,它的运行速度是原来的 10 倍。
让我们检查缓存大小。多少内存?哪个引擎? innodb_buffer_pool_size
的值是多少?
请为两个 table 提供 SHOW CREATE TABLE
。
请限定列名,以便我们可以分辨出每列 table。
如果BASE
和purdate
在同一个table,这个复合索引可能会加快查询速度(前和后复制 table):
INDEX(BASE, purdate)
如需更多讨论,请提供EXPLAIN SELECT ...
。
编辑
副本似乎有更多的索引。但它不一定有包含 both BASE
和 purdate
.