FreeSql (31) sub-district table

Partition

The partition is a data file and the index table is stored in a different physical dispersion file. A data table is divided into a plurality of N blocks, these blocks can be on the same disk, may be on different disks, different implementations different databases.

And different points table, after a large table partition, he was a table, the table does not become two, but he blocks stored data becomes more. The concept of partition, I think wanted to break through the disk I / O bottlenecks, want to improve literacy disk to increase database performance.

Partition implementation is relatively simple, the establishment of the partition table, the table usually built roots no difference, and on the development of the code is transparent to end.

The above table functions postgresql10 automatic partitioning points:

1, first create a primary partition table:

create table fenbiao(
id int,
year varchar
) partition by list(year)

Is set here in accordance with the year column data sub-table; navicat using invisible after creation;

2. Create the points table:

create table fenbiao_2017 partition of fenbiao for values in ('2017');
create table fenbiao_2018 partition of fenbiao for values in ('2018');

Such data will depend on these two days rule inserted into different points table, if you insert a data does not comply with the rules, it will report an error: no partition of relation "fenbiao" found for row.

Points table

Terms mean the surface part table, that is the table into a plurality of N small tables, small tables are each a positive finished table. After the part table data are stored in the sub table, a summary table just housing, accessing data in a place in which one part table.

After the part table concurrency improved single table, disk I / O performance is improved. Why concurrency to improve it, because it takes time to search for a shortened, if high concurrency appear, the total table according to different queries, concurrent pressure into different small tables inside.

Sub-library sub-table

Sub-library sub-table data originally stored in a block library stored on the plurality of libraries, the originally stored sub-data in a table on the block into a plurality of tables.

The amount of data in the database is not necessarily controllable, in the case without sub-library sub-table, with the development time and business library table will be more and more, the amount of data in the table will be increasingly the larger, accordingly, data manipulation, CRUD overhead will be growing; Further, a server resources (CPU, disk, memory, IO, etc.) are limited, the database can carry a final amount of data data processing capacity will hit a bottleneck.

FreeSql.Repository of the points table

FreeSql AsTable part table provides the basic method, GuidRepository as the sharing type table storage will be realized with sub-library fraction (sub-library server does not support cross) package.

var logRepository = fsql.GetGuidRepository<Log>(null, oldname => $"{oldname}_201903");

Above we get a chronological log storage points table, will eventually use it CURD operations Log_201903 table.

Precautions:

  • You can not use CodeFirst migration points table, can still migrate Log table when development environment;
  • Do not use "delayed loading" Entity Type part tables in sub-library;

Cross-table query

var sql = fsql.Select<User>()
    .AsTable((type, oldname) => "table_1")
    .AsTable((type, oldname) => "table_2")
    .AsTable((type, oldname) => "table_3")
    .ToSql(a => a.Id);

Get SQL:

select * from (SELECT a."Id" as1 FROM "table_1" a) ftb 
UNION ALL
select * from (SELECT a."Id" as1 FROM "table_2" a) ftb 
UNION ALL
select * from (SELECT a."Id" as1 FROM "table_3" a) ftb

Multi-table query:

var sql = fsql.Select<User>().LeftJoin<UserGroup>((a,b) => a.UserGroupId == b.Id)
    .AsTable((type, oldname) => oldname + "_1")
    .AsTable((type, oldname) => oldname + "_2")
    .AsTable((type, oldname) => oldname + "_3")
    .ToSql(a => a.Id);

Expect more divergent. . .

Using AsTable

var sql = fsql.Select<User>()
    .AsTable((a, b) => "(select * from tb_topic where clicks > 10)")
    .Page(1, 10).ToList()

Guess you like

Origin www.cnblogs.com/FreeSql/p/11531435.html