按字符串或整数搜索/索引

时间:2013-12-04 21:35:57

标签: mysql database database-design

好吧,这可能是一个新手问题,但我无法想象在搜索中如何最好地说出来,所以我只想问。下面哪个选项更有效,为什么。更改的列是一个将被用作搜索参数的列,我将对其进行索引。

选项A,在交易表中使用categoryName:

Table Name: Categories
+--------------+
| categoryName |
+--------------+
| Gas          |
+--------------+
| Coffee       |
+--------------+
| Restaurants  |
+--------------+

Table Name: Transactions
+--------+--------------+-------------+---------+----------+--------+
| userID | categoryName | description | date    | budgeted | actual |
+--------+--------------+-------------+---------+----------+--------+
| 0      | Gas          | blurb       | 12-1-13 | 5.00     | 5.00   |
+--------+--------------+-------------+---------+----------+--------+
| 0      | Coffee       | blurb       | 12-1-13 | 5.00     | 5.00   |
+--------+--------------+-------------+---------+----------+--------+
| 0      | Restaurant   | blurb       | 12-1-13 | 5.00     | 5.00   |
+--------+--------------+-------------+---------+----------+--------+
| 0      | Restaurant   | blurb       | 12-1-13 | 5.00     | 5.00   |
+--------+--------------+-------------+---------+----------+--------+
| 0      | Restaurant   | blurb       | 12-1-13 | 5.00     | 5.00   |
+--------+--------------+-------------+---------+----------+--------+
| 1      | Gas          | blurb       | 12-1-13 | 5.00     | 5.00   |
+--------+--------------+-------------+---------+----------+--------+
| 1      | Coffee       | blurb       | 12-1-13 | 5.00     | 5.00   |
+--------+--------------+-------------+---------+----------+--------+
| 1      | Restaurant   | blurb       | 12-1-13 | 5.00     | 5.00   |
+--------+--------------+-------------+---------+----------+--------+
| 2      | Coffee       | blurb       | 12-1-13 | 5.00     | 5.00   |
+--------+--------------+-------------+---------+----------+--------+
| 2      | Coffee       | blurb       | 12-1-13 | 5.00     | 5.00   |
+--------+--------------+-------------+---------+----------+--------+

选项B,为每个categoryName分配一个categoryID,并在Transactions表格中使用categoryID

Table Name: Categories
+-----------+--------------+
|categoryID | categoryName |
+-----------+--------------+
| 1         | Gas          |
+-----------+--------------+
| 2         | Coffee       |
+-----------+--------------+
| 3         | Restaurants  |
+-----------+--------------+

Table Name: Transactions
+--------+------------+-------------+---------+----------+--------+
| userID | categoryID | description | date    | budgeted | actual |
+--------+------------+-------------+---------+----------+--------+
| 0      | 1          | blurb       | 12-1-13 | 5.00     | 5.00   |
+--------+------------+-------------+---------+----------+--------+
| 0      | 2          | blurb       | 12-1-13 | 5.00     | 5.00   |
+--------+------------+-------------+---------+----------+--------+
| 0      | 3          | blurb       | 12-1-13 | 5.00     | 5.00   |
+--------+------------+-------------+---------+----------+--------+
| 0      | 3          | blurb       | 12-1-13 | 5.00     | 5.00   |
+--------+------------+-------------+---------+----------+--------+
| 0      | 3          | blurb       | 12-1-13 | 5.00     | 5.00   |
+--------+------------+-------------+---------+----------+--------+
| 1      | 1          | blurb       | 12-1-13 | 5.00     | 5.00   |
+--------+------------+-------------+---------+----------+--------+
| 1      | 2          | blurb       | 12-1-13 | 5.00     | 5.00   |
+--------+------------+-------------+---------+----------+--------+
| 1      | 3          | blurb       | 12-1-13 | 5.00     | 5.00   |
+--------+------------+-------------+---------+----------+--------+
| 2      | 2          | blurb       | 12-1-13 | 5.00     | 5.00   |
+--------+------------+-------------+---------+----------+--------+
| 2      | 2          | blurb       | 12-1-13 | 5.00     | 5.00   |
+--------+------------+-------------+---------+----------+--------+

1 个答案:

答案 0 :(得分:1)

选项B就是答案。想象一下如果你不得不改变其中一个值会发生什么。 Gas变为Fuel。在选项A中,另一个表将包含一堆Gas的列,这些列将毫无意义。

在整数上使用密钥比使用varchar更有效。