简体   繁体   中英

how to improve performance of a database query in JS

I have a table in javascript with about 100 lines. The column 1 is item no, column 2 is price which is empty.

Now I want to query price by item no from a database and fill in column 2. For now I go through the table and query the data, the code like below:

set conn=Server.CreateObject("ADODB.Connection")
conn.Provider="Microsoft.Jet.OLEDB.4.0"
conn.Open(Server.Mappath("/db/northwind.mdb"))
set rs = Server.CreateObject("ADODB.recordset")
for (i=0;i<table.length;i++)
{
    sql=select price from table where item=table[i][0];
    rs.open sql,conn
    table[i][1]=rs.value
    rs.close
}

It runs pretty slow. I guess because for each row in the table it will query the database once. How to improve it?

If I use only one query to fetch all the data into an array variable and do the match inside JS, will it be quicker?

If the whole dataset is large than 10000 lines, how to improve it? Thx.

If your database does not have too many record, you can read them all using one select SQL. (If you database has too many record, you have to loop your table to get all item and make you SQL to just select those item you want.)

select price, item from table;

You should get an array like

[
  { price: 1, item: xxx},
  ...
]

Then use one for loop to transfer this array to a map and use item as a key. You should get something like:

{
  { item1: { price: 1, item: 'item1' } }
  ...
}

Finally, you can loop your table to get price from your map. You will just need one database operation, one loop for the database result, and on loop for the table.

As you guess, make them array of object. Reduce api calls as much as possible. And about more than 10000 rows, you need pagination. Divide them into proper size(about 100) then give index each call like a normal posting board system. You can fetch data with lazy load or infinity load.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM