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:
- Use the
['FieldName'] orFieldByName() syntax only once, and store a reference to the field in a variable. - 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');
精彩评论