CRITICAL
Rule Definition
Calling the Select method on a DataTable in a loop generally involve performance problems because the data is not indexed.
It's better to use indexed access methods.
Remediation
Rather use the .Find method if using the Primary Key or create a DataView (with the required columns on sort) on the required columns and use the Find/FindRows methods to retrieve the data repeatedly because this will use indexed data.
If using DataView, use the constructor with all arguments to avoid index rebuilding.
Violation Code Sample
..
delegate void TestFunction();
private const int m_iTotalRows = 5000;
private const int m_iTotalLoop = 1000;
private void FillDataSet()
{
for (int i = 0; i < m_iTotalRows; i++)
dataSet11.DataTable1.AddDataTable1Row(i);
}
private void LoopWithSelect()
{
for (int i = 0; i < m_iTotalLoop; i++ )
dataSet11.DataTable1.Select("Column1 = " + i);
}
Fixed Code Sample
// Replace LoopWithSelect method by:
private void LoopWithFind()
{
DataView dv = new DataView( dataSet11.DataTable1, "", "Column1", DataViewRowState.CurrentRows);
for (int i = 0; i < m_iTotalLoop; i++ )
dv.Find(i);
}
Reference
In this sample, the remediation is about 4 times faster.
CISQ rule: ASCPEM-PRF-8.
Related Technologies
.Net
Technical Criterion
CWE-1050 - Excessive Platform Resource Consumption within a Loop
About CAST Appmarq
CAST Appmarq is by far the biggest repository of data about real IT systems. It's built on thousands of analyzed applications, made of 35 different technologies, by over 300 business organizations across major verticals. It provides IT Leaders with factual key analytics to let them know if their applications are on track.