我们计划将您的数据库结构调整为更灵活的布局。对于每个人,存储一组值,这些值因客户而异(即客户的客户)。我们想要更改当前的设计(简化):
T_PersonsData
==============
*#Person_Id Field_01 Field_02 Field_03 ... Field_N
-------------------------------------------------------------
01 'Bob' 'Simpson' '1980-03-02' ... NULL
02 'John' 'Smith' '1972-04-10' ... NULL
03 'Sanders' 'Michael' 'Chicago' ... NULL
04 'Andrews' '1978-10-02' NULL ... NULL
T_PersonData_Definitions
=========================
*#Client_Id *Field Name #Type_Id
-------------------------------------------------
101 01 'First name' 1 (String)
101 02 'Last name' 1 (String)
101 03 'Date of birth' 3 (DateTime)
... ... ... ...
203 N 'Address' 1 (String)
(* = Primary key)
(# = Foreign key)
这样的事情:
T_PersonsData_New
=================
*#Person_Id *#Field_Id Value
--------------------------------------
01 01 'Bob'
01 02 'Simpson'
01 03 '1980-03-02'
... ... ...
T_PersonsData_Definitions_New
=============================
*Field_Id #Client_Id Name #Type
-----------------------------------------------
01 101 'First Name' 1
02 101 'Last Name' 1
03 101 'Date of Birth' 3
... ... ... ...
明显的优点是:
然而,一个简单的人搜索,例如:
SELECT Person_Id
FROM T_Persons_Data
WHERE Field_01 = 'Bob'
AND FIeld_02 LIKE 'S%'
AND ...
...可以添加几个搜索选项作为WHERE子句,如下所示:
SELECT DISTINCT Person_Id
FROM T_PersonsData_New
WHERE (SELECT Value
FROM T_PersonsData_New
WHERE Field_Id = 01
AND Person_Id = Person_Id) = 'Bob'
AND (SELECT Value
FROM T_PersonsData_New
WHERE Field_Id = 02
AND Person_Id = Person_Id) LIKE 'S%'
AND ...
...在新设计中。
当然,如果有更优雅的方式来实现这样的搜索,我将不胜感激。你会建议改变这样的数据库设计吗?
此外,我想实现(简单!)性能测试来比较两种设计。你会建议什么,这看起来怎么样?哪种情况对性能至关重要?
提前致谢!