Reputation: 1046
What i need to do is this: I got an array with lets say 100000 values, For each value i need to make the same query ,just change that specific value. Now, i am thinking that if i loop all this values in my c#/ java code and reach for a query it would take a lot of time. My other option is doing all the work in my db, populate a temp table and than reading back in my code from that temp table.
What is the fastest way of doing such thing?
private void GetValues(List<Element> _Elements)
{
foreach (Element e in _Elements)
{
using (OracleCommand cmd = new OracleCommand())
{
cmd.Connection = _conn;
cmd.CommandText = "select value from table where something = " +e.Indicator;
using(OracleDataReader r = cmd.ExecuteReader());
while (r.Read())
{
e.setvalue = r.GetString(1);
}
r.Close();
}
}
}
}
[Editor note: question was originally unclear as to whether it was C# or Java -- but the languages are largely equivalent, and answers should be applicable to both.]
Upvotes: 1
Views: 237
Reputation: 35716
Do one query,
select value from table where something between min and max;
or
select value from table where something in (a, b, c, ... );
or
select value from table where something in
(select things from tempTable);
or
select value from table where something in
(select things from @tablevariable);
whichever of these approaches is most applicable to your database and problem.
Repeating all the processing over and over, than amalgamating the results must be slower than taking a set based approach in the first place.
It all rather depends on on the type and distribution of the Indicator
property of the Element
List
.
Upvotes: 2
Reputation: 6692
The faster way is to use a dynamic query. In a loop, you build up a statement to make use of several values at once.
In the sample you gave, it mean building up statements like those.
select value from table where something in (n1, n2,... ,n500)
select value from table where something in (n501, n502,... ,n1000)
You may not have to make several queries at all depending of the (characters?) limitations you face.
Upvotes: 1
Reputation: 3724
There are a lot of optimiziation tips, but for your specific case, here are 2:
Upvotes: 0