使用索引提高性能

时间:2016-02-02 09:53:31

标签: php mysql

我正在研究DWH,我对php和mySQL这么新。我有一个包含6个索引的事实表,我有6个维度表。维度表有两个字段,ID和Value。事实表具有ID作为主键,并且它具有所有维表的ID作为外键。它还有其他一些领域。我需要将事实表与维度表连接以获取所有维度表的值。事实表中有很多记录,当我查询它时,执行需要很长时间。我在这些表上使用简单的连接查询。任何人都可以帮助我说我应该如何使用索引查询表以提高性能?查询如下:

select *  from dw.bohran_fct_etelaatenavegankhodroyi
INNER JOIN (select NameDastgahID as e1NameDastgahID, NameDastgahTitle from dw.excel_dim_namedastgah) e1 on (bohran_fct_etelaatenavegankhodroyi.NameDastgahID=e1NameDastgahID) 
INNER JOIN (select NameKhodroID as e2NameKhodroID, NameKhodroTitle from dw.excel_dim_namekhodro) e2 on (bohran_fct_etelaatenavegankhodroyi.NameKhodroID=e2NameKhodroID) 
INNER JOIN (select NoeNavganDastgahID as e3NoeNavganDastgahID, NoeNavganDastgahTitle from dw.excel_dim_noenavgandastgah) e3 on (bohran_fct_etelaatenavegankhodroyi.NoeNavganID=e3NoeNavganDastgahID) 
INNER JOIN (select KarbarieKhodroID as e4KarbarieKhodroID, KarbarieKhodroTitle from dw.excel_dim_karbariekhodro) e4 on (bohran_fct_etelaatenavegankhodroyi.KarbariID=e4KarbarieKhodroID) 
INNER JOIN (select ShahreKhodroID as e5ShahreKhodroID, ShahreKhodroTitle from dw.excel_dim_shahrekhodro) e5 on (bohran_fct_etelaatenavegankhodroyi.ShahrID=e5ShahreKhodroID)  where 1=1 

explain output of the statement

1 个答案:

答案 0 :(得分:0)

最后,我找到了在很短的时间内完成此查询的方法!查询是:

select distinct (B.NameDastgahID), B.ZarfiateHmaleBar, B.ShomarePelak, B1.NameDastgahTitle, B2.NameKhodroTitle, B3.NoeKhodroTitle, B4.KarbarieKhodroTitle, B5.ShahreKhodroTitle  
from dw.bohran_fct_etelaatenavegankhodroyi B
INNER JOIN dw.excel_dim_namedastgah B1 using (NameDastgahID) 
INNER JOIN dw.excel_dim_namekhodro B2 using (NameKhodroID) 
INNER JOIN dw.excel_dim_noenavgandastgah) B3 using (NoeKhodroID) 
INNER JOIN dw.excel_dim_karbariekhodro) B4 using (KarbarieKhodroID) 
INNER JOIN dw.excel_dim_shahrekhodro) B5 using (ShahreKhodroID)  
where 1=1

应该注意的是,维度表B1,B2,B3,B4,B5中的字段名称(使用后在括号中)应该与事实表B中的字段名称相同。