简体   繁体   English

从不可解析的表SQL Server查询和导出

[英]Query and export from unsortable table SQL Server

First I am sorry for my bad English, is not my language. 首先,我很抱歉我的英语不好,不是我的语言。

My problem is: I have a table with around 10 million records of transaction of bank. 我的问题是:我有一张约有1000万条银行交易记录的表格。 It don't have PK and didn't sort as any column. 它没有PK,也没有排序为任何列。

My work is create a page to filter and export it to csv. 我的工作是创建一个页面来过滤并将其导出到csv。 But limit of rows to export Csv is around 200k records. 但是导出Csv的行数限制大约是20万条记录。

I have some idea like: 我有一些想法:

  • create 800 tables of 800 ATMs (just an idea, I know it's stupid) and send data from main table to it 1 time per day => export to 800 file csv 创建800个800台ATM表(只是一个想法,我知道它是愚蠢的)并从主表发送数据到它每天1次=>导出到800文件csv

  • use Linq to get 100k record per time then next time, I skip those. 使用Linq每次获得100k记录,然后下次,我跳过这些。 But I am stuck when Skip command need OrderBy and I got OutOfMemoryException with it 但是当Skip命令需要OrderBy时我被卡住了,我得到了OutOfMemoryException

     db.tblEJTransactions.OrderBy(u => u.Id).Take(100000).ToList() 

Can anyone help me, every idea is welcome (my boss said I can use anything includes create hundred of tables, use Nosql ... ) 任何人都可以帮助我,每个想法都是受欢迎的(我的老板说我可以使用任何包括创建数百个表,使用Nosql ...)

If you don't have a primary key in your table, then add one. 如果表中没有主键,则添加一个主键。 The simplest and easiest is to add an int IDENTITY column. 最简单和最简单的方法是添加一个int IDENTITY列。

ALTER TABLE dbo.T 
ADD ID int NOT NULL IDENTITY (1, 1)

ALTER TABLE dbo.T 
ADD CONSTRAINT PK_T PRIMARY KEY CLUSTERED (ID)

If you can't alter the original table, create a copy. 如果无法更改原始表,请创建副本。

Once the table has a primary key you can sort by it and select chunks/pages of 200K rows with predictable results. 一旦表具有主键,您就可以对其进行排序,并选择具有可预测结果的200K行的块/页。

I'm not sure about my solution. 我不确定我的解决方案。 But you can refer and try it: 但你可以参考并尝试一下:

select top 1000000 *, row_number() over (order by (select null)) from tblEJTransactions

The above query returns sorted list. 以上查询返回排序列表。

And then you can use Linq to get the result. 然后你可以使用Linq来获得结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM