3

I am trying to build a robust php function that allows me to traverse over my normalized database. My mySQL database has 6 tables with the following column names (I am only including the primary and foreign keys, as well as some limited table columns for simplicity) so that you can see how they are related.

tableA:
partID (primary key)

tableABJunction
itemID (foreign key)
partID (foreign key)

tableB
itemID (primary key) itemName

sales
customerID (foreign key)
itemID (foreign key)

partDate
itemID (foreign key)

customer
customerID (primary key) nameFirst nameLast

When I need to generate a query, such as: What are the names of the customers that ordered itemID = 12? I have to first do a query from the sales database for all customerIDs where itemID=12 and then query the customer table to find out their first and last names. Some times, I may need to perform a query where I have to return data from all 6 tables, based on a query asking for all information pertaining to customer whose name is John Smith. Is there any easy way to build a function to handle this variety of queries, without having to build a query for every possible type of search?

Currently, my approach is to pass the following to php via AJAX: web_conditionArray (contains the column name and value of the data provided. Such as nameFirst => 'John', nameLast => 'Smith'); web_resultArray (contains the table name and the columns that I am requesting: sales => 'itemID, itemName').

The issue that I am having with this approach is a way to store the relationships between all of the mySQL datatables with their foreign keys so that my php program knows how to link all the tables together to run the correct query to get from the data provided from one table to the data requested in another table. Any suggestions or a better way to solve this? I was initially thinking of a doubly linked list but the flow from table to table is not linear given that there is a fork where the tableB links to the sales and partDate tables.

I tried to be as specific as I could in describing this situation without writing a novel; however, please let me know if you need any additional information to refine my question further.

4

2 回答 2

1

查看您的表结构,我想可以构造逻辑来计算表之间的关系,并动态构造查询,但在我看来,这比为您的特定数据库手动构造查询要多得多。我假设您的表中有更多字段,但是您只包含了最重要的字段,并且肯定包含了所有主键和外键。

基于此,您的数据库中只有三个信息对象:零件、项目和客户。因此,您应该不需要超过 12 个手动构建的查询来使您的系统正常工作。您只需要确保简化查询以处理整个信息对象,然后使用 PHP 层过滤它们。

因此,您将查询逻辑简化为:

"Fetch me all [Parts, Items or Customers] (and possibly also all [Parts, Items or Customers]) related to [Part, Item or Custromer] (and possibly [Part, Item or Customer])"

这会导致以下查询:

  • 一个零件的所有客户
  • 一个项目的所有客户
  • 零件和项目的所有客户
  • 零件的所有项目
  • 客户的所有项目
  • 零件和客户的所有项目
  • 一个项目的所有部分
  • 客户的所有零件
  • 客户和项目的所有零件
  • 一个项目的所有零件和客户
  • 零件的所有客户和项目
  • 客户的所有项目和零件

(这是逻辑关系的完整列表 - 有些实际上可能没有任何意义,这让你的生活更轻松)

因此,您的 PHP 脚本需要执行以下任务:

  • 确定查询条件需要哪些对象。这基于提供的字段。
  • 为您的查询构造一个WHERE子句,该子句从传递的字段中标识标准对象的主键。
  • 根据请求的字段确定查询结果需要哪些对象。
  • 根据条件选择查询并返回对象,并插入构造的WHERE子句。
  • 执行查询,提取有关请求对象的所有可用信息
  • 过滤结果,只提取需要的信息
  • 返回最终结果。
于 2013-01-12T07:15:53.420 回答
1

首先,要知道我的答案很可能会被否决(因为尽管这种方法是正确的,但它却经常被否决)。DBA 想让你相信,仅仅因为一个复杂的查询可以用一条它应该完成的 SQL 语句来完成(比如服务器端如何认为所有客户端都应该用服务器端完成,或者客户端如何认为布局应该用客户端而不是 CSS)。不会。复杂查询是为那些坐在命令行上的人准备的,他们需要出于特定的、非常规的原因提出按需数据抓取。为了处理速度,SELECTing、UPDATEing 和DELETEing 应始终在 PK 服务器端完成。

听起来您有一组合法的大表。

假设它很大并且速度是主要关注点(而不是开发时间),只使用一个主键而不使用其他索引,因为你拥有的索引越多,当 DBA 真正进行比较时,数据库需要重新索引的索引就越多你有没有更快的服务器端。

主键会花一些功夫,但它是超越数据类型和长度的最重要的事情。例如,非 FK、独立表(如tableAtableB和变种较少的ed FK 首先。例如,在我的网站中,我按用户 ID 和链接 ID 存储链接的投票总数。如果用户已登录,他们将需要知道他们在链接上投了多少票,因此 userID 更改的可能性较小,所以这是我在该表上的 PK 中的第一个。将这种按需数据库端或服务器端计算在内是一场性能噩梦。 customerINTINTSELECT

只需几行代码,您将大大提高速度。通过 php 对 PK 进行排序将延迟减少 50%。将 s吸收JOIN到 php 中会降低延迟峰值的发生率。没有按需 MySQL 计算将使您的站点不会瘫痪。

如果您摆脱那种仅仅因为 SQL 语句就可以得到结果的教条,那么您应该使用 SQL 语句而不是服务器端语言(C++ 是最快的),您会看到性能飙升。

如果您可以更具体地处理要混淆的表,我可以更具体,但您可能明白了。

AJAX 改变了游戏规则并强制重新聚焦。用于布局的 CSS;js用于客户端编程;服务器端用于...服务器端处理;用于存储持续时间超过片刻的所有内容的数据库。

投反对票!哈哈

于 2013-01-12T06:20:00.433 回答