關於一個資料庫列設計的問題

litterbaby發表於2007-04-09
關於一個資料庫列設計的問題(from askTom)[@more@]

From askTom

ASKTOM上有一個問題是:

Hi Tom,

I am in a situation to define a primary key values.

My primary value contains data in the following format..

100020051225000001

Here the first 4 digits represent site id (1000)

Second set of 8 digits represent Date in YYYYMMDD (20051225)

the last 6 digits represent transaction # (0000001)

This is my requirement. Now I have a question

1.Which data type is best suitable ?

Varchar2 or Number ? I heard that number search is fast when compared to

character. But if I define number, it takes 21 bytes where as if I define char

it takes 18 bytes. [Looks char takes less than number]

2.In fact I would like to define it as follows with numeric data type

100020051225.000001

In this case, the transaction id is the value after decimal. If I define the

column as NUMBER and data as said above, what are disadvantages and advantages?

The reason why I am looking for this choice is that, in future if the

transaction value goes beyond 6 digits or site id goes beyond 4 digits, I donot

need to worry. But I need to look for the space and performance relates issues.

I feel you are the best person for this to answer and I am sure you are the

right choice :)

Please brief the following considerations in this case.

1.Space

2.Performance

What is your best suggestion?

-Naveen

意思就是說,有一個表上的關鍵字需要有三部分組成:site id,日期和事務號,問題的提出者想問的是,如何設計這個關鍵字為字元型或者是數值型能夠達到最優的效果,當然他從存放的空間多少和效能上考慮這個問題。

Tom給出的解決方案是這樣的:

One would say "why isn't this three fields as it should be?" That isn't a

requirement you've specified, that is a design decision you have made that you

*might* well want to reconsider.

Site_id - number

transaction_date DATE <<== it is after all a DATE!!!!

transaction_id number

Forget space and everything, this is about data. I can imagine wanting to do

things like

partition by site....

partition or subpartition by date....

And all of your concerns by transaction id going beyond 6 digits - not a

problem.

I will be very repetitive in my followups to this. You want three attributes.

They should be a number, date, number

Never put multiple attributes into a single column.

就是說將這個關鍵字拆開來設計,最後一句話很重要,不能將多個屬性的資料放置到一列上。

從以前我經理的專案上看到很多這樣的情況,由於設計的問題給以後效能上產生了很大的影響,Tom說的這種方式我認為很好,給以後的系統的最佳化,SQL的書寫,索引的建立等提供很多的方便。同時引申一下,在以前接觸的專案中有的專案為了省心或者方便,將一個表設計很複雜,一個表有幾十列,甚至是上百列,而且很多業務同時使用這個表上的列,這樣就造成了,這個系統的最佳化的極為複雜性。希望以後引以為戒。

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/222350/viewspace-909444/,如需轉載,請註明出處,否則將追究法律責任。

相關文章