MySql,索引和加速查询
大家好,这里的好人。根据类似问题的一些答案,我相信向我的表中添加索引将有助于下面的查询。挑战是,我对使用索引不太熟悉,也不太习惯,因为如果使用太多索引,可能会减慢其他查询的速度。 只是想找个人帮我指引正确的方向。事先谢谢你的帮助 查询:MySql,索引和加速查询,mysql,indexing,database-performance,Mysql,Indexing,Database Performance,大家好,这里的好人。根据类似问题的一些答案,我相信向我的表中添加索引将有助于下面的查询。挑战是,我对使用索引不太熟悉,也不太习惯,因为如果使用太多索引,可能会减慢其他查询的速度。 只是想找个人帮我指引正确的方向。事先谢谢你的帮助 查询: SELECT territories.territoryID, territories.territory_name, territories_meta.tm_color, territories.territory_descri
SELECT
territories.territoryID,
territories.territory_name,
territories_meta.tm_color,
territories.territory_description,
territories.territory_state,
GROUP_CONCAT(distinct(territories_zips.tz_zip)SEPARATOR ', ' ) AS ZipCodes,
count(distinct(users.userID)) as AgentsAssigned,
GROUP_CONCAT(distinct(concat(users.user_Fname,' ',users.user_Lname))SEPARATOR ', ')
AS AgentName,
a.sumTerr as TotalOpp
from(
SELECT
territories_zips.tz_terrID as terrID,
sum(boundaries_meta.bm_opportunity) as sumTerr
FROM territories_zips
INNER JOIN boundaries ON boundaries.boundary_name = territories_zips.tz_zip
INNER JOIN boundaries_meta ON boundaries.boundary_id = boundaries_meta.bm_boundariesID
where tz_status = 1
group by tz_terrID
)as a
inner join territories on territories.territoryID = a.terrId
INNER JOIN territories_zips ON territories.territoryID = territories_zips.tz_terrID
INNER JOIN territories_assign ON territories.territoryID = territories_assign.ta_territoryID
INNER JOIN users ON users.userID = territories_assign.ta_repID
INNER JOIN territories_meta ON territories_meta.tm_territoryID = territories.territoryID
WHERE
territories_zips.tz_status = 1 AND
territories_assign.ta_repStatus = 1 AND
users.user_status = 1
GROUP BY territoryID
说明:
id select_type table typw possible_keys key key_len ref rows extra
1 PRIMARY <derived2> ALL 97 Using temporary; Using filesort
1 PRIMARY territories_meta ALL 121 Using where; Using join buffer
1 PRIMARY territories_zips ALL 1739 Using where; Using join buffer
1 PRIMARY territories_assign ALL 138 Using where; Using join buffer
1 PRIMARY users eq_ref PRIMARY PRIMARY 8 msb_db.territories_assign.ta_repID 1 Using where
1 PRIMARY territories eq_ref PRIMARY PRIMARY 8 msb_db.territories_meta.tm_territoryID 1 Using where
2 DERIVED territories_zips ALL 1739 Using where; Using temporary; Using filesort
2 DERIVED boundaries_meta ALL 42995 Using join buffer
2 DERIVED boundaries eq_ref PRIMARY PRIMARY 4 msb_db.boundaries_meta.bm_boundariesID 1 Using where
这就是解释:
id select_type table typw possible_keys key key_len ref rows extra
1 SIMPLE territories_zips ALL 1739 Using where; Using temporary; Using filesort
1 SIMPLE boundaries_meta ALL 42995 Using join buffer
1 SIMPLE boundaries eq_ref PRIMARY PRIMARY 4 mb_db.boundaries_meta.bm_boundariesID 1 Using where
我已经为这个子查询包含了下面的表,如果我需要重新发布其他表结构,请告诉我
表:
CREATE TABLE `boundaries` (
`boundary_id` int(11) NOT NULL AUTO_INCREMENT,
`boundary_name` varchar(20) DEFAULT NULL,
`geometry_type` varchar(12) DEFAULT NULL,
`boundary_geometry` mediumtext,
`boundary_type` varchar(5) DEFAULT NULL,
`boundary_state` varchar(4) DEFAULT NULL,
PRIMARY KEY (`boundary_id`)
) ENGINE=MyISAM AUTO_INCREMENT=64504 DEFAULT CHARSET=utf8;
CREATE TABLE `boundaries_meta` (
`boundaries_metaID` bigint(20) NOT NULL AUTO_INCREMENT,
`bm_boundariesID` bigint(20) NOT NULL,
`bm_opportunity` int(5) NOT NULL,
PRIMARY KEY (`boundaries_metaID`)
) ENGINE=MyISAM AUTO_INCREMENT=51201 DEFAULT CHARSET=utf8;
CREATE TABLE `territories_zips` (
`terr_zipsID` bigint(10) NOT NULL AUTO_INCREMENT,
`tz_terrID` bigint(10) NOT NULL,
`tz_zip` varchar(5) CHARACTER SET latin1 NOT NULL,
`tz_status` smallint(1) NOT NULL,
PRIMARY KEY (`terr_zipsID`)
) ENGINE=MyISAM AUTO_INCREMENT=2576 DEFAULT CHARSET=utf8;
再次感谢您的帮助
编辑:
我用索引更新了一些表,得到了难以置信的改进(再次感谢国王艾萨克)。我在子查询中加入了新的解释,因为我仍然不知道这有什么帮助,为什么有帮助,或者我是否真的在正确的部分创建了索引。给一个人一条鱼吃一天,教他如何钓鱼,然后
id select_type table type possible keys key key_len ref rows extra
1 SIMPLE territories_zips ALL 1739 Using where; Using temporary; Using filesort
1 SIMPLE boundaries ref PRIMARY,bndIDindex,bndNameindex bndNameindex 63 func 1 Using where
1 SIMPLE boundaries_meta eq_ref bmBndIDindex bmBndIDindex 8 mb_db.boundaries.boundary_id 1 Using where
看起来您的第一步将是处理
tz_zip
和boundary_name
连接。我的第一个问题是:这些是独一无二的吗?对这些表应用唯一索引应该会大大加快子查询的速度。如果它们不是唯一的,那么标准索引仍将为您提供足够高的基数,以看到速度的提高
所有表上的“状态”字段也应编入索引。即使这些最终是低基数索引,它也将有利于查询,而不会造成太多索引开销
您可能还想看看是否可以重构此查询以消除“from”子句中的子查询。这导致整个查询依赖于一个临时表,在查询过程继续之前,必须完全建立临时表。我会冒险说,这也是你看到这么多“所有”类型的原因。查询分析器无法对数据的子集进行操作,因此它正在进行全表扫描。当它发生在一张桌子上时,这是不好的,在你的例子中,它发生在五张桌子上
我将把处理boundary\u meta
看作是另一个连接,并在SELECT中处理SUM(bounders\u meta.bm\u opportunity)
。它可能需要是一个依赖的子查询,但您仍然可以看到性能的提高
至于您对索引速度的担忧:在向表中添加多个索引时,过度索引可能是一个问题,但通常情况下,这不是一个问题,除非您对几个基于“char”的列进行索引。因为我们只讨论两个varchar(5)
列,所以这不应该是一个问题
是否为列编制索引始终是一个成本/效益问题。成本以规模衡量,效益以基数衡量
您最好在这里使用查询结构和索引。如果有必要(也可以选择),请将数据库克隆到单独的服务器上,然后尝试不同的解决方案,直到找到一个可行的解决方案。谢谢国王艾萨克。非常透彻的回顾,我期待着深入挖掘您的观点,并将很快发回一些答案和可能更多的问题。关于第一个问题:tz_zip和boundary_name不是唯一的。我不确定什么是标准索引,也不确定在本例中如何将其应用于这两种索引中的任何一种。我现在将研究子查询。再次感谢你,你绝对应该把唯一的索引放在你能放的地方。我所说的“标准索引”是指您可以通过
altertableaddindex
添加的索引。再次感谢你,以撒国王。此外,我第一次尝试删除子查询并使其成为常规联接,实际上使处理时间增加了一倍。我打算在这里再尝试一些东西。另一个快速问题:如果这些tz_zip和boundary_名称不是唯一值,那么将它们作为标准索引是否有害?我在创建索引方面有点运气。我试着在评论中发布新的解释,但意识到这不是适合他们的地方。将返回原始问题并添加编辑。再次感谢你
id select_type table type possible keys key key_len ref rows extra
1 SIMPLE territories_zips ALL 1739 Using where; Using temporary; Using filesort
1 SIMPLE boundaries ref PRIMARY,bndIDindex,bndNameindex bndNameindex 63 func 1 Using where
1 SIMPLE boundaries_meta eq_ref bmBndIDindex bmBndIDindex 8 mb_db.boundaries.boundary_id 1 Using where