I am trying to read millions of data from sql server and oracle rdbms.
Sql Server - select Id as LinkedColumn,CompareColumn from Source order by LinkedColumn
Oracle -select Id as LinkedColumn,CompareColumn from Target order by LinkedColumn
This Database resides on different server and i want to read this data from different server.
So idea is to keep database server free as much as possible.
I am thinking to read this millions of data in chunks as opposed to reading data using data reader which will execute whole query on database and database server will open those records in memory stream and from there data reader will read the records.
This is taking huge amount of time when there is multiple jobs because of load on database server.
Records in Source : 12377200
Records in Target : 12266800
Because of this order by it is taking way too much time.
So is there any way to execute query on database and somehow get data directly in to my server memory(datatable or list or array
etc.) without putting load on database server?
My below code is taking way way too much time(more than 2 hours just to read data from source and target) for 24 millions of records.
Code :
public void Test(SqlConnection srcCon, SqlConnection tgtCon)
{
int srcChunkSize = 1000;
int srcCurCount = 1;
int tgtChunkSize = 1000;
int tgtCurCount = 1;
bool srcBreak = false;
bool tgtBreak = false;
Stopwatch stopwatch = new Stopwatch();
stopwatch.Start();
var da1 = new SqlDataAdapter(null, srcCon);
var da2 = new SqlDataAdapter(null, tgtCon);
da1.SelectCommand.CommandTimeout = 0;
da2.SelectCommand.CommandTimeout = 0;
while (true)
{
var srcDt = new DataTable();
var tgtDt = new DataTable();
if (!srcBreak)
{
string srcQuery = "select Id as LinkedColumn,CompareColumn from Source order by LinkedColumn" +
" OFFSET ((" + srcCurCount + " - 1) * " + srcChunkSize + " ) ROWS FETCH NEXT " + srcChunkSize + " ROWS ONLY;";
da1.SelectCommand.CommandText = srcQuery;
srcDt = GetDatatable(da1);
}
if (!tgtBreak)
{
string tgtQuery = "select Id as LinkedColumn,CompareColumn from Target order by LinkedColumn" +
" OFFSET ((" + tgtCurCount + " - 1) * " + tgtChunkSize + " ) ROWS FETCH NEXT " + tgtChunkSize + " ROWS ONLY;";
da2.SelectCommand.CommandText = tgtQuery;
tgtDt = GetDatatable(da2);
}
if (srcDt.Rows.Count == 0) srcBreak = true;
srcCurCount++;
if (tgtDt.Rows.Count == 0) tgtBreak = true;
tgtCurCount++;
if (srcBreak && tgtBreak) break;
}
stopwatch.Stop();
string a = stopwatch.Elapsed.ToString(@"d\.hh\:mm\:ss");
Console.WriteLine(a);
}
private DataTable GetDatatable(SqlDataAdapter da)
{
DataTable dt = new DataTable();
da.Fill(dt);
return dt;
}