开发者

Performance of accessing dataSet fields using Field-names instead of indexes

开发者 https://www.devze.com 2023-03-01 03:35 出处:网络
Is the performance negligible? For example, myQuery.FieldbyName(\"MyField\").AsString; myQuery.Fields[0].AsString;

Is the performance negligible?

For example,

myQuery.FieldbyName("MyField").AsString;
myQuery.Fields[0].AsString;

Cases: Table with a decent number of fields, say > 50 fields

Accessing large resultsets, say > 100,000 rows

Is the readability benefit of field names w开发者_如何学编程orth the performance decrease?


Here is an interesting post by François Gaillard about FieldByName performance issues.


The performance may not be negligible, depending on how often you access the field by name. If you use it for every field and every row you may notice a performance decrease (see for example http://www.delphifeeds.com/go/s/74559). To mantain readability yet improve performance you could:

  1. Use the ['FieldName'] or FieldByName() syntax only once, and store a reference to the field in a variable.
  2. Use "static" persistent field declaration, right-clicking the dataset, select Field Editor and adding needed fields. It will declare the proper TField descendant, and let you assign a name.

Also the AsXXXXX calls may be slower than using a TField descendant native Value property.


I have found FieldByName to be noticeable slower.

I normally access the database through an intermediate layer, that access entire records from the same table alot of times. On creation of that layer I assign the index of each field to an variable. I then use the variables for later access, to still have readable code.

ADODataSet.CommandText := 'select * from [TABLE] where 1 = 0'; //table layout
ADODataSet.Open;
ADODataSet.GetFieldNames(List);
varMyField := List.IndexOf('MyField');
0

精彩评论

暂无评论...
验证码 换一张
取 消