Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ionic-framework/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
通过PHP从大型层次结构的Mysql数据库获取数据的最佳方法_Php_Mysql_Sql_Database_Stored Procedures - Fatal编程技术网

通过PHP从大型层次结构的Mysql数据库获取数据的最佳方法

通过PHP从大型层次结构的Mysql数据库获取数据的最佳方法,php,mysql,sql,database,stored-procedures,Php,Mysql,Sql,Database,Stored Procedures,我有这样的数据层次结构: Country -President // A Person --Central ---Prime Minister // A Person ----State -----Chief Minister // A Person ------District -------District Minister // A Person --------Area ---------Area Minister (AM) // A Person area_manager_id |

我有这样的数据层次结构:

Country
-President // A Person
--Central
---Prime  Minister // A Person
----State
-----Chief Minister // A Person
------District
-------District Minister // A Person
--------Area
---------Area Minister (AM) // A Person
area_manager_id | country_name | president | prime_minister | etc.
所以基本上我想做的是,我想把注意力集中在地区部长上。因为他将是我申请的灵魂。其他的只是层次结构的一部分。比如说,如果我增加一名
地区部长
,那么他/她将在
地区
之下,
地区
地区部长
之下,
地区部长
地区
之下,而
地区
首席部长
之下,等等……

我在这里做的是将
Area Minister
的这些详细信息添加到我的
MySQL数据库中
,所以我在数据库中所做的是,我创建了以下表:

  • 国家/地区//用于添加条目
  • 总统//用于在国家/地区添加总统(因此此处国家/地区id为外键)
  • Central//用于在总统下添加Central(因此总统id在此处为外键)
  • Area//用于在District manager下添加区域(因此District\u manager\u id在此处为外键)
  • Area_Minister//用于在Area中添加Area Minister(AM)(因此Area_id在此为外键)
  • 问题是如何通过
    **PHP**
    获取
    AM
    的详细信息。我正在从
    **MySQL**
    获取数据,因为
    am
    是我正在寻找的核心数据,但在某些点上也需要相关数据。但核心是
    AM-Data
    。然而,如果我采用这种方式,我将尝试从
    10-10个表中获取数据。我感觉到的是疼痛

    因为为了获得所需的数据,我将
    AM
    中的
    地区
    ID匹配,然后将
    地区
    地区
    中的
    地区
    ID匹配到层次结构的起始级别

    我能做些什么来减少这个过程。因为我想要的核心数据就是i.e区域经理的数据

    只要我能把它组织好。或者,如果需要任何帮助,有人可以向我提供见解,如果这可以通过存储过程来实现

    我只是在寻找一种方法来降低PHP中代码的复杂性,以及MySQL中的许多表,而核心数据只包含
    am


    敬请告知。

    您的基本表结构是正确的,您指定的表数据结构似乎是合理的。如果要减少访问区域管理器时打开的表的数量,可以做以下几件事

  • 使用装饰器。您可以创建一个区域管理器对象,该对象在获取区域管理器时只加载所需的最小数据。向该对象添加从其他表获取数据的方法,并仅在需要该数据时运行其查询。例如:

    class AreaManager {
    
        public function __construct() {
            //do code which loads the basic table data for an area manager
        }
    
        public function getPresident() {
             // do the SQL needed to load the president from the relational tables
        }
    
    }
    
  • 对数据进行非规范化处理。您可以周期性地运行一个脚本,将您的数据放入一个表中,因此您的结构如下:

    Country
    -President // A Person
    --Central
    ---Prime  Minister // A Person
    ----State
    -----Chief Minister // A Person
    ------District
    -------District Minister // A Person
    --------Area
    ---------Area Minister (AM) // A Person
    
    area_manager_id | country_name | president | prime_minister | etc.
    
    此脚本必须在您更改关系时更新,但允许您通过单个查询轻松访问数据


  • 尽管如此,您已经按照应该使用的方式设置了关系表,10表查询通常不是世界末日

    只要为外键编制索引,然后在获取的内容上使用连接,数据就不会那么糟糕


    最后不是关于你的表,因为你的表是用正确的关系键正确构造的,而是你的索引优化和你将运行的查询。

    FWIW,我认为十个表没有那么多。@lc。实际上还有很多其他的桌子。但是,我制作这些表只是为了获取
    区域管理器的详细信息,因此仅用于引用
    区域管理器的9个表对我来说成本相当高。正因为如此,所以我正在寻找你们这些人在这方面的专家建议和见解。@lc。基本问题是,每当我为
    区域管理器
    获取详细信息时,我都需要从
    10个表
    获取详细信息。