Mega Code Archive

 
Categories / Delphi / Examples
 

How to speed up your app and save resources

When using Tables and Databases: index your table to speed up your searches ! avoid reading the whole thing, better use a) conditional indexes (preferred) or b) queries sometimes using special algorithms is better than doing manual searches / sorting on tables don't use too many fields per table ! Having more than 100, 120 fields in one table can slow down your app because of exhaustive memory useage and forced data transfer. Better split up the informations you need in more tables and open only the tables you need ! This one is a bit tricky to decide. Opening a table takes its time, so if you had to open and close tables very often in a special part of your program, better keep them opened permanently. If your app is built of several parts (for example: one part for maintenance of your clients, one for articles, one for suppliers, one for statistics etc.), then you shouldn't keep ALL of your Tables opened permanently - instead open just the tables you need in the FormCreate-procedure of the individual Form and close them again in its OnClose procedure. This can also prevent locking and update-problems. when using ranges with Server databases: Server databases are set oriented and, therefore, do not understand record navigation very well. If you are trying to change the records in the range you should get much better performance by using an UPDATE query (to see the SQL generated by SetRange use the SQL Explorer). The fastest way to change a bunch of records in a server table is alway with an UPDATE query that runs entirely on the server and doesn't require you to read the records to client and send the updates back. Generally: at some database operations using TQuery is much faster than performing all and everything only by TTable methods. Try it ! Make use of TTable.DisableControls ! Updating all of the controls connected to your dataset takes its time - save this time by calling DisableControls .. after performing all the required operations, call TTable.EnableControls (evt. followed by TTable.Refresh) and everything will be updated again ! Avoid OnCalcFields unless necessary ! This function gets called like you wouldn't believe - that's why it can slow your app down enourmously... Instead you could make use of the TDataSource.OnDataChange event to update anything special on your form when a record changes. Order the results of queries by indexed fields ! Using dBase Tables with SQL can be very slow, because the Server processes the query locally (= on the server) and then returns only the result, which is not the case with dBase tables, which must be transported entirely through the net to your client, processed on your client, which then shows the result. If you have a slow net and large dBase tables this becomes very, very slow. Using Paradox files instead can really speed up things if you have a primary index in which you search for the value (the TQuery looks up for the primary index and tries to use it !). Use QuickReports with tQuery ! It's faster than ReportSmith and gives unlimited queries. (tQuery with ChartFX is also pretty cool!) Avoid tQuery in Database Grids and so forth - it appears too slow. Could you make use of the SetRange function ? It beats anything around for speed. Although it works on an IndexFieldName, you can trick it by creating filtered indexes. This allows you to have special choice combo-boxes that only display appropriate data. (Table1.SetRange([Cust_A],[Cust_A]) will only show Cust_A data...) For example IndexName: 'LiveOrders' (index on Cust_No for Paid=0 TAG LiveOrders) 'paidorders' (INDEX on Cust_No for Paid=1 TAG PaidOrders) Both work as IndexFieldName='Cust_No', but you set the index as follows: Table1.IndexName='LiveOrders';... Table1.SetRange([var],[var]); (Apollo has a similar function that is even a bit more flexible.) Consider using 3rd party database engines (see Link at my Delphi Tools area)! Engines like Advantage Database or Apollo not only use optimized search and query algorithms, they also support combined, conditional and full-text indexes. Additionally you'll only need 2, relatively small DLL-files instead of the big-sized BDE.