Should I use flat tables or a normalized database?

前端 未结 7 980
南方客
南方客 2021-02-19 03:39

I have a web application that I am currently working on that uses a MySQL database for the back-end, and I need to know what is better for my situation before I continue any fur

相关标签:
7条回答
  • 2021-02-19 04:18

    Altering the schema during runtime is rarely a good idea. What you want to consider is the EAV (Entity-Attribute-Value) model.

    Wikipedia has some very good info on the pros and cons, as well as implementation details. EAV is to be avoided when possible, but for situations like yours with an unknown number of columns for each form, EAV is woth considering.

    0 讨论(0)
  • 2021-02-19 04:22

    Keep your data normalized. If you index properly, you will not encounter performance issues for a very long time.

    Regarding security: The flat approach will require you to write lots of create/drop table, alter table etc statements, ie a lot more code and a lot more points of failure.

    The only reason to have flat files would be when your users can connect to the DB directly (you could still go for row level security). But in that case, you are really reimplementing a variant of phpmyadmin

    0 讨论(0)
  • 2021-02-19 04:26

    Rule of thumb. It's easier to go from normalized to denormalized than the other way around.

    Start with a reasonable level of database normalization (by reasonable I mean readable, maintainable, and efficient but not prematurely optimized), then if you hit performance issues as you grow, you have the option of looking into ways in which denormalization may increase performance.

    0 讨论(0)
  • 2021-02-19 04:27

    The way I would handle this is to use a normalized, extensible "Property" table, such as below:

    Table: FormProperty
     id: pk
     form_id: fk(Form)
     key: varchar(128)
     value: varchar(2048)
    

    The above is just an example, but I've used this pattern in many cases, and it tends to work out pretty well. The only real "gotcha" is that you need to serialize the value as a string/varchar and then deserialize it to whatever it needs to be, so there is a little added responsibility on the client.

    0 讨论(0)
  • 2021-02-19 04:31

    Normalized == fast searches, easier to maintain indexes, slower insert transactions (on multiple rows)

    Denormalized == fast inserts, ususally this is used when there are a lot of inserts (data warehouses that collect and record chronological data)

    0 讨论(0)
  • 2021-02-19 04:32

    ...in this application users will be able to construct their own forms with any number fields...

    Yikes! Then how could you possibly do any sort of normalization when the users are, in essense, making the database decisions for you.

    I think you either need to manage it step by step or let your freak flag fly and just keeping buying hardware to keep up with the thrashing you're going to get when the users really start to get into it....Case in point, look what happens when users start to understand how to make new forms and views in SharePoint...CRIKY!! Talk about scope creep!!

    0 讨论(0)
提交回复
热议问题