简体   繁体   中英

ngx-pagination - rendering page very slow

I am trying to build an application using Nodejs, Angular 6 and SQL. In my database I have more than 80.000 items, and when I try to paginate using ngx-pagination , it runs very slow (the get from database happens in 15 seconds, but the page is rendered in more that 1,5 minutes)

Here you have a snippet of my code:

.html

<table>
    <thead>
    ...
    </thead>
    <tbody>
        <tr *ngFor="let ded of filteredArray | orderBy : key : reverse | { itemsPerPage: crtPage, currentPage: p, totalItems: itemsLength() }"
            <td>{{ ded.name }}</td>
            <td>{{ ded.group }}</td>
            <td>{{ ded.age }}</td>
        <tr>

    </tbody>

    <div class="hint-text">
        <span>
            Showing {{ itemsLength() }} entries.
        </span>
    </div>

<table>

.ts

ngOnInit() {
    this.refreshItemsList()
}

refreshItemsList() {
    this.dedService.getNNItems().subscribe((res) => {
      this.extractItems(res)
    });
}

extractItems(res) {
    this.filteredArray = res
}

itemsLength() {
    return this.filteredArray.length
}

What can I do to render items faster? Thank you.

So if you're trying to get the 80k elements and page client side, it's not a good approach. It's using a lot of memory and compute power, each time you refresh.

Take a look at server-side paging : https://www.npmjs.com/package/ngx-pagination#server-side-paging


Nodejs side you can paginate your results with something like this :

var query = "Select * from ?? DESC limit ? OFFSET ?";

//Mention table from where you want to fetch records example-users & send limit and start 

var table = ["your table",LimitNum,startNum];
query = mysql.format(query, table);
connection.query(query, function(err, rest) {
 if(err){
  res.json(err);
}
else{
// Total Count varibale display total Count in Db and data display the records
   res.json("Total Count": totalCount , "data":rest)
}

Source

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