Datatable. Select method performance problems

Source: Internet
Author: User
Let's first look at an example:
Bool a, B, c, d; A = true; B = false; C = false; D = false;
If we execute if (A or B or C or D) {}, we find that when Program When a is executed, if it is true, the following statement is executed. Therefore, it is unnecessary to judge the values of B, C, and D.

Recently, some large data volumes have been compared for multiple times. Because some record blocks are compared for multiple times, these records are stored in the datatable to improve the search performance. The datatable is used. select (string filterexpression) to find related records. However, I found that execution is slow. Then I split the XPath accepted by the Select method into several conditions for execution, pass the conditions most likely to narrow down to filterexpression, and then filter other conditions in the found set. It is found that the execution time is much less than the original execution time. Therefore, I estimate that the select Algorithm Is to calculate all the conditions before returning this record.
I use reflector.exe to view the algorithm. Source code It is found that this method divides the filterexpression expression into multiple expressions, then traverses the records, computes these subconditions, and obtains records that meet the conditions (the algorithm is complicated internally and the details are not fully read, if you have good opinions, you are welcome to raise them. This leads to low efficiency.
Then I made a simple example to verify it: 1 Datatable dt =   New Datatable ();
2 DT. Columns. Add ( " A " , Typeof ( Int ));
3 DT. Columns. Add ( " B " , Typeof ( String ));
4
5 For ( Int I = 0 ; I < 1000000 ; I ++ )
6 {
7 Datarow row = DT. newrow ();
8 Row [ " A " ] =   0 ;
9 Row [ " B " ] =   " 2 " ;
10 DT. Rows. Add (ROW );
11 }
12 Long Tick = System. datetime. Now. ticks;
13 Datarow [] rows = DT. Select ( " A = 0 or (B = '1' or B = '3' or B = '5' or B = '6 ') " );
14 System. Console. writeline (system. datetime. Now. ticks - Tick ));
15 Tick = System. datetime. Now. ticks;
16 Rows = DT. Select ( " A = 0 " );
17 System. Console. writeline (system. datetime. Now. ticks - Tick ));

In the local test, we found that the first execution time is 12 times that of the second one!

Therefore, when using this method, you must optimize your own query conditions. In particular, when a condition can effectively narrow the query range, you should first query the condition and then execute the condition on the result.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.