optimize jquery datatable with a database with more than 30,000 records

Asked

Viewed 1,954 times

2

What is the best way to optimize database data return? the ideal would be to store (on the server-side) all data (select * from data) in an array of objects and send it to the client-side? from there I do the processing of this array via javascript and I populate in my table?

do something like this? follow the jsfiddle link below http://jsfiddle.net/V2Kdz/

some tips?

  • Ever thought of using LIMIT ?, ? and paging? Your question is more for SQL than javascript itself or I’m mistaken?

  • i have a record with 30k+ lines, in php I simply gave a select * from data and had it printed in html, there in html I have a datatable that organizes all this data. The problem is it’s taking a long time to load everything. the idea is to print all the data so that the person can consult anything via client-side.

  • Yes, but no one loads all data at once, it is totally unfeasible, so there is paging and so I suggested the LIMIT.

  • Dude I made one with 10k already gave problem. Try to bring less data in the query. The browser hangs with this much data.

  • @João noticed that all your previous questions have answers, but you did not accept any, if any of those answers solved your other problems mark it as correct, if you do not know how to do this make a tour: http://answall.com/tour

3 answers

5


Uploading all data at once is a bad idea, regardless of whether you save it on the front end or back end, always limit your results, in the case of "database" the best solution is to use LIMIT (I believe that in SQL Server we use OFFSET instead of LIMIT), so you should only send the requested page data to the front-end.

I don’t know which bank you’re using, but the syntax is very similar in most banks, it works like this:

SELECT ... LIMIT [offset,] row_count

The offset is the position in the bank, the row_count is the result limit that you will display, note that if you use it like this:

SELECT ... LIMIT 10

He will do the offset be equal to 1 because he was omitted and 10 will be the row_count.

Usually for each pagination we use a limit of 15 to 30 (this varies according to each one).

In your code there’s a <select> in the option sLengthMenu of $.dataTable, it should send the selected value to the back-end, in case it selected to show 10, it should send a request to the server and should run the query like this (http://localhost/page.php?limit=10):

Note: I don’t know if you are using mysql or other type of database, but the logic is the same

$offset = 1;
$row_count = $_GET['limit'];

$stmt = $mysqli->prepare('SELECT ... LIMIT ?, ?');

$stmt->bind_param('i', $offset);
$stmt->bind_param('i', $row_count);

//query: SELECT ... LIMIT 1, 10;

If you want to go to page 2, you must multiply the $offset, by the amount of lines that will display (http://localhost/page.php/?page=2&limit=10):

$offset = 1;

if (isset($_GET['page']) && is_numeric($_GET['page']) && $_GET['page'] > 1) {
    $offset = ($_GET['page'] - 1) * $_GET['limit'];
}

$row_count = $_GET['limit'];

$stmt = $mysqli->prepare('SELECT ... LIMIT ?, ?');

$stmt->bind_param('i', $offset);
$stmt->bind_param('i', $row_count);

//query: SELECT ... LIMIT 10, 10;
  • Page 3 (http://localhost/page.php.php?page=3&limit=10) will generate:

    SELECT ... LIMIT 20, 10;
    
  • Page 4 (http://localhost/page.php.php?page=4&limit=10) will generate:

    SELECT ... LIMIT 30, 10;
    
  • Page 5 (http://localhost/page.php.php?page=5&limit=10) will generate:

    SELECT ... LIMIT 40, 10;
    

To use with WHERE, do something like:

SELECT * FROM tabela WHERE foo='abc' OR foo='xyz' LIMIT 1, 10

And so on and so forth.

Documentation

  • 2

    recalling that OFFSET and FETCH are new features, although very useful, are only available in Sqlserver 2012. for older versions have to work around this problem using ROW_NUMBER()

  • @Tobymosque Thank you, edited.

  • in the search field (an input text), it searches the entire table by typing a string (returns from the database all words containing that typed string), if I do with limit, the search will not be limited only to that limit I did?

  • @John You want to know the amount of words found?

  • @Guilhermenascimento the search system works like that jsfiddle posted, isn’t it necessary to print the entire database for that search function to work? from what I understand, all data is printed in the database in the table, hence the jquery datatable plugin only does a search without accessing the database, only by the printed table. this implementation is not a good idea for very large tables, it is not?

  • @John I do not think because it will take the back-end to bring all the data, even if you only bring the first time.

  • would it be better to implement what you said (of the Limits) and do a more optimized search, for example: if I just want to search the name, I pass the name and capture in the back end, and send a select * from table name WHERE name='$name sought'; isn’t it? I’m kind of new at this, rs. I have a giant table and I’m studying ways to implement it.

  • That @John would look something like: select * from nometabela WHERE nome='$nomeprocurada' LIMIT 1, 10;

Show 3 more comments

1

In the option data ajax, pass a function:

"data": function ( d ) {
    d.suavariavel = "seu valor";
}

So you can, through variables, make a select limiting the number of records returned. Follows an example that I use and works perfectly:

var dtini  = $('#dtini').val();
var dtfim  = $('#dtfim').val();
var temp = $('#dtini').val().split("/");
var d1     = new Date(temp[2], temp[1]-1,temp[0]);
var temp = $('#dtfim').val().split("/");
var d2     = new Date(temp[2], temp[1]-1,temp[0]);
if (d2 < d1){
    errorAlert("Data final não pode ser maior que data inicial");
}else{
    var totcli = $('#totcli').dataTable({
        "bDeferRender"   : true,
        "bProcessing"    : true,
        "aaSorting"      : [],
        "sPaginationType": "full_numbers",
        "ajax"           : {
            "url" : "../php/rotinas.php",
            "type": "POST",
            "data": function (a) {
                a.vendaspr = 0;
                a.dtini    = dtini;
                a.dtfim    = dtfim;             
            }   
         }
    }); 
}

In this example, return the records that date between the initial and final date.

0

Use the filter limit SQL and client-side paging technique, no need to send a flood of data at once.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.