Optimal database structure - 'wider' table with empty fields or greater number of tables?

前端 未结 5 2124
臣服心动
臣服心动 2020-12-28 20:23

I need to fit in additional data into a database, and I have a choice between modifying an existing table (table_existing) or creating new tables.

This is how table_

5条回答
  •  孤独总比滥情好
    2020-12-28 20:57

    What is the more optimal database structure from the speed point of view?

    Well, what is correct, best practice, etc, is called Normalisation. If you do that correctly, there will be no optional columns (not fields), no Nulls. The optional columns will be in a separate table, with fewer rows. Sure, you can arrange the tables so that they are sets of optional columns, rather than (one PK plus) one column each.

    Combining the rows from the sub-tables into one 5NF row is easy, do that i a view (but do not update via the view, do that directly to each sub-table, via a transactional stored proc).

    More, smaller tables, are the nature of a Normalised Relational database. Get used to it. Fewer, larger tables are slower, due to lack of normalisation, duplicates and Nulls. Joining is cumbersome in SQL< but that is all we have. There is no cost in joins themselves, only it the tables being joined (rows, row width, join columns, datatypes, mismatches, indices [or not] ). Databases are optimised for Normalised tables, not for data heaps. And large numbers of tables.

    Which happens to be optimal re performance, no surprise. For two reasons:

    1. The tables are narrower, so there are more rows per page, you get more rows per physical I/O, and more rows in the same cache space.

    2. Since you have No Nulls, those columns are fixed len, no unpacking to extract the contents of the column.

    There are no pros for large tables with many optional (null) columns, only cons. There never is a pro for breaching standards.

    The answer is unchanged regardless of whether you are contemplating 4 or 400 new tables.

    • One recommendation if you are seriously considering that many tables: you are heading in the direction of Sixth Normal Form, without realising it. So realise it, and do so formally. The 400 tables will be much better controlled. If you get a professional to do it, they will normalise that, and end up back at less than 100.

提交回复
热议问题