Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/62.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
mysql从具有不同表设计的多个表集中数据的最佳实践_Mysql_Database Design_Relational Database - Fatal编程技术网

mysql从具有不同表设计的多个表集中数据的最佳实践

mysql从具有不同表设计的多个表集中数据的最佳实践,mysql,database-design,relational-database,Mysql,Database Design,Relational Database,我的数据库包含大约20个保存用户信息的表。比如它有 个人:保存用户个人信息 文件:上载的文件 活动: 等 每个表都包含一个user_Id列,用于将它们一对多关系连接在一起,以及不同的表特定列和约束 我的问题是如何从所有这些表中获取单个用户的所有数据 当前,当我加载用户时,应用程序需要执行以下操作 Select * from table1 where user_Id = x; Select * from table2 where user_Id = x; Select * from table3

我的数据库包含大约20个保存用户信息的表。比如它有

个人:保存用户个人信息 文件:上载的文件 活动: 等 每个表都包含一个user_Id列,用于将它们一对多关系连接在一起,以及不同的表特定列和约束

我的问题是如何从所有这些表中获取单个用户的所有数据

当前,当我加载用户时,应用程序需要执行以下操作

Select * from table1 where user_Id = x;
Select * from table2 where user_Id = x;
Select * from table3 where user_Id = x;
..etc
因为我使用的是php oop,所以这不是一件坏事,因为每个表都有自己的模型来检索它。但我担心性能,因为我目前每次加载页面时都会运行超过20个查询。由于这些数据是动态更新的。缓存没有多大帮助

那么,解决这个问题的最佳方法是什么

表结构示例

CREATE TABLE IF NOT EXISTS `documents` (
  `id` int(10) unsigned NOT NULL,
  `user_id` int(10) unsigned NOT NULL,
  `collection` text NOT NULL,
  `photo_date` timestamp NULL DEFAULT NULL,
  `gallery` varchar(50) NOT NULL,
  `cover` int(1) DEFAULT NULL,
  `upload_date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=8 ;

CREATE TABLE IF NOT EXISTS `problemlist` (
  `id` int(10) unsigned NOT NULL,
  `user_id` int(10) unsigned NOT NULL,
  `visit_id` int(10) unsigned NOT NULL,
  `pt_id` int(10) unsigned NOT NULL,
  `problem` varchar(200) NOT NULL,
  `severity` int(2) unsigned NOT NULL,
  `note` text,
  `solved` int(1) unsigned NOT NULL,
  `datetime` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=6 ;
CREATE TABLE IF NOT EXISTS `visits` (
  `id` int(10) unsigned NOT NULL,
  `pt_id` int(10) unsigned NOT NULL,
  `user_id` int(10) unsigned NOT NULL,
  `visit_date` timestamp NULL DEFAULT NULL,
  `visit_end` timestamp NULL DEFAULT NULL,
  `complain` varchar(250) DEFAULT NULL,
  `dx` varchar(200) DEFAULT NULL,
  `note` text,
  `stats` enum('booked','waitting','finished','noshow','canceled','inroom') NOT NULL,
  `deleted` int(1) DEFAULT NULL,
  `booked_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `arrived_at` timestamp NULL DEFAULT NULL,
  `started_at` timestamp NULL DEFAULT NULL,
  `checkout_at` timestamp NULL DEFAULT NULL,
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=212 ;

@JoeWatkins连接了20个表,每个表的平均列数为4-9?20有点多,但我假设您没有听说过连接,并且一旦您意识到连接是一件事,您就会优化您的结构以减少表的数量和查询的复杂性。为什么要在一个页面上加载所有数据?为什么不按需加载?不幸的是,这是一个医疗应用程序。我要讲的是病人的病史,所有的数据都要清晰可见,这样医生才能做出判断。编辑、添加更多活动处理、标志等。您需要阅读数据库设计简介。