<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    posts - 262,  comments - 221,  trackbacks - 0
    【一】關(guān)于ResultSet metadata的使用

    Minimizing the Use of Database Metadata Methods
    Compared to other JDBC methods, database metadata methods that generate ResultSet objects are relatively slow. Applications should cache information returned from result sets that generate database metadata methods so that multiple executions are not needed.

    Avoiding Search Patterns
    Using null arguments or search patterns in database metadata methods results in generating time-consuming queries. In addition, network traffic potentially increases due to unwanted results. Always supply as many non-null arguments to result sets that generate database metadata methods as possible.

    Using a Dummy(虛擬) Query to Determine Table Characteristics
    Avoid using getColumns() to determine characteristics about a table. Instead, use a dummy query with getMetadata().

    ResultSet WSrc = WSc.getColumns (... "UnknownTable" ...);

    PreparedStatement WSps = WSc.prepareStatement("SELECT * from UnknownTable WHERE 1 = 0");

    In summary, always use result set metadata to retrieve table column information such as column names, column data types, and column precision and scale. Only use getColumns() when the requested information cannot be obtained from result set metadata (i.e. table column default values).


    【二】僅僅獲取所需的數(shù)據(jù)

    ①Retrieving Long Data

    Unless it is necessary, applications should not request long data because retrieving long data across a network is slow and resource-intensive.

    ②Reducing the Size of Data Retrieved
    To reduce network traffic and improve performance, you can reduce the size of any data being retrieved to some manageable limit by calling setMaxRows(), setMaxFieldSize(), and the driver-specific SetFetchSize(). Another method of reducing the size of the data being retrieved is to decrease the column size. If the driver allows you to define the packet size, use the smallest packet size that will meet your needs.

    ③Choosing the Right Data Type
    Retrieving and sending certain data types can be expensive. When you design a schema, select the data type that can be processed most efficiently.

    ④Retrieving Result Sets
    Most JDBC drivers cannot implement scrollable cursors because of limited support for scrollable cursors in the database system. Unless you are certain that the database supports using a scrollable result set, (for example, rs), do not call rs.last() and rs.getRow() methods to find out how many rows the result set has. For JDBC drivers that emulate scrollable cursors, calling rs.last() results in the driver retrieving all results across the network to reach the last row. Instead, you can either count the rows by iterating through the result set or get the number of rows by submitting a query with a COUNT column in the SELECT clause.

    【三】檢索JDBC對象的方法

    ①Using Parameter Markers as Arguments to Stored Procedures
    The database server would parse the SQL query, consult database metadata to determine the parameter contract of the procedure, isolate the single argument value 12345, then convert the string ‘12345’ into an integer value before finally executing the procedure as a SQL language event.

    By invoking an RPC inside the database server, the overhead of using a SQL character string is avoided. Instead, a JDBC driver will construct a network packet that contains the parameters in their native data type formats and execute the procedure remotely.

    ②Using the Statement Object Instead of the PreparedStatement Object
    The Statement object is optimized for a single execution of a SQL statement. In contrast, the PreparedStatement object is optimized for SQL statements that will be executed two or more times.

    The overhead for the initial execution of a PreparedStatement object is high. A JDBC driver will most likely process the prepare request by making a network request to the database server to parse and optimize the query. The execute then results in another network request.

    If my application will only make a request like this once during the life of the application, then use the Statement object. With a Statement object, the same query execution will result in only a single network roundtrip to the database server.

    if a query is ad-hoc and will likely never be executed again, then use the Statement object. If a query will be executed infrequently, but may be executed again during the life of a statement pool inside a connection pool, then use a PreparedStatement. Under the same circumstances without statement pooling, use the Statement object.

    ③Using Batches Instead of Prepared Statements
    Updating large amounts of data typically is done by preparing an INSERT statement and executing that statement multiple times, resulting in numerous network roundtrips. To reduce the number of JDBC calls and improve performance, you can send multiple queries to the database at a time using the addBatch() method of the PreparedStatement object.

    PreparedStatement ps = conn.prepareStatement(
       "INSERT into employees values (?, ?, ?)");

    for (n = 0; n < 100; n++) {

      ps.setString(name[n]);
      ps.setLong(id[n]);
      ps.setInt(salary[n]);
      ps.addBatch();
    }
    ps.executeBatch();

    Remember that the biggest gains in performance with JDBC drivers is found by reducing network communication between the JDBC driver and the database server.

    ④Choosing the Right Cursor
    A forward-only cursor provides excellent performance for sequential reads of all of the rows in a table.

    Insensitive cursors used by JDBC drivers are ideal for applications that require high levels of concurrency on the database server and require the ability to scroll forwards and backwards through result sets. The first request to an insensitive cursor fetches all (or many but not all rows when a JDBC driver using “lazy” fetching) of the rows and stores them on the client. Thus, the first request is very slow, especially when long data is retrieved. Subsequent requests do not require any network traffic (or limited network traffic when a driver uses lazy fetching) and are processed quickly.

    Because the first request is processed slowly, insensitive cursors should not be used for a single request of one row. Developers should also avoid using insensitive cursors when long data is returned, because memory can be exhausted.

    Sensitive cursors, sometimes called keyset-driven cursors, use identifiers, such as a ROWID. At execute time or when the first row is requested, a JDBC driver would not execute the SELECT statement that was provided by the application. Instead, the JDBC driver would replace the SELECT list of the query with the key identifier, for example, ROWID.

    Each request from the application for a result row directs the JDBC driver to look up the key value for the appropriate row in it’s local cache, construct an optimized query that contains a WHERE clause similar to ‘WHERE ROWID = ?’, execute the modified query, and then retrieve the single result row from the server.

    備注:

    此 游標(biāo)策略就是Hibernate中的1+N次查詢。即先不執(zhí)行Select *,而是內(nèi)部偷偷替換成Select id, rowid,得到一批ID后緩存到本地。如果程序不是連續(xù)訪問,而是跳躍式訪問、隨機式訪問的話。可以在游標(biāo)指向需要的記錄時才根據(jù)該ID/Rowid發(fā) 出一個Select *.... where rowid = ?這樣的SQL語句。

    這樣在客戶端的緩存就將只存儲一批Id而已,可以大大的減輕程序的負擔(dān)。但是在最糟糕的情況下他會發(fā)出N條子查詢,反而可能加重系統(tǒng)負擔(dān)


    ⑤Using get Methods Effectively
    The getObject() method is the most generic and provides the worst performance when the non-default mappings are specified. This is because the JDBC driver must do extra processing to determine the type of the value being retrieved and generate the appropriate mapping. Always use the specific method for the data type.

    To further improve performance, provide the column number of the column being retrieved, for example, getString(1), getLong(2), and getInt(3), instead of the column name. If column numbers are not specified, network traffic is unaffected, but costly conversions and lookups increase.

    To further improve performance, provide the column number of the column being retrieved, for example, getString(1), getLong(2), and getInt(3), instead of the column name. If column numbers are not specified, network traffic is unaffected, but costly conversions and lookups increase.


    ⑥Retrieving Auto-Generated Keys
    An optional feature of the JDBC 3.0 specification is the ability to retrieve auto-generated key information for a row when the row is inserted into a table.

    For example:
    int rowcount = stmt.executeUpdate ("insert into LocalGeniusList (name) values ('Karen')",
                    Statement.RETURN_GENERATED_KEYS);
    ResultSet rs = stmt.getGeneratedKeys (); // key is automatically available


    -------------------------------------------------------------
    生活就像打牌,不是要抓一手好牌,而是要盡力打好一手爛牌。
    posted on 2010-03-19 23:11 Paul Lin 閱讀(573) 評論(0)  編輯  收藏 所屬分類: J2SE
    <2010年3月>
    28123456
    78910111213
    14151617181920
    21222324252627
    28293031123
    45678910

    常用鏈接

    留言簿(21)

    隨筆分類

    隨筆檔案

    BlogJava熱點博客

    好友博客

    搜索

    •  

    最新評論

    閱讀排行榜

    評論排行榜

    主站蜘蛛池模板: 四虎最新永久免费视频| 免费人成在线观看网站品爱网 | 最近免费中文字幕大全免费| 精品国产无限资源免费观看| 成**人免费一级毛片| 区三区激情福利综合中文字幕在线一区亚洲视频1 | 亚洲高清专区日韩精品| 亚洲无圣光一区二区| 亚洲av无码一区二区三区人妖 | 国产在线不卡免费播放| 国产亚洲精品国看不卡| 久久国产亚洲精品无码| 中文字幕亚洲情99在线| 一级做a爱过程免费视| 久久免费福利视频| 午夜视频在线在免费| 亚洲性猛交XXXX| 亚洲AV无码一区二区三区在线 | 亚洲AV无码国产剧情| 99re8这里有精品热视频免费| 久久久久av无码免费网| 免费人成视网站在线观看不卡| 亚洲AV无码一区东京热久久| 亚洲夂夂婷婷色拍WW47| 99久久免费国产特黄| 大地资源二在线观看免费高清 | 免费在线观看毛片| 久久亚洲熟女cc98cm| 国产成人亚洲综合在线| 99爱在线精品视频免费观看9| 拔擦拔擦8x华人免费久久| 亚洲AV无码专区电影在线观看| 亚洲经典千人经典日产| 精品国产麻豆免费人成网站| 蜜臀91精品国产免费观看| 精品亚洲综合久久中文字幕| 亚洲成a人无码亚洲成www牛牛 | 高清永久免费观看| 麻豆国产入口在线观看免费| 久热综合在线亚洲精品| 老司机午夜精品视频在线观看免费 |