简体   繁体   English

如何提高JS中数据库查询的性能

[英]how to improve performance of a database query in JS

I have a table in javascript with about 100 lines. 我在javascript中有一张table ,大约有100行。 The column 1 is item no, column 2 is price which is empty. 第1栏是商品编号,第2栏是空价格。

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: 现在,我想按项目编号从数据库中查询价格并填写第2列。现在,我遍历表格并查询数据,如下所示:

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? 如果仅使用一个查询将所有数据提取到数组变量中,然后在JS中进行匹配,会更快吗?

If the whole dataset is large than 10000 lines, how to improve it? 如果整个数据集大于10000行,该如何改进? Thx. 谢谢。

If your database does not have too many record, you can read them all using one select SQL. 如果您的数据库没有太多记录,则可以使用一个选择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.) (如果数据库中的记录太多,则必须循环表以获取所有项目,并使您的SQL仅选择所需的项目。)

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. 然后使用一个for循环将此数组传输到地图,并使用item作为键。 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. 尽可能减少api调用。 And about more than 10000 rows, you need pagination. 而大约10000行以上,则需要分页。 Divide them into proper size(about 100) then give index each call like a normal posting board system. 将它们划分为适当的大小(大约100),然后像正常发布板系统一样为每个呼叫分配索引。 You can fetch data with lazy load or infinity load. 您可以使用延迟加载或无限加载来获取数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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