In the case of a small amount of data, the performance of program writing is basically not much different, but when we face tens of thousands of data, I think performance is a problem that has to be considered, every time you write a method, every data fill must take into account performance issues. Otherwise, the server will bear huge execution overhead. If the server performance is poor, it may immediately die there, therefore, we must consider how to improve the performance of pages that are frequently accessed with a large amount of data. This article will provide a way to improve the access performance with cache to solve this problem, to a large extent, the performance of page loading data is improved. This document lists the examples of loading data on the post list page in the Forum.
Body:
The list of posts in each forum corresponds to a cache name. For example, we can set it
# Region -- cachename setting --
Boardcachename = "board" + boardid. tostring ();
# Endregion
Here we also use the data set filling datatable method to create data. However, because we have a cache, after loading the data for the first time, we will compress the data into the cache and make a judgment before filling the able, if the cache is empty, it is loaded. If it is not empty, it is not loaded.
Private datatable builddatatable ()
{
// Data cache mechanism
If (Cache [boardcachename]! = NULL)
{
// Create datatable from Cache
Datatable dt = (datatable) cache [boardcachename];
Return DT;
}
Else
{
// Create datatable from database
Datatable dt = new datatable ();
# Region -- create datatable --
DT. Columns. Add ("topicid", system. type. GetType ("system. int32 "));
// Removes the need to add n similar fields
DT. Columns. Add ("coolstate", system. type. GetType ("system. int32 "));
Datacolumn [] keys = new datacolumn [1];
Keys [0] = DT. Columns [0];
DT. primarykey = keys;
# Endregion
# Region -- add datarow --
Bbstopiccollection BTC = new bbstopiccollection ();
BTC. boardid = This. boardid;
BTC. topicstate = 1;
If (! BTC. getinfo ())
{
Return DT;
}
For (INT I = 0; I <BTC. Count; I ++)
{
Datarow DR = DT. newrow ();
// Post ID
Dr ["topicid"] = BTC [I]. ID;
// Save the assignment of n similar fields
// Cool state
Dr ["coolstate"] = BTC [I]. coolstate;
DT. Rows. Add (DR );
}
# Endregion
// Push datatable to cache
Cache [boardcachename] = DT;
Return DT;
}
}
The above code completes the data filling process, but more importantly, it manages the data. For example, we change some status positions of a residence to implement some functions, for example, we add "cool stickers" to the sub-posts. We need to perform operations on the cache at this time. Pay special attention to this. In the code above, we also set the topicid column as the table's primary key, in this way, we can quickly locate the data information to be managed. The method is as follows.
# Region -- Cache Management --
If (Cache ["board" + this. boardid. tostring ()]! = NULL)
{
Datatable dt = (datatable) cache ["board" + this. boardid. tostring ()];
Datarow DR = DT. Rows. Find (topicid );
If (Dr! = NULL)
{
Dr ["coolstate"] = 1;
Dr. acceptchanges ();
DT. acceptchanges ();
}
}
# Endregion
Note: The topicid is the Unique Identifier Field of the information you want to operate on. The acceptchanges method updates and stores all the changes to the object data since the last update, in addition, after you perform operations on the cache, remember to re-bind the data and update the data in the database at the same time. This article assumes that the reader has the ability to operate on the database data and will not explain it.
How can we delete data records? Can we use
After dr. Delete () Is it all done to delete the data? The answer is yes. This operation may cause problems. The addition and update operations on the cache will take effect immediately, but the action to delete a record will not take effect immediately, this will result in asynchronous data operations. This is not acceptable. At the same time, Version 1.1 is a little better than Version 1.0, but it still cannot solve the problem of Asynchronization, so do we have to get rid of the cache and fill it out again? If you are willing to do so, it is naturally understandable. Here I will provide another idea for your reference.
Our solution is to add a delete flag when creating a table, for example, deletestate. When it is loaded from the database, it will all be 1. After the deletion operation, place the delete mark of this information at 0 (do not forget to operate the data in the database at the same time), and then filter dataview when binding, DV. rowfilter = "deletestate = 1" to simulate the deletion effect.
After such processing, the access performance will be improved several hundred times, and the data will be reloaded only after the cache expires. users' access to the data is a cache operation, besides, the cache is a server variable that is shared with all users. In this way, if there are one hundred users simultaneously accessing the same cache, one hundred accesses are also made to the same cache, and the program accesses the cache very quickly, if the cache is not used, we need to perform one hundred database operations, with poor performance. Especially when massive users access massive data, the server is miserable, therefore, using cache to relieve load is a quite necessary and relatively good solution, but it is hard for the user who visits the page for the first time after the cache becomes invalid, however, it is worthwhile to sacrifice the high performance of others.