Slow Performance to Fetch Million Rows from MySQL Database and write to csv
EDIT: Iterating over Reader takes time, Execute Reader returns in few seconds. Is there some faster way instead of iterating ?
I am trying to fetch data from MySQL based on Filters. Previously i was using EF it was very slow, so then i decided to switch to Reading data using MySQLDataReader, it works fine when the result contains few records but is terribly slow on large result lets say Million rows.
I am using following function to formulate the query.
public string GetCorrespondingQuery()
{
string zips = """ + string.Join("","", _zips) + """;
string counties = """ + string.Join("","", _counties) + """;
string states = """ + string.Join("","", _states) + """;
string cities = """ + string.Join("","", _cities) + """;
if ((_zips.Count == 0) && (_states.Count == 0) && (_counties.Count == 0) && (_cities.Count == 0))
throw new Exception("Alert! No Filters Selected.");
string query = "select * from dbcustomer JOIN dbzipcode On dbcustomer.ZIPCODE = dbzipcode.ZIP";
if (_zips.Count > 0)
query += " where dbzipcode.Zip in (" + zips + ")";
if (_states.Count > 0)
query += " and dbzipcode.STATE in (" + states + ")";
if (_cities.Count > 0)
query += " and dbzipcode.City in (" + cities + ")";
if (_counties.Count > 0)
query += " and dbzipcode.County in (" + counties + ")";
return query + ";";
}
Above query takes few seconds when executed in MySQL Workbench, but takes several minutes using C#.
Following is the Code to fetch Data from MySQL Database.
public List<CustomerDTO> FetchCustomersUsingQuery(CustomersFilter filter)
{
string query = filter.GetCorrespondingQuery();
List<CustomerDTO> customers = new List<CustomerDTO>();
using (MySqlConnection con = new MySqlConnection(_connectionString))
{
MySqlCommand cmd = new MySqlCommand(query, con);
cmd.CommandTimeout = 0;
cmd.CommandType = CommandType.Text;
con.Open();
using (MySqlDataReader reader = cmd.ExecuteReader())
{
while (reader.Read())
{
var customer = new CustomerDTO()
{
PHONE = reader.GetString(0),
FIRSTNAME = reader.GetString(1),
LASTNAME = reader.GetString(2),
ADDRESS = reader.GetString(3),
CStatus = reader.GetString(4),
Campaign = reader.GetString(5),
ListName = reader.GetString(6),
Email = reader.GetString(7),
Recording = reader.GetBoolean(8),
ZIP = reader.GetString(9),
CITY = reader.GetString(11),
COUNTY = reader.GetString(12),
STATE = reader.GetString(13),
};
customers.Add(customer);
}
reader.Close();
}
con.Close();
}
//s.Stop();
//throw new Exception("Time to Fetch " + customers.Count + " records = " + s.Elapsed);
return customers;
}
I have tried whatever i could,Can anyone guide me to speed it up? I am struggling to improve this from past few weeks
c# mysql wpf mysqldatareader
|
show 6 more comments
EDIT: Iterating over Reader takes time, Execute Reader returns in few seconds. Is there some faster way instead of iterating ?
I am trying to fetch data from MySQL based on Filters. Previously i was using EF it was very slow, so then i decided to switch to Reading data using MySQLDataReader, it works fine when the result contains few records but is terribly slow on large result lets say Million rows.
I am using following function to formulate the query.
public string GetCorrespondingQuery()
{
string zips = """ + string.Join("","", _zips) + """;
string counties = """ + string.Join("","", _counties) + """;
string states = """ + string.Join("","", _states) + """;
string cities = """ + string.Join("","", _cities) + """;
if ((_zips.Count == 0) && (_states.Count == 0) && (_counties.Count == 0) && (_cities.Count == 0))
throw new Exception("Alert! No Filters Selected.");
string query = "select * from dbcustomer JOIN dbzipcode On dbcustomer.ZIPCODE = dbzipcode.ZIP";
if (_zips.Count > 0)
query += " where dbzipcode.Zip in (" + zips + ")";
if (_states.Count > 0)
query += " and dbzipcode.STATE in (" + states + ")";
if (_cities.Count > 0)
query += " and dbzipcode.City in (" + cities + ")";
if (_counties.Count > 0)
query += " and dbzipcode.County in (" + counties + ")";
return query + ";";
}
Above query takes few seconds when executed in MySQL Workbench, but takes several minutes using C#.
Following is the Code to fetch Data from MySQL Database.
public List<CustomerDTO> FetchCustomersUsingQuery(CustomersFilter filter)
{
string query = filter.GetCorrespondingQuery();
List<CustomerDTO> customers = new List<CustomerDTO>();
using (MySqlConnection con = new MySqlConnection(_connectionString))
{
MySqlCommand cmd = new MySqlCommand(query, con);
cmd.CommandTimeout = 0;
cmd.CommandType = CommandType.Text;
con.Open();
using (MySqlDataReader reader = cmd.ExecuteReader())
{
while (reader.Read())
{
var customer = new CustomerDTO()
{
PHONE = reader.GetString(0),
FIRSTNAME = reader.GetString(1),
LASTNAME = reader.GetString(2),
ADDRESS = reader.GetString(3),
CStatus = reader.GetString(4),
Campaign = reader.GetString(5),
ListName = reader.GetString(6),
Email = reader.GetString(7),
Recording = reader.GetBoolean(8),
ZIP = reader.GetString(9),
CITY = reader.GetString(11),
COUNTY = reader.GetString(12),
STATE = reader.GetString(13),
};
customers.Add(customer);
}
reader.Close();
}
con.Close();
}
//s.Stop();
//throw new Exception("Time to Fetch " + customers.Count + " records = " + s.Elapsed);
return customers;
}
I have tried whatever i could,Can anyone guide me to speed it up? I am struggling to improve this from past few weeks
c# mysql wpf mysqldatareader
How to output MySQL query results in CSV format?
– Lukasz Szozda
Dec 28 '18 at 17:49
@LukaszSzozda Can it be done using C# ?
– user3261186
Dec 28 '18 at 17:52
I propose to change strategy instead of pulling 1+MLN rows to application and concatenate in app you could do the work directly on DB server and just grab results
– Lukasz Szozda
Dec 28 '18 at 17:53
How am i supposed to do that ? By usingInto OutFile "filename.csv"? But what if server is not running on localhost ?
– user3261186
Dec 28 '18 at 17:56
2
There is certainly no good reason to allocate 1 million items in a collection - write out the CSV directly from the datareader.
– WelcomeOverflow
Dec 28 '18 at 17:56
|
show 6 more comments
EDIT: Iterating over Reader takes time, Execute Reader returns in few seconds. Is there some faster way instead of iterating ?
I am trying to fetch data from MySQL based on Filters. Previously i was using EF it was very slow, so then i decided to switch to Reading data using MySQLDataReader, it works fine when the result contains few records but is terribly slow on large result lets say Million rows.
I am using following function to formulate the query.
public string GetCorrespondingQuery()
{
string zips = """ + string.Join("","", _zips) + """;
string counties = """ + string.Join("","", _counties) + """;
string states = """ + string.Join("","", _states) + """;
string cities = """ + string.Join("","", _cities) + """;
if ((_zips.Count == 0) && (_states.Count == 0) && (_counties.Count == 0) && (_cities.Count == 0))
throw new Exception("Alert! No Filters Selected.");
string query = "select * from dbcustomer JOIN dbzipcode On dbcustomer.ZIPCODE = dbzipcode.ZIP";
if (_zips.Count > 0)
query += " where dbzipcode.Zip in (" + zips + ")";
if (_states.Count > 0)
query += " and dbzipcode.STATE in (" + states + ")";
if (_cities.Count > 0)
query += " and dbzipcode.City in (" + cities + ")";
if (_counties.Count > 0)
query += " and dbzipcode.County in (" + counties + ")";
return query + ";";
}
Above query takes few seconds when executed in MySQL Workbench, but takes several minutes using C#.
Following is the Code to fetch Data from MySQL Database.
public List<CustomerDTO> FetchCustomersUsingQuery(CustomersFilter filter)
{
string query = filter.GetCorrespondingQuery();
List<CustomerDTO> customers = new List<CustomerDTO>();
using (MySqlConnection con = new MySqlConnection(_connectionString))
{
MySqlCommand cmd = new MySqlCommand(query, con);
cmd.CommandTimeout = 0;
cmd.CommandType = CommandType.Text;
con.Open();
using (MySqlDataReader reader = cmd.ExecuteReader())
{
while (reader.Read())
{
var customer = new CustomerDTO()
{
PHONE = reader.GetString(0),
FIRSTNAME = reader.GetString(1),
LASTNAME = reader.GetString(2),
ADDRESS = reader.GetString(3),
CStatus = reader.GetString(4),
Campaign = reader.GetString(5),
ListName = reader.GetString(6),
Email = reader.GetString(7),
Recording = reader.GetBoolean(8),
ZIP = reader.GetString(9),
CITY = reader.GetString(11),
COUNTY = reader.GetString(12),
STATE = reader.GetString(13),
};
customers.Add(customer);
}
reader.Close();
}
con.Close();
}
//s.Stop();
//throw new Exception("Time to Fetch " + customers.Count + " records = " + s.Elapsed);
return customers;
}
I have tried whatever i could,Can anyone guide me to speed it up? I am struggling to improve this from past few weeks
c# mysql wpf mysqldatareader
EDIT: Iterating over Reader takes time, Execute Reader returns in few seconds. Is there some faster way instead of iterating ?
I am trying to fetch data from MySQL based on Filters. Previously i was using EF it was very slow, so then i decided to switch to Reading data using MySQLDataReader, it works fine when the result contains few records but is terribly slow on large result lets say Million rows.
I am using following function to formulate the query.
public string GetCorrespondingQuery()
{
string zips = """ + string.Join("","", _zips) + """;
string counties = """ + string.Join("","", _counties) + """;
string states = """ + string.Join("","", _states) + """;
string cities = """ + string.Join("","", _cities) + """;
if ((_zips.Count == 0) && (_states.Count == 0) && (_counties.Count == 0) && (_cities.Count == 0))
throw new Exception("Alert! No Filters Selected.");
string query = "select * from dbcustomer JOIN dbzipcode On dbcustomer.ZIPCODE = dbzipcode.ZIP";
if (_zips.Count > 0)
query += " where dbzipcode.Zip in (" + zips + ")";
if (_states.Count > 0)
query += " and dbzipcode.STATE in (" + states + ")";
if (_cities.Count > 0)
query += " and dbzipcode.City in (" + cities + ")";
if (_counties.Count > 0)
query += " and dbzipcode.County in (" + counties + ")";
return query + ";";
}
Above query takes few seconds when executed in MySQL Workbench, but takes several minutes using C#.
Following is the Code to fetch Data from MySQL Database.
public List<CustomerDTO> FetchCustomersUsingQuery(CustomersFilter filter)
{
string query = filter.GetCorrespondingQuery();
List<CustomerDTO> customers = new List<CustomerDTO>();
using (MySqlConnection con = new MySqlConnection(_connectionString))
{
MySqlCommand cmd = new MySqlCommand(query, con);
cmd.CommandTimeout = 0;
cmd.CommandType = CommandType.Text;
con.Open();
using (MySqlDataReader reader = cmd.ExecuteReader())
{
while (reader.Read())
{
var customer = new CustomerDTO()
{
PHONE = reader.GetString(0),
FIRSTNAME = reader.GetString(1),
LASTNAME = reader.GetString(2),
ADDRESS = reader.GetString(3),
CStatus = reader.GetString(4),
Campaign = reader.GetString(5),
ListName = reader.GetString(6),
Email = reader.GetString(7),
Recording = reader.GetBoolean(8),
ZIP = reader.GetString(9),
CITY = reader.GetString(11),
COUNTY = reader.GetString(12),
STATE = reader.GetString(13),
};
customers.Add(customer);
}
reader.Close();
}
con.Close();
}
//s.Stop();
//throw new Exception("Time to Fetch " + customers.Count + " records = " + s.Elapsed);
return customers;
}
I have tried whatever i could,Can anyone guide me to speed it up? I am struggling to improve this from past few weeks
c# mysql wpf mysqldatareader
c# mysql wpf mysqldatareader
edited Dec 29 '18 at 20:59
user3261186
asked Dec 28 '18 at 17:45
user3261186user3261186
1086
1086
How to output MySQL query results in CSV format?
– Lukasz Szozda
Dec 28 '18 at 17:49
@LukaszSzozda Can it be done using C# ?
– user3261186
Dec 28 '18 at 17:52
I propose to change strategy instead of pulling 1+MLN rows to application and concatenate in app you could do the work directly on DB server and just grab results
– Lukasz Szozda
Dec 28 '18 at 17:53
How am i supposed to do that ? By usingInto OutFile "filename.csv"? But what if server is not running on localhost ?
– user3261186
Dec 28 '18 at 17:56
2
There is certainly no good reason to allocate 1 million items in a collection - write out the CSV directly from the datareader.
– WelcomeOverflow
Dec 28 '18 at 17:56
|
show 6 more comments
How to output MySQL query results in CSV format?
– Lukasz Szozda
Dec 28 '18 at 17:49
@LukaszSzozda Can it be done using C# ?
– user3261186
Dec 28 '18 at 17:52
I propose to change strategy instead of pulling 1+MLN rows to application and concatenate in app you could do the work directly on DB server and just grab results
– Lukasz Szozda
Dec 28 '18 at 17:53
How am i supposed to do that ? By usingInto OutFile "filename.csv"? But what if server is not running on localhost ?
– user3261186
Dec 28 '18 at 17:56
2
There is certainly no good reason to allocate 1 million items in a collection - write out the CSV directly from the datareader.
– WelcomeOverflow
Dec 28 '18 at 17:56
How to output MySQL query results in CSV format?
– Lukasz Szozda
Dec 28 '18 at 17:49
How to output MySQL query results in CSV format?
– Lukasz Szozda
Dec 28 '18 at 17:49
@LukaszSzozda Can it be done using C# ?
– user3261186
Dec 28 '18 at 17:52
@LukaszSzozda Can it be done using C# ?
– user3261186
Dec 28 '18 at 17:52
I propose to change strategy instead of pulling 1+MLN rows to application and concatenate in app you could do the work directly on DB server and just grab results
– Lukasz Szozda
Dec 28 '18 at 17:53
I propose to change strategy instead of pulling 1+MLN rows to application and concatenate in app you could do the work directly on DB server and just grab results
– Lukasz Szozda
Dec 28 '18 at 17:53
How am i supposed to do that ? By using
Into OutFile "filename.csv" ? But what if server is not running on localhost ?– user3261186
Dec 28 '18 at 17:56
How am i supposed to do that ? By using
Into OutFile "filename.csv" ? But what if server is not running on localhost ?– user3261186
Dec 28 '18 at 17:56
2
2
There is certainly no good reason to allocate 1 million items in a collection - write out the CSV directly from the datareader.
– WelcomeOverflow
Dec 28 '18 at 17:56
There is certainly no good reason to allocate 1 million items in a collection - write out the CSV directly from the datareader.
– WelcomeOverflow
Dec 28 '18 at 17:56
|
show 6 more comments
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53962377%2fslow-performance-to-fetch-million-rows-from-mysql-database-and-write-to-csv%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53962377%2fslow-performance-to-fetch-million-rows-from-mysql-database-and-write-to-csv%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
How to output MySQL query results in CSV format?
– Lukasz Szozda
Dec 28 '18 at 17:49
@LukaszSzozda Can it be done using C# ?
– user3261186
Dec 28 '18 at 17:52
I propose to change strategy instead of pulling 1+MLN rows to application and concatenate in app you could do the work directly on DB server and just grab results
– Lukasz Szozda
Dec 28 '18 at 17:53
How am i supposed to do that ? By using
Into OutFile "filename.csv"? But what if server is not running on localhost ?– user3261186
Dec 28 '18 at 17:56
2
There is certainly no good reason to allocate 1 million items in a collection - write out the CSV directly from the datareader.
– WelcomeOverflow
Dec 28 '18 at 17:56