Database speed optimization: few tables with many rows, or many tables with few rows?

前端 未结 7 840
鱼传尺愫
鱼传尺愫 2021-01-19 07:23

I have a big doubt.

Let\'s take as example a database for a whatever company\'s orders.

Let\'s say that this company make around 2000 orders per month, so, a

相关标签:
7条回答
  • 2021-01-19 07:56

    Before you worry about query speed, consider the costs.

    If you split the code into separate code, you will have to have code that handles it. Every bit of code you write has the chance to be wrong. You are asking for your code to be buggy at the expense of some unmeasured and imagined performance win.

    Also consider the cost of machine time vs. programmer time.

    0 讨论(0)
  • 2021-01-19 08:00

    Look into partitioning your tables in time slices. Partitioning is good for the log-like table case where no foreign keys point to the tables.

    0 讨论(0)
  • 2021-01-19 08:01

    I agree that smaller tables are faster. But it depends on your business logic if it makes sense to split a single entity over multiple tables. If you need a lot of code to manage all the tables than it might not be a good idea.

    It also depends on the database what logic you're able to use to tackle this problem. In Oracle a table can be partitioned (on year for example). Data is stored physically in different table spaces which should make it faster to address (as I would assume that all data of a single year is stored together)

    An index will speed things up but if the data is scattered across the disk than a load of block reads are required which can make it slow.

    0 讨论(0)
  • 2021-01-19 08:05

    I would not split tables by year.

    Instead I would archive data to a reporting database every year, and use that when needed.

    Alternatively you could partition the data, amongst drives, thus maintaining performance, although i'm unsure if this is possible in postgresql.

    0 讨论(0)
  • 2021-01-19 08:07

    If you use indexes properly, you probably need not split it into multiple tables. Most modern DBs will optimize access.

    Another option you might consider is to have a table for the current year, and at the end append the data to another table which has data for all the previous years. ?

    0 讨论(0)
  • 2021-01-19 08:10

    For the volume of data you're looking at splitting the data seems like a lot of trouble for little gain. Postgres can do partitioning, but the fine manual [1] says that as a rule of thumb you should probably only consider it for tables that exceed the physical memory of the server. In my experience, that's at least a million rows.

    1. http://www.postgresql.org/docs/current/static/ddl-partitioning.html
    0 讨论(0)
提交回复
热议问题