Performance Optimization - 4.4 [Traversal technology] Load from database in parallel
Sometimes we need to read data from the database to execute complex operations, however, we often find the time to read data is very long when database is not heavily loaded. This is mainly due to poorer performance of database’s access driver; this problem can be effectively alleviated by using the parallel technology.
|3||=B1.query("select * from T where id>=? and id<?,B2(1),B2(2))|
This code will fetch the records in T table whose id value is between MinID and MaxID in parallel. The conditional interval is divided into multiple segments, and each thread takes one of segments. Note that the database needs to be connected separately in each thread, otherwise, database will force the requests of same connection to be executed serially, as a result, the parallel logic fails.
Database does not have an interface to achieve segmentation split, you can only use where to split, and you can use a more appropriate split scheme based on your understanding on the data characteristics. Since where computing will consume database resources, the parallel data-fetching can be used only when database resources are sufficient (slow data-fetching is due to slow driver rather than the slow database computing). Too complex where computing will also make the effect of parallel data fetching worse.
If the database cursor is needed due to the large amount of fetched data, only when the read data is processed in each thread can the parallel effect be achieved, and you cannot return the delayed cursor in the thread without substantive calculation.
|3||=B1.cursor("select * from T where id>=? and id<?,B2(1),B2(2))|
Parallel schemes can also be used while data-fetching with multiple different SQLs:
For multiple SQLs, some bigger parallel thread numbers can be used. Since different SQL has different execution speeds, the load can also be balanced even if there is no more number of threads physically. When the threads are assigned to SQLs having faster execution speed, more tasks can be executed to avoid thread waiting.